-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtrain_inversion.py
executable file
·135 lines (118 loc) · 5.95 KB
/
train_inversion.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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, shutil
from data import FaceScrub, CelebA
from model import Classifier, Inversion
import torch.nn.functional as F
import torchvision.utils as vutils
# 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('--ngf', type=int, default=128)
parser.add_argument('--nz', type=int, default=530)
parser.add_argument('--truncation', type=int, default=530)
parser.add_argument('--c', type=float, default=50.)
parser.add_argument('--num_workers', type=int, default=1, metavar='')
def train(classifier, inversion, log_interval, device, data_loader, optimizer, epoch):
classifier.eval()
inversion.train()
for batch_idx, (data, target) in enumerate(data_loader):
data, target = data.to(device), target.to(device)
optimizer.zero_grad()
with torch.no_grad():
prediction = classifier(data, release=True)
reconstruction = inversion(prediction)
loss = F.mse_loss(reconstruction, data)
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, inversion, device, data_loader, epoch, msg):
classifier.eval()
inversion.eval()
mse_loss = 0
plot = True
with torch.no_grad():
for data, target in data_loader:
data, target = data.to(device), target.to(device)
prediction = classifier(data, release=True)
reconstruction = inversion(prediction)
mse_loss += F.mse_loss(reconstruction, data, reduction='sum').item()
if plot:
truth = data[0:32]
inverse = reconstruction[0:32]
out = torch.cat((inverse, truth))
for i in range(4):
out[i * 16:i * 16 + 8] = inverse[i * 8:i * 8 + 8]
out[i * 16 + 8:i * 16 + 16] = truth[i * 8:i * 8 + 8]
vutils.save_image(out, 'out/recon_{}_{}.png'.format(msg.replace(" ", ""), epoch), normalize=False)
plot = False
mse_loss /= len(data_loader.dataset) * 64 * 64
print('\nTest inversion model on {} set: Average MSE loss: {:.6f}\n'.format(msg, mse_loss))
return mse_loss
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 = CelebA('./data/celebA', transform=transform)
# Inversion attack on TRAIN data of facescrub classifier
test1_set = FaceScrub('./data/facescrub', transform=transform, train=True)
# Inversion attack on TEST data of facescrub classifier
test2_set = FaceScrub('./data/facescrub', transform=transform, train=False)
train_loader = torch.utils.data.DataLoader(train_set, batch_size=args.batch_size, shuffle=True, **kwargs)
test1_loader = torch.utils.data.DataLoader(test1_set, batch_size=args.test_batch_size, shuffle=False, **kwargs)
test2_loader = torch.utils.data.DataLoader(test2_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)
inversion = nn.DataParallel(Inversion(nc=args.nc, ngf=args.ngf, nz=args.nz, truncation=args.truncation, c=args.c)).to(device)
optimizer = optim.Adam(inversion.parameters(), lr=0.0002, betas=(0.5, 0.999), amsgrad=True)
# Load classifier
path = 'out/classifier.pth'
try:
checkpoint = torch.load(path)
classifier.load_state_dict(checkpoint['model'])
epoch = checkpoint['epoch']
best_cl_acc = checkpoint['best_cl_acc']
print("=> loaded classifier checkpoint '{}' (epoch {}, acc {:.4f})".format(path, epoch, best_cl_acc))
except:
print("=> load classifier checkpoint '{}' failed".format(path))
return
# Train inversion model
best_recon_loss = 999
for epoch in range(1, args.epochs + 1):
train(classifier, inversion, args.log_interval, device, train_loader, optimizer, epoch)
recon_loss = test(classifier, inversion, device, test1_loader, epoch, 'test1')
test(classifier, inversion, device, test2_loader, epoch, 'test2')
if recon_loss < best_recon_loss:
best_recon_loss = recon_loss
state = {
'epoch': epoch,
'model': inversion.state_dict(),
'optimizer': optimizer.state_dict(),
'best_recon_loss': best_recon_loss
}
torch.save(state, 'out/inversion.pth')
shutil.copyfile('out/recon_test1_{}.png'.format(epoch), 'out/best_test1.png')
shutil.copyfile('out/recon_test2_{}.png'.format(epoch), 'out/best_test2.png')
if __name__ == '__main__':
main()