-
Notifications
You must be signed in to change notification settings - Fork 5
/
digital_test.py
831 lines (665 loc) · 36.2 KB
/
digital_test.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 17 13:53:37 2022
A script that saves the objectness score of cars detected into json files. These
files are used to calculate AORR in aorr.py script.
@author: andrew
"""
import pickle
import sys
import time
import os
import torch
torch.cuda.set_device(0) # select gpu to run on
import torch.optim as optim
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import Dataset
from torchvision import transforms
from PIL import Image, ImageDraw
from utils import *
from darknet import *
from load_data import PatchTransformations, PatchApplier, LoadDataset
import json
import random
import weather
# Set random seed for reproducibility
torch.backends.cudnn.deterministic = True
random.seed(0)
torch.manual_seed(0)
torch.cuda.manual_seed(0)
np.random.seed(0)
###############################################################################
############################### ATTACK SETTINGS ###############################
###############################################################################
savedir = "digital_test"
scene = 'sidestreet' # sidestreet, carpark
mode = 'adversarial' # clean, adversarial, random
weather_augmentations = 'on' # on, off
setting = 'test' # test, train
# select a patch to evaluate
patch_name = 'saved_patches/sidestreet-on-gc' # exp12
# patch_name = 'saved_patches/sidestreet-on-gcw' # exp11
# patch_name = 'saved_patches/sidestreet-off-gc' # exp6C
# patch_name = 'saved_patches/carpark-on-gc' # exp04
# patch_name = 'saved_patches/carpark-on-gcw' # exp03
patch_num = 1 # 1 for ON patches, 3 for OFF patches
###############################################################################
###############################################################################
###############################################################################
# model parameters
cfgfile = "cfg/yolov3-cowc.cfg"
weightfile = "weights/yolov3-cowc-256/yolov3-cowc_best_256.weights"
namesfile = "data/cowc.names"
# load model
darknet_model = Darknet(cfgfile)
darknet_model.load_weights(weightfile)
darknet_model = darknet_model.eval().cuda()
# patch functions
patch_applier = PatchApplier().cuda()
patch_transformations = PatchTransformations().cuda()
# define model parameters
batch_size = 1
max_lab = 10
img_size = darknet_model.height
# load patch
patch_list = []
for i in range(patch_num):
patchfile = f"{patch_name}/patch_{i}.jpg"
patch_img = Image.open(patchfile).convert('RGB')
tf = transforms.ToTensor()
adv_patch_cpu = tf(patch_img)
adv_patch = adv_patch_cpu.cuda()
adv_patch = adv_patch.unsqueeze(0)
patch_list.append(adv_patch)
if patch_num == 1:
adv_patch = torch.cat([patch_list[0]], dim=0)
elif patch_num == 2 or patch_num == 3:
adv_patch = torch.cat([patch_list[0], patch_list[1], patch_list[2]], dim=0)
# define list
clean_results = []
random_results = []
patch_results = []
object_score = []
final_results = []
print("Done")
###########################################################################################################################
###########################################################################################################################
###########################################################################################################################
if mode == 'clean':
"CLEAN IMAGE"
# create folders to save results
if setting == 'train':
imgdir = f'#{scene}/data/train_images' # select folder of full sized images to run detection on
if weather_augmentations == 'off':
folder1 = 'clean_train'
folder2 = 'clean_train_bb'
else:
folder1 = 'clean_train_weather'
folder2 = 'clean_train_weather_bb'
elif setting == 'test':
imgdir = f'#{scene}/data/test_images' # select folder of full sized images to run detection on
if weather_augmentations == 'off':
folder1 = 'clean_test'
folder2 = 'clean_test_bb'
else:
folder1 = 'clean_test_weather'
folder2 = 'clean_test_weather_bb'
os.makedirs(f'{savedir}/{scene}/{mode}/' + folder1)
os.makedirs(f'{savedir}/{scene}/{mode}/' + folder1 + '/yolo-labels')
os.makedirs(f'{savedir}/{scene}/{mode}/' + folder2)
for imgfile in os.listdir(imgdir):
print("new clean image")
if imgfile.endswith('.jpg') or imgfile.endswith('.png'):
# clean image file without extension (e.g. .jpg or .png)
name = os.path.splitext(imgfile)[0]
# label file
txtname = name + '.txt'
# directory path of label file (to save in)
if setting == 'train':
txtpath = os.path.abspath(os.path.join(savedir, scene, mode, folder1, 'yolo-labels/', txtname))
elif setting == 'test':
txtpath = os.path.abspath(os.path.join(savedir, scene, mode, folder1, 'yolo-labels/', txtname))
# directory path of clean image file
imgfile = os.path.abspath(os.path.join(imgdir, imgfile))
# open clean image
img = Image.open(imgfile).convert('RGB')
#######################################################################
# RESIZE IMAGE AND RUN DETECTION
# width and height of clean image
w,h = img.size
# pad clean image with width = height
if w==h:
padded_img = img
else:
dim_to_pad = 1 if w<h else 2
if dim_to_pad == 1:
padding = (h - w) / 2
padded_img = Image.new('RGB', (h,h), color=(127,127,127))
padded_img.paste(img, (int(padding), 0))
else:
padding = (w - h) / 2
padded_img = Image.new('RGB', (w, w), color=(127,127,127))
padded_img.paste(img, (0, int(padding)))
if weather_augmentations == 'on':
""" WEATHER TRANSFORMATION """
# apply weather augmentations
transform = transforms.ToTensor()
padded_img = transform(padded_img).cuda()
padded_img = padded_img.unsqueeze(0)
weather_type = random.randint(0,6)
# print('weather:', weather_type)
if weather_type == 0:
padded_img = weather.brighten(padded_img)
elif weather_type == 1:
padded_img= weather.darken(padded_img)
elif weather_type == 2:
padded_img = weather.add_snow(padded_img)
elif weather_type == 3:
padded_img = weather.add_rain(padded_img)
elif weather_type == 4:
padded_img= weather.add_fog(padded_img)
elif weather_type == 5:
padded_img = weather.add_autumn(padded_img)
elif weather_type == 6:
padded_img = padded_img
# resize clean image
resize = transforms.Resize((img_size,img_size))
padded_img = resize(padded_img)
if weather_augmentations == 'on':
padded_img = padded_img.squeeze(0)
padded_img = transforms.ToPILImage('RGB')(padded_img.cpu())
# clean image file
cleanname = name + ".jpg"
# padded_img.save(os.path.join(savedir, 'clean/', cleanname))
# input clean image into detector
boxes = do_detect(darknet_model, padded_img, 0.50, 0.40, True)
# boxes = do_detect(darknet_model, padded_img, 0.01, 0.40, True)
# save clean image with labels if car is detected
if len(boxes) >= 0:
if setting =='train':
padded_img.save(os.path.join(savedir, scene, mode, folder1, cleanname)) # select folder to save detected images
elif setting == 'test':
padded_img.save(os.path.join(savedir, scene, mode, folder1, cleanname)) # select folder to save detected images
# save label
textfile = open(txtpath,'w+')
for box in boxes:
cls_id = box[6]
if(cls_id != 0):
x_center = box[0]
y_center = box[1]
width = box[2]
height = box[3]
# cls_id = 0 # 0 for json file / yolo labels and comment out for patch location
textfile.write(f'{cls_id} {x_center} {y_center} {width} {height}\n')
clean_results.append({'image_id': name, 'bbox': [x_center - width / 2,
y_center - height / 2,
width,
height],
'obj_score': box[4],
'category_id': 1})
object_score.append(box[4])
textfile.close()
# save image with plot of bounding box
class_names = load_class_names(namesfile)
plot_boxes(padded_img, boxes, f'{savedir}/{scene}/{mode}/{folder2}/{cleanname}', class_names) # select folder to save detected images
if setting == 'train':
if weather_augmentations == 'off':
with open(f'{savedir}/{scene}/{mode}/clean_train_detections.json', 'w') as fp:
json.dump(clean_results, fp)
else:
with open(f'{savedir}/{scene}/{mode}/clean_train_w_detections.json', 'w') as fp:
json.dump(clean_results, fp)
elif setting == 'test':
if weather_augmentations == 'off':
with open(f'{savedir}/{scene}/{mode}/clean_test_detections.json', 'w') as fp:
json.dump(clean_results, fp)
else:
with open(f'{savedir}/{scene}/{mode}/clean_test_w_detections.json', 'w') as fp:
json.dump(clean_results, fp)
# print('length of object score list:', len(object_score))
# print('average objectness score:', sum(object_score)/len(object_score))
###########################################################################################################################
###########################################################################################################################
###########################################################################################################################
elif mode == 'adversarial':
"ADVERSARIAL PATCH"
# create folders to save results
if setting == 'test':
imgdir = f'#{scene}/data/test_images' # select folder of images to run detection on
if weather_augmentations == 'off':
folder1 = 'patch_test'
folder2 = 'patch_test_bb'
else:
folder1 = 'patch_test_weather'
folder2 = 'patch_test_weather_bb'
os.makedirs(f'{savedir}/{scene}/{mode}/' + folder1)
os.makedirs(f'{savedir}/{scene}/{mode}/' + folder1 + '/yolo-labels')
os.makedirs(f'{savedir}/{scene}/{mode}/' + folder2)
for imgfile in os.listdir(imgdir):
print("new patched image")
if imgfile.endswith('.jpg') or imgfile.endswith('.png'):
# clean image file without extension (e.g. .jpg)
name = os.path.splitext(imgfile)[0]
# label file
txtname = name + '.txt'
# directory path of label file (to load from)
if setting =='test':
if weather_augmentations == 'off':
txtpath = os.path.abspath(os.path.join(f'#{scene}', 'data', 'test_labels', 'noweather', 'yolo-labels/', txtname))
else:
txtpath = os.path.abspath(os.path.join(f'#{scene}', 'data', 'test_labels', 'weather', 'yolo-labels/', txtname))
# load label
label = np.loadtxt(txtpath)
# # check to see if label file contains data.
# if os.path.getsize(txtpath): # file size (in bytes)
# label = np.loadtxt(txtpath)
# else:
# label = np.ones([5])
# convert label (numpy to tensor)
label = torch.from_numpy(label).float()
if label.dim() == 1:
label = label.unsqueeze(0)
# directory path of clean image file
imgfile = os.path.abspath(os.path.join(imgdir, imgfile))
# open clean image (PIL)
img = Image.open(imgfile).convert('RGB')
# width and height of clean image
w,h = img.size
# pad clean image if neccessary
if w==h:
padded_img = img
else:
dim_to_pad = 1 if w<h else 2
if dim_to_pad == 1:
padding = (h - w) / 2
padded_img = Image.new('RGB', (h,h), color=(127,127,127))
padded_img.paste(img, (int(padding), 0))
else:
padding = (w - h) / 2
padded_img = Image.new('RGB', (w, w), color=(127,127,127))
padded_img.paste(img, (0, int(padding)))
# # resize clean image
# resize = transforms.Resize((img_size,img_size))
# padded_img = resize(padded_img)
# convert clean image and its label to tensor
transform = transforms.ToTensor()
padded_img = transform(padded_img).cuda()
img_fake_batch = padded_img.unsqueeze(0)
lab_fake_batch = label.unsqueeze(0).cuda()
if weather_augmentations == 'off':
# apply transformation on patch
if scene == 'sidestreet':
if patch_num == 1:
adv_batch_t = patch_transformations(adv_patch, lab_fake_batch, img.size[0], 40, do_rotate=True, rand_loc=False) # ON patch
elif patch_num == 2 or patch_num == 3:
adv_batch_t = patch_transformations(adv_patch, lab_fake_batch, img.size[0], 160, do_rotate=False, rand_loc=False) # OFF patch
elif scene == 'carpark':
adv_batch_t = patch_transformations(adv_patch, lab_fake_batch, img.size[0], 85, do_rotate=True, rand_loc=False)
# apply patch to clean image
p_img_batch = patch_applier(img_fake_batch, adv_batch_t)
# resize image to 256x256
p_img_batch = F.interpolate(p_img_batch, (darknet_model.height, darknet_model.width))
else:
# only apply weather augmentations on clean images if no detections
if lab_fake_batch.nelement() == 0:
p_img_batch = img_fake_batch
""" WEATHER TRANSFORMATION """
weather_type = random.randint(0,6)
# print('weather:', weather_type)
if weather_type == 0:
p_img_batch = weather.brighten(p_img_batch)
elif weather_type == 1:
p_img_batch = weather.darken(p_img_batch)
elif weather_type == 2:
p_img_batch = weather.add_snow(p_img_batch)
elif weather_type == 3:
p_img_batch = weather.add_rain(p_img_batch)
elif weather_type == 4:
p_img_batch = weather.add_fog(p_img_batch)
elif weather_type == 5:
p_img_batch = weather.add_autumn(p_img_batch)
elif weather_type == 6:
p_img_batch = p_img_batch
# resize image to 256x256
p_img_batch = F.interpolate(p_img_batch, (darknet_model.height, darknet_model.width))
else:
# apply transformation on patch
if scene == 'sidestreet':
if patch_num == 1:
adv_batch_t = patch_transformations(adv_patch, lab_fake_batch, img.size[0], 40, do_rotate=True, rand_loc=False) # ON patch
elif patch_num == 2 or patch_num == 3:
adv_batch_t = patch_transformations(adv_patch, lab_fake_batch, img.size[0], 160, do_rotate=False, rand_loc=False) # OFF patch
elif scene == 'carpark':
adv_batch_t = patch_transformations(adv_patch, lab_fake_batch, img.size[0], 85, do_rotate=True, rand_loc=False)
# apply patch to clean image
p_img_batch = patch_applier(img_fake_batch, adv_batch_t)
""" WEATHER TRANSFORMATION """
weather_type = random.randint(0,6)
# print('weather:', weather_type)
if weather_type == 0:
p_img_batch = weather.brighten(p_img_batch)
elif weather_type == 1:
p_img_batch = weather.darken(p_img_batch)
elif weather_type == 2:
p_img_batch = weather.add_snow(p_img_batch)
elif weather_type == 3:
p_img_batch = weather.add_rain(p_img_batch)
elif weather_type == 4:
p_img_batch = weather.add_fog(p_img_batch)
elif weather_type == 5:
p_img_batch = weather.add_autumn(p_img_batch)
elif weather_type == 6:
p_img_batch = p_img_batch
# resize image to 256x256
p_img_batch = F.interpolate(p_img_batch, (darknet_model.height, darknet_model.width))
# # plot patched image
# img = p_img_batch[0, :, :, :]
# img = transforms.ToPILImage()(img.detach().cpu())
# img.show()
# save patched image
p_img = p_img_batch.squeeze(0)
p_img_pil = transforms.ToPILImage('RGB')(p_img.cpu())
properpatchedname = name + "_p.png"
p_img_pil.save(os.path.join(savedir, scene, mode, folder1, properpatchedname))
# patched label file
txtname = properpatchedname.replace('.png', '.txt')
# directory path of patched label file (save in)
txtpath = os.path.abspath(os.path.join(savedir, scene, mode, folder1, 'yolo-labels/', txtname))
# clean image file
cleanname = name + ".jpg"
# input patched image into detector
# boxes = do_detect(darknet_model, p_img_pil, 0.50, 0.40, True)
boxes = do_detect(darknet_model, p_img_pil, 0.01, 0.40, True)
""" PR curve and AP calculation - remember to set objectness score threshold to 0.01 """
# save labels
textfile = open(txtpath,'w+')
real_boxes = []
for box in boxes:
cls_id = box[6]
if(cls_id != 0): # if car
x_center = box[0]
y_center = box[1]
width = box[2]
height = box[3]
cls_id = 0
textfile.write(f'{cls_id} {x_center} {y_center} {width} {height}\n')
patch_results.append({'image_id': name, 'bbox': [x_center - width / 2,
y_center - height / 2,
width,
height],
'obj_score': box[4],
'category_id': 1})
textfile.close()
""""""
""" AORR calculation - obtain highest objectness score for each ground truth bounding box - remember to set objectness score threshold to 0.01 """
ground = []
final_boxes = []
for i in range(lab_fake_batch.shape[1]):
ground.append(lab_fake_batch[0][i].tolist())
# compare ground truth against yolo detections
if ground[0] != []:
tolerance = 0.05
for i in range(len(ground)):
temp_boxes = []
for box in boxes:
# compare centre points (could also use IOU here)
# x centre
if abs(ground[i][1] - box[0]) < tolerance:
# y centre
if abs(ground[i][2] - box[1]) < tolerance:
temp_boxes.append(box)
# remove multiple detections of same object (car)
if len(temp_boxes) == 0:
final_boxes.append([ground[i][1], ground[i][2], ground[i][3], ground[i][4], 0, 0, 0])
final_results.append({'image_id': name,
'centre_points': [ground[i][1], ground[i][2]],
'obj_score': 0})
elif len(temp_boxes) == 1:
final_boxes.append(temp_boxes[0])
final_results.append({'image_id': name,
'centre_points': [temp_boxes[0][0], temp_boxes[0][1]],
'obj_score': temp_boxes[0][4]})
elif len(temp_boxes) > 1:
from operator import itemgetter
def max_val(l, i):
return max(enumerate(map(itemgetter(i), l)),key=itemgetter(1))
index, obj_score = max_val(temp_boxes, -3)
final_boxes.append(temp_boxes[index])
final_results.append({'image_id': name,
'centre_points': [temp_boxes[index][0], temp_boxes[index][1]],
'obj_score': temp_boxes[index][4]})
""""""
# save image with plot of bounding box
class_names = load_class_names(namesfile)
plot_boxes(p_img_pil, final_boxes, f'{savedir}/{scene}/{mode}/{folder2}/{cleanname}', class_names)
# plot_boxes(p_img_pil, boxes, f'{savedir}/{scene}/{mode}/{folder2}/{cleanname}', class_names)
if weather_augmentations == 'off':
with open(f'{savedir}/{scene}/{mode}/patch_test_detections.json', 'w') as fp:
json.dump(final_results, fp)
else:
with open(f'{savedir}/{scene}/{mode}/patch_test_w_detections.json', 'w') as fp:
json.dump(final_results, fp)
###########################################################################################################################
###########################################################################################################################
###########################################################################################################################
elif mode == 'random':
"RANDOM PATCH"
random_patch = torch.rand(adv_patch.size()).cuda()
# create folders to save results
if setting == 'test':
imgdir = f'#{scene}/data/test_images' # select folder of images to run detection on
if weather_augmentations == 'off':
folder1 = 'random_test'
folder2 = 'random_test_bb'
else:
folder1 = 'random_test_weather'
folder2 = 'random_test_weather_bb'
os.makedirs(f'{savedir}/{scene}/{mode}/' + folder1)
os.makedirs(f'{savedir}/{scene}/{mode}/' + folder1 + '/yolo-labels')
os.makedirs(f'{savedir}/{scene}/{mode}/' + folder2)
for imgfile in os.listdir(imgdir):
print("new patched image")
if imgfile.endswith('.jpg') or imgfile.endswith('.png'):
# clean image file without extension (e.g. .jpg)
name = os.path.splitext(imgfile)[0]
# label file
txtname = name + '.txt'
# directory path of label file (load from)
if setting =='test':
if weather_augmentations == 'off':
txtpath = os.path.abspath(os.path.join(f'#{scene}', 'data', 'test_labels', 'noweather', 'yolo-labels/', txtname))
else:
txtpath = os.path.abspath(os.path.join(f'#{scene}', 'data', 'test_labels', 'weather', 'yolo-labels/', txtname))
# load label
label = np.loadtxt(txtpath)
# # check to see if label file contains data.
# if os.path.getsize(txtpath): # file size (in bytes)
# label = np.loadtxt(txtpath)
# else:
# label = np.ones([5])
# convert label (numpy to tensor)
label = torch.from_numpy(label).float()
if label.dim() == 1:
label = label.unsqueeze(0)
# directory path of clean image file
imgfile = os.path.abspath(os.path.join(imgdir, imgfile))
# open clean image (PIL)
img = Image.open(imgfile).convert('RGB')
# width and height of clean image
w, h = img.size
# pad clean image if neccessary
if w == h:
padded_img = img
else:
dim_to_pad = 1 if w < h else 2
if dim_to_pad == 1:
padding = (h - w) / 2
padded_img = Image.new('RGB', (h, h), color=(127, 127, 127))
padded_img.paste(img, (int(padding), 0))
else:
padding = (w - h) / 2
padded_img = Image.new('RGB', (w, w), color=(127, 127, 127))
padded_img.paste(img, (0, int(padding)))
# # resize clean image
# resize = transforms.Resize((img_size,img_size))
# padded_img = resize(padded_img)
# convert clean image and its label to tensor
transform = transforms.ToTensor()
padded_img = transform(padded_img).cuda()
img_fake_batch = padded_img.unsqueeze(0)
lab_fake_batch = label.unsqueeze(0).cuda()
if weather_augmentations == 'off':
# apply transformation on patch
if scene == 'sidestreet':
if patch_num == 1:
adv_batch_t = patch_transformations(random_patch, lab_fake_batch, img.size[0], 40, do_rotate=True, rand_loc=False) # ON patch
elif patch_num == 3:
adv_batch_t = patch_transformations(random_patch, lab_fake_batch, img.size[0], 160, do_rotate=False, rand_loc=False) # OFF patch
elif scene == 'carpark':
adv_batch_t = patch_transformations(random_patch, lab_fake_batch, img.size[0], 85, do_rotate=True, rand_loc=False)
# apply patch to clean image
p_img_batch = patch_applier(img_fake_batch, adv_batch_t)
# resize image to 256x256
p_img_batch = F.interpolate(p_img_batch, (darknet_model.height, darknet_model.width))
else:
# only apply weather augmentations on clean images if no detections
if lab_fake_batch.nelement() == 0:
p_img_batch = img_fake_batch
""" WEATHER TRANSFORMATION """
weather_type = random.randint(0,6)
# print('weather:', weather_type)
if weather_type == 0:
p_img_batch = weather.brighten(p_img_batch)
elif weather_type == 1:
p_img_batch = weather.darken(p_img_batch)
elif weather_type == 2:
p_img_batch = weather.add_snow(p_img_batch)
elif weather_type == 3:
p_img_batch = weather.add_rain(p_img_batch)
elif weather_type == 4:
p_img_batch = weather.add_fog(p_img_batch)
elif weather_type == 5:
p_img_batch = weather.add_autumn(p_img_batch)
elif weather_type == 6:
p_img_batch = p_img_batch
# resize image to 256x256
p_img_batch = F.interpolate(p_img_batch, (darknet_model.height, darknet_model.width))
else:
# apply transformation on patch
if scene == 'sidestreet':
if patch_num == 1:
adv_batch_t = patch_transformations(random_patch, lab_fake_batch, img.size[0], 40, do_rotate=True, rand_loc=False) # ON patch
elif patch_num == 2 or patch_num == 3:
adv_batch_t = patch_transformations(random_patch, lab_fake_batch, img.size[0], 160, do_rotate=False, rand_loc=False) # OFF patch
elif scene == 'carpark':
adv_batch_t = patch_transformations(random_patch, lab_fake_batch, img.size[0], 85, do_rotate=True, rand_loc=False) # ON patch
# apply patch to clean image
p_img_batch = patch_applier(img_fake_batch, adv_batch_t)
""" WEATHER TRANSFORMATION """
weather_type = random.randint(0,6)
# print('weather:', weather_type)
if weather_type == 0:
p_img_batch = weather.brighten(p_img_batch)
elif weather_type == 1:
p_img_batch = weather.darken(p_img_batch)
elif weather_type == 2:
p_img_batch = weather.add_snow(p_img_batch)
elif weather_type == 3:
p_img_batch = weather.add_rain(p_img_batch)
elif weather_type == 4:
p_img_batch = weather.add_fog(p_img_batch)
elif weather_type == 5:
p_img_batch = weather.add_autumn(p_img_batch)
elif weather_type == 6:
p_img_batch = p_img_batch
# resize image to 256x256
p_img_batch = F.interpolate(p_img_batch, (darknet_model.height, darknet_model.width))
# # plot patched image
# img = p_img_batch[0, :, :, :]
# img = transforms.ToPILImage()(img.detach().cpu())
# img.show()
# save patched image
p_img = p_img_batch.squeeze(0)
p_img_pil = transforms.ToPILImage('RGB')(p_img.cpu())
properpatchedname = name + "_r.png"
p_img_pil.save(os.path.join(savedir, scene, mode, folder1, properpatchedname))
# patched label file
txtname = properpatchedname.replace('.png', '.txt')
# directory path of patched label file (save in)
txtpath = os.path.abspath(os.path.join(savedir, scene, mode, folder1, 'yolo-labels/', txtname))
# clean image file
cleanname = name + ".jpg"
# input patched image into detector
boxes = do_detect(darknet_model, p_img_pil, 0.01, 0.40, True) # for json files
""" PR curve and AP calculation - remember to set objectness score threshold to 0.01 """
# save labels
textfile = open(txtpath, 'w+')
real_boxes = []
for box in boxes:
cls_id = box[6]
if(cls_id != 0):
x_center = box[0]
y_center = box[1]
width = box[2]
height = box[3]
cls_id = 0
textfile.write(
f'{cls_id} {x_center} {y_center} {width} {height}\n')
random_results.append({'image_id': name, 'bbox': [x_center - width / 2,
y_center - height / 2,
width,
height],
'score': box[4],
'category_id': 1})
textfile.close()
""""""
""" AORR calculation - obtain highest objectness score for each ground truth bounding box - remember to set objectness score threshold to 0.01 """
ground = []
final_boxes = []
for i in range(lab_fake_batch.shape[1]):
ground.append(lab_fake_batch[0][i].tolist())
# COMPARE GROUND TRUTH AGAINST YOLO DETECTIONS
if ground[0] != []:
tolerance = 0.05
for i in range(len(ground)):
temp_boxes = []
for box in boxes:
# COMPARE CENTRE POINTS (COULD ALSO USE IOU HERE)
# x centre
if abs(ground[i][1] - box[0]) < tolerance:
# y centre
if abs(ground[i][2] - box[1]) < tolerance:
temp_boxes.append(box)
# REMOVE MULTIPLE DETECTIONS
if len(temp_boxes) == 0:
final_boxes.append([ground[i][1], ground[i][2], ground[i][3], ground[i][4], 0, 0, 0])
final_results.append({'image_id': name,
'centre_points': [ground[i][1], ground[i][2]],
'obj_score': 0})
elif len(temp_boxes) == 1:
final_boxes.append(temp_boxes[0])
final_results.append({'image_id': name,
'centre_points': [temp_boxes[0][0], temp_boxes[0][1]],
'obj_score': temp_boxes[0][4]})
elif len(temp_boxes) > 1:
from operator import itemgetter
def max_val(l, i):
return max(enumerate(map(itemgetter(i), l)),key=itemgetter(1))
index, obj_score = max_val(temp_boxes, -3)
final_boxes.append(temp_boxes[index])
final_results.append({'image_id': name,
'centre_points': [temp_boxes[index][0], temp_boxes[index][1]],
'obj_score': temp_boxes[index][4]})
""""""
# save image with plot of bounding box
class_names = load_class_names(namesfile)
plot_boxes(p_img_pil, final_boxes, f'{savedir}/{scene}/{mode}/{folder2}/{cleanname}', class_names)
# plot_boxes(p_img_pil, boxes, f'{savedir}/{scene}/{mode}/{folder2}/{cleanname}', class_names)
if weather_augmentations == 'off':
with open(f'{savedir}/{scene}/{mode}/random_test_detections.json', 'w') as fp:
json.dump(final_results, fp)
else:
with open(f'{savedir}/{scene}/{mode}/random_test_w_detections.json', 'w') as fp:
json.dump(final_results, fp)