what.attacks.detection.yolo.TOG

 1# Deep Learning Libraries
 2import numpy as np
 3np.set_printoptions(suppress=True)
 4from keras.models import load_model
 5
 6import tensorflow as tf
 7tf.compat.v1.disable_eager_execution()
 8import keras.backend as K
 9
10from what.utils.proj import proj_lp
11
12class TOGAttack:
13    def __init__(self, model, classes, init="zero", decay=1.00):
14        self.classes = len(classes)
15        self.epsilon = 1
16        self.graph = tf.compat.v1.get_default_graph()
17        self.use_filter = False
18
19        if init == "uniform":
20            self.noise = np.random.uniform( -2 / 255.0, 2 / 255.0, size=(416, 416, 3))
21        else:
22            self.noise = np.zeros((416, 416, 3))
23
24        self.adv_patch_boxes = []
25        self.fixed = True
26
27        self.model = load_model(model)
28        self.model.summary()
29
30        self.delta = 0
31        loss = 0
32        self.lr = 4 / 255.0
33        self.decay = decay
34
35        self.c_h = []
36        for out in self.model.output:
37            c_h_i = tf.compat.v1.placeholder(dtype=tf.float32,
38                        shape=out.shape)
39            self.c_h.append(c_h_i)
40            loss = loss + K.sum(K.binary_crossentropy(c_h_i[..., 4:5], out[..., 4:5], from_logits=True))
41                # / K.sum(K.pow(K.sigmoid(self.model.output[layer][..., 2]) * K.sigmoid(self.model.output[layer][..., 3]), 2))
42
43        grads = K.gradients(loss, self.model.input)
44        self.delta = self.delta + K.sign(grads[0])
45
46        self.sess = tf.compat.v1.keras.backend.get_session()
47
48    def attack(self, input_cv_image):
49        with self.graph.as_default():
50
51            c_h = []
52            c_h.append(np.zeros((1, 13, 13, 3, 85)))
53            c_h.append(np.zeros((1, 26, 26, 3, 85)))
54            c_h.append(np.zeros((1, 52, 52, 3, 85)))
55            c_h[0][..., 4] = 1.0
56            c_h[1][..., 4] = 1.0
57            c_h[2][..., 4] = 1.0
58            c_h[0] = c_h[0].reshape((1, 13, 13, 255))
59            c_h[1] = c_h[1].reshape((1, 26, 26, 255))
60            c_h[2] = c_h[2].reshape((1, 52, 52, 255))
61
62            # Draw each adversarial patch on the input image
63            input_cv_image = input_cv_image + self.noise
64            input_cv_image = np.clip(input_cv_image, 0.0, 1.0).astype(np.float32)
65
66            if not self.fixed:
67                if len(self.model.output) == 2:
68                    # Yolo Tiny
69                    outputs, grads = self.sess.run([self.model.output, self.delta],
70                                                    feed_dict={self.model.input:np.array([input_cv_image]),
71                                                    self.c_h[0]: c_h[0],
72                                                    self.c_h[1]: c_h[1],
73                                                })
74                else:
75                    outputs, grads = self.sess.run([self.model.output, self.delta],
76                                                feed_dict={self.model.input:np.array([input_cv_image]),
77                                                self.c_h[0]: c_h[0],
78                                                self.c_h[1]: c_h[1],
79                                                self.c_h[2]: c_h[2],
80                                                })
81
82                self.noise = self.noise - self.lr * grads[0, :, :, :]
83                self.lr = self.lr * self.decay
84                self.noise = np.clip(self.noise, -1.0, 1.0)
85
86                self.noise = proj_lp(self.noise, xi=8/255.0, p = np.inf)
87
88            return input_cv_image, outputs
class TOGAttack:
13class TOGAttack:
14    def __init__(self, model, classes, init="zero", decay=1.00):
15        self.classes = len(classes)
16        self.epsilon = 1
17        self.graph = tf.compat.v1.get_default_graph()
18        self.use_filter = False
19
20        if init == "uniform":
21            self.noise = np.random.uniform( -2 / 255.0, 2 / 255.0, size=(416, 416, 3))
22        else:
23            self.noise = np.zeros((416, 416, 3))
24
25        self.adv_patch_boxes = []
26        self.fixed = True
27
28        self.model = load_model(model)
29        self.model.summary()
30
31        self.delta = 0
32        loss = 0
33        self.lr = 4 / 255.0
34        self.decay = decay
35
36        self.c_h = []
37        for out in self.model.output:
38            c_h_i = tf.compat.v1.placeholder(dtype=tf.float32,
39                        shape=out.shape)
40            self.c_h.append(c_h_i)
41            loss = loss + K.sum(K.binary_crossentropy(c_h_i[..., 4:5], out[..., 4:5], from_logits=True))
42                # / K.sum(K.pow(K.sigmoid(self.model.output[layer][..., 2]) * K.sigmoid(self.model.output[layer][..., 3]), 2))
43
44        grads = K.gradients(loss, self.model.input)
45        self.delta = self.delta + K.sign(grads[0])
46
47        self.sess = tf.compat.v1.keras.backend.get_session()
48
49    def attack(self, input_cv_image):
50        with self.graph.as_default():
51
52            c_h = []
53            c_h.append(np.zeros((1, 13, 13, 3, 85)))
54            c_h.append(np.zeros((1, 26, 26, 3, 85)))
55            c_h.append(np.zeros((1, 52, 52, 3, 85)))
56            c_h[0][..., 4] = 1.0
57            c_h[1][..., 4] = 1.0
58            c_h[2][..., 4] = 1.0
59            c_h[0] = c_h[0].reshape((1, 13, 13, 255))
60            c_h[1] = c_h[1].reshape((1, 26, 26, 255))
61            c_h[2] = c_h[2].reshape((1, 52, 52, 255))
62
63            # Draw each adversarial patch on the input image
64            input_cv_image = input_cv_image + self.noise
65            input_cv_image = np.clip(input_cv_image, 0.0, 1.0).astype(np.float32)
66
67            if not self.fixed:
68                if len(self.model.output) == 2:
69                    # Yolo Tiny
70                    outputs, grads = self.sess.run([self.model.output, self.delta],
71                                                    feed_dict={self.model.input:np.array([input_cv_image]),
72                                                    self.c_h[0]: c_h[0],
73                                                    self.c_h[1]: c_h[1],
74                                                })
75                else:
76                    outputs, grads = self.sess.run([self.model.output, self.delta],
77                                                feed_dict={self.model.input:np.array([input_cv_image]),
78                                                self.c_h[0]: c_h[0],
79                                                self.c_h[1]: c_h[1],
80                                                self.c_h[2]: c_h[2],
81                                                })
82
83                self.noise = self.noise - self.lr * grads[0, :, :, :]
84                self.lr = self.lr * self.decay
85                self.noise = np.clip(self.noise, -1.0, 1.0)
86
87                self.noise = proj_lp(self.noise, xi=8/255.0, p = np.inf)
88
89            return input_cv_image, outputs
TOGAttack(model, classes, init='zero', decay=1.0)
14    def __init__(self, model, classes, init="zero", decay=1.00):
15        self.classes = len(classes)
16        self.epsilon = 1
17        self.graph = tf.compat.v1.get_default_graph()
18        self.use_filter = False
19
20        if init == "uniform":
21            self.noise = np.random.uniform( -2 / 255.0, 2 / 255.0, size=(416, 416, 3))
22        else:
23            self.noise = np.zeros((416, 416, 3))
24
25        self.adv_patch_boxes = []
26        self.fixed = True
27
28        self.model = load_model(model)
29        self.model.summary()
30
31        self.delta = 0
32        loss = 0
33        self.lr = 4 / 255.0
34        self.decay = decay
35
36        self.c_h = []
37        for out in self.model.output:
38            c_h_i = tf.compat.v1.placeholder(dtype=tf.float32,
39                        shape=out.shape)
40            self.c_h.append(c_h_i)
41            loss = loss + K.sum(K.binary_crossentropy(c_h_i[..., 4:5], out[..., 4:5], from_logits=True))
42                # / K.sum(K.pow(K.sigmoid(self.model.output[layer][..., 2]) * K.sigmoid(self.model.output[layer][..., 3]), 2))
43
44        grads = K.gradients(loss, self.model.input)
45        self.delta = self.delta + K.sign(grads[0])
46
47        self.sess = tf.compat.v1.keras.backend.get_session()
def attack(self, input_cv_image):
49    def attack(self, input_cv_image):
50        with self.graph.as_default():
51
52            c_h = []
53            c_h.append(np.zeros((1, 13, 13, 3, 85)))
54            c_h.append(np.zeros((1, 26, 26, 3, 85)))
55            c_h.append(np.zeros((1, 52, 52, 3, 85)))
56            c_h[0][..., 4] = 1.0
57            c_h[1][..., 4] = 1.0
58            c_h[2][..., 4] = 1.0
59            c_h[0] = c_h[0].reshape((1, 13, 13, 255))
60            c_h[1] = c_h[1].reshape((1, 26, 26, 255))
61            c_h[2] = c_h[2].reshape((1, 52, 52, 255))
62
63            # Draw each adversarial patch on the input image
64            input_cv_image = input_cv_image + self.noise
65            input_cv_image = np.clip(input_cv_image, 0.0, 1.0).astype(np.float32)
66
67            if not self.fixed:
68                if len(self.model.output) == 2:
69                    # Yolo Tiny
70                    outputs, grads = self.sess.run([self.model.output, self.delta],
71                                                    feed_dict={self.model.input:np.array([input_cv_image]),
72                                                    self.c_h[0]: c_h[0],
73                                                    self.c_h[1]: c_h[1],
74                                                })
75                else:
76                    outputs, grads = self.sess.run([self.model.output, self.delta],
77                                                feed_dict={self.model.input:np.array([input_cv_image]),
78                                                self.c_h[0]: c_h[0],
79                                                self.c_h[1]: c_h[1],
80                                                self.c_h[2]: c_h[2],
81                                                })
82
83                self.noise = self.noise - self.lr * grads[0, :, :, :]
84                self.lr = self.lr * self.decay
85                self.noise = np.clip(self.noise, -1.0, 1.0)
86
87                self.noise = proj_lp(self.noise, xi=8/255.0, p = np.inf)
88
89            return input_cv_image, outputs