def graph(x, y, i, x_max, x_min, grad): eps = 2.0 * FLAGS.max_epsilon / 255.0 num_iter = FLAGS.num_iter alpha = eps / num_iter momentum = FLAGS.momentum num_classes = 1001 with slim.arg_scope(inception_v4.inception_v4_arg_scope()): logits_v4, end_points_v4 = inception_v4.inception_v4( x, num_classes=num_classes, is_training=False) pred = tf.argmax(end_points_v4['Predictions'], 1) first_round = tf.cast(tf.equal(i, 0), tf.int64) y = first_round * pred + (1 - first_round) * y one_hot = tf.one_hot(y, num_classes) logits = logits_v4 auxlogits = end_points_v4['AuxLogits'] cross_entropy = tf.losses.softmax_cross_entropy(one_hot, logits, label_smoothing=0.0, weights=1.0) cross_entropy += tf.losses.softmax_cross_entropy(one_hot, auxlogits, label_smoothing=0.0, weights=0.4) noise = tf.gradients(cross_entropy, x)[0] x = x + alpha * 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
def __init__(self): self.slim = tf.contrib.slim self.image_size = inception_v4.inception_v4.default_image_size self.checkpoints_dir = 'checkpoints' self.names = imagenet.create_readable_names_for_imagenet_labels() self.arg_scope = inception_v4.inception_v4_arg_scope() self.image = tf.placeholder(tf.uint8, [480, 640, 3]) self.processed_image = inception_preprocessing.preprocess_image(self.image, self.image_size, self.image_size, is_training=False) self.processed_images = tf.expand_dims(self.processed_image, 0) # processed_images will be a 1x299x299x3 tensor of float32 # Create the model, use the default arg scope to configure the batch norm parameters. with self.slim.arg_scope(self.arg_scope): self.logits, self.end_points = inception_v4.inception_v4(self.processed_images, num_classes=1001, is_training=False) self.probs = tf.nn.softmax(self.logits) self.init_fn = self.slim.assign_from_checkpoint_fn( os.path.join(self.checkpoints_dir, 'inception_v4.ckpt'), self.slim.get_model_variables('InceptionV4')) config = tf.compat.v1.ConfigProto() config.gpu_options.allow_growth = True self.session = tf.compat.v1.Session(config=config) self.init_fn(self.session)
def test_inception_v4(img_dir): """ Test Inception-V4 with a single image. :param img_dir: Path of the image to be classified :return: classification result and probability of a single image """ img = cv2.imread(img_dir) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = cv2.resize(img, (299, 299)) / 255 img = img.reshape((1, 299, 299, 3)) tf.reset_default_graph() inputs = tf.placeholder(name='input_images', shape=[None, 299, 299, 3], dtype=tf.float32) with slim.arg_scope(inception_v4_arg_scope()): _, _ = inception_v4(inputs, 1001, is_training=False) with tf.Session() as sess: tf.train.Saver().restore(sess, './models/inception_v4.ckpt') inputs = sess.graph.get_tensor_by_name('input_images:0') outputs = sess.graph.get_tensor_by_name( 'InceptionV4/Logits/Predictions:0') pred = tf.argmax(outputs, axis=1)[0] prob = tf.reduce_max(outputs, axis=1)[0] pred, prob = sess.run([pred, prob], feed_dict={inputs: img}) name = label_dict[pred] print('Result of Inception-V4:', name, prob) return name, prob
def graph(x, y, i, x_max, x_min, grad): x = x + grad eps = 2.0 * FLAGS.max_epsilon / 255.0 num_iter = FLAGS.num_iter alpha = eps / num_iter momentum = FLAGS.momentum num_classes = 1001 with slim.arg_scope( inception_v4.inception_v4_arg_scope()): # 作用是给list_ops中的内容设置默认值 logits_v4, end_points_v4 = inception_v4.inception_v4( x + grad * momentum, num_classes=num_classes, is_training=False) pred = tf.argmax(end_points_v4['Predictions'], 1) first_round = tf.cast(tf.equal(i, 0), tf.int64) y = first_round * pred + (1 - first_round) * y one_hot = tf.one_hot(y, num_classes) logits = (logits_v4) / 7.25 auxlogits = (end_points_v4['AuxLogits']) / 6.25 cross_entropy = tf.losses.softmax_cross_entropy(one_hot, logits, label_smoothing=0.0, weights=1.0) cross_entropy += tf.losses.softmax_cross_entropy(one_hot, auxlogits, label_smoothing=0.0, weights=0.4) noise = tf.gradients(cross_entropy, x)[0] noise = noise / tf.reduce_mean(tf.abs(noise), [1, 2, 3], keep_dims=True) noise = momentum * grad + noise x = x + alpha * 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
def main(argv=None): #加载预处理好的数据 #定义inception_v4的输入, init_data() images = tf.placeholder(dtype=tf.float32, shape=[FLAGS.batch_size, FLAGS.image_size, FLAGS.image_size, 3]) multi_hot_labels = tf.placeholder(dtype=tf.float32, shape=[FLAGS.batch_size, FLAGS.num_classes]) # labels = tf.one_hot(labels, depth=5) #定义inception_v4模型,因为谷歌给出的只有模型参数取值,所以这里需要在这个代码中定义inception_v4的模型结构。虽然理论上需要区分训练和 #测试中使用的模型,也就是说在测试时应该使用is_training=False, 但是因为预预先训练好的inception-v3模型中使用的batch normalization参数 #与新的数据会有差异,导致结果很差,所以这里直接使用同一个模型来进行测试 global_step = tf.Variable(0, trainable=False) with slim.arg_scope(inception_v4.inception_v4_arg_scope()): logits, _ = inception_v4.inception_v4(images, num_classes=FLAGS.num_classes, dropout_keep_prob=1.0, is_training=False, create_aux_logits=False) #获取需要训练的变量 final_tensor = tf.nn.sigmoid(logits, name=FLAGS.final_tensor_name) #定义保存新的训练好的模型函数 saver = tf.train.Saver() with tf.Session() as sess: #初始化没有加载进来的变量。注意这个过程一定要在模型加载之前,否则初始化过程会将已经加载好的变量重新加载 tf.global_variables_initializer().run() tf.local_variables_initializer().run() saver.restore(sess, FLAGS.ckpt_save_file_path) print("restored global_step:",global_step.eval()) predict_array = [] for step in range(total_steps): images_val, image_paths_val = get_image_data_array(step+1) # print(sess.graph.get_tensor_by_name("image_batch:0").eval()) final_tensor_val = sess.run(final_tensor, feed_dict={images:images_val}) for i in range(len(final_tensor_val)): sorted_array = final_tensor_val[i].argsort()[::-1] prediction_result=[LABEL_DICT[label_index+1]+"("+str(label_index+1)+"):"+str(final_tensor_val[i][label_index]) for label_index in sorted_array[:FLAGS.print_count]] predict_array.append(image_paths_val[i]+" "+str(prediction_result)) for i in range(len(all_image_paths)): print(predict_array[i])
def graph(x, y, i, x_max, x_min, grad): eps = 2.0 * FLAGS.max_epsilon / 255.0 num_iter = FLAGS.num_iter alpha = eps / num_iter momentum = FLAGS.momentum num_classes = 1001 # should keep original x here for output with slim.arg_scope(inception_v3.inception_v3_arg_scope()): logits_v3, end_points_v3 = inception_v3.inception_v3( input_diversity(x), num_classes=num_classes, is_training=False) logits_v3_rotated, _ = inception_v3.inception_v3( rotate(x), num_classes=num_classes, is_training=False, reuse=True) with slim.arg_scope(inception_v4.inception_v4_arg_scope()): logits_v4, end_points_v4 = inception_v4.inception_v4( input_diversity(x), num_classes=num_classes, is_training=False) logits_v4_rotated, _ = inception_v4.inception_v4( rotate(x), num_classes=num_classes, is_training=False, reuse=True) with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()): logits_res_v2, end_points_res_v2 = inception_resnet_v2.inception_resnet_v2( input_diversity(x), num_classes=num_classes, is_training=False, reuse=True) logits_res_v2_rotated, _ = inception_resnet_v2.inception_resnet_v2( rotate(x), num_classes=num_classes, is_training=False, reuse=True) with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits_resnet, end_points_resnet = resnet_v2.resnet_v2_152( input_diversity(x), num_classes=num_classes, is_training=False) logits_resnet_rotated, _ = resnet_v2.resnet_v2_152( rotate(x), num_classes=num_classes, is_training=False, reuse=True) logits = (logits_v3 + logits_v4 + logits_res_v2 + logits_resnet + logits_v3_rotated + logits_v4_rotated + logits_res_v2_rotated + logits_resnet_rotated) / 8 auxlogits = (end_points_v3['AuxLogits'] + end_points_v4['AuxLogits'] + end_points_res_v2['AuxLogits']) / 3 cross_entropy = tf.losses.softmax_cross_entropy(y, logits, label_smoothing=0.0, weights=1.0) cross_entropy += tf.losses.softmax_cross_entropy(y, auxlogits, label_smoothing=0.0, weights=0.4) noise = tf.gradients(cross_entropy, x)[0] # noise = tf.nn.depthwise_conv2d(noise, stack_kernel, strides=[1, 1, 1, 1], padding='SAME') noise = noise / tf.reduce_mean(tf.abs(noise), [1, 2, 3], keep_dims=True) noise = momentum * grad + noise x = x + alpha * 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
def main(argv=None): #加载预处理好的数据 #定义inception_v3的输入, images, labels, multi_hot_labels = get_image_batch() # labels = tf.one_hot(labels, depth=5) #定义inception_v3模型,因为谷歌给出的只有模型参数取值,所以这里需要在这个代码中定义inception_v3的模型结构。虽然理论上需要区分训练和 #测试中使用的模型,也就是说在测试时应该使用is_training=False, 但是因为预预先训练好的inception-v3模型中使用的batch normalization参数 #与新的数据会有差异,导致结果很差,所以这里直接使用同一个模型来进行测试 global_step = tf.Variable(0, trainable=False) with slim.arg_scope(inception_v4.inception_v4_arg_scope()): logits, _ = inception_v4.inception_v4(images, num_classes=FLAGS.num_classes, dropout_keep_prob=1.0) #获取需要训练的变量 final_tensor = tf.nn.sigmoid(logits, name=FLAGS.final_tensor_name) tf.summary.histogram(FLAGS.final_tensor_name + '/activations', final_tensor) #定义交叉熵损失,注意在模型定义的时候已经将正则化损失加入损失集合了 tf.losses.sigmoid_cross_entropy(multi_class_labels=multi_hot_labels, logits=logits, weights=1.0) #定义训练过程。这里minimize的过程中指定了需要优化的变量集合 # learning_rate = LEARNING_RATE_BASE learning_rate = tf.train.exponential_decay( FLAGS.learning_rate_base, global_step=global_step, decay_steps=FLAGS.learning_rate_decay_steps, decay_rate=FLAGS.learning_rate_decay) loss = tf.losses.get_total_loss() train_step = tf.train.RMSPropOptimizer(learning_rate).minimize( loss, global_step=global_step) #计算正确率 with tf.name_scope('evaluation'): correct_prediction = tf.equal(tf.round(final_tensor), multi_hot_labels) evaluation_step = tf.reduce_mean(tf.cast(correct_prediction, tf.float32), name='accuracy') #定义保存新的训练好的模型函数 saver = tf.train.Saver() with tf.Session() as sess: #初始化没有加载进来的变量。注意这个过程一定要在模型加载之前,否则初始化过程会将已经加载好的变量重新加载 tf.global_variables_initializer().run() tf.local_variables_initializer().run() # ckpt = tf.train.get_checkpoint_state(FLAGS.ckpt_save_dir) # if ckpt and ckpt.model_checkpoint_path: saver.restore(sess=sess, save_path=FLAGS.ckpt_save_file_path) print("restored global_step:", global_step.eval()) output_graph_def = graph_util.convert_variables_to_constants( sess, sess.graph.as_graph_def(), [ FLAGS.image_batch_name, FLAGS.final_tensor_name, FLAGS.accuracy_name ]) with gfile.FastGFile(FLAGS.output_graph, 'wb') as f: f.write(output_graph_def.SerializeToString()) print("export %s over!" % (FLAGS.output_graph))
def add_inference_node(self, is_training=True): with slim.arg_scope(inception_v4.inception_v4_arg_scope()): self.output, self.end_points = inception_v4.inception_v4( self.input, num_classes=trafficlight_datasets.NUM_CLASSES, is_training=is_training, dropout_keep_prob=0.8, create_aux_logits=True) return
def inference_inception_v4(x_input, dropout_keep_prob=1, num_classes=1001): with slim.arg_scope(inception_v4.inception_v4_arg_scope()): logits, _ = inception_v4.inception_v4(x_input, dropout_keep_prob=dropout_keep_prob, create_aux_logits=False, num_classes=num_classes, is_training=False) probs = tf.nn.softmax(logits) model_vars = [var for var in tf.global_variables() \ if var.name.startswith('InceptionV4/')] return probs, logits, model_vars
def graph(x, y, i, x_max, x_min, grad, grad2): eps = 2.0 * FLAGS.max_epsilon / 255.0 num_classes = 1001 with slim.arg_scope(inception_v3.inception_v3_arg_scope()): logits_v3, end_points_v3 = inception_v3.inception_v3( input_diversity(x), num_classes=num_classes, is_training=False, reuse=tf.AUTO_REUSE) with slim.arg_scope(inception_v4.inception_v4_arg_scope()): logits_v4, end_points_v4 = inception_v4.inception_v4( input_diversity(x), num_classes=num_classes, is_training=False, reuse=tf.AUTO_REUSE) with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()): logits_res_v2, end_points_res_v2 = inception_resnet_v2.inception_resnet_v2( input_diversity(x), num_classes=num_classes, is_training=False, reuse=tf.AUTO_REUSE) with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits_resnet, end_points_resnet = resnet_v2.resnet_v2_101( input_diversity(x), num_classes=num_classes, is_training=False, scope='resnet_v2_101', reuse=tf.AUTO_REUSE) logits = (logits_v3 + logits_v4 + logits_res_v2 + logits_resnet) / 4 auxlogits = (end_points_v3['AuxLogits'] + end_points_v4['AuxLogits'] + end_points_res_v2['AuxLogits']) / 3 cross_entropy = tf.losses.softmax_cross_entropy(y, logits, label_smoothing=0.0, weights=1.0) cross_entropy += tf.losses.softmax_cross_entropy(y, auxlogits, label_smoothing=0.0, weights=0.4) noise = tf.gradients(cross_entropy, x)[0] noise = tf.nn.depthwise_conv2d(noise, stack_kernel, strides=[1, 1, 1, 1], padding='SAME') noise1 = grad + 1.5 * noise noise2 = grad2 + 1.9 * noise * noise x = x + (eps / 17.6786) * ( (1 - 0.9**(i + 1)) / tf.sqrt(1 - 0.99**(i + 1))) * tf.tanh( 1.3 * noise1 / tf.sqrt(noise2)) x = tf.clip_by_value(x, x_min, x_max) i = tf.add(i, 1) return x, y, i, x_max, x_min, noise1, noise2
def graph(x, y, i, grad): num_classes = 1001 x = x with slim.arg_scope(inception_v4.inception_v4_arg_scope()): logits_v4, end_points_v4 = inception_v4.inception_v4( x, num_classes=num_classes, is_training=False) pred = tf.argmax(end_points_v4['Predictions'], 1) first_round = tf.cast(tf.equal(i, 0), tf.int64) y = first_round * pred + (1 - first_round) * y return x, y, i, grad
def discrimination_score(inputs, num_classes, is_training=True, reuse=tf.AUTO_REUSE, scope='InceptionV4'): """ Raw View Descriptor Generation first part of the network (FCN) to get the raw descriptor in the view level. The “FCN” part is the top five convolutional layers of GoogLeNet. (mid-level representation) Extract the raw view descriptors. Compared with deeper CNN, shallow FCN could have more position information, which is needed for the followed grouping module and the deeper CNN will have the content information which could represent the view feature better. Args: inputs: N x V x H x W x C tensor scope: """ raw_view_descriptors = [] final_view_descriptors = [] view_discrimination_scores = [] n_views = inputs.get_shape().as_list()[1] # transpose views: (NxVxHxWxC) -> (VxNxHxWxC) views = tf.transpose(inputs, perm=[1, 0, 2, 3, 4]) for index in range(n_views): batch_view = tf.gather(views, index) # N x H x W x C with slim.arg_scope(inception_v4.inception_v4_arg_scope()): raw_desc, net, end_points = \ inception_v4.inception_v4(batch_view, v_scope='_view' + str(index), is_training=is_training, reuse=reuse, scope=scope) raw_view_descriptors.append(raw_desc['raw_desc']) final_view_descriptors.append(net) # GAP layer to obtain the discrimination scores from raw view descriptors. raw = tf.reduce_mean(raw_desc['raw_desc'], [1, 2], keepdims=True) raw = slim.conv2d(raw, num_classes, [1, 1], activation_fn=None) raw = tf.reduce_max(raw, axis=[1, 2, 3]) batch_view_score = tf.nn.sigmoid(tf.log(tf.abs(raw))) view_discrimination_scores.append(batch_view_score) # # Print name and shape of parameter nodes (values not yet initialized) # tf.logging.info("++++++++++++++++++++++++++++++++++") # tf.logging.info("Parameters") # tf.logging.info("++++++++++++++++++++++++++++++++++") # for v in slim.get_model_variables(): # tf.logging.info('name = %s, shape = %s' % (v.name, v.get_shape())) return view_discrimination_scores, raw_view_descriptors, final_view_descriptors
def graph(x, y, i, x_max, x_min, grad): eps = 2.0 * FLAGS.max_epsilon / 255.0 num_iter = FLAGS.num_iter alpha = eps / num_iter momentum = FLAGS.momentum num_classes = 1001 with slim.arg_scope(inception_v3.inception_v3_arg_scope()): logits_v3, end_points_v3 = inception_v3.inception_v3( input_diversity(x), num_classes=num_classes, is_training=False) with slim.arg_scope(inception_v3.inception_v3_arg_scope()): logits_adv_v3, end_points_adv_v3 = inception_v3.inception_v3( input_diversity(x), num_classes=num_classes, is_training=False, scope='AdvInceptionV3') with slim.arg_scope(inception_v4.inception_v4_arg_scope()): logits_v4, end_points_v4 = inception_v4.inception_v4( input_diversity(x), num_classes=num_classes, is_training=False) with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()): logits_res_v2, end_points_res_v2 = inception_resnet_v2.inception_resnet_v2( input_diversity(x), num_classes=num_classes, is_training=False) with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()): logits_ensadv_res_v2, end_points_ensadv_res_v2 = inception_resnet_v2.inception_resnet_v2( input_diversity(x), num_classes=num_classes, is_training=False, scope='EnsAdvInceptionResnetV2') with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits_resnet, end_points_resnet = resnet_v2.resnet_v2_50( input_diversity(x), num_classes=num_classes, is_training=False) logits = (logits_v3 + 0.25 * logits_adv_v3 + logits_v4 + \ logits_res_v2 + logits_ensadv_res_v2 + logits_resnet) / 5.25 auxlogits = (end_points_v3['AuxLogits'] + 0.25 * end_points_adv_v3['AuxLogits'] + end_points_v4['AuxLogits'] + \ end_points_res_v2['AuxLogits'] + end_points_ensadv_res_v2['AuxLogits']) / 4.25 cross_entropy = tf.losses.softmax_cross_entropy(y, logits, label_smoothing=0.0, weights=1.0) cross_entropy += tf.losses.softmax_cross_entropy(y, auxlogits, label_smoothing=0.0, weights=0.4) noise = tf.gradients(cross_entropy, x)[0] noise = tf.nn.depthwise_conv2d(noise, stack_kernel, strides=[1, 1, 1, 1], padding='SAME') noise = noise / tf.reshape(tf.contrib.keras.backend.std(tf.reshape(noise, [FLAGS.batch_size, -1]), axis=1), [FLAGS.batch_size, 1, 1, 1]) noise = momentum * grad + noise noise = noise / tf.reshape(tf.contrib.keras.backend.std(tf.reshape(noise, [FLAGS.batch_size, -1]), axis=1), [FLAGS.batch_size, 1, 1, 1]) x = x - alpha * tf.clip_by_value(tf.round(noise), -2, 2) x = tf.clip_by_value(x, x_min, x_max) i = tf.add(i, 1) return x, y, i, x_max, x_min, noise
def _build(self, x_input=None): reuse = True if self.built else None if x_input is None: x_input = self.input with slim.arg_scope(inception_v4.inception_v4_arg_scope()): logits, end_points = inception_v4.inception_v4( x_input, num_classes=self.num_classes, is_training=False, reuse=reuse) self.built = True self.end_points = end_points self.logits = logits if not self.ckpt_loaded: saver = tf.train.Saver(slim.get_model_variables()) saver.restore(self.sess, ckpt_dir + 'inception_v4.ckpt') self.ckpt_loaded = True
def graph(x, y, i, x_max, x_min, grad, eg): eps = 2.0 * FLAGS.max_epsilon / 255.0 num_iter = FLAGS.num_iter alpha = eps / num_iter num_classes = 1001 ro = 0.9 beta = 0.89 v = 0.1 eg = ro * eg + (1 - ro) * tf.square(grad) rms = tf.sqrt(eg + 0.000000001) x_n = x + (alpha / rms)*grad with slim.arg_scope(inception_v3.inception_v3_arg_scope()): logits_v3, end_points_v3 = inception_v3.inception_v3( input_diversity(x_n), num_classes=num_classes, is_training=False, reuse=tf.AUTO_REUSE) with slim.arg_scope(inception_v4.inception_v4_arg_scope()): logits_v4, end_points_v4 = inception_v4.inception_v4( input_diversity(x_n), num_classes=num_classes, is_training=False, reuse=tf.AUTO_REUSE) with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()): logits_res_v2, end_points_res_v2 = inception_resnet_v2.inception_resnet_v2( input_diversity(x_n), num_classes=num_classes, is_training=False, reuse=tf.AUTO_REUSE) with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits_resnet, end_points_resnet = resnet_v2.resnet_v2_101( input_diversity(x_n), num_classes=num_classes, is_training=False, scope='resnet_v2_101', reuse=tf.AUTO_REUSE) logits = (logits_v3 + logits_v4 + logits_res_v2 + logits_resnet) / 4 auxlogits = (end_points_v3['AuxLogits'] + end_points_v4['AuxLogits'] + end_points_res_v2['AuxLogits']) / 3 cross_entropy = tf.losses.softmax_cross_entropy(y, logits, label_smoothing=0.0, weights=1.0) cross_entropy += tf.losses.softmax_cross_entropy(y, auxlogits, label_smoothing=0.0, weights=0.4) noise = tf.gradients(cross_entropy, x_n)[0] noise = tf.nn.depthwise_conv2d(noise, stack_kernel, strides=[1, 1, 1, 1], padding='SAME') noise1 = noise / tf.reduce_mean(tf.abs(noise), [1, 2, 3], keep_dims=True) noise = beta * grad + (1-beta) * noise1 noise2 = (1-v) * noise + v * noise1 x = x + alpha * tf.sign(noise2) x = tf.clip_by_value(x, x_min, x_max) i = tf.add(i, 1) return x, y, i, x_max, x_min, noise, eg
def graph(x, adv, y, t_y, i, x_max, x_min, grad, amplification): target_one_hot = tf.one_hot(t_y, 1001) true_one_hot = tf.one_hot(y, 1001) eps = 2.0 * FLAGS.max_epsilon / 255.0 num_iter = FLAGS.num_iter alpha = eps / num_iter alpha_beta = alpha gamma = alpha_beta momentum = FLAGS.momentum num_classes = 1001 # with slim.arg_scope(inception_v3.inception_v3_arg_scope()): # logits_v3, end_points_v3 = inception_v3.inception_v3( # adv, num_classes=num_classes, is_training=False, reuse = True) # auxlogit_v3 = end_points_v3['AuxLogits'] with slim.arg_scope(inception_v4.inception_v4_arg_scope()): logits_v4, end_points_v4 = inception_v4.inception_v4( adv, num_classes=num_classes, is_training=False, reuse=True) auxlogit_v4 = end_points_v4['AuxLogits'] with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits_resnet_152, end_points_resnet = resnet_v2.resnet_v2_152( adv, num_classes=num_classes, is_training=False, reuse=True) with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()): logits_Incres, end_points_IR = inception_resnet_v2.inception_resnet_v2( adv, num_classes=num_classes, is_training=False, reuse=True) auxlogit_IR = end_points_IR['AuxLogits'] logits = (logits_Incres + logits_resnet_152 + logits_v4) / 3.0 auxlogit = (auxlogit_IR + auxlogit_v4) / 2.0 target_cross_entropy = tf.losses.softmax_cross_entropy(target_one_hot, logits, label_smoothing=0.0, weights=1.0) target_cross_entropy += tf.losses.softmax_cross_entropy( target_one_hot, auxlogit, label_smoothing=0.0, weights=1.0) noise = tf.gradients(target_cross_entropy, adv)[0] adv = adv - alpha * n_staircase_sign(noise, num_of_K) adv = tf.clip_by_value(adv, x_min, x_max) i = tf.add(i, 1) return x, adv, y, t_y, i, x_max, x_min, noise, amplification
def output_features(image_batch): if FLAGS.model == 'inceptionv4': with slim.arg_scope(inception_v4.inception_v4_arg_scope()): net, _ = inception_v4.inception_v4(image_batch, None, is_training=False) net = tf.squeeze(net, [1, 2]) elif FLAGS.model == 'resnet101v2': with slim.arg_scope(resnet_v2.resnet_arg_scope()): net, _ = resnet_v2.resnet_v2_101(image_batch, None, is_training=False, global_pool=True) net = tf.squeeze(net, [1, 2]) else: raise KeyError('{} is not supported'.format(FLAGS.model)) return net
def target_model(x): num_classes=1001 with slim.arg_scope(inception_v3.inception_v3_arg_scope()): logits_v3, end_points_v3 = inception_v3.inception_v3( input_diversity(x), num_classes=num_classes, is_training=False) with slim.arg_scope(inception_v4.inception_v4_arg_scope()): logits_v4, end_points_v4 = inception_v4.inception_v4( input_diversity(x), num_classes=num_classes, is_training=False) with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()): logits_res_v2, end_points_res_v2 = inception_resnet_v2.inception_resnet_v2( input_diversity(x), num_classes=num_classes, is_training=False, reuse=True) with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits_resnet, end_points_resnet = resnet_v2.resnet_v2_152( input_diversity(x), num_classes=num_classes, is_training=False) with slim.arg_scope(inception_v3.inception_v3_arg_scope()): logits_adv_v3, end_points_adv_v3 = inception_v3.inception_v3( input_diversity(x), num_classes=num_classes, is_training=False, scope='AdvInceptionV3') with slim.arg_scope(inception_v3.inception_v3_arg_scope()): logits_ens3_adv_v3, end_points_ens3_adv_v3 = inception_v3.inception_v3( input_diversity(x), num_classes=num_classes, is_training=False, scope='Ens3AdvInceptionV3') with slim.arg_scope(inception_v3.inception_v3_arg_scope()): logits_ens4_adv_v3, end_points_ens4_adv_v3 = inception_v3.inception_v3( input_diversity(x), num_classes=num_classes, is_training=False, scope='Ens4AdvInceptionV3') with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()): logits_ensadv_res_v2, end_points_ensadv_res_v2 = inception_resnet_v2.inception_resnet_v2( input_diversity(x), num_classes=num_classes, is_training=False, scope='EnsAdvInceptionResnetV2') logits = (logits_v3 +1.6*logits_adv_v3+logits_v4 +logits_res_v2 + logits_resnet+logits_ens3_adv_v3+logits_ens4_adv_v3+logits_ensadv_res_v2) / 8.6 auxlogits = ( 1.6*end_points_adv_v3['AuxLogits'] +end_points_v3['AuxLogits']+end_points_ens3_adv_v3['AuxLogits'] + end_points_v4['AuxLogits']+end_points_res_v2['AuxLogits']+end_points_ens4_adv_v3['AuxLogits']+end_points_ensadv_res_v2['AuxLogits']) / 7.6 return logits,auxlogits
def _tower_fn(im, is_training=False): with slim.arg_scope(inception_v4.inception_v4_arg_scope()): net, _ = inception_v4.inception_v4(im, None, is_training=False) net = tf.squeeze(net, [1, 2]) with tf.variable_scope('Generator'): feat = slim.fully_connected(net, FLAGS.mem_dim, activation_fn=None) feat = tf.nn.l2_normalize(feat, axis=1) embedding = tf.get_variable(name='embedding', shape=[FLAGS.vocab_size, FLAGS.emb_dim], initializer=tf.random_uniform_initializer( -0.08, 0.08)) softmax_w = tf.matrix_transpose(embedding) softmax_b = tf.get_variable('softmax_b', [FLAGS.vocab_size]) cell = tf.nn.rnn_cell.BasicLSTMCell(FLAGS.mem_dim) if is_training: cell = tf.nn.rnn_cell.DropoutWrapper(cell, FLAGS.keep_prob, FLAGS.keep_prob) zero_state = cell.zero_state(FLAGS.batch_size, tf.float32) _, state = cell(feat, zero_state) init_state = state tf.get_variable_scope().reuse_variables() state_feed = tf.placeholder(dtype=tf.float32, shape=[None, sum(cell.state_size)], name="state_feed") state_tuple = tf.split(value=state_feed, num_or_size_splits=2, axis=1) input_feed = tf.placeholder( dtype=tf.int64, shape=[None], # batch_size name="input_feed") inputs = tf.nn.embedding_lookup(embedding, input_feed) out, state_tuple = cell(inputs, state_tuple) tf.concat(axis=1, values=state_tuple, name="state") logits = tf.nn.bias_add(tf.matmul(out, softmax_w), softmax_b) tower_pred = tf.nn.softmax(logits, name="softmax") return tf.concat(init_state, axis=1, name='initial_state')
def inceptionv3(filenames, session_id, res, perplexity, early_exaggeration, learning_rate, dpi): # Load images images = np.zeros((len(filenames), 224, 224, 3), dtype=np.float32) for i, imageName in enumerate(filenames): print i, imageName img = skimage.io.imread(imageName) if len(img.shape) == 2: img = np.expand_dims(img, 2) img = np.repeat(img, 3, axis=2) img = scipy.misc.imresize(img, (224, 224)) images[i, :, :, :] = img in_images = tf.placeholder(tf.float32, images.shape) # processing with slim.arg_scope(inception_v4.inception_v4_arg_scope()): model, intermed = inception_v4.inception_v4(in_images, 1001, reuse=tf.AUTO_REUSE) # embed() PreLogitsFlatten = intermed['PreLogitsFlatten'] restored_variables = tf.contrib.framework.get_variables_to_restore() restorer = tf.train.Saver(restored_variables) with tf.Session() as sess: img_net_path = 'models/inception_v4.ckpt' restorer.restore(sess, img_net_path) features = sess.run(PreLogitsFlatten, feed_dict={in_images: images}) # Clean up model tf.reset_default_graph() features = features.squeeze() # remove dimensions that are only 1 long utils.save_features_to_csv_file( features, filenames, session_id, 'inceptionv3_features:Resolution:%d_Perplexity:%d_EarlyExaggeration:%d_LearningRate:%d_DPI:%d.csv' % (res, perplexity, early_exaggeration, learning_rate, dpi)) return features
def main(_): start = time.clock() # tf.set_random_seed(1) # np.random.seed(1) # Images for inception classifier are normalized to be in [-1, 1] interval,3 # eps is a difference between pixels so it should be in [0, 2] interval. # Renormalizing epsilon from [0, 255] to [0, 2]. eps = 2.0 * FLAGS.max_epsilon / 255.0 batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3] tf.logging.set_verbosity(tf.logging.INFO) x = tf.placeholder(dtype=tf.float32, shape=batch_shape) # , name='x_Placeholder' x_max = tf.clip_by_value(x + eps, -1.0, 1.0) x_min = tf.clip_by_value(x - eps, -1.0, 1.0) # y = tf.zeros(shape=FLAGS.batch_size, dtype=tf.int64) # i = tf.zeros(shape=1, dtype=tf.int8) grad = tf.zeros(shape=batch_shape) # pred = tf.zeros(shape=FLAGS.batch_size, dtype=tf.int64) # noise = tf.zeros(shape=[FLAGS.batch_size, 299, 299, 3],dtype=tf.float32) # cross_entropy = tf.zeros(shape=FLAGS.batch_size,dtype=tf.float32) with tf.Session() as sess: eps = 2.0 * FLAGS.max_epsilon / 255.0 num_iter = FLAGS.num_iter alpha = eps / num_iter momentum = FLAGS.momentum num_classes = 1001 # pred = tf.zeros(shape=FLAGS.batch_size, dtype=tf.int64) # noise = tf.zeros(shape=[FLAGS.batch_size, 299, 299, 3], dtype=tf.float32) # cross_entropy = tf.zeros(shape=FLAGS.batch_size, dtype=tf.float32) with slim.arg_scope(inception_v3.inception_v3_arg_scope()): logits_v3, end_points_v3 = inception_v3.inception_v3( x, num_classes=num_classes, is_training=False) with slim.arg_scope(inception_v3.inception_v3_arg_scope()): logits_adv_v3, end_points_adv_v3 = inception_v3.inception_v3( x, num_classes=num_classes, is_training=False, scope='AdvInceptionV3') with slim.arg_scope(inception_v3.inception_v3_arg_scope()): logits_ens3_adv_v3, end_points_ens3_adv_v3 = inception_v3.inception_v3( x, num_classes=num_classes, is_training=False, scope='Ens3AdvInceptionV3') with slim.arg_scope(inception_v3.inception_v3_arg_scope()): logits_ens4_adv_v3, end_points_ens4_adv_v3 = inception_v3.inception_v3( x, num_classes=num_classes, is_training=False, scope='Ens4AdvInceptionV3') with slim.arg_scope(inception_v4.inception_v4_arg_scope()): logits_v4, end_points_v4 = inception_v4.inception_v4( x, num_classes=num_classes, is_training=False) with slim.arg_scope( inception_resnet_v2.inception_resnet_v2_arg_scope()): logits_res_v2, end_points_res_v2 = inception_resnet_v2.inception_resnet_v2( x, num_classes=num_classes, is_training=False) with slim.arg_scope( inception_resnet_v2.inception_resnet_v2_arg_scope()): logits_ensadv_res_v2, end_points_ensadv_res_v2 = inception_resnet_v2.inception_resnet_v2( x, num_classes=num_classes, is_training=False, scope='EnsAdvInceptionResnetV2') with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits_resnet, end_points_resnet = resnet_v2.resnet_v2_101( x, num_classes=num_classes, is_training=False) pred_dict = end_points_v3['Predictions'] + end_points_adv_v3['Predictions'] + end_points_ens3_adv_v3['Predictions'] + \ end_points_ens4_adv_v3['Predictions'] + end_points_v4['Predictions'] + \ end_points_res_v2['Predictions'] + end_points_ensadv_res_v2['Predictions'] + end_points_resnet['predictions'] pred = tf.argmax(pred_dict, axis=1) logits_dict = [ logits_v3, logits_ens3_adv_v3, logits_ens4_adv_v3, logits_v4, logits_res_v2, logits_ensadv_res_v2, logits_resnet ] # for j in range(FLAGS.batch_size): # end_points_v3_Pred = end_points_v3['Predictions'][j][pred[j]] # # end_points_adv_v3_Pred = end_points_adv_v3['Predictions'] # end_points_ens3_adv_v3_Pred = end_points_ens3_adv_v3['Predictions'][j][pred[j]] # end_points_ens4_adv_v3_Pred = end_points_ens4_adv_v3['Predictions'][j][pred[j]] # end_points_v4_Pred = end_points_v4['Predictions'][j][pred[j]] # end_points_res_v2_Pred = end_points_res_v2['Predictions'][j][pred[j]] # end_points_ensadv_res_v2_Pred = end_points_ensadv_res_v2['Predictions'][j][pred[j]] # end_points_resnet_Pred = end_points_resnet['Predictions'][j][pred[j]] # # # ens_Pred_Value = [end_points_v3_Pred * -1, end_points_ens3_adv_v3_Pred * -1, # end_points_ens4_adv_v3_Pred * -1, # end_points_v4_Pred * -1, end_points_res_v2_Pred * -1, # end_points_ensadv_res_v2_Pred * -1, # end_points_resnet_Pred * -1] # TopKFit, TopKFitIndx = tf.nn.top_k(ens_Pred_Value, FLAGS.LOW_K) # # TopKFit = TopKFit * -1 # # logits_dictionary = tf.placeholder(dtype=tf.int32, shape=(7, FLAGS.batch_size, 1001)) # TopKFitIndex = tf.placeholder(dtype=tf.int32, shape=(FLAGS.batch_size)) # logits[j] = (logits_dictionary[TopKFitIndex[0]] * 5.0 + logits_dictionary[TopKFitIndex[1]] * 4.0 + logits_dictionary[TopKFitIndex[2]] * 3.0 + logits_dictionary[TopKFitIndex[3]] * 2.0 + logits_dictionary[TopKFitIndex[4]] * 1.0) / 15.0 #i_iteration = tf.placeholder(dtype=tf.int32) #first_round = tf.cast(tf.equal(i_iteration, 0), tf.int64) #y = first_round * pred y = tf.placeholder( dtype=tf.int32, shape=[ 2, ] ) # shape= FLAGS.batchsize !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #y1 = first_round * pred + (1 - first_round) * y one_hot = tf.one_hot(y, num_classes) logits_gravity = tf.placeholder(dtype=tf.float32, shape=[FLAGS.batch_size, 7]) # logits = tf.zeros(shape=[FLAGS.batch_size, 1001], dtype=tf.float32) #for i in range(2): ########### 2 is the FLAGS.batch_size logits1 = (logits_dict[0][0] * logits_gravity[0][0] + logits_dict[1][0] * logits_gravity[0][1] + logits_dict[2][0] * logits_gravity[0][2] + logits_dict[3][0] * logits_gravity[0][3] + logits_dict[4][0] * logits_gravity[0][4] + logits_dict[5][0] * logits_gravity[0][5] + logits_dict[6][0] * logits_gravity[0][6]) / 28.0 logits2 = (logits_dict[0][1] * logits_gravity[1][0] + logits_dict[1][1] * logits_gravity[1][1] + logits_dict[2][1] * logits_gravity[1][2] + logits_dict[3][1] * logits_gravity[1][3] + logits_dict[4][1] * logits_gravity[1][4] + logits_dict[5][1] * logits_gravity[1][5] + logits_dict[6][1] * logits_gravity[1][6]) / 28.0 logits1 = tf.reshape(logits1, [1, 1001]) logits2 = tf.reshape(logits2, [1, 1001]) logits = tf.concat([logits1, logits2], 0) # logits = (logits_v3 + 0.25 * logits_adv_v3 + logits_ens3_adv_v3 + \ # logits_ens4_adv_v3 + logits_v4 + \ # logits_res_v2 + logits_ensadv_res_v2 + logits_resnet) / 7.25 # auxlogits = (end_points_v3['AuxLogits'] + 0.25 * end_points_adv_v3['AuxLogits'] + end_points_ens3_adv_v3['AuxLogits'] + \ # end_points_ens4_adv_v3['AuxLogits'] + end_points_v4['AuxLogits'] + \ # end_points_res_v2['AuxLogits'] + end_points_ensadv_res_v2['AuxLogits']) / 6.25 # cross_entropy = tf.zeros(shape=(FLAGS.batch_size)) #print(str(gravity)) cross_entropy = tf.losses.softmax_cross_entropy( one_hot, tf.clip_by_value(logits, 1e-8, tf.reduce_max(logits)), label_smoothing=0.0, weights=1.0) # cross_entropy += tf.losses.softmax_cross_entropy(one_hot, # auxlogits, # label_smoothing=0.0, # weights=0.4) #print(str(cross_entropy)) noise = tf.gradients(cross_entropy, x)[0] #print(str(tf.gradients(cross_entropy, x)) + '=========') # if(noise == None): # noise = tf.zeros(shape=[FLAGS.batch_size, 299, 299, 3]) # print('noise出了问题!') noise = noise / tf.reduce_mean(tf.abs(noise), [1, 2, 3], keep_dims=True) grad = momentum * grad + noise adv = x + alpha * tf.sign(grad) adv = tf.clip_by_value(adv, x_min, x_max) # x_adv, y, _, _, _, _, pred = tf.while_loop(stop, graph, [x, y, i, x_max, x_min, grad, pred]) # Run computation s1 = tf.train.Saver(slim.get_model_variables(scope='InceptionV3')) s2 = tf.train.Saver(slim.get_model_variables(scope='AdvInceptionV3')) s3 = tf.train.Saver( slim.get_model_variables(scope='Ens3AdvInceptionV3')) s4 = tf.train.Saver( slim.get_model_variables(scope='Ens4AdvInceptionV3')) s5 = tf.train.Saver(slim.get_model_variables(scope='InceptionV4')) s6 = tf.train.Saver( slim.get_model_variables(scope='InceptionResnetV2')) s7 = tf.train.Saver( slim.get_model_variables(scope='EnsAdvInceptionResnetV2')) s8 = tf.train.Saver(slim.get_model_variables(scope='resnet_v2')) s1.restore(sess, FLAGS.checkpoint_path_inception_v3) s2.restore(sess, FLAGS.checkpoint_path_adv_inception_v3) s3.restore(sess, FLAGS.checkpoint_path_ens3_adv_inception_v3) s4.restore(sess, FLAGS.checkpoint_path_ens4_adv_inception_v3) s5.restore(sess, FLAGS.checkpoint_path_inception_v4) s6.restore(sess, FLAGS.checkpoint_path_inception_resnet_v2) s7.restore(sess, FLAGS.checkpoint_path_ens_adv_inception_resnet_v2) s8.restore(sess, FLAGS.checkpoint_path_resnet) #sess.run(tf.global_variables_initializer()) ####### # test # for filenames, images in load_images(FLAGS.input_dir, batch_shape): # logits_v3_, Pred_dict = sess.run([logits_v3, pred_dict], feed_dict={x: images}) # label = np.argmax(Pred_dict, axis=1) # print('label::::::', label) # print('=================\n', logits_v3_) ####### sum = 0 success_num = 0 l2_distance = 0 label_distance = 0 for filenames, images in load_images(FLAGS.input_dir, batch_shape): sum += len(filenames) images = images.astype(np.float32) images_flatten_initial = images.reshape((2, 268203)) # true_label = [] # for i in range(len(filenames)): # true_label.append(filenames[i].split('-')[2][:-4]) #print('image.astype::::::::', images) #adv_images = tf.zeros(shape=batch_shape) #adv_images = [] #prediction = 0!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! prediction = [] for i in range(FLAGS.num_iter): logits_proportion = [] if i == 0: prediction = sess.run(pred, feed_dict={x: images}) print('true_label::::::::', prediction) end_points_v3_run, end_points_ens3_adv_v3_run, end_points_ens4_adv_v3_run, end_points_v4_run, end_points_res_v2_run, end_points_ensadv_res_v2_run, end_points_resnet_run = \ sess.run([end_points_v3, end_points_ens3_adv_v3, end_points_ens4_adv_v3, end_points_v4, end_points_res_v2, end_points_ensadv_res_v2, end_points_resnet], feed_dict={x: images, y: prediction}) else: adv_pred, end_points_v3_run, end_points_ens3_adv_v3_run, end_points_ens4_adv_v3_run, end_points_v4_run, end_points_res_v2_run, end_points_ensadv_res_v2_run, end_points_resnet_run = \ sess.run([pred, end_points_v3, end_points_ens3_adv_v3, end_points_ens4_adv_v3, end_points_v4, end_points_res_v2, end_points_ensadv_res_v2, end_points_resnet], feed_dict={x: images, y: prediction}) # print('prediction::::::::::', prediction) # print('end_points_v3_run[Predictions]:::::::', end_points_v3_run['Predictions']) if i == FLAGS.num_iter - 1: print('adv_pred:::::::', adv_pred) for l in range(len(filenames)): label_distance += abs(prediction[l] - adv_pred[l]) if int(prediction[l]) == adv_pred[ l]: # The test tag is always 1 more than the real tag, so I subtract 1 here success_num += 1 #print('sucess_num:::::', success_num) for j in range(FLAGS.batch_size): end_points_v3_Pred = end_points_v3_run['Predictions'][j][ prediction[j]] # end_points_adv_v3_Pred = end_points_adv_v3['Predictions'] end_points_ens3_adv_v3_Pred = end_points_ens3_adv_v3_run[ 'Predictions'][j][prediction[j]] end_points_ens4_adv_v3_Pred = end_points_ens4_adv_v3_run[ 'Predictions'][j][prediction[j]] end_points_v4_Pred = end_points_v4_run['Predictions'][j][ prediction[j]] end_points_res_v2_Pred = end_points_res_v2_run[ 'Predictions'][j][prediction[j]] end_points_ensadv_res_v2_Pred = end_points_ensadv_res_v2_run[ 'Predictions'][j][prediction[j]] end_points_resnet_Pred = end_points_resnet_run[ 'predictions'][j][prediction[j]] ens_Pred_Value = [ end_points_v3_Pred, end_points_ens3_adv_v3_Pred, end_points_ens4_adv_v3_Pred, end_points_v4_Pred, end_points_res_v2_Pred, end_points_ensadv_res_v2_Pred, end_points_resnet_Pred ] TopKFitIndx = np.argsort(ens_Pred_Value) # logits_dictionary = tf.placeholder(dtype=tf.int32, shape=(7, FLAGS.batch_size, 1001)) # TopKFitIndex = tf.placeholder(dtype=tf.int32, shape=(FLAGS.batch_size)) a = [0.0] * 7 # print('ens_Pred_Value:::::::::', ens_Pred_Value) # print('TopKFitIndx::::::', TopKFitIndx) for m in range(7): a[TopKFitIndx[m]] = 7 - m logits_proportion.append(a) # One_hot, Logits_dict, Logits1, Logits2, Logits, images, Cross_Entropy, Noise, gradient = \ # sess.run([one_hot, logits_dict, logits1, logits2, logits, adv, cross_entropy, noise, grad], # feed_dict={x: images, logits_gravity: logits_proportion, y: prediction}) images = sess.run(adv, feed_dict={ x: images, logits_gravity: logits_proportion, y: prediction }) if i == FLAGS.num_iter - 1: images_flatten_adv = images.reshape((2, 268203)) l2_distance_list = np.linalg.norm( (images_flatten_initial - images_flatten_adv), axis=1, keepdims=True) for n in range(len(filenames)): l2_distance += l2_distance_list[n] # print('One_hot::::::::::::', One_hot.shape, One_hot) # print('Logits_dict::::::::::', Logits_dict) # print('Logits1::::::::::',Logits1.shape, Logits1) # print('Logits2::::::::::',Logits2.shape, Logits2) # # print('Logits:::::::::::', Logits) # print('Cross_Entropy:::::::::::::', Cross_Entropy) # print('Noise:::::::::::', Noise) # print('gradient::::::::::', gradient) save_images(images, filenames, FLAGS.output_dir) # for filenames, images in load_images(FLAGS.input_dir, batch_shape): # #label = sess.run([pred, end_points_v3_Pred, end_points_ens3_adv_v3_Pred, end_points_ens4_adv_v3_Pred, end_points_v4_Pred, end_points_res_v2_Pred, end_points_ensadv_res_v2_Pred, end_points_resnet_Pred],) # adv_images= sess.run([x_adv], feed_dict={x_input: images}) # save_images(adv_images, filenames, FLAGS.output_dir) print('sum::::', sum) print('success_num::::', success_num) rate_wrong = success_num / sum print('rate_wrong:::::::', rate_wrong) print('l2_distance:::::::', l2_distance) print('label_distance:::::::', label_distance) end = time.clock() print('run time:::::', end - start)
def main(_): start = time.clock() # Images for inception classifier are normalized to be in [-1, 1] interval, # eps is a difference between pixels so it should be in [0, 2] interval. # Renormalizing epsilon from [0, 255] to [0, 2]. # eps = 2.0 * FLAGS.max_epsilon / 255.0 batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3] tf.logging.set_verbosity(tf.logging.INFO) eps = 2.0 * FLAGS.max_epsilon / 255.0 num_iter = FLAGS.num_iter alpha = eps / num_iter momentum = FLAGS.momentum num_classes = 1001 with tf.Session() as sess: x = tf.placeholder(dtype=tf.float32, shape=batch_shape) with slim.arg_scope(inception_v3.inception_v3_arg_scope()): logits_v3, end_points_v3 = inception_v3.inception_v3( x, num_classes=num_classes, is_training=False) with slim.arg_scope(inception_v3.inception_v3_arg_scope()): logits_adv_v3, end_points_adv_v3 = inception_v3.inception_v3( x, num_classes=num_classes, is_training=False, scope='AdvInceptionV3') with slim.arg_scope(inception_v3.inception_v3_arg_scope()): logits_ens3_adv_v3, end_points_ens3_adv_v3 = inception_v3.inception_v3( x, num_classes=num_classes, is_training=False, scope='Ens3AdvInceptionV3') with slim.arg_scope(inception_v3.inception_v3_arg_scope()): logits_ens4_adv_v3, end_points_ens4_adv_v3 = inception_v3.inception_v3( x, num_classes=num_classes, is_training=False, scope='Ens4AdvInceptionV3') with slim.arg_scope(inception_v4.inception_v4_arg_scope()): logits_v4, end_points_v4 = inception_v4.inception_v4( x, num_classes=num_classes, is_training=False) with slim.arg_scope( inception_resnet_v2.inception_resnet_v2_arg_scope()): logits_res_v2, end_points_res_v2 = inception_resnet_v2.inception_resnet_v2( x, num_classes=num_classes, is_training=False) with slim.arg_scope( inception_resnet_v2.inception_resnet_v2_arg_scope()): logits_ensadv_res_v2, end_points_ensadv_res_v2 = inception_resnet_v2.inception_resnet_v2( x, num_classes=num_classes, is_training=False, scope='EnsAdvInceptionResnetV2') with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits_resnet, end_points_resnet = resnet_v2.resnet_v2_101( x, num_classes=num_classes, is_training=False) pred = tf.argmax( end_points_v3['Predictions'] + end_points_adv_v3['Predictions'] + end_points_ens3_adv_v3['Predictions'] + \ end_points_ens4_adv_v3['Predictions'] + end_points_v4['Predictions'] + \ end_points_res_v2['Predictions'] + end_points_ensadv_res_v2['Predictions'] + end_points_resnet['predictions'], 1) y = tf.placeholder(tf.int32, shape=[ FLAGS.batch_size, ]) one_hot = tf.one_hot(y, num_classes) logits_dict = [ logits_v3, logits_adv_v3, logits_ens3_adv_v3, logits_ens4_adv_v3, logits_v4, logits_res_v2, logits_ensadv_res_v2, logits_resnet ] logits_gravity = tf.placeholder(dtype=tf.float32, shape=[FLAGS.batch_size, 8]) logits0 = (logits_dict[0][0] * logits_gravity[0][0] + logits_dict[1][0] * logits_gravity[0][1] + logits_dict[2][0] * logits_gravity[0][2]\ + logits_dict[3][0] * logits_gravity[0][3] + logits_dict[4][0] * logits_gravity[0][4] + logits_dict[5][0] * logits_gravity[0][5]\ + logits_dict[6][0] * logits_gravity[0][6] + logits_dict[7][0] * logits_gravity[0][7]) / 36 logits1 = (logits_dict[0][1] * logits_gravity[1][0] + logits_dict[1][1] * logits_gravity[1][1] + logits_dict[2][1] * logits_gravity[1][2]\ + logits_dict[3][1] * logits_gravity[1][3] + logits_dict[4][1] * logits_gravity[1][4] + logits_dict[5][1] * logits_gravity[1][5]\ + logits_dict[6][1] * logits_gravity[1][6] + logits_dict[7][1] * logits_gravity[1][7]) / 36 logits0 = tf.reshape(logits0, [1, 1001]) logits1 = tf.reshape(logits1, [1, 1001]) logits = tf.concat([logits0, logits1], 0) cross_entropy = tf.losses.softmax_cross_entropy(one_hot, logits, label_smoothing=0.0, weights=1.0) noise = tf.gradients(cross_entropy, x)[0] noise = noise / tf.reduce_mean( tf.abs(noise), [1, 2, 3], keep_dims=True ) # 可以改成 noise = noise / tf.reduce_sum(tf.abs(noise), [1, 2, 3], keep_dims=True) grad = tf.placeholder(tf.float32, shape=batch_shape) noise = momentum * grad + noise adv = x + alpha * tf.sign(noise) x_max = tf.placeholder(tf.float32, shape=batch_shape) x_min = tf.placeholder(tf.float32, shape=batch_shape) adv = tf.clip_by_value(adv, x_min, x_max) # Run computation s1 = tf.train.Saver(slim.get_model_variables(scope='InceptionV3')) s2 = tf.train.Saver(slim.get_model_variables(scope='AdvInceptionV3')) s3 = tf.train.Saver( slim.get_model_variables(scope='Ens3AdvInceptionV3')) s4 = tf.train.Saver( slim.get_model_variables(scope='Ens4AdvInceptionV3')) s5 = tf.train.Saver(slim.get_model_variables(scope='InceptionV4')) s6 = tf.train.Saver( slim.get_model_variables(scope='InceptionResnetV2')) s7 = tf.train.Saver( slim.get_model_variables(scope='EnsAdvInceptionResnetV2')) s8 = tf.train.Saver(slim.get_model_variables(scope='resnet_v2')) s1.restore(sess, FLAGS.checkpoint_path_inception_v3) s2.restore(sess, FLAGS.checkpoint_path_adv_inception_v3) s3.restore(sess, FLAGS.checkpoint_path_ens3_adv_inception_v3) s4.restore(sess, FLAGS.checkpoint_path_ens4_adv_inception_v3) s5.restore(sess, FLAGS.checkpoint_path_inception_v4) s6.restore(sess, FLAGS.checkpoint_path_inception_resnet_v2) s7.restore(sess, FLAGS.checkpoint_path_ens_adv_inception_resnet_v2) s8.restore(sess, FLAGS.checkpoint_path_resnet) sum = 0 failure_num = 0 l2_distance = 0 label_distance = 0 images = [] for filenames, images in load_images(FLAGS.input_dir, batch_shape): images = images.astype(np.float32) #不知道需不需要!!!!!!!!!!!!!!!!!!!!!!! images_flatten_initial = images.reshape((2, 268203)) sum += len(filenames) # 对于每个图片在迭代生成对抗样本的过程中,x_max和x_min 是不变的!!!!!!! x_Max = np.clip(images + eps, -1.0, 1.0) x_Min = np.clip(images - eps, -1.0, 1.0) prediction = [] Noise = [] for i in range(FLAGS.num_iter): if i == 0: prediction = sess.run(pred, feed_dict={x: images}) print('true_label::::::::', prediction) # x可能有问题!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! End_points_v3, End_points_adv_v3, End_points_ens3_adv_v3, End_points_ens4_adv_v3, End_points_v4, End_points_res_v2, End_points_ensadv_res_v2, End_points_resnet = \ sess.run([end_points_v3, end_points_adv_v3, end_points_ens3_adv_v3, end_points_ens4_adv_v3, end_points_v4, end_points_res_v2, end_points_ensadv_res_v2, end_points_resnet], feed_dict={x: images, y: prediction}) logits_proportion = [] for j in range(FLAGS.batch_size): end_points_v3_Pred = End_points_v3['Predictions'][j][ prediction[j]] end_points_adv_v3_Pred = End_points_adv_v3['Predictions'][ j][prediction[j]] end_points_ens3_adv_v3_Pred = End_points_ens3_adv_v3[ 'Predictions'][j][prediction[j]] end_points_ens4_adv_v3_Pred = End_points_ens4_adv_v3[ 'Predictions'][j][prediction[j]] end_points_v4_Pred = End_points_v4['Predictions'][j][ prediction[j]] end_points_res_v2_Pred = End_points_res_v2['Predictions'][ j][prediction[j]] end_points_ensadv_res_v2_Pred = End_points_ensadv_res_v2[ 'Predictions'][j][prediction[j]] end_points_resnet_Pred = End_points_resnet['predictions'][ j][prediction[j]] print('end_points_v3_Pred::::::', end_points_v3_Pred) print('end_points_adv_v3_Pred::::::', end_points_adv_v3_Pred) print('end_points_ens3_adv_v3_Pred::::::', end_points_ens3_adv_v3_Pred) print('end_points_ens4_adv_v3_Pred::::::', end_points_ens4_adv_v3_Pred) print('end_points_v4_Pred::::::', end_points_v4_Pred) print('end_points_res_v2_Pred::::::', end_points_res_v2_Pred) print('end_points_ensadv_res_v2_Pred::::::', end_points_ensadv_res_v2_Pred) print('end_points_resnet_Pred::::::', end_points_resnet_Pred) ens_Pred_Value = np.array([ end_points_v3_Pred, end_points_adv_v3_Pred, end_points_ens3_adv_v3_Pred, end_points_ens4_adv_v3_Pred, end_points_v4_Pred, end_points_res_v2_Pred, end_points_ensadv_res_v2_Pred, end_points_resnet_Pred ]) print('ens_Pred_Value:::::', ens_Pred_Value) TopKFitIndx = np.argsort(ens_Pred_Value) a = [0.0] * 8 for m in range(8): a[TopKFitIndx[m]] = 8 - m # a[m] = 1 logits_proportion.append(a) if i == 0: Grad = np.zeros(shape=[FLAGS.batch_size, 299, 299, 3], dtype=np.float32) Noise, images = sess.run( [noise, adv], feed_dict={ x: images, y: prediction, logits_gravity: logits_proportion, grad: Grad, x_max: x_Max, x_min: x_Min }) # x可能有问题!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! else: Noise, images = sess.run( [noise, adv], feed_dict={ x: images, y: prediction, logits_gravity: logits_proportion, grad: Noise, x_max: x_Max, x_min: x_Min }) # Noise可能有问题!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! print('images::::::', images) adv_prediction = sess.run(pred, feed_dict={x: images}) images_flatten_adv = images.reshape((2, 268203)) save_images(images, filenames, FLAGS.output_dir) l2_diatance_list = np.linalg.norm( (images_flatten_initial - images_flatten_adv), axis=1, keepdims=True) for n in range(len(filenames)): l2_distance += l2_diatance_list[n] for j in range(len(filenames)): label_distance += abs(prediction[j] - adv_prediction[j]) if int(prediction[j]) == adv_prediction[j]: failure_num += 1 print('failure_num:::::', failure_num) print('Prediction:::::::', adv_prediction) print('sum::::', sum) print('failure_num::::', failure_num) rate_wrong = failure_num / sum print('rate_wrong:::::::', rate_wrong) print('l2_distance:::::::', l2_distance) print('label_distance::::::', label_distance) end = time.clock() print('run time::::::::', end - start)
with slim.arg_scope(inception_v3.inception_v3_arg_scope()): logits_ens3_adv_v3, end_points_ens3_adv_v3 = inception_v3.inception_v3( x_input, num_classes=num_classes, is_training=False, scope='Ens3AdvInceptionV3') with slim.arg_scope(inception_v3.inception_v3_arg_scope()): logits_ens4_adv_v3, end_points_ens4_adv_v3 = inception_v3.inception_v3( x_input, num_classes=num_classes, is_training=False, scope='Ens4AdvInceptionV3') with slim.arg_scope(inception_v4.inception_v4_arg_scope()): logits_v4, end_points_v4 = inception_v4.inception_v4( x_input, num_classes=num_classes, is_training=False) with slim.arg_scope( inception_resnet_v2.inception_resnet_v2_arg_scope()): logits_res_v2, end_points_res_v2 = inception_resnet_v2.inception_resnet_v2( x_input, num_classes=num_classes, is_training=False) with slim.arg_scope( inception_resnet_v2.inception_resnet_v2_arg_scope()): logits_ens_adv_res_v2, end_points_ens_adv_res_v2 = inception_resnet_v2.inception_resnet_v2( x_input, num_classes=num_classes, is_training=False, scope='EnsAdvInceptionResnetV2')
def __init__(self, name, model_ckpt): """ Class initializer. Args: name: name of the CNN network model_ckpt: path to ckpt file of the pre-trained CNN model Raise: ValueError: if provided network name is not provided """ self.net_name = name # intermediate convolutional layers to extract features if name == 'inception': from nets import inception_v4 self.desired_size = inception_v4.inception_v4.default_image_size arg_scope = inception_v4.inception_v4_arg_scope() network = inception_v4.inception_v4 preprocess = self.inc_preprocess self.layers = [ 'Mixed_3a', 'Mixed_4a', 'Mixed_5e', 'Mixed_6h', 'Mixed_7b' ] elif name == 'resnet': from nets import resnet_v1 self.desired_size = resnet_v1.resnet_v1.default_image_size arg_scope = resnet_v1.resnet_arg_scope() network = resnet_v1.resnet_v1_152 preprocess = self.vgg_preprocess self.layers = [ 'resnet_v1_152/block1', 'resnet_v1_152/block2', 'resnet_v1_152/block3', 'resnet_v1_152/block4' ] elif name == 'vgg': from nets import vgg self.desired_size = vgg.vgg_16.default_image_size arg_scope = vgg.vgg_arg_scope() network = vgg.vgg_16 preprocess = self.vgg_preprocess self.layers = [ 'vgg_16/conv2/conv2_1', 'vgg_16/conv2/conv2_2', 'vgg_16/conv3/conv3_1', 'vgg_16/conv3/conv3_2', 'vgg_16/conv3/conv3_3', 'vgg_16/conv4/conv4_1', 'vgg_16/conv4/conv4_2', 'vgg_16/conv4/conv4_3', 'vgg_16/conv5/conv5_1', 'vgg_16/conv5/conv5_2', 'vgg_16/conv5/conv5_3' ] else: raise ValueError( 'Network not found. Supported networks for Tensorflow framework: vgg, resnet, inception' ) self.input = tf.placeholder(tf.uint8, shape=(None, self.desired_size, self.desired_size, 3), name='input') vid_processed = preprocess(self.input) # create the CNN network with tf.contrib.slim.arg_scope(arg_scope): _, net = network(vid_processed, num_classes=None, is_training=False) # 1. normalize on channel dimension # 2. global max-pooling on channel dimension # 3. normalize feature vector net = [ tf.nn.l2_normalize(tf.reduce_max(tf.nn.l2_normalize(tf.nn.relu( net[l]), 3, epsilon=1e-15), axis=(1, 2)), 1, epsilon=1e-15) for l in self.layers ] self.output = tf.concat(net, axis=1) self.final_sz = self.output.get_shape()[1] init = self.load_model(model_ckpt) config = tf.ConfigProto() config.gpu_options.per_process_gpu_memory_fraction = 0.90 config.gpu_options.allow_growth = True self.sess = tf.Session(config=config) self.sess.run(init)
def graph(x, y, i, x_max, x_min, grad, amplification): one_hot = tf.one_hot(y, FLAGS.num_classes) eps = 2.0 * FLAGS.max_epsilon / 255.0 num_iter = FLAGS.num_iter alpha = eps / num_iter momentum = FLAGS.momentum # amplification factor beta = alpha * FLAGS.amplification_factor gamma = beta # DIM: https://arxiv.org/abs/1803.06978 # input_diversity(FLAG, x) with slim.arg_scope(inception_v3.inception_v3_arg_scope()): logits_v3, end_points_v3 = inception_v3.inception_v3( x, num_classes=FLAGS.num_classes, is_training=False) auxlogits_v3 = end_points_v3['AuxLogits'] with slim.arg_scope(inception_v4.inception_v4_arg_scope()): logits_v4, end_points_v4 = inception_v4.inception_v4( x, num_classes=FLAGS.num_classes, is_training=False) auxlogits_v4 = end_points_v4['AuxLogits'] with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits_resnet, end_points_resnet = resnet_v2.resnet_v2_152( x, num_classes=FLAGS.num_classes, is_training=False) with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()): logits_Incres, end_points_IR = inception_resnet_v2.inception_resnet_v2( x, num_classes=FLAGS.num_classes, is_training=False) auxlogits_IR = end_points_IR['AuxLogits'] logits = (logits_v3 + logits_v4 + logits_resnet + logits_Incres) / 4.0 auxlogits = (auxlogits_v3 + auxlogits_v4 + auxlogits_IR) / 3.0 cross_entropy = tf.losses.softmax_cross_entropy(one_hot, logits, label_smoothing=0.0, weights=1.0) cross_entropy += tf.losses.softmax_cross_entropy(one_hot, auxlogits, label_smoothing=0.0, weights=1.0) noise = tf.gradients(cross_entropy, x)[0] # TI-FGSM: https://arxiv.org/pdf/1904.02884.pdf # noise = tf.nn.depthwise_conv2d(noise, T_kern, strides=[1, 1, 1, 1], padding='SAME') # MI-FGSM: https://arxiv.org/pdf/1710.06081.pdf # noise = noise / tf.reduce_mean(tf.abs(noise), [1, 2, 3], keep_dims=True) # noise = momentum * grad + noise # Project cut noise amplification += beta * tf.sign(noise) cut_noise = tf.clip_by_value(abs(amplification) - eps, 0.0, 10000.0) * tf.sign(amplification) projection = gamma * tf.sign(project_noise(cut_noise, P_kern, kern_size)) # Occasionally, when the adversarial examples are crafted for an ensemble of networks with residual block by combined methods, # you may neet to comment the following line to get better result. amplification += projection x = x + beta * tf.sign(noise) + projection x = tf.clip_by_value(x, x_min, x_max) i = tf.add(i, 1) return x, y, i, x_max, x_min, noise, amplification
def main(argv=None): #加载预处理好的数据 #定义inception_v3的输入, with tf.device('/cpu:0'): images, labels, multi_hot_labels = get_image_batch() # labels = tf.one_hot(labels, depth=5) #定义inception_v3模型,因为谷歌给出的只有模型参数取值,所以这里需要在这个代码中定义inception_v3的模型结构。虽然理论上需要区分训练和 #测试中使用的模型,也就是说在测试时应该使用is_training=False, 但是因为预预先训练好的inception-v3模型中使用的batch normalization参数 #与新的数据会有差异,导致结果很差,所以这里直接使用同一个模型来进行测试 global_step = tf.Variable(0, trainable=False) with slim.arg_scope(inception_v4.inception_v4_arg_scope()): logits, _ = inception_v4.inception_v4(images, num_classes=FLAGS.num_classes, dropout_keep_prob=0.6) #获取需要训练的变量 final_tensor = tf.nn.sigmoid(logits, name=FLAGS.final_tensor_name) tf.summary.histogram(FLAGS.final_tensor_name + '/activations', final_tensor) #定义交叉熵损失,注意在模型定义的时候已经将正则化损失加入损失集合了 tf.losses.sigmoid_cross_entropy(multi_class_labels=multi_hot_labels, logits=logits, weights=1.0) #定义训练过程。这里minimize的过程中指定了需要优化的变量集合 # learning_rate = LEARNING_RATE_BASE learning_rate = tf.train.exponential_decay(FLAGS.learning_rate_base, global_step=global_step, decay_steps=FLAGS.learning_rate_decay_steps, decay_rate=FLAGS.learning_rate_decay) loss = tf.losses.get_total_loss() update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(update_ops): train_step = tf.train.RMSPropOptimizer(learning_rate).minimize(loss, global_step=global_step) #计算正确率 with tf.name_scope('evaluation'): correct_prediction = tf.equal(tf.round(final_tensor), multi_hot_labels) evaluation_step = tf.reduce_mean(tf.cast(correct_prediction, tf.float32), name='accuracy') #定义加载模型的函数 ckpt = tf.train.get_checkpoint_state(FLAGS.ckpt_save_dir) if ckpt and ckpt.model_checkpoint_path: ckpt_file = ckpt.model_checkpoint_path trainable_variables = get_trainable_variables() else: #谷歌提供的训练好的模型文件地址 ckpt_file = FLAGS.initial_ckpt_file trainable_variables = None # print("ckpt.model_checkpoint_path:",ckpt.model_checkpoint_path) print("ckpt_file:",ckpt_file) load_fn = slim.assign_from_checkpoint_fn( ckpt_file, get_tuned_variables(trainable_variables, global_step=global_step), ignore_missing_vars=True ) #定义保存新的训练好的模型函数 saver = tf.train.Saver() with tf.Session() as sess: #初始化没有加载进来的变量。注意这个过程一定要在模型加载之前,否则初始化过程会将已经加载好的变量重新加载 tf.global_variables_initializer().run() tf.local_variables_initializer().run() coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) print('Loading tuned variables from %s' % ckpt_file) load_fn(sess) print("restored global_step:",global_step.eval()) #保存graph for i in range(FLAGS.train_steps): #运行训练过程,这里不会更新全部的参数,只会更新指定的部分参数 start_time = time.time() sess.run(train_step) duration = time.time() - start_time if (i+1) % 100 == 0 or (i+1) == FLAGS.train_steps: examples_per_sec = FLAGS.batch_size / duration sec_per_batch = float(duration) step = global_step.eval() saver.save(sess=sess, save_path=os.path.join(FLAGS.ckpt_save_dir, FLAGS.ckpt_save_file_name), global_step=(step if (i+1) == FLAGS.train_steps else None)) validation_accuracy = sess.run(evaluation_step) loss_value = sess.run(loss) print("Step %d: Validation accuracy = %.2f%%, loss = %.5f (%.1f examples/sec; %.3f sec/batch)" % (step, validation_accuracy*100.0, loss_value, examples_per_sec, sec_per_batch)) coord.request_stop() coord.join(threads) # print("evaluation_step.name:",evaluation_step.name) # print("multi_hot_labels.name:",multi_hot_labels.name) output_graph_def = graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(), [FLAGS.image_batch_name, FLAGS.final_tensor_name, FLAGS.accuracy_name]) with gfile.FastGFile(FLAGS.output_graph, 'wb') as f: f.write(output_graph_def.SerializeToString())
def graph(x, mask, y, i, x_max, x_min, grad): eps = 2.0 * FLAGS.max_epsilon / 255.0 alpha = eps / 10 momentum = FLAGS.momentum num_classes = 1001 with slim.arg_scope(inception_v3.inception_v3_arg_scope()): logits_v3, end_points_v3 = inception_v3.inception_v3( input_diversity(x), num_classes=num_classes, is_training=False) logits_v3_rotated, _ = inception_v3.inception_v3( rotate(x), num_classes=num_classes, is_training=False, reuse=True) with slim.arg_scope(inception_v4.inception_v4_arg_scope()): logits_v4, end_points_v4 = inception_v4.inception_v4( input_diversity(x), num_classes=num_classes, is_training=False) logits_v4_rotated, _ = inception_v4.inception_v4( rotate(x), num_classes=num_classes, is_training=False, reuse=True) with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()): logits_res_v2, end_points_res_v2 = inception_resnet_v2.inception_resnet_v2( input_diversity(x), num_classes=num_classes, is_training=False, reuse=True) logits_res_v2_rotated, _ = inception_resnet_v2.inception_resnet_v2( rotate(x), num_classes=num_classes, is_training=False, reuse=True) with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits_resnet, end_points_resnet = resnet_v2.resnet_v2_152( input_diversity(x), num_classes=num_classes, is_training=False) logits_resnet_rotated, _ = resnet_v2.resnet_v2_152( rotate(x), num_classes=num_classes, is_training=False, reuse=True) with slim.arg_scope(inception_v3.inception_v3_arg_scope()): logits_ens3_adv_v3, end_points_ens3_adv_v3 = inception_v3.inception_v3( input_diversity(x), num_classes=num_classes, is_training=False, scope='Ens3AdvInceptionV3') logits_ens3_adv_v3_rotated, _ = inception_v3.inception_v3( rotate(x), num_classes=num_classes, is_training=False, scope='Ens3AdvInceptionV3', reuse=True) with slim.arg_scope(inception_v3.inception_v3_arg_scope()): logits_ens4_adv_v3, end_points_ens4_adv_v3 = inception_v3.inception_v3( input_diversity(x), num_classes=num_classes, is_training=False, scope='Ens4AdvInceptionV3') logits_ens4_adv_v3_rotated, _ = inception_v3.inception_v3( rotate(x), num_classes=num_classes, is_training=False, scope='Ens4AdvInceptionV3', reuse=True) # with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()): # logits_ens_advres_v2, end_points_ens_advres_v2 = inception_resnet_v2.inception_resnet_v2( # input_diversity(x), num_classes=num_classes, is_training=False, scope='EnsAdvInceptionResnetV2') # logits_ens_advres_v2_rotated, _ = inception_resnet_v2.inception_resnet_v2( # rotate(x), num_classes=num_classes, is_training=False, scope='EnsAdvInceptionResnetV2', reuse=True) logits = (logits_v3 + logits_v4 + logits_res_v2 + logits_resnet + logits_v3_rotated + logits_v4_rotated + logits_res_v2_rotated + logits_resnet_rotated + logits_ens3_adv_v3 + logits_ens4_adv_v3 + logits_ens3_adv_v3_rotated + logits_ens4_adv_v3_rotated) / 12 auxlogits = ( end_points_v3['AuxLogits'] + end_points_v4['AuxLogits'] + end_points_res_v2['AuxLogits'] + end_points_ens3_adv_v3['AuxLogits'] + end_points_ens4_adv_v3['AuxLogits'] # + end_points_ens_advres_v2['AuxLogits'] ) / 5 cross_entropy = tf.losses.softmax_cross_entropy(y, logits, label_smoothing=0.0, weights=1.0) cross_entropy += tf.losses.softmax_cross_entropy(y, auxlogits, label_smoothing=0.0, weights=0.4) noise = tf.gradients(cross_entropy, x)[0] noise = tf.nn.depthwise_conv2d(noise, stack_kernel, strides=[1, 1, 1, 1], padding='SAME') noise = noise / tf.reduce_mean(tf.abs(noise), [1, 2, 3], keep_dims=True) noise = momentum * grad + noise noise = noise * mask[:, :, :, np.newaxis] x = x + alpha * tf.sign(noise) x = tf.clip_by_value(x, x_min, x_max) i = tf.add(i, 1) return x, mask, y, i, x_max, x_min, noise
def graph(x, y, i, x_max, x_min, grad, y_target, y_logits): eps = 2.0 * FLAGS.max_epsilon / 255.0 num_iter = FLAGS.num_iter alpha = eps / num_iter momentum = FLAGS.momentum num_classes = 1001 # should keep original x here for output with slim.arg_scope(inception_v3.inception_v3_arg_scope()): logits_v3, end_points_v3 = inception_v3.inception_v3( input_diversity(x), num_classes=num_classes, is_training=False) with slim.arg_scope(inception_v4.inception_v4_arg_scope()): logits_v4, end_points_v4 = inception_v4.inception_v4( input_diversity(x), num_classes=num_classes, is_training=False) with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()): logits_res_v2, end_points_res_v2 = inception_resnet_v2.inception_resnet_v2( input_diversity(x), num_classes=num_classes, is_training=False) with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits_resnet, end_points_resnet = resnet_v2.resnet_v2_152( input_diversity(x), num_classes=num_classes, is_training=False, reuse=tf.AUTO_REUSE) # with slim.arg_scope(resnet_v2.resnet_arg_scope()): # logits_resnet_50, end_points_resnet_50 = resnet_v2.resnet_v2_50( # input_diversity(x), num_classes=num_classes, is_training=False, reuse=tf.AUTO_REUSE) with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits_resnet_101, end_points_resnet_101 = resnet_v2.resnet_v2_101( input_diversity(x), num_classes=num_classes, is_training=False, reuse=tf.AUTO_REUSE) logits = (logits_resnet + logits_v3 + logits_v4 + logits_res_v2 + logits_resnet_101 ) / 5 y_oh = tf.one_hot(y, num_classes) y_target_oh = tf.one_hot(y_target, num_classes) # loss = -FLAGS.W_crs * tf.losses.softmax_cross_entropy(y_oh, # logits, # label_smoothing=0.0, # weights=1.0) # loss = - Poincare_dis(tf.clip_by_value((y_oh-0.01), 0.0, 1.0), # logits / tf.reduce_sum(tf.abs(logits), [1], keep_dims=True) ) loss_ce = tf.losses.softmax_cross_entropy(y_target_oh, logits, label_smoothing=0.0, weights=1.0) loss_po = Poincare_dis(tf.clip_by_value((y_target_oh-0.00001), 0.0, 1.0), logits / tf.reduce_sum(tf.abs(logits), [1], keep_dims=True)) loss_cos = tf.clip_by_value((Cos_dis(y_oh, logits) - Cos_dis(y_target_oh, logits) + FLAGS.a), 0.0, 2.1) # loss_cos = tf.maximum(loss_ce - tf.losses.softmax_cross_entropy(y_oh, logits, label_smoothing=0.0, weights=1.0) + FLAGS.a, 0.0) if FLAGS.loss == "ce": loss = loss_ce elif FLAGS.loss == "po": loss = loss_po elif FLAGS.loss == "trip_po": loss = loss_po + FLAGS.W_cos*loss_cos # loss += cross_entropy # loss += FLAGS.W_cos*loss_cos noise = -tf.gradients(loss, x)[0] # TI- noise = tf.nn.depthwise_conv2d(noise, stack_kernel, strides=[1, 1, 1, 1], padding='SAME') # CE Cross-entry loss must add this term if FLAGS.loss == 'ce': noise = noise / tf.reduce_mean(tf.abs(noise), [1, 2, 3], keep_dims=True) noise = momentum * grad + noise x = x + alpha * 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, y_target, logits
import os from config import config as FLAGS from nets import inception_v4 as nets _CHECKPOINT_NAME = 'inception_v4.ckpt' checkpoint_path = os.path.join(FLAGS.checkpoint_path, _CHECKPOINT_NAME) arg_scope = nets.inception_v4_arg_scope(weight_decay=0.0) func = nets.inception_v4
def graph(x, y, i, x_max, x_min, grad): eps = 2.0 * FLAGS.max_epsilon / 255.0 num_iter = FLAGS.num_iter alpha = eps / num_iter momentum = FLAGS.momentum num_classes = 1001 with slim.arg_scope(inception_v3.inception_v3_arg_scope()): logits_v3, end_points_v3 = inception_v3.inception_v3( x, num_classes=num_classes, is_training=False) with slim.arg_scope(inception_v3.inception_v3_arg_scope()): logits_adv_v3, end_points_adv_v3 = inception_v3.inception_v3( x, num_classes=num_classes, is_training=False, scope='AdvInceptionV3') with slim.arg_scope(inception_v3.inception_v3_arg_scope()): logits_ens3_adv_v3, end_points_ens3_adv_v3 = inception_v3.inception_v3( x, num_classes=num_classes, is_training=False, scope='Ens3AdvInceptionV3') with slim.arg_scope(inception_v3.inception_v3_arg_scope()): logits_ens4_adv_v3, end_points_ens4_adv_v3 = inception_v3.inception_v3( x, num_classes=num_classes, is_training=False, scope='Ens4AdvInceptionV3') with slim.arg_scope(inception_v4.inception_v4_arg_scope()): logits_v4, end_points_v4 = inception_v4.inception_v4( x, num_classes=num_classes, is_training=False) with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()): logits_res_v2, end_points_res_v2 = inception_resnet_v2.inception_resnet_v2( x, num_classes=num_classes, is_training=False) with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()): logits_ensadv_res_v2, end_points_ensadv_res_v2 = inception_resnet_v2.inception_resnet_v2( x, num_classes=num_classes, is_training=False, scope='EnsAdvInceptionResnetV2') with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits_resnet, end_points_resnet = resnet_v2.resnet_v2_101( x, num_classes=num_classes, is_training=False) pred = tf.argmax(end_points_v3['Predictions'] + end_points_adv_v3['Predictions'] + end_points_ens3_adv_v3['Predictions'] + \ end_points_ens4_adv_v3['Predictions'] + end_points_v4['Predictions'] + \ end_points_res_v2['Predictions'] + end_points_ensadv_res_v2['Predictions'] + end_points_resnet['predictions'], 1) first_round = tf.cast(tf.equal(i, 0), tf.int64) y = first_round * pred + (1 - first_round) * y one_hot = tf.one_hot(y, num_classes) logits = (logits_v3 + 0.25 * logits_adv_v3 + logits_ens3_adv_v3 + \ logits_ens4_adv_v3 + logits_v4 + \ logits_res_v2 + logits_ensadv_res_v2 + logits_resnet) / 7.25 auxlogits = (end_points_v3['AuxLogits'] + 0.25 * end_points_adv_v3['AuxLogits'] + end_points_ens3_adv_v3['AuxLogits'] + \ end_points_ens4_adv_v3['AuxLogits'] + end_points_v4['AuxLogits'] + \ end_points_res_v2['AuxLogits'] + end_points_ensadv_res_v2['AuxLogits']) / 6.25 cross_entropy = tf.losses.softmax_cross_entropy(one_hot, logits, label_smoothing=0.0, weights=1.0) cross_entropy += tf.losses.softmax_cross_entropy(one_hot, auxlogits, label_smoothing=0.0, weights=0.4) noise = tf.gradients(cross_entropy, x)[0] noise = noise / tf.reduce_mean(tf.abs(noise), [1, 2, 3], keep_dims=True) noise = momentum * grad + noise x = x + alpha * 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