def build_graph(self, x, y): with freeze_variables(stop_gradient=False, skip_collection=True): step_size = FLAGS.step_size / 255.0 max_epsilon = FLAGS.max_epsilon / 255.0 x_max = tf.clip_by_value(x + max_epsilon, 0., 1.0) x_min = tf.clip_by_value(x - max_epsilon, 0., 1.0) logits, _, _ = network.model(x, FLAGS.attack_networks[0]) loss = tf.nn.sparse_softmax_cross_entropy_with_logits( logits=logits, labels=y) noise = tf.gradients(loss, x)[0] with tf.variable_scope('RHP'): noise = conv_with_rn(noise) with freeze_variables(stop_gradient=False, skip_collection=True): G = tf.get_default_graph() with G.gradient_override_map({"Sign": "Identity"}): x = x + step_size * tf.sign(noise) x = tf.clip_by_value(x, x_min, x_max) # evaluate after add perturbation logits, _, _ = network.model(x, FLAGS.attack_networks[0]) loss_to_optimize = tf.reduce_mean( tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=y), name='train_loss') return -loss_to_optimize
def build_graph(self): batch_shape = [FLAGS.batch_size, 299, 299, 3] self.x_input = tf.placeholder(tf.float32, shape=batch_shape) self.y_input = tf.placeholder(tf.int64, shape=batch_shape[0]) self.acc_list = [] for network_name in FLAGS.test_networks: acc = network.model(self.x_input, network_name, label=self.y_input) self.acc_list.append(acc)
def build_graph(self): batch_shape = [None, 299, 299, 3] self.x_input = tf.placeholder(tf.float32, shape=batch_shape) self.y_input = tf.placeholder(tf.int64, shape=batch_shape[0]) self.acc_list = [] self.predictions = [] with TowerContext("model_tower", is_training=False): for network_name in FLAGS.test_networks: acc, predictions = network.model(self.x_input, network_name, label=self.y_input) self.acc_list.append(acc) self.predictions.append(predictions)
def graph(self, x, y, i, x_max, x_min): with TowerContext("model_tower", is_training=False): logits, _, endpoints = network.model(x, FLAGS.attack_networks[0]) loss = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=y) noise = tf.gradients(loss, x)[0] if not FLAGS.universal else tf.zeros_like(x) with TowerContext('RHP_tower', is_training=False): with tf.variable_scope('RHP'): noise = conv_with_rn(noise) noise = noise / ( tf.reduce_mean(tf.abs(noise), [1, 2, 3], keepdims=True) + 1e-12) x = x + self.step_size * tf.sign(noise) x = tf.clip_by_value(x, x_min, x_max) i = tf.add(i, 1) return x, y, i, x_max, x_min
def graph(self, x, y, i, x_max, x_min, grad): logits_list = [] for network_name in FLAGS.attack_networks: logits, _, endpoints = network.model(x, network_name) logits_list.append(logits) logits_mean = tf.reduce_mean(logits_list, axis=0) # pred = tf.argmax(logits_mean, axis=1) # first_round = tf.cast(tf.equal(i, 0), tf.int64) # y = first_round * pred + (1 - first_round) * y loss = tf.nn.sparse_softmax_cross_entropy_with_logits( logits=logits_mean, labels=y) noise = tf.gradients(loss, x)[0] noise = noise / ( tf.reduce_mean(tf.abs(noise), [1, 2, 3], keepdims=True) + 1e-12) noise = FLAGS.momentum * grad + noise x = x + self.step_size * tf.sign(noise) x = tf.clip_by_value(x, x_min, x_max) i = tf.add(i, 1) return x, y, i, x_max, x_min, noise