forked from yziqi/adversarial-model-inversion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_classifier.py
executable file
·103 lines (87 loc) · 4.2 KB
/
train_classifier.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from __future__ import print_function
import argparse
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import transforms
import os
import torch.nn.functional as F
from data import FaceScrub
from model import Classifier
# Training settings
parser = argparse.ArgumentParser(description='Adversarial Model Inversion Demo')
parser.add_argument('--batch-size', type=int, default=128, metavar='')
parser.add_argument('--test-batch-size', type=int, default=1000, metavar='')
parser.add_argument('--epochs', type=int, default=100, metavar='')
parser.add_argument('--lr', type=float, default=0.01, metavar='')
parser.add_argument('--momentum', type=float, default=0.5, metavar='')
parser.add_argument('--no-cuda', action='store_true', default=False)
parser.add_argument('--seed', type=int, default=1, metavar='')
parser.add_argument('--log-interval', type=int, default=10, metavar='')
parser.add_argument('--nc', type=int, default=1)
parser.add_argument('--ndf', type=int, default=128)
parser.add_argument('--nz', type=int, default=530)
parser.add_argument('--num_workers', type=int, default=1, metavar='')
def train(classifier, log_interval, device, data_loader, optimizer, epoch):
classifier.train()
for batch_idx, (data, target) in enumerate(data_loader):
data, target = data.to(device), target.to(device)
optimizer.zero_grad()
output = classifier(data)
loss = F.nll_loss(output, target)
loss.backward()
optimizer.step()
if batch_idx % log_interval == 0:
print('Train Epoch: {} [{}/{}]\tLoss: {:.6f}'.format( epoch, batch_idx * len(data),
len(data_loader.dataset), loss.item()))
def test(classifier, device, data_loader):
classifier.eval()
test_loss = 0
correct = 0
with torch.no_grad():
for data, target in data_loader:
data, target = data.to(device), target.to(device)
output = classifier(data)
test_loss += F.nll_loss(output, target, reduction='sum').item()
pred = output.max(1, keepdim=True)[1]
correct += pred.eq(target.view_as(pred)).sum().item()
test_loss /= len(data_loader.dataset)
print('\nTest classifier: Average loss: {:.6f}, Accuracy: {}/{} ({:.4f}%)\n'.format(
test_loss, correct, len(data_loader.dataset), 100. * correct / len(data_loader.dataset)))
return correct / len(data_loader.dataset)
def main():
args = parser.parse_args()
print("================================")
print(args)
print("================================")
os.makedirs('out', exist_ok=True)
use_cuda = not args.no_cuda and torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
kwargs = {'num_workers': args.num_workers, 'pin_memory': True} if use_cuda else {}
torch.manual_seed(args.seed)
transform = transforms.Compose([transforms.ToTensor()])
train_set = FaceScrub('./data/facescrub', transform=transform, train=True)
test_set = FaceScrub('./data/facescrub', transform=transform, train=False)
train_loader = torch.utils.data.DataLoader(train_set, batch_size=args.batch_size, shuffle=True, **kwargs)
test_loader = torch.utils.data.DataLoader(test_set, batch_size=args.test_batch_size, shuffle=False, **kwargs)
classifier = nn.DataParallel(Classifier(nc=args.nc, ndf=args.ndf, nz=args.nz)).to(device)
optimizer = optim.Adam(classifier.parameters(), lr=0.0002, betas=(0.5, 0.999), amsgrad=True)
best_cl_acc = 0
best_cl_epoch = 0
# Train classifier
for epoch in range(1, args.epochs + 1):
train(classifier, args.log_interval, device, train_loader, optimizer, epoch)
cl_acc = test(classifier, device, test_loader)
if cl_acc > best_cl_acc:
best_cl_acc = cl_acc
best_cl_epoch = epoch
state = {
'epoch': epoch,
'model': classifier.state_dict(),
'optimizer': optimizer.state_dict(),
'best_cl_acc': best_cl_acc,
}
torch.save(state, 'out/classifier.pth')
print("Best classifier: epoch {}, acc {:.4f}".format(best_cl_epoch, best_cl_acc))
if __name__ == '__main__':
main()