what.attacks.detection.yolo.TOG API documentation

# what.attacks.detection.yolo.TOG

View Source

     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, attack_type, classes, init="zero", decay=0.99):
    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        self.attack_type = attack_type
    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

class TOGAttack: View Source

    13class TOGAttack:
    14    def __init__(self, model, attack_type, classes, init="zero", decay=0.99):
    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        self.attack_type = attack_type
    31
    32        self.delta = 0
    33        loss = 0
    34        self.lr = 4 / 255.0
    35        self.decay = decay
    36
    37        self.c_h = []
    38        for out in self.model.output:
    39            c_h_i = tf.compat.v1.placeholder(dtype=tf.float32,
    40                        shape=out.shape)
    41            self.c_h.append(c_h_i)
    42            loss = loss + K.sum(K.binary_crossentropy(c_h_i[..., 4:5], out[..., 4:5], from_logits=True))
    43                # / K.sum(K.pow(K.sigmoid(self.model.output[layer][..., 2]) * K.sigmoid(self.model.output[layer][..., 3]), 2))
    44
    45        grads = K.gradients(loss, self.model.input)
    46        self.delta = self.delta + K.sign(grads[0])
    47
    48        self.sess = tf.compat.v1.keras.backend.get_session()
    49
    50    def attack(self, input_cv_image):
    51        with self.graph.as_default():
    52
    53            c_h = []
    54            c_h.append(np.zeros((1, 13, 13, 3, 85)))
    55            c_h.append(np.zeros((1, 26, 26, 3, 85)))
    56            c_h.append(np.zeros((1, 52, 52, 3, 85)))
    57            c_h[0][..., 4] = 1.0
    58            c_h[1][..., 4] = 1.0
    59            c_h[2][..., 4] = 1.0
    60            c_h[0] = c_h[0].reshape((1, 13, 13, 255))
    61            c_h[1] = c_h[1].reshape((1, 26, 26, 255))
    62            c_h[2] = c_h[2].reshape((1, 52, 52, 255))
    63
    64            # Draw each adversarial patch on the input image
    65            input_cv_image = input_cv_image + self.noise
    66            input_cv_image = np.clip(input_cv_image, 0.0, 1.0).astype(np.float32)
    67
    68            if not self.fixed:
    69                if len(self.model.output) == 2:
    70                    # Yolo Tiny
    71                    outputs, grads = self.sess.run([self.model.output, self.delta],
    72                                                    feed_dict={self.model.input:np.array([input_cv_image]),
    73                                                    self.c_h[0]: c_h[0],
    74                                                    self.c_h[1]: c_h[1],
    75                                                })
    76                else:
    77                    outputs, grads = self.sess.run([self.model.output, self.delta],
    78                                                feed_dict={self.model.input:np.array([input_cv_image]),
    79                                                self.c_h[0]: c_h[0],
    80                                                self.c_h[1]: c_h[1],
    81                                                self.c_h[2]: c_h[2],
    82                                                })
    83
    84                self.noise = self.noise - self.lr * grads[0, :, :, :]
    85                self.lr = self.lr * self.decay
    86                self.noise = np.clip(self.noise, -1.0, 1.0)
    87
    88                self.noise = proj_lp(self.noise, xi=8/255.0, p = np.inf)
    89
    90            return input_cv_image, outputs

TOGAttack(model, attack_type, classes, init='zero', decay=0.99) View Source

    14    def __init__(self, model, attack_type, classes, init="zero", decay=0.99):
    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        self.attack_type = attack_type
    31
    32        self.delta = 0
    33        loss = 0
    34        self.lr = 4 / 255.0
    35        self.decay = decay
    36
    37        self.c_h = []
    38        for out in self.model.output:
    39            c_h_i = tf.compat.v1.placeholder(dtype=tf.float32,
    40                        shape=out.shape)
    41            self.c_h.append(c_h_i)
    42            loss = loss + K.sum(K.binary_crossentropy(c_h_i[..., 4:5], out[..., 4:5], from_logits=True))
    43                # / K.sum(K.pow(K.sigmoid(self.model.output[layer][..., 2]) * K.sigmoid(self.model.output[layer][..., 3]), 2))
    44
    45        grads = K.gradients(loss, self.model.input)
    46        self.delta = self.delta + K.sign(grads[0])
    47
    48        self.sess = tf.compat.v1.keras.backend.get_session()

def attack(self, input_cv_image): View Source

    50    def attack(self, input_cv_image):
    51        with self.graph.as_default():
    52
    53            c_h = []
    54            c_h.append(np.zeros((1, 13, 13, 3, 85)))
    55            c_h.append(np.zeros((1, 26, 26, 3, 85)))
    56            c_h.append(np.zeros((1, 52, 52, 3, 85)))
    57            c_h[0][..., 4] = 1.0
    58            c_h[1][..., 4] = 1.0
    59            c_h[2][..., 4] = 1.0
    60            c_h[0] = c_h[0].reshape((1, 13, 13, 255))
    61            c_h[1] = c_h[1].reshape((1, 26, 26, 255))
    62            c_h[2] = c_h[2].reshape((1, 52, 52, 255))
    63
    64            # Draw each adversarial patch on the input image
    65            input_cv_image = input_cv_image + self.noise
    66            input_cv_image = np.clip(input_cv_image, 0.0, 1.0).astype(np.float32)
    67
    68            if not self.fixed:
    69                if len(self.model.output) == 2:
    70                    # Yolo Tiny
    71                    outputs, grads = self.sess.run([self.model.output, self.delta],
    72                                                    feed_dict={self.model.input:np.array([input_cv_image]),
    73                                                    self.c_h[0]: c_h[0],
    74                                                    self.c_h[1]: c_h[1],
    75                                                })
    76                else:
    77                    outputs, grads = self.sess.run([self.model.output, self.delta],
    78                                                feed_dict={self.model.input:np.array([input_cv_image]),
    79                                                self.c_h[0]: c_h[0],
    80                                                self.c_h[1]: c_h[1],
    81                                                self.c_h[2]: c_h[2],
    82                                                })
    83
    84                self.noise = self.noise - self.lr * grads[0, :, :, :]
    85                self.lr = self.lr * self.decay
    86                self.noise = np.clip(self.noise, -1.0, 1.0)
    87
    88                self.noise = proj_lp(self.noise, xi=8/255.0, p = np.inf)
    89
    90            return input_cv_image, outputs