-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
red_attack.py
256 lines (208 loc) · 6.34 KB
/
red_attack.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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
import cv2
from PIL import Image
import os
# Reading the input images and putting them into a numpy array
data=[]
labels=[]
height = 30
width = 30
channels = 3
classes = 43
n_inputs = height * width * channels
for i in range(classes):
path = "dataset/train/{0}/".format(i)
print(path)
Class = os.listdir(path)
for a in Class:
try:
image = cv2.imread(path+a)
image_from_array = Image.fromarray(image, 'RGB')
size_image = image_from_array.resize((height, width))
data.append(np.array(size_image))
labels.append(i)
except AttributeError:
print(" ")
Cells = np.array(data)
labels = np.array(labels)
# Randomize the order of the input images
s = np.arange(Cells.shape[0])
np.random.seed(43)
np.random.shuffle(s)
Cells = Cells[s]
labels = labels[s]
from tensorflow.keras.models import load_model
model = load_model("gtsrb_model_final.h5")
print(path)
# Predicting with the test data
y_test = pd.read_csv("dataset/Test.csv")
labels = y_test['Path'].to_numpy()
y_test = y_test['ClassId'].values
data = []
for f in labels:
image = cv2.imread("dataset/test/" + f.replace('Test/', ''))
image_from_array = Image.fromarray(image, 'RGB')
size_image = image_from_array.resize((height, width))
data.append(np.array(size_image))
X_test = np.array(data)
X_test = X_test.astype('float32') / 255
pred = model.predict_classes(X_test)
predict_x = model.predict(X_test)
pred = np.argmax(predict_x, axis=1)
print(pred)
# Accuracy with the test data
from sklearn.metrics import accuracy_score
accuracy_score(y_test, pred)
def max_diff(img1,img2):
img = img1 - img2
return np.amax(img)
def pred(image):
data = []
data.append(image)
X_test = np.array(data)
X_test = X_test.astype('float32')/255
X_test = X_test.reshape(1,30,30,3)
#print(X_test.shape)
predict_x=model.predict(X_test)
pred_target_image=np.argmax(predict_x,axis=1)
#pred_target_image=pred_target_image[0]
#pred = model.predict_classes(X_test)
return pred_target_image[0]
def boundary_estimation(source, target, dmin):
Ii = ((source + target)/2.0)
k = pred(Ii)
delta = max_diff(source, Ii)
Ia2 = source
Ib2 = target
p = Ib2
while (delta > dmin):
if (pred(Ia2) != k):
Ib2 = Ii
else:
Ia2 = Ii
Ii = ((Ia2+Ib2)/2.0)
k = pred(Ii)
delta = max_diff(Ia2,Ii)
return Ii
def go_out(source,iout,alpha):
i_diff = iout - source
pred_source = pred(source)
inew = iout
while (pred(inew)==pred_source):
inew = inew + alpha*(i_diff)
return inew
source_image_path = "C:/Users/legra/Downloads/dataset/test/00001.png";
target_image_path = "C:/Users/legra/Downloads/dataset/test/00009.png";
print("SOURCE IMAGE:\n")
print(source_image)
img = (np.asarray(Image.open(source_image_path)))
image_from_array = Image.fromarray(img, 'RGB')
img1 = image_from_array.resize((height, width))
img1=np.array(img1)
img1=img1.reshape(30,30,3)
source_image = np.array(img1)
img = (np.asarray(Image.open(target_image_path)))
image_from_array = Image.fromarray(img, 'RGB')
img2 = image_from_array.resize((height, width))
img2=np.array(img2)
img2=img2.reshape(30,30,3)
target_image = np.array(img2)
i = boundary_estimation(source_image,target_image,1.0)
print (pred(i))
print (pred(source_image)) #Class of source image
print (pred(target_image)) #Class of target image
ii = go_out(source_image,i,0.01)
pred(ii)
Image.fromarray(i.astype('uint8')).show()
Image.fromarray(ii.astype('uint8')).show()
def array_diff(d1):
sumd1 = 0.0
for i in range(0,3):
for j in range(0,30):
for k in range(0,30):
d1[j][k][i] = d1[j][k][i]*d1[j][k][i]
sumd1 = sumd1 + d1[j][k][i]
return (sumd1)
def gradient_estimation(source, target, adversarial, n, theta):
Ia = source
Ib = target
Ii = adversarial
Io = np.zeros((2700))
X = np.random.randint(0,2700, size=n)
for i in X:
Io[i] = 255
Io = Io.reshape((30,30,3))
# print(Io*theta)
Ii2 = Ii + theta*Io
Ii2_new = boundary_estimation(Ia, Ii2, 1.0)
Ii2_new = go_out(source,Ii2_new,0.01)
diff2 = Ii2_new - Ia
diff1 = Ii - Ia
d2 = array_diff(diff2)
d1 = array_diff(diff1)
if (d2 > d1):
return (-1, Ii2_new)
elif (d1 > d2):
return (1, Ii2_new)
else:
return (0,Ii2_new)
def efficient_update(source, target, adversarial, I2, g, j):
Ia = source
Ib = target
Ii = adversarial
Ii2 = I2
delta = g*(Ii2 - Ii)
l = j
Inew = Ii + l*delta
diff1 = Inew - Ia
diff2 = Ii - Ia
d1 = array_diff(diff1)
d2 = array_diff(diff2)
ii = 0
it = 0
while(d1 > d2):
l = (l/2.0)
Inew = Ii + l*delta
if(pred(Inew)==pred(source)):
Inew = go_out(source,Inew,0.01)
it = it + 1
d1 = array_diff(Inew-Ia)
if(it>100):
break
if (d1 > d2):
print(ii)
ii = ii + 1
Inew = Ii
return Inew
def iteration(itr, source, target, n, theta, j, dmin):
targett = target
sourcee = source
for i in range(itr):
print ("\n Iteration: ",i)
adversarial_image = boundary_estimation(sourcee, targett, dmin)
adversarial_image = go_out(sourcee,adversarial_image,0.01)
(g, Iii2) = gradient_estimation(sourcee, targett, targett, n, theta)
targett = efficient_update(sourcee, targett, adversarial_image, Iii2, g, j)
if (pred(targett) == pred(source)):
j = j/2.0
fin = targett
if(pred(targett)==pred(sourcee)):
fin = go_out(sourcee,targett,0.01)
if(array_diff(fin-sourcee)<array_diff(adversarial_image-sourcee)):
targett = fin
#print("uopp")
return fin
final = iteration(1000,source_image,target_image,5,0.196,5.0,1.0)
pred(final)
Image.fromarray(source_image.astype('uint8')).save('original_image.png')
Image.fromarray(final.astype('uint8')).save('perturbed_image.png')
# s = measure.compare_ssim(arr[1],arr[0])
original = cv2.imread("original_image.png")
perturb = cv2.imread("perturbed_image.png")
#s = measure.compare_ssim(original,perturb,multichannel=True)
#print(s)
print(perturb)
print(original)