-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtrain_patch_mbntv2_ssd_lite.py
327 lines (252 loc) · 15.4 KB
/
train_patch_mbntv2_ssd_lite.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
321
322
"""
Training code for Adversarial patch training
"""
import PIL
import load_data
from tqdm import tqdm
from load_data import *
import gc
import matplotlib.pyplot as plt
from torch import autograd
from torchvision import transforms
from torch.autograd import Variable
# from tensorboardX import SummaryWriter
import subprocess
import patch_config
import sys
import time
import os
#from lib_ssd.modeling.model_builder import create_model
#from lib_ssd.utils.config_parse import cfg, cfg_from_file
from vision.ssd.mobilenet_v2_ssd_lite import create_mobilenetv2_ssd_lite, create_mobilenetv2_ssd_lite_predictor
from vision.ssd.config import mobilenetv1_ssd_config
if __name__ == '__main__':
class PatchTrainer(object):
os.environ['CUDA_VISIBLE_DEVICES'] = '2'
def __init__(self, mode):
self.config = patch_config.patch_configs[mode]() # select the mode for the patch
# load cfg file (.yaml) and override default cfg options in lib_ssd.utils.config_parse
# cfg_from_file(self.config.cfgfile_ssds)
# self.cfgfile_ssds = cfg
self.device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
print(torch.cuda.device_count())
#self.darknet_model = Darknet(self.config.cfgfile)
#self.darknet_model.load_weights(self.config.weightfile)
#self.mbntv2_ssdlite_model, self.priorbox = create_model(self.cfgfile_ssds.MODEL) # COCO
#self.priors = Variable(self.priorbox.forward(), volatile=True) # num_priors = grid x grid x num_anchors
self.mbntv2_ssdlite_model = create_mobilenetv2_ssd_lite(21, is_test=True) # VOC
self.mbntv2_ssdlite_model.load(self.config.ssdlitembntv2_model_path)
if use_cuda:
#self.darknet_model = self.darknet_model.eval().to(self.device) # Why eval? test!
self.mbntv2_ssdlite_model = self.mbntv2_ssdlite_model.eval().to(self.device)
self.patch_applier = PatchApplier().to(self.device)
self.patch_transformer = PatchTransformer().to(self.device)
#self.prob_extractor = MaxProbExtractor(0, 80, self.config).to(self.device)
self.score_extractor_ssd = ssd_feature_output_manage(15, 21, self.config).to(self.device) # 15 is person class in VOC (with 21 elements)
self.nps_calculator = NPSCalculator(self.config.printfile, self.config.patch_size).to(self.device)
self.total_variation = TotalVariation().to(self.device)
else:
#self.darknet_model = self.darknet_model.eval() # Why eval? test!
self.mbntv2_ssdlite_model = self.mbntv2_ssdlite_model.eval()
self.patch_applier = PatchApplier()
self.patch_transformer = PatchTransformer()
#self.prob_extractor = MaxProbExtractor(0, 80, self.config)
self.score_extractor_ssd = ssd_feature_output_manage(15, 21, self.config).to(self.device)
self.nps_calculator = NPSCalculator(self.config.printfile, self.config.patch_size)
self.total_variation = TotalVariation()
# __________________________________________________________________________________________________________________________________-
# self.writer = self.init_tensorboard(mode)
# def init_tensorboard(self, name=None):
# subprocess.Popen(['tensorboard', '--logdir=runs'])
# if name is not None:
# time_str = time.strftime("%Y%m%d-%H%M%S")
# return SummaryWriter(f'runs/{time_str}_{name}')
# else:
# return SummaryWriter()
# ___________________________________________________________________________________________________________________________________
def train(self):
"""
Optimize a patch to generate an adversarial example.
:return: Nothing
"""
destination_path = "./"
destination_name = 'loss_tracking_mbntv2ssdlite_wmeanstd_objcls_1k_p200_thresh.txt'
destination_name2 = 'loss_tracking_compact_batch_mbntv2ssdlite_wmeanstd_objcls_1k_p200_thresh.txt'
destination_name3 = 'loss_tracking_compatc_epochs_mbntv2ssdlite_wmeanstd_objcls_1k_p200_thresh.txt'
destination = os.path.join(destination_path, destination_name)
destination2 = os.path.join(destination_path, destination_name2)
destination3 = os.path.join(destination_path, destination_name3)
textfile = open(destination, 'w+')
textfile2 = open(destination2, 'w+')
textfile3 = open(destination3, 'w+')
img_size = mobilenetv1_ssd_config.image_size # 300 for ssd family
# self.device = 'cuda' if torch.cuda.is_available() else 'cpu'
# self.darknet_model = torch.nn.DataParallel(self.darknet_model)
# self.darknet_model.to(self.device)
batch_size = self.config.batch_size
n_epochs = 1000
max_lab = 14
#time_str = time.strftime("%Y%m%d-%H%M%S")
# Generate starting point
adv_patch_cpu = self.generate_patch("gray")
# adv_patch_cpu = self.read_image("saved_patches/patchnew0.jpg")
adv_patch_cpu.requires_grad_(True)
train_loader = torch.utils.data.DataLoader(
InriaDataset(self.config.img_dir, self.config.lab_dir, max_lab, img_size,
shuffle=True),
batch_size=batch_size,
shuffle=True,
num_workers=10)
self.epoch_length = len(train_loader)
print(f'One epoch is {len(train_loader)}')
optimizer = optim.Adam([adv_patch_cpu], lr=self.config.start_learning_rate,
amsgrad=True) # starting lr = 0.03
scheduler = self.config.scheduler_factory(optimizer)
# scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'min', patience=50) # write it directly
et0 = time.time() # epoch start
for epoch in range(n_epochs):
ep_det_loss = 0
ep_nps_loss = 0
ep_tv_loss = 0
ep_loss = 0
bt0 = time.time() # batch start
for i_batch, (img_batch, lab_batch) in tqdm(enumerate(train_loader), desc=f'Running epoch {epoch}',
total=self.epoch_length):
with autograd.detect_anomaly():
if use_cuda:
img_batch = img_batch.to(self.device)
lab_batch = lab_batch.to(self.device)
adv_patch = adv_patch_cpu.to(self.device)
else:
img_batch = img_batch
lab_batch = lab_batch
adv_patch = adv_patch_cpu
adv_batch_t = self.patch_transformer(adv_patch, lab_batch, img_size, do_rotate=True, rand_loc=False)
p_img_batch = self.patch_applier(img_batch, adv_batch_t)
p_img_batch = F.interpolate(p_img_batch, (img_size, img_size))
# output = self.darknet_model(p_img_batch) # TODO apply YOLOv2 to all (patched) images in the batch (6)
#output = self.mbntv2_ssdlite_model(p_img_batch, phase='eval')
#image preprocessing: mean, std_dev
#image_mean = torch.FloatTensor([127, 127, 127]).to(self.device)
image_mean = 127.0
#image_mean = torch.full((3, image_size, image_size), 0.5*255)
#image_mean = torch.from_numpy(image_mean)
image_std = 128.0
p_img_batch = p_img_batch*255 #bring in the range 0-255 for pre-processing
p_img_batch = (p_img_batch - image_mean)/image_std #subtract mean, divide for std ---> range -1 to 1
output = self.mbntv2_ssdlite_model.forward(p_img_batch)
loss_type = "max_approach"
#max_prob = self.prob_extractor(output)
score_ssdlite_mbntv2 = self.score_extractor_ssd(output, loss_type)
nps = self.nps_calculator(adv_patch)
tv = self.total_variation(adv_patch)
nps_loss = nps * 0.01
tv_loss = tv * 2.5
# batch_op: mean, max...
det_loss = torch.mean(score_ssdlite_mbntv2)
if use_cuda:
loss = det_loss + nps_loss + torch.max(tv_loss, torch.tensor(0.1).to(self.device))
else:
loss = det_loss + nps_loss + torch.max(tv_loss, torch.tensor(0.1))
ep_det_loss += det_loss.detach().cpu().numpy() / len(train_loader)
ep_nps_loss += nps_loss.detach().cpu().numpy()
ep_tv_loss += tv_loss.detach().cpu().numpy()
ep_loss += loss
# Optimization step + backward
loss.backward()
optimizer.step()
optimizer.zero_grad()
adv_patch_cpu.data.clamp_(0, 1) # keep patch in image range
bt1 = time.time() # batch end
if i_batch % 1 == 0:
# Plot the adversarial patch in learning phase during one epoch for each batch (remember one batch = 6 images, around 100 batches in tot)
im = transforms.ToPILImage('RGB')(adv_patch_cpu)
#plt.imshow(im)
#plt.show()
# Plot the adv patch in learning phase during one epoch applied on one image of the six composing a single batch.
# In total there are 100 batches, i.e. 6 images are picked for 100 times, and this is one epoch. In total, there are 10000 epochs.
# img = p_img_batch[1, :, :, ]
# img = transforms.ToPILImage()(img.detach().cpu())
# img.show()
#iteration = self.epoch_length * epoch + i_batch
print(' BATCH NR: ', i_batch)
print('BATCH LOSS: ', loss) # .detach().cpu().numpy())
print(' DET LOSS: ', det_loss) # .detach().cpu().numpy())
print(' NPS LOSS: ', nps_loss) # .detach().cpu().numpy())
print(' TV LOSS: ', tv_loss) # .detach().cpu().numpy())
print('BATCH TIME: ', bt1 - bt0)
textfile.write(f'i_batch: {i_batch}\nb_tot_loss:{loss}\nb_det_loss: {det_loss}\nb_nps_loss: {nps_loss}\nb_TV_loss: {tv_loss}\n\n')
textfile2.write(f'{i_batch} {loss} {det_loss} {nps_loss} {tv_loss}\n')
# self.writer.add_scalar('total_loss', loss.detach().cpu().numpy(), iteration)
# self.writer.add_scalar('loss/det_loss', det_loss.detach().cpu().numpy(), iteration)
# self.writer.add_scalar('loss/nps_loss', nps_loss.detach().cpu().numpy(), iteration)
# self.writer.add_scalar('loss/tv_loss', tv_loss.detach().cpu().numpy(), iteration)
# self.writer.add_scalar('misc/epoch', epoch, iteration)
# self.writer.add_scalar('misc/learning_rate', optimizer.param_groups[0]["lr"], iteration)
# self.writer.add_scalar('batch_time', bt1-bt0, iteration)
# self.writer.add_image('patch', adv_patch_cpu, iteration)
if i_batch + 1 >= len(train_loader):
print('\n')
else:
del adv_batch_t, output, score_ssdlite_mbntv2, det_loss, p_img_batch, nps_loss, tv_loss, loss
if use_cuda:
torch.cuda.empty_cache()
bt0 = time.time()
et1 = time.time() # epoch end
ep_det_loss = ep_det_loss / len(train_loader)
ep_nps_loss = ep_nps_loss / len(train_loader)
ep_tv_loss = ep_tv_loss / len(train_loader)
ep_loss = ep_loss / len(train_loader)
scheduler.step(ep_loss)
if True:
print(' EPOCH NR: ', epoch),
print('EPOCH LOSS: ', ep_loss)
print(' DET LOSS: ', ep_det_loss)
print(' NPS LOSS: ', ep_nps_loss)
print(' TV LOSS: ', ep_tv_loss)
print('EPOCH TIME: ', et1 - et0)
textfile.write(f'\ni_epoch: {epoch}\ne_total_loss:{ep_loss}\ne_det_loss: {ep_det_loss}\ne_nps_loss: {ep_nps_loss}\ne_TV_loss: {ep_tv_loss}\n\n')
textfile3.write(f'{epoch} {ep_loss} {ep_det_loss} {ep_nps_loss} {ep_tv_loss}\n')
# Plot the final adv_patch (learned) and save it
im = transforms.ToPILImage('RGB')(adv_patch_cpu)
# plt.imshow(im)
# plt.show()
im.save("./saved_patches_mytrial/mbntv2_ssdlite_max_objcls_thresh_1000epochs_patchsize200_meanstd.jpg")
del adv_batch_t, output, score_ssdlite_mbntv2, det_loss, p_img_batch, nps_loss, tv_loss, loss
if use_cuda:
torch.cuda.empty_cache()
et0 = time.time()
# TODO __________________________________________________________________________________________________________________________________________________
def generate_patch(self, type):
"""
Generate a random patch as a starting point for optimization.
:param type: Can be 'gray' or 'random'. Whether or not generate a gray or a random patch.
:return:
"""
if type == 'gray':
adv_patch_cpu = torch.full((3, self.config.patch_size, self.config.patch_size), 0.5)
elif type == 'random':
adv_patch_cpu = torch.rand((3, self.config.patch_size, self.config.patch_size))
return adv_patch_cpu
def read_image(self, path):
"""
Read an input image to be used as a patch
:param path: Path to the image to be read.
:return: Returns the transformed patch as a pytorch Tensor.
"""
patch_img = Image.open(path).convert('RGB')
tf = transforms.Resize((self.config.patch_size, self.config.patch_size))
patch_img = tf(patch_img)
tf = transforms.ToTensor()
adv_patch_cpu = tf(patch_img)
return adv_patch_cpu
# def main():
# if len(sys.argv) != 2:
# print('You need to supply (only) a configuration mode.')
# print('Possible modes are:')
# print(patch_config.patch_configs)
#
#
use_cuda = 1
trainer = PatchTrainer('paper_obj')
trainer.train()