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 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 predict_slim(sample_images, print_func=print): """ Code modified from here: [https://github.com/tensorflow/models/issues/429] """ # Setup preprocessing input_tensor = tf.placeholder(tf.float32, shape=(None, 299, 299, 3), name='input_image') scaled_input_tensor = tf.scalar_mul((1.0 / 255), input_tensor) scaled_input_tensor = tf.subtract(scaled_input_tensor, 0.5) scaled_input_tensor = tf.multiply(scaled_input_tensor, 2.0) # Setup session sess = tf.Session() arg_scope = slim_irv2.inception_resnet_v2_arg_scope() with slim.arg_scope(arg_scope): _, end_points = slim_irv2.inception_resnet_v2(scaled_input_tensor, is_training=False) # Load the model print_func("Loading TF-slim checkpoint...") saver = tf.train.Saver() saver.restore(sess, SLIM_CKPT) # Make prediction predict_values = [] for image in sample_images: im = Image.open(image).resize((299, 299)) arr = np.expand_dims(np.array(im), axis=0) y_pred = sess.run([end_points['Predictions']], feed_dict={input_tensor: arr}) y_pred = y_pred[0].ravel() y_pred = y_pred[1:] / y_pred[1:].sum() # remove background class and renormalize print_func("{} class={} prob={}".format(image, np.argmax(y_pred), np.max(y_pred))) predict_values.append(y_pred) return predict_values
def graph(x, y, i, x_max, x_min, grad, eps_inside): num_iter = FLAGS.num_iter alpha = eps_inside / num_iter momentum = FLAGS.momentum num_classes = 1001 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) pred = tf.argmax(end_points_res_v2['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_res_v2 auxlogits = end_points_res_v2['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, eps_inside
def inference(hypes, images, train=True): """ Build ResNet encoder :param hypes: :param images: :param train: :return: """ is_training = tf.convert_to_tensor(train, dtype='bool', name='is_training') num_classes = 2 # images = tf.placeholder(tf.float32, shape=(3, 224, 224, 3)) with slim.arg_scope(inception_resnet_v2_arg_scope()): logit, endpoints = inception_resnet_v2(images, num_classes=num_classes, is_training=is_training) for key in endpoints: print(key, endpoints[key].get_shape().as_list()) print(logit.get_shape().as_list()) exit(0) return { 'early_feat': endpoints['Mixed_5b'], 'deep_feat': endpoints['Conv2d_4a_3x3'] }
def CreateFrozenGraph(checkpointFile, outputFile): graph = tf.Graph() with tf.Session(graph=graph) as sess: with slim.arg_scope(inception_arg_scope()): images_input = tf.placeholder(tf.float32, [None, None, None, 3], 'input_images') images_processed = tf.image.resize_images( images_input, (ImageNetEmbedder.EMBED_SIZE, ImageNetEmbedder.EMBED_SIZE)) images_processed = tf.subtract( images_processed, 0.5) #Inception net preprocessing images_processed = tf.multiply(images_processed, 2.0) images_processed = tf.identity(images_processed, name="processed_images") _, end_points = inception_resnet_v2.inception_resnet_v2( images_processed, is_training=False, create_aux_logits=False) predictions = tf.identity(end_points['Predictions'], name="predictions") embeddings = tf.identity(end_points['PreLogitsFlatten'], name="embeddings") restorer = tf.train.Saver() restorer.restore(sess, checkpointFile) output_graph_def = tf.graph_util.convert_variables_to_constants( sess, graph.as_graph_def(), ["processed_images", "predictions", "embeddings"]) with tf.gfile.GFile(outputFile, "wb") as f: f.write(output_graph_def.SerializeToString())
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_resnet_v2.inception_resnet_v2_arg_scope()): logits_res_v2, end_points_res_v2 = inception_resnet_v2.inception_resnet_v2( x + momentum * grad, num_classes=num_classes, is_training=False) pred = tf.argmax(end_points_res_v2['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_res_v2) / 7.25 auxlogits = (end_points_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
def test_inception_resnet_v2(img_dir): """ Test Inception-ResNet-V2 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_resnet_v2_arg_scope()): _, _ = inception_resnet_v2(inputs, 1001, is_training=False) with tf.Session() as sess: tf.train.Saver().restore(sess, './models/inception_resnet_v2.ckpt') inputs = sess.graph.get_tensor_by_name('input_images:0') outputs = sess.graph.get_tensor_by_name( 'InceptionResnetV2/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-ResNet-V2:', name, prob) return name, prob
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 model(images, weight_decay=1e-5, is_training=True): images = mean_image_subtraction(images) with slim.arg_scope(inception_arg_scope(weight_decay=weight_decay)): logits, end_points = inception_resnet_v2(images, num_classes=None, is_training=is_training) for key in end_points.keys(): print(key, end_points[key]) return logits, end_points
def inference_inception_resnet_v2(x_input, dropout_keep_prob=1, num_classes=1001): with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()): logits, _ = inception_resnet_v2.inception_resnet_v2(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('InceptionResnetV2/')] 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 main(_): # 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 num_classes = 1001 batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3] tf.logging.set_verbosity(tf.logging.INFO) print(time.time() - start_time) with tf.Graph().as_default(): # Prepare graph x_input = tf.placeholder(tf.float32, shape=batch_shape) x_max = tf.clip_by_value(x_input + eps, -1.0, 1.0) x_min = tf.clip_by_value(x_input - eps, -1.0, 1.0) with slim.arg_scope( inception_resnet_v2.inception_resnet_v2_arg_scope()): _, end_points = inception_resnet_v2.inception_resnet_v2( x_input, num_classes=num_classes, is_training=False) predicted_labels = tf.argmax(end_points['Predictions'], 1) y = tf.one_hot(predicted_labels, num_classes) i = tf.constant(0) grad = tf.zeros(shape=batch_shape) x_adv, _, _, _, _, _ = tf.while_loop( stop, graph, [x_input, y, i, x_max, x_min, grad]) # Run computation s1 = tf.train.Saver(slim.get_model_variables(scope='InceptionV3')) s2 = tf.train.Saver(slim.get_model_variables(scope='AdvInceptionV3')) 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')) with tf.Session() as sess: s1.restore(sess, FLAGS.checkpoint_path_inception_v3) s2.restore(sess, FLAGS.checkpoint_path_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) print(time.time() - start_time) for filenames, images in load_images(FLAGS.input_dir, batch_shape): adv_images = sess.run(x_adv, feed_dict={x_input: images}) save_images(adv_images, filenames, FLAGS.output_dir) print(time.time() - start_time)
def predict(self, img_file): ''' 对给定的图片进行预测,给出Top5类别的名称及可能的概率 img_file:全路径文件名 ''' print('image file:{0}'.format(img_file)) names = self.get_imagenet_label_names() with tf.Graph().as_default(): with self.slim.arg_scope( inception_resnet_v2.inception_resnet_v2_arg_scope()): testImage_string = tf.gfile.FastGFile(img_file, 'rb').read() testImage = tf.image.decode_jpeg(testImage_string, channels=3) processed_image = inception_preprocessing.preprocess_image( testImage, self.image_size, self.image_size, is_training=False) processed_images = tf.expand_dims(processed_image, 0) logits, _ = inception_resnet_v2.inception_resnet_v2( processed_images, num_classes=1001, is_training=False) probabilities = tf.nn.softmax(logits) init_fn = self.slim.assign_from_checkpoint_fn( os.path.join(self.ckpt_dir, 'inception_resnet_v2_2016_08_30.ckpt'), self.slim.get_model_variables('InceptionResnetV2')) with tf.Session() as sess: init_fn(sess) np_image, probabilities = sess.run( [processed_images, probabilities]) probabilities = probabilities[0, 0:] sorted_inds = [ i[0] for i in sorted(enumerate(-probabilities), key=lambda x: x[1]) ] #names = imagenet.create_readable_names_for_imagenet_labels() for i in range(5): index = sorted_inds[i] print((probabilities[index], names[index])) print('show:{0}'.format(img_file)) img = np.array(Image.open(img_file)) plt.rcParams['font.sans-serif'] = [ 'SimHei' ] # 在图片上显示中文,如果直接显示形式为:u'内容' plt.rcParams['axes.unicode_minus'] = False # 显示-号 plt.figure() plt.imshow(img) myfont = FontProperties( fname='/usr/share/fonts/truetype/arphic/uming.ttc') plt.suptitle(names[sorted_inds[0]], fontsize=14, fontweight='bold', fontproperties=myfont) plt.axis('off') plt.show()
def graph(x, y, i, grad): num_classes = 1001 x = x 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) pred = tf.argmax(end_points_res_v2['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 _inception_resnet_v2(self, X, num_classes, dropout_keep_prob=0.8, is_train=False): arg_scope = inception_resnet_v2_arg_scope() with slim.arg_scope(arg_scope): net, end_points = inception_resnet_v2( X, num_classes=num_classes, dropout_keep_prob=dropout_keep_prob, is_training=is_train) return net
def eval(adv_imgs, labels, x_inputs, total_score, total_count): image = (((adv_imgs + 1.0) * 0.5) * 255.0) processed_imgs_inv1 = preprocess_for_model(image, 'inception_v1') with slim.arg_scope(inception.inception_v1_arg_scope()): logits_inc_v1, end_points_inc_v1 = inception.inception_v1( processed_imgs_inv1, num_classes=FLAGS.num_classes, is_training=False, scope='InceptionV1', reuse=True) pred_inception = tf.argmax(end_points_inc_v1['Predictions'], 1) # rescale pixle range from [-1, 1] to [0, 255] for resnet_v1 and vgg's input processed_imgs_res_v1_50 = preprocess_for_model(image, 'resnet_v1_50') with slim.arg_scope(resnet_v1.resnet_arg_scope()): logits_res_v1_50, end_points_res_v1_50 = resnet_v1.resnet_v1_50( processed_imgs_res_v1_50, num_classes=FLAGS.num_classes, is_training=False, scope='resnet_v1_50', reuse=True) end_points_res_v1_50['logits'] = tf.squeeze(end_points_res_v1_50['resnet_v1_50/logits'], [1, 2]) end_points_res_v1_50['probs'] = tf.nn.softmax(end_points_res_v1_50['logits']) pred_resnet = tf.argmax(end_points_res_v1_50['probs'], 1) processed_imgs_inv3 = preprocess_for_model(image, 'inception_v3') with slim.arg_scope(inception_v3.inception_v3_arg_scope()): logits_res_inception_v3, end_points_inception_v3 = inception_v3.inception_v3( processed_imgs_inv3, num_classes=FLAGS.num_classes, is_training=False, scope='InceptionV3', reuse=True) pred_inception_v3 = tf.argmax(end_points_inception_v3['Predictions'], 1) processed_imgs_inv_res = preprocess_for_model(image, 'inception_resnet_v2') with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()): logits_res_inception_resnet, end_points_inception_resnet = inception_resnet_v2.inception_resnet_v2( processed_imgs_inv_res, num_classes=FLAGS.num_classes, is_training=False, scope='InceptionResnetV2') pred_ince_res = tf.argmax(end_points_inception_resnet['Predictions'], 1) for i in range(adv_imgs.shape[0]): def f1(total_score, total_count): total_score = tf.add(total_score, 64) return total_score, total_count def f2(total_score, total_count): adv = (((adv_imgs[i] + 1.0) * 0.5) * 255.0) ori = (((x_inputs[i] + 1.0) * 0.5) * 255.0) diff = tf.reshape(adv, [-1, 3]) - tf.reshape(ori, [-1, 3]) distance = tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.square(diff), axis=1))) total_score = tf.add(total_score, distance) total_count = tf.add(total_count, 1) return total_score, total_count total_score, total_count = tf.cond(tf.equal(pred_inception[i], labels[i]), lambda: f2(total_score, total_count), lambda: f1(total_score, total_count)) total_score, total_count = tf.cond(tf.equal(pred_resnet[i], labels[i]), lambda: f2(total_score, total_count), lambda: f1(total_score, total_count)) # total_score, total_count = tf.cond(tf.equal(pred_inception_v3[i], labels[i]), lambda: f2(total_score, total_count), lambda: f1(total_score, total_count)) total_score, total_count = tf.cond(tf.equal(pred_ince_res[i], labels[i]), lambda: f2(total_score, total_count), lambda: f1(total_score, total_count)) return total_score, total_count
def feature_extractor(image_path, options=None): """Runs a trained version of inception-resnet-v2 on an image and extracts the inputs from the final layer before the fully connected stages. Args: image_path: the path to the image options: in this case options is not used Return: an array of features which depending on the config options """ # size of images inc-resnet is compatible with image_size = inception_resnet_v2.inception_resnet_v2.default_image_size checkpoint_path = os.path.join( dir, 'checkpoints/inception_resnet_v2_2016_08_30.ckpt') image_string = urllib2.urlopen(image_path).read() # JPEG format converted to unit8 tensor image = tf.image.decode_jpeg(image_string, channels=3) # inception specific pre processing processed_image = inception_preprocessing.preprocess_image( image, image_size, image_size, is_training=False) # the model accepts images in batches processed_images = tf.expand_dims(processed_image, 0) with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()): logits, end_points = inception_resnet_v2.inception_resnet_v2( processed_images, num_classes=1001, is_training=False) weights_from_file = slim.get_variables_to_restore(exclude=['logits']) init_fn = slim.assign_from_checkpoint_fn(checkpoint_path, weights_from_file) # last layer before fully connected layer features = end_points['PreLogitsFlatten'] # run the image through the network with tf.Session() as sess: init_fn(sess) features = sess.run([features]) return features[0][0]
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_resnet_v2.inception_resnet_v2_arg_scope()): logits, end_points = inception_resnet_v2.inception_resnet_v2( 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_resnet_v2.ckpt') self.ckpt_loaded = True
def main(): """ You can also run these commands manually to generate the pb file 1. git clone https://github.com/tensorflow/models.git 2. export PYTHONPATH=Path_to_your_model_folder 3. python alexnet.py """ height, width = 299, 299 inputs = tf.Variable(tf.random_uniform((2, height, width, 3)), name='input') net, end_points = inception_resnet_v2.inception_resnet_v2(inputs,is_training = False) print("nodes in the graph") for n in end_points: print(n + " => " + str(end_points[n])) net_outputs = map(lambda x: tf.get_default_graph().get_tensor_by_name(x), argv[2].split(',')) run_model(net_outputs, argv[1])
def get_inception_resnet_v2_model(x, args, is_training): with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope( weight_decay=args.weight_decay, batch_norm_decay=0.9997, batch_norm_epsilon=0.001, activation_fn=tf.nn.relu)): _, end_points = inception_resnet_v2.inception_resnet_v2(x, num_classes=None, is_training=is_training, dropout_keep_prob=args.dropout_keep_prob, reuse=None, scope='InceptionResnetV2', create_aux_logits=False, activation_fn=tf.nn.relu) net = end_points['PreAuxLogits'] return get_classifier_and_reconstruct_model(net, args, is_training, end_points)
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 startup(self): names = self.get_imagenet_label_names() with tf.Graph().as_default(): with self.slim.arg_scope( inception_resnet_v2.inception_resnet_v2_arg_scope()): img = tf.placeholder(tf.float32, shape=[299, 299, 3]) imgs = tf.placeholder(tf.float32, shape=[1, 299, 299, 3]) logits, _ = inception_resnet_v2.inception_resnet_v2( imgs, num_classes=1001, is_training=False) probabilities = tf.nn.softmax(logits) init_fn = self.slim.assign_from_checkpoint_fn( os.path.join(self.ckpt_dir, 'inception_resnet_v2_2016_08_30.ckpt'), self.slim.get_model_variables('InceptionResnetV2')) input = imgs with tf.Session() as sess: init_fn(sess) params = ag.img_csf_q.get(block=True) while params: req_id = params['req_id'] testImage_string = tf.gfile.FastGFile( params['img_file'], 'rb').read() testImage = tf.image.decode_jpeg(testImage_string, channels=3) processed_image = inception_preprocessing.preprocess_image( testImage, self.image_size, self.image_size, is_training=False) processed_images = tf.expand_dims(processed_image, 0) np_image, rst_p = sess.run( [imgs, probabilities], feed_dict={imgs: processed_images.eval()}) pros = rst_p[0, 0:] sorted_inds = [ i[0] for i in sorted(enumerate(-pros), key=lambda x: x[1]) ] index = sorted_inds[0] rst = {} rst['req_id'] = req_id rst['img_rst'] = names[index] rst['probability'] = pros[index] ag.app_db[req_id] = rst print('******运行Tensorflow成功:{0}({1})'.format( rst['img_rst'], rst['probability'])) params = ag.img_csf_q.get(block=True)
def main(): """ You can also run these commands manually to generate the pb file 1. git clone https://github.com/tensorflow/models.git 2. export PYTHONPATH=Path_to_your_model_folder 3. python alexnet.py """ tf.set_random_seed(1) height, width = 299, 299 inputs = tf.Variable(tf.random_uniform((2, height, width, 3)), name='input') inputs = tf.identity(inputs, "input_node") net, end_points = inception_resnet_v2.inception_resnet_v2(inputs,is_training = False) print("nodes in the graph") for n in end_points: print(n + " => " + str(end_points[n])) net_outputs = map(lambda x: tf.get_default_graph().get_tensor_by_name(x), argv[2].split(',')) run_model(net_outputs, argv[1], 'InceptionResnetV2', argv[3] == 'True')
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 endpoints(image, is_training): if image.get_shape().ndims != 4: raise ValueError('Input must be of size [batch, height, width, 3]') image = image - tf.constant( _RGB_MEAN, dtype=tf.float32, shape=(1, 1, 1, 3)) with tf.contrib.slim.arg_scope( inception_resnet_v2_arg_scope(batch_norm_decay=0.9, weight_decay=0.0)): _, endpoints = inception_resnet_v2(image, num_classes=None, is_training=is_training) endpoints['model_output'] = endpoints['global_pool'] = tf.reduce_mean( endpoints['Conv2d_7b_1x1'], [1, 2], name='final_pool', keep_dims=False) return endpoints, 'inception_resnet_v2'
def graph_small(x, target_class_input, i, x_max, x_min, grad): eps = 2.0 * FLAGS.max_epsilon / 255.0 alpha = eps / 28 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_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') one_hot_target_class = tf.one_hot(target_class_input, num_classes) logits = (logits_v3 + 2 * logits_ensadv_res_v2) / 3 auxlogits = (end_points_v3['AuxLogits'] + 2 * end_points_ensadv_res_v2['AuxLogits']) / 3 cross_entropy = tf.losses.softmax_cross_entropy(one_hot_target_class, logits, label_smoothing=0.0, weights=1.0) cross_entropy += tf.losses.softmax_cross_entropy(one_hot_target_class, auxlogits, label_smoothing=0.0, weights=0.4) noise = tf.gradients(cross_entropy, x)[0] 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, target_class_input, i, x_max, x_min, noise
def run(self, images): sess = tf.InteractiveSession() input_tensor = tf.placeholder(tf.float32, shape=(None,299,299,3), name='input_image') scaled_input_tensor = tf.scalar_mul((1.0/255), input_tensor) scaled_input_tensor = tf.subtract(scaled_input_tensor, 0.5) scaled_input_tensor = tf.multiply(scaled_input_tensor, 2.0) with slim.arg_scope(inception_resnet_v2_arg_scope()): logits, end_points = inception_resnet_v2(scaled_input_tensor, num_classes=1001, is_training=False) saver = tf.train.Saver() saver.restore(sess, self.checkpoints_filename) for image in images: im = Image.open(image).resize((299,299)) im = np.array(im) im = im.reshape(-1, 299, 299, 3) predict_values, logit_values = sess.run([end_points['Predictions'], logits], feed_dict={input_tensor: im}) print (np.max(predict_values), np.max(logit_values)) print (np.argmax(predict_values), np.argmax(logit_values))
def model_fn(images, labels, num_classes, mode): with tf.contrib.slim.arg_scope( inception_resnet_v2.inception_resnet_v2_arg_scope()): logits, end_points = inception_resnet_v2.inception_resnet_v2( images, num_classes, is_training=(mode == tf.estimator.ModeKeys.TRAIN)) predictions = { 'classes': tf.argmax(input=logits, axis=1), 'probabilities': tf.nn.softmax(logits, name='softmax_tensor') } if mode == tf.estimator.ModeKeys.PREDICT: return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions) exclude = ['InceptionResnetV2/Logits', 'InceptionResnetV2/AuxLogits'] variables_to_restore = tf.contrib.slim.get_variables_to_restore( exclude=exclude) scopes = {os.path.dirname(v.name) for v in variables_to_restore} tf.train.init_from_checkpoint('inception_resnet_v2_2016_08_30.ckpt', {s + '/': s + '/' for s in scopes}) tf.losses.softmax_cross_entropy(onehot_labels=labels, logits=logits) total_loss = tf.losses.get_total_loss( ) #obtain the regularization losses as well # Configure the training op if mode == tf.estimator.ModeKeys.TRAIN: global_step = tf.train.get_or_create_global_step() optimizer = tf.train.AdamOptimizer(learning_rate=0.00002) train_op = optimizer.minimize(total_loss, global_step) else: train_op = None return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions, loss=total_loss, train_op=train_op)
def model(inputs, tag, num_class, is_training, regular=0.0001): net = mean_image_subtraction(inputs) if tag == 0: return le_net(net, num_class, is_training, regular) if tag == 1: return vgg.vgg_16(net, num_classes=num_class, is_training=is_training)[0] if tag == 2: with slim.arg_scope(resnet_v1.resnet_arg_scope()): logits, end_points = resnet_v1.resnet_v1_50( net, num_classes=num_class, is_training=is_training, scope='resnet_v1_50') return tf.squeeze(logits, [1, 2], name='SpatialSqueeze') if tag == 3: return inception_v3.inception_v3(net, num_classes=num_class, is_training=is_training, scope='inception_v3')[0] if tag == 4: return inception_resnet_v2.inception_resnet_v2( net, num_class, True, scope='incep_resnetv2')[0]
def __init__(self): self.sessIR = tf.Session() self.labels_names = self.imagenet_labels() self.image_sizeIR = inception_resnet_v2.inception_resnet_v2.default_image_size # Define Inception Resnet v2 neural network self.processed_imagesIR = tf.placeholder(tf.float32, shape=(None, self.image_sizeIR, self.image_sizeIR, 3)) with slim.arg_scope( inception_resnet_v2.inception_resnet_v2_arg_scope()): logitsIR, _ = inception_resnet_v2.inception_resnet_v2( self.processed_imagesIR, num_classes=1001, is_training=False) self.probabilitiesIR = tf.nn.softmax(logitsIR) # Load saved checkpoint for Resnet v2 neural network checkpoints_dir = './checkpoints' init_fn = slim.assign_from_checkpoint_fn( os.path.join(checkpoints_dir, 'inception_resnet_v2_2016_08_30.ckpt'), slim.get_model_variables('InceptionResnetV2')) init_fn(self.sessIR)