-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpatch_trainer.py
300 lines (255 loc) · 12.8 KB
/
patch_trainer.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
import torch
import torchvision
from torchvision.transforms import Normalize
import datetime
import image_processing.image_processing as i
import utils.utils as u
import constants.constants as c
import transformation.transformation as t
import printability.new_printability as pt
import total_variation.new_total_variation as tv
import pickle
from configs import config
from PIL import Image
import numpy as np
class PatchTrainer():
def __init__(self, config=config,
path_image_init=None,
target_class=1,
flee_class=0,
patch_relative_size=0.05,
n_epochs=2):
self.pretty_printer = u.PrettyPrinter(self)
self.date = datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S")
u.setup_config(config)
self.pretty_printer.print_config(config)
self.path_image_init = path_image_init
self.target_class = target_class
self.flee_class = flee_class
self.patch_relative_size = patch_relative_size
self.n_epochs = n_epochs
self.model = u.load_model()
self.model.eval()
self.train_loader, self.test_loader = u.load_dataset()
image_size = c.consts["IMAGE_DIM"] ** 2
patch_size = image_size * self.patch_relative_size
self.patch_dim = int(patch_size ** (0.5))
if self.patch_dim % 2 != 0 :
self.patch_dim -= 1
self.r0, self.c0 = c.consts["IMAGE_DIM"]//2 - self.patch_dim//2, \
c.consts["IMAGE_DIM"]//2 - self.patch_dim//2
self.normalize = Normalize(c.consts["NORMALIZATION_MEAN"],
c.consts["NORMALIZATION_STD"])
self.patch_processing_module = i.PatchProcessingModule()
self.transfo_tool = t.TransformationTool(self.patch_dim)
self.print_module = pt.PrintabilityModule(self.patch_dim)
self.tv_module = tv.TotalVariationModule()
self.patch = self._random_patch_init()
self.target_proba_train = {}
self.success_rate_test = {}
self.patches = {}
def _random_patch_init(self):
patch = torch.zeros(1, 3, c.consts["IMAGE_DIM"], c.consts["IMAGE_DIM"])
if torch.cuda.is_available():
patch = patch.to(torch.device("cuda"))
if self.path_image_init is not None :
image_init = u.array_to_tensor(np.asarray(Image.open(self.path_image_init))/255)
resize = torchvision.transforms.Resize((self.patch_dim, self.patch_dim))
patch[:, :, self.r0:self.r0 + self.patch_dim,
self.c0:self.c0 + self.patch_dim] = resize(image_init)
else :
rand = torch.rand(3, self.patch_dim, self.patch_dim) + 1e-5
patch[:, :, self.r0:self.r0 + self.patch_dim,
self.c0:self.c0 + self.patch_dim] = rand
return patch
def test_model(self):
success, total = 0, 0
for loader in [self.train_loader, self.test_loader]:
for batch, labels in loader:
if torch.cuda.is_available():
batch = batch.to(torch.device("cuda"))
output = self.model(self.normalize(batch))
success += (labels == output.argmax(1)).sum()
total += len(labels)
print('sucess/total : %d/%d accuracy : %.2f'
% (success, total, (100 * success / float(total))))
@staticmethod
def _get_mask(patch):
mask = torch.zeros_like(patch)
if torch.cuda.is_available():
mask = mask.to(torch.device("cuda"))
mask[patch != 0] = 1
return mask
def _get_patch(self):
return self.patch[:, :, self.r0:self.r0 + self.patch_dim,
self.c0:self.c0 + self.patch_dim]
def _apply_specific_grads(self):
patch_ = self._get_patch()
patch_.requires_grad = True
loss = self.print_module(patch_)
loss.backward()
print_grad = patch_.grad.clone()
patch_.grad.zero_()
loss = self.tv_module(patch_)
loss.backward()
tv_grad = patch_.grad.clone()
patch_.requires_grad = False
patch_ -= c.consts["LAMBDA_TV"] * tv_grad + c.consts["LAMBDA_PRINT"] * print_grad
self.patch[:, :, self.r0:self.r0 + self.patch_dim,
self.c0:self.c0 + self.patch_dim] = patch_
def attack(self, batch):
transformed, map_ = self.transfo_tool.random_transform(self.patch)
mask = self._get_mask(transformed)
transformed.requires_grad = True
for i in range(c.consts["MAX_ITERATIONS"] + 1) :
modified = self.patch_processing_module(transformed)
attacked = torch.mul(1 - mask, batch) + \
torch.mul(mask, modified)
normalized = self.normalize(attacked)
vector_scores = self.model(normalized)
vector_proba = torch.nn.functional.softmax(vector_scores, dim=1)
target_proba = float(torch.mean(vector_proba[:, self.target_class]))
if i == 0:
first_target_proba = target_proba
successes = len(batch[vector_proba[:, self.target_class] >= c.consts["THRESHOLD"]])
else:
self.pretty_printer.update_iteration(i, target_proba)
if target_proba >= c.consts["THRESHOLD"] :
break
if self.flee_class is not None :
loss = -torch.nn.functional.log_softmax(vector_scores, dim=1)
torch.mean(loss[:, self.target_class]).backward(retain_graph=True)
target_grad = transformed.grad.clone()
transformed.grad.zero_()
torch.mean(loss[:, self.flee_class]).backward()
with torch.no_grad():
transformed -= target_grad - transformed.grad
transformed.clamp_(0, 1)
else :
loss = -torch.nn.functional.log_softmax(vector_scores, dim=1)
torch.mean(loss[:, self.target_class]).backward()
with torch.no_grad():
transformed -= transformed.grad
transformed.clamp_(0, 1)
transformed.grad.zero_()
self.patch = self.transfo_tool.undo_transform(self.patch, transformed.detach(),
map_)
self._apply_specific_grads()
self.patch.clamp_(0, 1)
return first_target_proba, successes, normalized
def train(self):
self.pretty_printer.training()
for epoch in range(self.n_epochs):
total, successes = 0, 0
self.target_proba_train[epoch] = []
i = 0
for batch, true_labels in self.train_loader:
if torch.cuda.is_available():
batch = batch.to(torch.device("cuda"))
true_labels = true_labels.to(torch.device("cuda"))
vector_scores = self.model(self.normalize(batch))
model_labels = torch.argmax(vector_scores, axis=1)
logical = torch.logical_and(model_labels == true_labels,
model_labels != self.target_class)
batch = batch[logical]
true_labels = true_labels[logical]
if len(batch) == 0:
continue
if total == 0 :
success_rate = None
else :
success_rate = 100 * (successes / float(total))
self.pretty_printer.update_batch(epoch, success_rate, i, len(batch))
total += len(batch)
self.patch_processing_module.jitter()
first_target_proba, s, attacked = self.attack(batch)
successes += s
self.target_proba_train[epoch].append(first_target_proba)
if i % c.consts["N_ENREG_IMG"] == 0:
torchvision.utils.save_image(batch[0],
c.consts["PATH_IMG_FOLDER"] +
'epoch%d_batch%d_label%d_original.png'
% (epoch, i, true_labels[0]))
torchvision.utils.save_image(attacked[0],
c.consts["PATH_IMG_FOLDER"] +
'epoch%d_batch%d_attacked.png'
% (epoch, i))
torchvision.utils.save_image(self._get_patch(),
c.consts["PATH_IMG_FOLDER"] +
'epoch%d_batch%d_patch.png'
% (epoch, i))
i += 1
if c.consts["LIMIT_TRAIN_EPOCH_LEN"] != -1 and \
i >= c.consts["LIMIT_TRAIN_EPOCH_LEN"] :
break
self.test(epoch)
def test(self, epoch):
total, successes = 0, 0
i = 0
for batch, true_labels in self.test_loader:
if torch.cuda.is_available():
batch = batch.to(torch.device("cuda"))
true_labels = true_labels.to(torch.device("cuda"))
vector_scores = self.model(self.normalize(batch))
model_labels = torch.argmax(vector_scores, axis=1)
logical = torch.logical_and(model_labels == true_labels,
model_labels != self.target_class)
batch = batch[logical]
if len(batch) == 0:
continue
total += len(batch)
normalized, vector_scores = self.attack_batch(batch)
self.patch_processing_module.jitter()
attacked_label = torch.argmax(vector_scores, axis=1)
vector_proba = torch.nn.functional.softmax(vector_scores, dim=1)
target_proba = vector_proba[:, self.target_class]
successes += len(batch[attacked_label == self.target_class])
if i % c.consts["N_ENREG_IMG"] == 0:
torchvision.utils.save_image(normalized[0], c.consts["PATH_IMG_FOLDER"] +
'test_epoch%d_batch_%dtarget_proba%.2f_label%d.png'
% (epoch, i, target_proba[0], attacked_label[0]))
self.pretty_printer.update_test(epoch, 100*successes/float(total), i, len(batch))
i += 1
if c.consts["LIMIT_TEST_LEN"] != -1 and i >= c.consts["LIMIT_TEST_LEN"] :
break
sucess_rate = 100 * (successes / float(total))
self.success_rate_test[epoch] = sucess_rate
self.patches[epoch] = (self._get_patch(), sucess_rate)
def save_patch(self, path):
self.consts = c.consts
self.model = None
self.train_loader = None
self.test_loader = None
self.best_epoch, best_patch, best_success_rate = 0, self._get_patch(), 0
for e, (patch, success_rate) in self.patches.items() :
if success_rate >= best_success_rate :
best_patch = patch
best_success_rate = success_rate
self.best_epoch = e
self.patch, self.patches = None, None
self.print_loss = float(self.print_module(best_patch))
self.tv_loss = float(self.tv_module(best_patch))
self.best_patch = best_patch.to(torch.device("cpu"))
self.max = float(torch.max(best_patch))
self.min = float(torch.min(best_patch))
self.patch_processing_module = None
self.tv_module = None
self.print_module = None
pickle.dump(self, open(path, "wb"))
def load(self, config=config):
u.setup_config(config)
self.model = u.load_model()
self.patch = torch.zeros(1, 3, c.consts["IMAGE_DIM"], c.consts["IMAGE_DIM"])
self.patch[:, :, self.r0:self.r0 + self.patch_dim,
self.c0:self.c0 + self.patch_dim] = self.best_patch
self.patch_processing_module = i.PatchProcessingModule()
self.tv_module = tv.TotalVariationModule()
self.print_module = pt.PrintabilityModule(self.patch_dim)
def attack_batch(self, batch):
transformed, _ = self.transfo_tool.random_transform(self.patch)
mask = self._get_mask(transformed)
modified = self.patch_processing_module(transformed)
attacked = torch.mul(1 - mask, batch) + torch.mul(mask, modified)
normalized = self.normalize(attacked)
vector_scores = self.model(normalized)
return normalized, vector_scores