-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIA#2.py
83 lines (55 loc) · 1.76 KB
/
IA#2.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
import tensorflow as tf
import keras
import matplotlib.pyplot as plt
import numpy as np
from keras.applications.inception_v3 import InceptionV3, decode_predictions
from keras import backend as K
from keras.preprocessing import image
from PIL import Image
import tensorflow as tf
import keras
import matplotlib.pyplot as plt
import numpy as np
from keras.applications.inception_v3 import InceptionV3, decode_predictions
from keras import backend as K
from keras.preprocessing import image
from PIL import Image
iv3 = InceptionV3()
#iv3.summary()
path_img = "C://Users//Atlas//Desktop//imagenes//persona.jpg"
x = image.img_to_array(image.load_img(path_img, target_size=(299, 299)))
# Cambio de rango, 0 a 255 -> -1 a 1
x /= 255
x -= 0.5
x *= 2
x = x.reshape([1, x.shape[0], x.shape[1], x.shape[2]])
y = iv3.predict(x)
decode_predictions(y)
#Hasta aqui ya predice la imagen colocada, a partir de ahora implementamos ataques adversarios
inp_layer = iv3.layers[0].input
out_layer = iv3.layers[-1].output
#Numero de la clase que queremos que salga
target_class = 951
loss = out_layer[0, target_class]
grad = K.gradients(loss, inp_layer)[0]
optimize_gradient = K.function([inp_layer, K.learning_phase()], [grad, loss])
adv = np.copy(x)
pert = 0.01
max_pert = x + 0.01
min_pert = x - 0.01
cost = 0.0
while cost < 0.95:
gr, cost = optimize_gradient([adv, 0])
adv += gr
adv = np.clip(adv, min_pert, max_pert)
adv = np.clip(adv, -1, 1)
hacked_img = np.copy(adv)
adv /= 2
adv += 0.5
adv *= 255
plt.imshow(adv[0].astype(np.uint8))
plt.show()
path_hack = "C://Users//Atlas//Desktop//imagenes//hacked.png"
#Con jpgs y otras resoluciones no suele engañar a la IA pero siempre va a depender de lo robusto del programa
im = Image.fromarray(adv[0].astype(np.uint8))
im.save(path_hack)