-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom_adv_script.py
More file actions
177 lines (147 loc) · 5.56 KB
/
random_adv_script.py
File metadata and controls
177 lines (147 loc) · 5.56 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
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
import numpy as np
import json
import os
import keras
import tensorflow as tf
from keras.layers import Input, Layer
from keras_applications import inception_v3, inception_resnet_v2, resnet
from keras.preprocessing import image
from keras.models import Model
from keras.optimizers import Adam
import matplotlib.pyplot as plt
import glob
from math import log, inf
import itertools
import pickle
from datetime import datetime
def load_images():
images = dict()
for label in LABELS:
files = glob.glob("images/square_%dp/squares_%dp_%s_*.png" % (CENTER_SIZE, CENTER_SIZE, label))
print('images for label', label, len(files))
images[label] = [image.load_img(f, target_size=(CENTER_SIZE, CENTER_SIZE)) for f in files[:MAX_IMAGES_PER_CLASS]]
return images
def classify(img, model='I_V3'):
# ugly
if model == 'RN50':
model = rn50_model
application = resnet
elif model == 'RN152':
model = rn152_model
application = resnet
elif model == 'I_V3':
model = i_v3_model
application = inception_v3
elif model == 'I_RN_V2':
model = i_rn_v2_model
application = inception_resnet_v2
# preprocess
img_prep = image.img_to_array(img)
img_prep = np.expand_dims(img_prep, axis=0)
img_prep = application.preprocess_input(img_prep,
backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils)
# predict
predictions = model.predict(img_prep)
#results
return application.decode_predictions(predictions, top=2, utils=keras.utils)[0]
def evaluate(model_name:str, images: {str: []}, adv_program: []):
results = {}
start = int((IMAGE_SIZE - CENTER_SIZE) / 2)
end = int((IMAGE_SIZE - CENTER_SIZE) / 2 + CENTER_SIZE)
for k, class_images in images.items():
class_results = []
for img in class_images:
adv_program[start:end, start:end, :] = img
# Append best match
class_results.append(classify(adv_program, model_name)[0][1])
results[k] = class_results
return results
flatten = lambda l: [item for sublist in l for item in sublist]
value_dict = dict()
key_dict = dict()
def results_to_matrix(results: {str:[]}) -> [[]]:
keys = results.keys()
for key in keys:
if(key not in key_dict.keys()):
key_dict[key]=len(key_dict.keys())
values = set(flatten(results.values()))
for val in values:
if(val not in value_dict.keys()):
value_dict[val]=len(value_dict.keys())
# if(isinstance(matrix, list)):
matrix = np.zeros((len(keys), len(value_dict.keys())))
for i in results.keys():
for j in results[i]:
matrix[key_dict[i]][value_dict[j]]+=1
#for value in results.values():
# el=[]
# for c in classes: el.append(value.count(c))
# matrix.append(el)
return matrix
def compute_matrix_loss(matrix: [[]]) -> float:
matrix = matrix/matrix.sum(axis=1)[:,None]
usedLabels = []
loss=0
matrix = matrix.transpose()
for i in matrix:
highest_probability = 0
probability_id = inf
for j in range(0,len(i)):
if(j not in usedLabels):
if(i[j]>=highest_probability):
highest_probability=i[j]
probability_id=j
usedLabels.append(probability_id)
if(highest_probability==0):
loss = loss+10
else:
loss = loss - log(highest_probability)
loss = loss+(len(matrix[1])-len(matrix))*10
return loss
def train(model_name:str, images:{str:[]}) -> ([[]], [[]], float):
best_adv_program = []
best_loss = inf
best_matrix = []
try:
for i in itertools.count(0):
adv_program = np.random.rand(IMAGE_SIZE,IMAGE_SIZE,3) * 255
adv_program = adv_program.astype(int)
result = evaluate(model_name, images, adv_program)
mresult = results_to_matrix(result)
loss = compute_matrix_loss(mresult)
if loss < best_loss:
best_loss = loss
best_adv_program = adv_program
best_matrix = mresult
print('round: ', i, 'best loss: ', best_loss)
if best_loss < BEST_LOSS_GOAL:
return (best_adv_program, best_matrix, best_loss)
except KeyboardInterrupt as e:
print('\nkeyboardInterupt:', e)
return (best_adv_program, best_matrix, best_loss)
except Exception as e:
print('\nexception:', e)
return (best_adv_program, best_matrix, best_loss)
if __name__ == "__main__":
### SETUP PARAMETERS ###
CENTER_SIZE = 35
IMAGE_SIZE = 299
LABELS = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
MAX_IMAGES_PER_CLASS = 10
BEST_LOSS_GOAL = 0.01
### END SETUP PARAMETERS ###
# Load Images
images = load_images()
# Load model
i_v3_model = inception_v3.InceptionV3(weights='imagenet', input_tensor=Input(shape=(IMAGE_SIZE, IMAGE_SIZE, 3)),
backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils)
try:
best_program, best_matrix, best_loss = train('I_V3', images)
if not os.path.exists("results/random/"):
os.makedirs("results/random/")
now = datetime.now()
now_string = now.strftime("%d-%m-%Y_%H-%M-%S")
pickle.dump(best_program, open('results/random/adv_program-%s-%.3f' % (now_string, best_loss), 'wb'))
pickle.dump(best_matrix, open('results/random/best_matrix-%s-%.3f' % (now_string, best_loss), 'wb'))
except Exception as e:
print('error', e)