-
Notifications
You must be signed in to change notification settings - Fork 3
/
rec_imgs.py
320 lines (288 loc) · 14.5 KB
/
rec_imgs.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
"""Mechanisms for image reconstruction from parameter gradients."""
import os
import time
import torch
import torchvision
from utils import get_target_data
from collections import defaultdict
from medianfilt import MedianPool2d
from metrics import InceptionScore
from metrics import total_variation as TV
class GradientReconstructor:
"""Instantiate a reconstruction algorithm."""
def __init__(self, model, config, rec_exp_dir, mean_std=(0.0, 1.0), num_images=1, loss_thresh=1e-4):
"""Initialize with algorithm setup."""
self.config = config
self.model = model
self.setup = dict(device=next(model.parameters()).device, dtype=next(model.parameters()).dtype)
self.mean_std = mean_std
self.num_images = num_images
self.reconstruct_label = True
self.early_stop = True
self.loss_thresh = loss_thresh
self.exp_dir = rec_exp_dir
if self.config['scoring_choice'] == 'inception':
self.inception = InceptionScore(batch_size=1, setup=self.setup)
self.loss_fn = torch.nn.CrossEntropyLoss(reduction='mean')
def reconstruct(self, input_data, labels, img_shape=(3, 32, 32), dryrun=False, eval=True, tol=None,
aux_data=None):
"""Reconstruct image from gradient."""
start_time = time.time()
if not os.path.exists(self.exp_dir):
os.makedirs(self.exp_dir)
if eval:
self.model.eval()
stats = defaultdict(list)
x = self._init_images(img_shape, labels, aux_data)
scores = torch.zeros(self.config['restarts'])
if labels is None:
def loss_fn(pred, label):
label = torch.nn.functional.softmax(label, dim=-1)
return torch.mean(torch.sum(- label * torch.nn.functional.log_softmax(pred, dim=-1), 1))
self.loss_fn = loss_fn
else:
assert labels.shape[0] == self.num_images
self.reconstruct_label = False
try:
for trial in range(self.config['restarts']):
x_trial, labels = self._run_trial(x[trial], input_data, labels, dryrun=dryrun)
# Finalize
scores[trial] = self._score_trial(x_trial, input_data, labels)
x[trial] = x_trial
if tol is not None and scores[trial] <= tol:
break
if dryrun:
break
except KeyboardInterrupt:
print('Trial procedure manually interruped.')
pass
# Choose optimal result:
if self.config['scoring_choice'] in ['pixelmean', 'pixelmedian']:
x_optimal, stats = self._average_trials(x, labels, input_data, stats)
else:
print('Choosing optimal result ...')
scores = scores[torch.isfinite(scores)] # guard against NaN/-Inf scores?
optimal_index = torch.argmin(scores)
print(f'Optimal result score: {scores[optimal_index]:2.4f}')
stats['opt'] = scores[optimal_index].item()
x_optimal = x[optimal_index]
print(f'Total time: {time.time() - start_time}.')
return x_optimal.detach(), stats
def _init_images(self, img_shape, labels=None, aux_data=None):
if self.config['init'] == 'randn':
return torch.randn((self.config['restarts'], self.num_images, *img_shape), **self.setup)
elif self.config['init'] == 'rand':
return (torch.rand((self.config['restarts'], self.num_images, *img_shape), **self.setup) - 0.5) * 2
elif self.config['init'] == 'zeros':
return torch.zeros((self.config['restarts'], self.num_images, *img_shape), **self.setup)
elif self.config['init'] == 'auxiliary':
assert aux_data is not None, 'Please provide an auxiliary dataset'
target_id = 0
images = []
for _ in range(self.config['restarts']):
print('get data')
image, target_id = get_target_data(aux_data, labels, target_id,
device=self.setup['device'])
images.append(image)
print('finish init')
return images
else:
raise ValueError()
def _run_trial(self, x_trial, input_data, labels, dryrun=False):
x_trial.requires_grad = True
if self.reconstruct_label:
output_test = self.model(x_trial)[0]
labels = torch.randn(output_test.shape[1]).to(**self.setup).requires_grad_(True)
if self.config['optim'] == 'adam':
optimizer = torch.optim.Adam([x_trial, labels], lr=self.config['lr'])
elif self.config['optim'] == 'sgd': # actually gd
optimizer = torch.optim.SGD([x_trial, labels], lr=0.01, momentum=0.9, nesterov=True)
elif self.config['optim'] == 'LBFGS':
optimizer = torch.optim.LBFGS([x_trial, labels])
else:
raise ValueError()
else:
if self.config['optim'] == 'adam':
optimizer = torch.optim.Adam([x_trial], lr=self.config['lr'])
elif self.config['optim'] == 'sgd': # actually gd
optimizer = torch.optim.SGD([x_trial], lr=0.01, momentum=0.9, nesterov=True)
elif self.config['optim'] == 'LBFGS':
optimizer = torch.optim.LBFGS([x_trial])
else:
raise ValueError()
max_iterations = self.config['max_iterations']
dm, ds = self.mean_std
if self.config['lr_decay']:
scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer,
milestones=[max_iterations // 2.667, max_iterations // 1.6,
max_iterations // 1.142],
gamma=0.1) # 3/8 5/8 7/8
try:
if self.config['init'] == 'auxiliary':
if self.config['filter'] == 'none':
pass
elif self.config['filter'] == 'median':
x_trial.data = MedianPool2d(kernel_size=3, stride=1, padding=1, same=False)(x_trial)
else:
raise ValueError()
aux_den = torch.clamp(x_trial * ds + dm, 0, 1)
for j in range(self.num_images):
filename = f'aux_{j}.png'
torchvision.utils.save_image(aux_den[j:j + 1, ...],
os.path.join(self.exp_dir, filename))
print('start iteration steps')
start_iteration = 0
for iteration in range(start_iteration, max_iterations):
log_num = 100
save_interval = 500
closure = self._gradient_closure(optimizer, x_trial, input_data, labels)
rec_loss = optimizer.step(closure)
if self.config['lr_decay']:
scheduler.step()
with torch.no_grad():
# Project into image space
if self.config['boxed']:
x_trial.data = torch.max(torch.min(x_trial, (1 - dm) / ds), -dm / ds)
if (iteration + 1 == max_iterations) or iteration % (max(1, max_iterations / log_num)) == 0:
print(f'It: {iteration}. Rec. loss: {rec_loss.item():2.4f}.')
if (iteration + 1) % save_interval == 0 or iteration == 0:
if self.config['filter'] == 'none':
pass
elif self.config['filter'] == 'median':
x_trial.data = MedianPool2d(kernel_size=3, stride=1, padding=1, same=False)(x_trial)
else:
raise ValueError()
output_den = torch.clamp(x_trial * ds + dm, 0, 1)
for j in range(self.num_images):
filename = f'rec_{j}.png'
torchvision.utils.save_image(output_den[j:j + 1, ...],
os.path.join(self.exp_dir, filename))
if self.early_stop and rec_loss < self.loss_thresh:
print(f'Early stopping recovery in iteration {iteration}!')
break
if dryrun:
break
except KeyboardInterrupt:
print(f'Recovery interrupted manually in iteration {iteration}!')
pass
return x_trial.detach(), labels
def _gradient_closure(self, optimizer, x_trial, input_gradient, label):
def closure():
optimizer.zero_grad()
self.model.zero_grad()
loss = self.loss_fn(self.model(x_trial)[0], label)
gradient = torch.autograd.grad(loss, self.model.parameters(), create_graph=True)
rec_loss = reconstruction_costs([gradient], input_gradient,
cost_fn=self.config['cost_fn'], indices=self.config['indices'],
weights=self.config['weights'])
if self.config['total_variation'] > 0:
rec_loss += self.config['total_variation'] * TV(x_trial)
if self.config['l2_norm'] > 0:
rec_loss += self.config['l2_norm'] * torch.norm(x_trial, p=2)
rec_loss.backward()
if self.config['signed']:
x_trial.grad.sign_()
return rec_loss
return closure
def _score_trial(self, x_trial, input_gradient, label):
if self.config['scoring_choice'] == 'loss':
self.model.zero_grad()
x_trial.grad = None
loss = self.loss_fn(self.model(x_trial)[0], label)
gradient = torch.autograd.grad(loss, self.model.parameters(), create_graph=False)
return reconstruction_costs([gradient], input_gradient,
cost_fn=self.config['cost_fn'], indices=self.config['indices'],
weights=self.config['weights'])
elif self.config['scoring_choice'] == 'tv':
return TV(x_trial)
elif self.config['scoring_choice'] == 'inception':
# We do not care about diversity here!
return self.inception(x_trial)
elif self.config['scoring_choice'] in ['pixelmean', 'pixelmedian']:
return 0.0
else:
raise ValueError()
def _average_trials(self, x, labels, input_data, stats):
print(f'Computing a combined result via {self.config["scoring_choice"]} ...')
if self.config['scoring_choice'] == 'pixelmedian':
x_optimal, _ = x.median(dim=0, keepdims=False)
elif self.config['scoring_choice'] == 'pixelmean':
x_optimal = x.mean(dim=0, keepdims=False)
self.model.zero_grad()
if self.reconstruct_label:
labels = self.model(x_optimal)[0].softmax(dim=1)
loss = self.loss_fn(self.model(x_optimal)[0], labels)
gradient = torch.autograd.grad(loss, self.model.parameters(), create_graph=False)
stats['opt'] = reconstruction_costs([gradient], input_data,
cost_fn=self.config['cost_fn'],
indices=self.config['indices'],
weights=self.config['weights'])
print(f'Optimal result score: {stats["opt"]:2.4f}')
return x_optimal, stats
def reconstruction_costs(gradients, input_gradient, cost_fn='l2', indices='def', weights='equal'):
"""Input gradient is given data."""
if isinstance(indices, list):
pass
elif indices == 'def':
indices = torch.arange(len(input_gradient))
elif indices == 'batch':
indices = torch.randperm(len(input_gradient))[:8]
elif indices == 'topk-1':
_, indices = torch.topk(torch.stack([p.norm() for p in input_gradient], dim=0), 4)
elif indices == 'top10':
_, indices = torch.topk(torch.stack([p.norm() for p in input_gradient], dim=0), 10)
elif indices == 'top20':
_, indices = torch.topk(torch.stack([p.norm() for p in input_gradient], dim=0), 20)
elif indices == 'top30':
_, indices = torch.topk(torch.stack([p.norm() for p in input_gradient], dim=0), 30)
elif indices == 'top50':
_, indices = torch.topk(torch.stack([p.norm() for p in input_gradient], dim=0), 50)
elif indices in ['first', 'first4']:
indices = torch.arange(0, 4)
elif indices == 'first5':
indices = torch.arange(0, 5)
elif indices == 'first10':
indices = torch.arange(0, 10)
elif indices == 'first50':
indices = torch.arange(0, 50)
elif indices == 'last5':
indices = torch.arange(len(input_gradient))[-5:]
elif indices == 'last10':
indices = torch.arange(len(input_gradient))[-10:]
elif indices == 'last50':
indices = torch.arange(len(input_gradient))[-50:]
else:
raise ValueError()
ex = input_gradient[0]
if weights == 'linear':
weights = torch.arange(len(input_gradient), 0, -1, dtype=ex.dtype, device=ex.device) / len(input_gradient)
elif weights == 'exp':
weights = torch.arange(len(input_gradient), 0, -1, dtype=ex.dtype, device=ex.device)
weights = weights.softmax(dim=0)
weights = weights / weights[0]
else:
weights = input_gradient[0].new_ones(len(input_gradient))
total_costs = 0
for trial_gradient in gradients:
pnorm = [0, 0]
costs = 0
if indices == 'topk-2':
_, indices = torch.topk(torch.stack([p.norm().detach() for p in trial_gradient], dim=0), 4)
for i in indices:
if cost_fn == 'l2':
costs += ((trial_gradient[i] - input_gradient[i]).pow(2)).sum() * weights[i]
elif cost_fn == 'l1':
costs += ((trial_gradient[i] - input_gradient[i]).abs()).sum() * weights[i]
elif cost_fn == 'max':
costs += ((trial_gradient[i] - input_gradient[i]).abs()).max() * weights[i]
elif cost_fn == 'sim':
costs -= (trial_gradient[i] * input_gradient[i]).sum() * weights[i]
pnorm[0] += trial_gradient[i].pow(2).sum() * weights[i]
pnorm[1] += input_gradient[i].pow(2).sum() * weights[i]
else:
raise NotImplementedError('Cost function not implemented.')
if cost_fn == 'sim':
costs = 1 + costs / pnorm[0].sqrt() / pnorm[1].sqrt()
# Accumulate final costs
total_costs += costs
return total_costs / len(gradients)