-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
147 lines (123 loc) · 7.5 KB
/
Copy pathmain.py
File metadata and controls
147 lines (123 loc) · 7.5 KB
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
from yolov3 import load_yolo_model, load_image_pixels, predict_with_yolo
from utils import is_grayscale, crop_gadget, prepare_img_for_yolo
from timing_based_evasion import timing_based_evasion
from pathlib import Path
import numpy as np
import scipy
import argparse
import os
def extract_gadgets_from_img(model, photo_path, input_w, input_h):
# load and prepare image for yolo
image, image_wI, image_hI = load_image_pixels(photo_path, (input_w, input_h))
# get the details of the detected objects
v_boxes, v_labels, v_scores, yolo_runtime, nms_runtime, bb_counter = predict_with_yolo(model,
image,
image_wI,
image_hI,
input_w,
input_h)
print(f'detected objects before cropping: {v_labels}')
gadget_lst = list()
# go over each label
for i in range(len(v_labels)):
# get cropped gadget from image
gadget = crop_gadget(photo_path, [v_boxes[i]], 10)
gadget_w, gadget_h = gadget.width, gadget.height
original_gadget_label = v_labels[i]
gadget_id = i
# prepare cropped object for yolo
test_cropped_gadget = prepare_img_for_yolo(gadget.copy(), input_w, input_h)
# send cropped gadget to yolo
test_gadget_v_boxes, test_gadget_v_labels, test_gadget_v_scores, test_gadget_yolo_runtime, \
test_gadget_nms_runtime, test_gadget_bb_counter = predict_with_yolo(model,
test_cropped_gadget,
gadget_w,
gadget_h,
input_w,
input_h)
# check if the object is still detected after cropping
if len(test_gadget_v_labels) == 1:
if test_gadget_v_labels[0] == original_gadget_label:
gadget_lst.append((gadget, original_gadget_label, [v_boxes[i]], gadget_w, gadget_h, gadget_id))
else:
print(f'gadget-{gadget_id} ({original_gadget_label}) is not detected after cropping')
else:
print(f'gadget-{gadget_id} ({original_gadget_label}) is not detected after cropping')
# returns a list of objects detected after cropping with information about each gadget
return gadget_lst
def attack(args):
model_path = os.path.join('YOLO', 'model', args.model_file)
model = load_yolo_model(model_path)
for filename in os.listdir(args.dataset_name):
if filename.endswith('.jpg') or filename.endswith('.png'):
photo_path = os.path.join(args.dataset_name, filename)
if is_grayscale(photo_path) is not True:
print(f'attack image: {filename}')
# define the expected input shape for the model
input_w, input_h = 416, 416
# get list of gadgets from image
gadget_lst = extract_gadgets_from_img(model, photo_path, input_w, input_h)
for i in range(len(gadget_lst)):
gadget_id = gadget_lst[i][5]
filename_without_extension = filename[0: filename.index(".")]
gadget_num = f'gadget_{gadget_id}'
img_id = f'{filename_without_extension}_{gadget_num}'
# create a folder to save samples
save_files_dir = f'time_attack_samples/Attack - delta={args.delta} ' \
f'max_iteartions={args.max_iterations} epsilon={args.epsilon} ' \
f'population={args.population}/{filename_without_extension}/'
Path(save_files_dir).mkdir(parents=True, exist_ok=True)
gadget = gadget_lst[i][0]
original_gadget_label = gadget_lst[i][1]
gadget_location = gadget_lst[i][2]
gadget_wI = gadget_lst[i][3]
gadget_hI = gadget_lst[i][4]
# defining factors for the amplified gadget
factorW = int(input_w / gadget_wI)
factorH = int(input_h / gadget_hI)
if factorW == 0 and factorH == 0:
factorW = 1
factorH = 1
elif factorW == 0:
factorW = 1
elif factorH == 0:
factorH = 1
print(f'Attack gadget: {gadget_num} - {original_gadget_label}')
normalized_org_gadget, perturbed_gadget = timing_based_evasion(model,
gadget.copy(),
original_gadget_label,
gadget_wI, gadget_hI,
factorW, factorH,
input_w, input_h,
clip_max=1,
clip_min=0,
delta=args.delta,
epsilon=args.epsilon,
max_iterations=args.max_iterations,
population=args.population,
channels=args.channels,
gradf_epsilon=args.gradf_epsilon)
image = np.concatenate([normalized_org_gadget, np.zeros((gadget_hI, 8, 3)), perturbed_gadget], axis=1)
scipy.misc.imsave(f'{save_files_dir}{gadget_num}.png', image)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--delta', type=float,
default=25.0)
parser.add_argument('--epsilon', type=float,
default=0.5)
parser.add_argument('--population', type=float,
default=20)
parser.add_argument('--max_iterations', type=int,
default=300)
parser.add_argument('--channels', type=int,
default=3)
parser.add_argument('--gradf_epsilon', type=float,
default=0.001)
parser.add_argument('--model_file', type=str,
choices=['yolo_model.h5'],
default='yolo_model.h5')
parser.add_argument('--dataset_name', type=str,
choices=['COCO'],
default='COCO')
args = parser.parse_args()
attack(args)