def testNoBatchNormScaleByDefault(self): height, width = 299, 299 num_classes = 1000 inputs = tf.placeholder(tf.float32, (1, height, width, 3)) with tf.slim.arg_scope(inception.inception_v4_arg_scope()): inception.inception_v4(inputs, num_classes, is_training=False) self.assertEqual(tf.global_variables('.*/BatchNorm/gamma:0$'), [])
def testNoBatchNormScaleByDefault(self): height, width = 299, 299 num_classes = 1000 inputs = tf.placeholder(tf.float32, (1, height, width, 3)) with tf.contrib.slim.arg_scope(inception.inception_v4_arg_scope()): inception.inception_v4(inputs, num_classes, is_training=False) self.assertEqual(tf.global_variables('.*/BatchNorm/gamma:0$'), [])
def testBatchNormScale(self): height, width = 299, 299 num_classes = 1000 inputs = tf.placeholder(tf.float32, (1, height, width, 3)) with tf.contrib.slim.arg_scope( inception.inception_v4_arg_scope(batch_norm_scale=True)): inception.inception_v4(inputs, num_classes, is_training=False) gamma_names = set( v.op.name for v in tf.global_variables('.*/BatchNorm/gamma:0$')) self.assertGreater(len(gamma_names), 0) for v in tf.global_variables('.*/BatchNorm/moving_mean:0$'): self.assertIn(v.op.name[:-len('moving_mean')] + 'gamma', gamma_names)
def testBatchNormScale(self): height, width = 299, 299 num_classes = 1000 inputs = tf.placeholder(tf.float32, (1, height, width, 3)) with tf.slim.arg_scope( inception.inception_v4_arg_scope(batch_norm_scale=True)): inception.inception_v4(inputs, num_classes, is_training=False) gamma_names = set( v.op.name for v in tf.global_variables('.*/BatchNorm/gamma:0$')) self.assertGreater(len(gamma_names), 0) for v in tf.global_variables('.*/BatchNorm/moving_mean:0$'): self.assertIn(v.op.name[:-len('moving_mean')] + 'gamma', gamma_names)
def __call__(self, x_input, batch_size=None, is_training=False): """Constructs model and return probabilities for given input.""" reuse = True if self.built else None with slim.arg_scope(inception.inception_v4_arg_scope()): with tf.variable_scope(self.ckpt): logits, end_points = inception.inception_v4( x_input, num_classes=self.num_classes, is_training=is_training, reuse=reuse) preds = tf.argmax(logits, axis=1) self.built = True self.logits = logits self.preds = preds return logits
def __call__(self, x_input): """Constructs model and return probabilities for given input.""" reuse = True if self.built else None x_input = image_normalize(x_input, normalization_method[3]) with slim.arg_scope(inception.inception_v4_arg_scope()): _, end_points = inception.inception_v4( x_input, num_classes=self.num_classes, is_training=False, reuse=reuse) self.built = True output = end_points['Predictions'] # Strip off the extra reshape op at the output probs = output.op.inputs[0] return output
def inception_v4(inputs, is_training, opts): with slim.arg_scope(inception.inception_v4_arg_scope( weight_decay=opts.weight_decay, use_batch_norm=opts.use_batch_norm, batch_norm_decay=opts.batch_norm_decay, batch_norm_epsilon=opts.batch_norm_epsilon, activation_fn=tf.nn.relu)): return inception.inception_v4( inputs, num_classes=opts.num_classes, is_training=is_training, dropout_keep_prob=opts.dropout_keep_prob, reuse=None, create_aux_logits=opts.create_aux_logits, global_pool=opts.global_pool)
def use_fined_model(self): image_size = inception.inception_v4.default_image_size batch_size = 3 flowers_data_dir = "../../data/flower" train_dir = '/tmp/inception_finetuned/' with tf.Graph().as_default(): tf.logging.set_verbosity(tf.logging.INFO) dataset = flowers.get_split('train', flowers_data_dir) images, images_raw, labels = self.load_batch(dataset, height=image_size, width=image_size) # Create the model, use the default arg scope to configure the batch norm parameters. with slim.arg_scope(inception.inception_v4_arg_scope()): logits, _ = inception.inception_v4( images, num_classes=dataset.num_classes, is_training=True) probabilities = tf.nn.softmax(logits) checkpoint_path = tf.train.latest_checkpoint(train_dir) init_fn = slim.assign_from_checkpoint_fn( checkpoint_path, slim.get_variables_to_restore()) with tf.Session() as sess: with slim.queues.QueueRunners(sess): sess.run(tf.initialize_local_variables()) init_fn(sess) np_probabilities, np_images_raw, np_labels = sess.run( [probabilities, images_raw, labels]) for i in range(batch_size): image = np_images_raw[i, :, :, :] true_label = np_labels[i] predicted_label = np.argmax(np_probabilities[i, :]) predicted_name = dataset.labels_to_names[ predicted_label] true_name = dataset.labels_to_names[true_label] plt.figure() plt.imshow(image.astype(np.uint8)) plt.title('Ground Truth: [%s], Prediction [%s]' % (true_name, predicted_name)) plt.axis('off') plt.show() return
def run_inference_on_image(image): """Runs inference on an image. Args: image: Image file name. Returns: Nothing """ dest_directory = FLAGS.model_dir image_size = inception.inception_v4.default_image_size if not tf.gfile.Exists(image): tf.logging.fatal('File does not exist %s', image) image_string = tf.gfile.FastGFile(image, 'rb').read() with tf.Graph().as_default(): image = tf.image.decode_jpeg(image_string, channels=3) processed_image = inception_preprocessing.preprocess_image( image, image_size, image_size, is_training=False) processed_images = tf.expand_dims(processed_image, 0) # Create the model, use the default arg scope to configure the batch # norm parameters. with slim.arg_scope(inception.inception_v4_arg_scope()): logits, _ = inception.inception_v4( processed_images, num_classes=1001, is_training=False) probabilities = tf.nn.softmax(logits) init_fn = slim.assign_from_checkpoint_fn( os.path.join(dest_directory, 'inception_v4.ckpt'), slim.get_model_variables('InceptionV4')) with tf.Session() as sess: init_fn(sess) probabilities = sess.run(probabilities) probabilities = probabilities[0, 0:] sorted_inds = [i[0] for i in sorted( enumerate(-probabilities), key=lambda x:x[1])] names = create_readable_names_for_imagenet_labels() top_k = FLAGS.num_top_predictions for i in range(top_k): index = sorted_inds[i] print('%s (score = %.5f)' % (names[index], probabilities[index]))
def fine_tune_inception(self): train_dir = '/tmp/inception_finetuned/' image_size = inception.inception_v4.default_image_size checkpoint_path = "../../data/trained_models/inception_v4/inception_v4.ckpt" flowers_data_dir = "../../data/flower" with tf.Graph().as_default(): tf.logging.set_verbosity(tf.logging.INFO) dataset = flowers.get_split('train', flowers_data_dir) images, _, labels = self.load_batch(dataset, height=image_size, width=image_size) # Create the model, use the default arg scope to configure the batch norm parameters. with slim.arg_scope(inception.inception_v4_arg_scope()): logits, _ = inception.inception_v4( images, num_classes=dataset.num_classes, is_training=True) # Specify the loss function: one_hot_labels = slim.one_hot_encoding(labels, dataset.num_classes) total_loss = slim.losses.softmax_cross_entropy( logits, one_hot_labels) # total_loss = slim.losses.get_total_loss(add_regularization_losses=False) # total_loss = slim.losses.get_total_loss() # Create some summaries to visualize the training process: tf.summary.scalar('losses/Total_Loss', total_loss) # Specify the optimizer and create the train op: optimizer = tf.train.AdamOptimizer(learning_rate=0.001) train_op = slim.learning.create_train_op(total_loss, optimizer) # Run the training: number_of_steps = math.ceil(dataset.num_samples / 32) * 1 final_loss = slim.learning.train( train_op, logdir=train_dir, init_fn=self.get_init_fn(checkpoint_path), number_of_steps=number_of_steps) print('Finished training. Last batch loss %f' % final_loss) return
def getResult(photo_filenames): labels = [] maps = getMaps(LABELS_TXT) with tf.Graph().as_default(): tf.logging.set_verbosity(tf.logging.INFO) input_x = tf.placeholder(dtype=tf.string) image_raw = tf.image.decode_jpeg(input_x, channels=3) processed_image = inception_preprocessing.preprocess_image( image_raw, 320, 320, is_training=False) processed_images = tf.expand_dims(processed_image, 0) with slim.arg_scope(inception.inception_v4_arg_scope()): logits, _ = inception.inception_v4(processed_images, num_classes=100, is_training=False) probabilities = tf.nn.softmax(logits) init_fn = slim.assign_from_checkpoint_fn( checkpoint_path, slim.get_model_variables('InceptionV4')) with tf.Session() as sess: init_fn(sess) with open(photo_filenames, 'rb') as g: content = g.read() probabilities_val = sess.run([probabilities], feed_dict={input_x: content}) p_val = np.squeeze(probabilities_val) #top_k = p_val.argsort()[-5:][::-1] #print(top_k) sorted_inds = [ i[0] for i in sorted( enumerate(p_val), key=lambda x: x[1], reverse=True) ][:5] label = '' for indx in sorted_inds: label += maps[str(indx)] labels.append(label) return labels, labels[0]
def use_inceptionv4(self): image_size = inception.inception_v4.default_image_size img_path = "../../data/misec_images/EnglishCockerSpaniel_simon.jpg" checkpoint_path = "../../data/trained_models/inception_v4/inception_v4.ckpt" with tf.Graph().as_default(): image_string = tf.read_file(img_path) image = tf.image.decode_jpeg(image_string, channels=3) processed_image = inception_preprocessing.preprocess_image( image, image_size, image_size, is_training=False) processed_images = tf.expand_dims(processed_image, 0) # Create the model, use the default arg scope to configure the batch norm parameters. with slim.arg_scope(inception.inception_v4_arg_scope()): logits, _ = inception.inception_v4(processed_images, num_classes=1001, is_training=False) probabilities = tf.nn.softmax(logits) init_fn = slim.assign_from_checkpoint_fn( checkpoint_path, slim.get_model_variables('InceptionV4')) with tf.Session() as sess: init_fn(sess) np_image, probabilities = sess.run([image, probabilities]) probabilities = probabilities[0, 0:] sorted_inds = [ i[0] for i in sorted(enumerate(-probabilities), key=lambda x: x[1]) ] self.disp_names(sorted_inds, probabilities) plt.figure() plt.imshow(np_image.astype(np.uint8)) plt.axis('off') plt.title(img_path) plt.show() return
def __call__(self, image_input, training=False, keep_prob=1.0, endpoint_name='Mixed_7d'): weight_decay = FLAGS.weight_decay activation_fn = tf.nn.relu end_points = {} with slim.arg_scope( inception.inception_v4_arg_scope( weight_decay=FLAGS.weight_decay)): with tf.variable_scope("", reuse=self.reuse): with tf.variable_scope(None, 'InceptionV4', [image_input]) as scope: with slim.arg_scope([slim.batch_norm, slim.dropout], is_training=training): net, end_points = inception.inception_v4_base( image_input, scope=scope) feature_map = end_points[endpoint_name] self.reuse = True return feature_map
def __init__(self, name): super(Classifier, self).__init__(name) maybe_download_and_extract() import logging from logging.handlers import RotatingFileHandler file_handler = RotatingFileHandler(FLAGS.log, maxBytes=1024 * 1024 * 100, backupCount=20) file_handler.setLevel(logging.INFO) formatter = logging.Formatter( "%(asctime)s - %(name)s - %(levelname)s - %(message)s") file_handler.setFormatter(formatter) self.logger.addHandler(file_handler) self.names = imagenet.create_readable_names_for_imagenet_labels() self.image_size = inception.inception_v4.default_image_size self.image_str_placeholder = tf.placeholder(tf.string) image = tf.image.decode_jpeg(self.image_str_placeholder, channels=3) processed_image = inception_preprocessing.preprocess_image( image, self.image_size, self.image_size, is_training=False) processed_images = tf.expand_dims(processed_image, 0) # Create the model, use the default arg scope to configure the # batch norm parameters. with slim.arg_scope(inception.inception_v4_arg_scope()): logits, _ = inception.inception_v4(processed_images, num_classes=1001, is_training=False) self.probabilities = tf.nn.softmax(logits) dest_directory = FLAGS.model_dir init_fn = slim.assign_from_checkpoint_fn( os.path.join(dest_directory, 'inception_v4.ckpt'), slim.get_model_variables('InceptionV4')) self.sess = tf.Session() init_fn(self.sess)
def export(): with tf.Graph().as_default(): # build inference model # imagenet labels names = imagenet.create_readable_names_for_imagenet_labels() names_tensor = tf.constant(names.values()) names_lookup_table = tf.contrib.lookup.index_to_string_table_from_tensor( names_tensor) # input transformation serialized_tf_example = tf.placeholder(tf.string, name='tf_example') feature_configs = { 'image/encoded': tf.FixedLenFeature(shape=[], dtype=tf.string), } tf_example = tf.parse_example(serialized_tf_example, feature_configs) jpegs = tf_example['image/encoded'] images = tf.map_fn(preprocess_image, jpegs, dtype=tf.float32) # run inference with slim.arg_scope(inception.inception_v4_arg_scope()): # inception v4 models logits, end_points = inception.inception_v4( images, num_classes=NUM_CLASSES + 1, is_training=False) # logits = tf.Print(logits, [logits]) probs = tf.nn.softmax(logits) # transform output to topk result topk_probs, topk_indices = tf.nn.top_k(probs, NUM_TOP_CLASSES) topk_names = names_lookup_table.lookup(tf.to_int64(topk_indices)) init_fn = slim.assign_from_checkpoint_fn( os.path.join(FLAGS.checkpoint_dir, 'inception_v4.ckpt'), slim.get_model_variables('InceptionV4')) # sess config config = tf.ConfigProto( # device_count = { # 'GPU': 0 # }, gpu_options={ 'allow_growth': 1, # 'per_process_gpu_memory_fraction': 0.01 }, allow_soft_placement=True, log_device_placement=False, ) with tf.Session(config=config) as sess: init_fn(sess) # init on 2017.08.05 # prelogits = sess.graph.get_tensor_by_name( # 'InceptionV4/Logits/PreLogitsFlatten/Reshape:0' # ) # update on 2017.10.22 # note: looks like the graphdef is updated for slim-inception-v4 # print('Graph Node Tensor Name:') # for node_tensor in tf.get_default_graph().as_graph_def().node: # if str(node_tensor.name).startswith('InceptionV4/Logits'): # print str(node_tensor.name) prelogits = sess.graph.get_tensor_by_name( 'InceptionV4/Logits/PreLogitsFlatten/flatten/Reshape:0') # an optional alternative # prelogits = end_points['PreLogitsFlatten'] # export inference model. output_path = os.path.join( tf.compat.as_bytes(FLAGS.output_dir), tf.compat.as_bytes(str(FLAGS.model_version))) print 'Exporting trained model to', output_path builder = tf.saved_model.builder.SavedModelBuilder(output_path) # build the signature_def_map. predict_inputs_tensor_info = tf.saved_model.utils.build_tensor_info( jpegs) classes_output_tensor_info = tf.saved_model.utils.build_tensor_info( topk_names) scores_output_tensor_info = tf.saved_model.utils.build_tensor_info( topk_probs) prelogits_output_tensor_info = tf.saved_model.utils.build_tensor_info( prelogits) prediction_signature = ( tf.saved_model.signature_def_utils.build_signature_def( inputs={'images': predict_inputs_tensor_info}, outputs={ 'classes': classes_output_tensor_info, 'scores': scores_output_tensor_info, 'prelogits': prelogits_output_tensor_info }, method_name=tf.saved_model.signature_constants. PREDICT_METHOD_NAME)) legacy_init_op = tf.group(tf.tables_initializer(), name='legacy_init_op') builder.add_meta_graph_and_variables( sess, [tf.saved_model.tag_constants.SERVING], signature_def_map={ 'predict_images': prediction_signature, }, legacy_init_op=legacy_init_op) builder.save() print 'Successfully exported model to %s' % FLAGS.output_dir
def train(): eps = 2.0 * float(FLAGS.max_epsilon) / 256.0 tf.logging.set_verbosity(tf.logging.INFO) with tf.Graph().as_default(): # Design architecture # input x_data = tf.placeholder(tf.float32, [None, FLAGS.img_height, FLAGS.img_width, 3], name="x_data") y_label = tf.placeholder(tf.float32, [None, FLAGS.num_classes], name="y_label") # generator x_generated, g_params = build_generator(x_data, FLAGS) x_generated = x_generated * eps x_generated = x_data + x_generated # discriminator(inception v3) with slim.arg_scope(inception.inception_v3_arg_scope()): _, end_points = inception.inception_v3( x_generated, num_classes=FLAGS.num_classes, is_training=False) predicted_labels = tf.argmax(end_points['Predictions'], 1) predicted_logits = end_points['Logits'] disc_var_list = slim.get_model_variables() # discriminator(resnet v2 50) x_generated2 = tf.image.resize_bilinear(x_generated, [224, 224], align_corners=False) with slim.arg_scope(resnet_utils.resnet_arg_scope()): _, end_points2 = resnet_v2.resnet_v2_50( x_generated2, num_classes=FLAGS.num_classes, is_training=False) predicted_labels2 = tf.argmax(end_points2['predictions'], 1) predicted_logits2 = end_points2['predictions'] disc_var_list2 = slim.get_model_variables()[len(disc_var_list):] # discriminator(resnet v2 152) with slim.arg_scope(resnet_utils.resnet_arg_scope()): _, end_points3 = resnet_v2.resnet_v2_152( x_generated2, num_classes=FLAGS.num_classes, is_training=False) predicted_labels3 = tf.argmax(end_points3['predictions'], 1) predicted_logits3 = end_points3['predictions'] disc_var_list3 = slim.get_model_variables()[(len(disc_var_list) + len(disc_var_list2)):] # discriminator(resnet v2 101) with slim.arg_scope(resnet_utils.resnet_arg_scope()): _, end_points4 = resnet_v2.resnet_v2_101( x_generated2, num_classes=FLAGS.num_classes, is_training=False) predicted_labels4 = tf.argmax(end_points4['predictions'], 1) predicted_logits4 = end_points4['predictions'] disc_var_list4 = slim.get_model_variables()[(len(disc_var_list) + len(disc_var_list2) + len(disc_var_list3)):] # discriminator(inception v4) with slim.arg_scope(inception.inception_v4_arg_scope()): _, end_points5 = inception.inception_v4( x_generated, num_classes=FLAGS.num_classes, is_training=False) predicted_labels5 = tf.argmax(end_points5['Predictions'], 1) predicted_logits5 = end_points['Logits'] disc_var_list5 = slim.get_model_variables()[(len(disc_var_list) + len(disc_var_list2) + len(disc_var_list3) + len(disc_var_list4)):] """ # discriminator(vgg 19) with slim.arg_scope(vgg.vgg_arg_scope()): _, end_points3 = vgg.vgg_19( x_generated2, num_classes=FLAGS.num_classes, is_training=False) predicted_labels3 = tf.argmax(end_points3['vgg_19/fc8'], 1); predicted_logits3 = end_points3['vgg_19/fc8']; disc_var_list3=slim.get_model_variables()[(len(disc_var_list)+len(disc_var_list2)):]; """ # loss and optimizer gen_acc = tf.reduce_mean( tf.cast(tf.equal(predicted_labels, tf.argmax(y_label, 1)), tf.float32)) cross_entropy = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=predicted_logits, labels=y_label)) gen_acc2 = tf.reduce_mean( tf.cast(tf.equal(predicted_labels2, tf.argmax(y_label, 1)), tf.float32)) cross_entropy2 = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=predicted_logits2, labels=y_label)) gen_acc3 = tf.reduce_mean( tf.cast(tf.equal(predicted_labels3, tf.argmax(y_label, 1)), tf.float32)) cross_entropy3 = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=predicted_logits3, labels=y_label)) gen_acc4 = tf.reduce_mean( tf.cast(tf.equal(predicted_labels4, tf.argmax(y_label, 1)), tf.float32)) cross_entropy4 = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=predicted_logits4, labels=y_label)) gen_acc5 = tf.reduce_mean( tf.cast(tf.equal(predicted_labels5, tf.argmax(y_label, 1)), tf.float32)) cross_entropy5 = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=predicted_logits5, labels=y_label)) infi_norm = tf.reduce_mean( tf.norm(tf.reshape(abs(x_data - x_generated), [-1, FLAGS.img_size]), ord=np.inf, axis=1)) g_loss = -1 * cross_entropy - 1 * cross_entropy2 - 1 * cross_entropy3 - 1 * cross_entropy4 - 1 * cross_entropy5 optimizer = tf.train.AdamOptimizer(0.0001) g_trainer = optimizer.minimize(g_loss, var_list=g_params) # get the data and label img_list = np.sort(glob.glob(FLAGS.input_folder + "*.png")) total_data = np.zeros( (len(img_list), FLAGS.img_height, FLAGS.img_width, 3), dtype=float) for i in range(len(img_list)): total_data[i] = imread(img_list[i], mode='RGB').astype( np.float) / 255.0 total_data[i] = total_data[i] * 2.0 - 1.0 # 0~1 -> -1~1 val_data = np.copy(total_data[0]) f = open(FLAGS.label_folder + "true_label", "r") total_label2 = np.array([i[:-1].split(",")[1] for i in f.readlines()], dtype=int) total_label = np.zeros((len(total_data), FLAGS.num_classes), dtype=int) for i in range(len(total_data)): total_label[i, total_label2[i]] = 1 val_label = np.copy(total_label[0]) # shuffle total_idx = range(len(total_data)) np.random.shuffle(total_idx) total_data = total_data[total_idx] total_label = total_label[total_idx] # Run computation saver = tf.train.Saver(disc_var_list) saver2 = tf.train.Saver(disc_var_list2) saver3 = tf.train.Saver(disc_var_list3) saver_gen = tf.train.Saver(g_params) # initialization init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) saver.restore(sess, FLAGS.checkpoint_path + FLAGS.checkpoint_file_name) saver2.restore(sess, FLAGS.checkpoint_path + FLAGS.checkpoint_file_name2) saver3.restore(sess, FLAGS.checkpoint_path + FLAGS.checkpoint_file_name3) # training for i in range(FLAGS.max_epoch): tr_infi = 0 tr_ce = 0 tr_gen_acc = 0 tr_ce2 = 0 tr_gen_acc2 = 0 tr_ce3 = 0 tr_gen_acc3 = 0 tr_ce4 = 0 tr_gen_acc4 = 0 tr_ce5 = 0 tr_gen_acc5 = 0 for j in range(len(total_data) / FLAGS.batch_size): batch_data = total_data[j * FLAGS.batch_size:(j + 1) * FLAGS.batch_size] batch_label = total_label[j * FLAGS.batch_size:(j + 1) * FLAGS.batch_size] acc_p3, ce_p3, acc_p2, ce_p2, acc_p, ce_p, infi_p, _ = sess.run( [ gen_acc3, cross_entropy3, gen_acc2, cross_entropy2, gen_acc, cross_entropy, infi_norm, g_trainer ], feed_dict={ x_data: batch_data, y_label: batch_label }) tr_infi += infi_p tr_ce += ce_p tr_gen_acc += acc_p tr_ce2 += ce_p2 tr_gen_acc2 += acc_p2 tr_ce3 += ce_p3 tr_gen_acc3 += acc_p3 print( str(i + 1) + " Epoch InfiNorm:" + str(tr_infi / (j + 1)) + ",CE: " + str(tr_ce / (j + 1)) + ",Acc: " + str(tr_gen_acc / (j + 1)) + ",CE2: " + str(tr_ce2 / (j + 1)) + ",Acc2: " + str(tr_gen_acc2 / (j + 1)) + ",CE3: " + str(tr_ce3 / (j + 1)) + ",Acc3: " + str(tr_gen_acc3 / (j + 1))) total_idx = range(len(total_data)) np.random.shuffle(total_idx) total_data = total_data[total_idx] total_label = total_label[total_idx] saver_gen.save( sess, "my-models_iv3_rv250_rv2152/my-model_" + str(FLAGS.max_epsilon) + ".ckpt")
def __init__(self, num_classes, train_layers=None, weights_path='DEFAULT'): """Create the graph of the inceptionv4 model. """ # Parse input arguments into class variables if weights_path == 'DEFAULT': self.WEIGHTS_PATH = "./pre_trained_models/inception_v4.ckpt" else: self.WEIGHTS_PATH = weights_path self.train_layers = train_layers with tf.variable_scope("input"): self.image_size = inception.inception_v4.default_image_size self.x_input = tf.placeholder(tf.float32, [None, self.image_size, self.image_size, 3], name="x_input") self.y_input = tf.placeholder(tf.float32, [None, num_classes], name="y_input") self.learning_rate = tf.placeholder(tf.float32, name="learning_rate") self.keep_prob = tf.placeholder(tf.float32, name="keep_prob") # train with arg_scope(inception.inception_v4_arg_scope()): self.logits, _ = inception.inception_v4(self.x_input, num_classes=num_classes, is_training=True, reuse=tf.AUTO_REUSE, dropout_keep_prob=self.keep_prob ) # validation with arg_scope(inception.inception_v4_arg_scope()): self.logits_val, _ = inception.inception_v4(self.x_input, num_classes=num_classes, is_training=False, reuse=tf.AUTO_REUSE, dropout_keep_prob=self.keep_prob ) with tf.name_scope("loss"): self.loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=self.logits, labels=self.y_input)) self.loss_val = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=self.logits_val, labels=self.y_input)) with tf.name_scope("train"): self.global_step = tf.Variable(0, name="global_step", trainable=False) update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) var_list = [v for v in tf.trainable_variables() if v.name.split('/')[-2] in train_layers or v.name.split('/')[-3] in train_layers ] gradients = tf.gradients(self.loss, var_list) self.grads_and_vars = list(zip(gradients, var_list)) optimizer = tf.train.GradientDescentOptimizer(self.learning_rate) with tf.control_dependencies(update_ops): self.train_op = optimizer.apply_gradients(grads_and_vars=self.grads_and_vars, global_step=self.global_step) with tf.name_scope("probability"): self.probability = tf.nn.softmax(self.logits_val, name="probability") with tf.name_scope("prediction"): self.prediction = tf.argmax(self.logits_val, 1, name="prediction") with tf.name_scope("accuracy"): correct_prediction = tf.equal(self.prediction, tf.argmax(self.y_input, 1)) self.accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"), name="accuracy")
def _build_graph(self, input_tensor): with tf.name_scope('inputs'): if input_tensor is None: input_tensor = tf.placeholder(tf.float32, shape=self.image_shape, name='input_img') else: assert self.image_shape == tuple(input_tensor.shape.as_list()) self.input_tensor = input_tensor if self.model_name == 'vgg_16': self.ckpt_path = slim_models_path + "vgg_16.ckpt" from nets.vgg import vgg_16, vgg_arg_scope with slim.arg_scope(vgg_arg_scope()): self.output, self.outputs = vgg_16( self.input_tensor, num_classes=self.num_classes, is_training=self.is_training, global_pool=self.global_pool) if self.model_name == 'resnet_v2_152': self.ckpt_path = slim_models_path + "resnet_v2_152.ckpt" from nets.resnet_v2 import resnet_v2_152, resnet_arg_scope with slim.arg_scope(resnet_arg_scope()): self.output, self.outputs = resnet_v2_152( self.input_tensor, num_classes=self.num_classes, is_training=self.is_training, global_pool=self.global_pool) elif self.model_name == 'resnet_v2_101': self.ckpt_path = slim_models_path + "resnet_v2_101.ckpt" from nets.resnet_v2 import resnet_v2_101, resnet_arg_scope with slim.arg_scope(resnet_arg_scope()): self.output, self.outputs = resnet_v2_101( self.input_tensor, num_classes=self.num_classes, is_training=self.is_training, global_pool=self.global_pool) elif self.model_name == 'resnet_v2_50': self.ckpt_path = slim_models_path + "resnet_v2_50.ckpt" from nets.resnet_v2 import resnet_v2_50, resnet_arg_scope with slim.arg_scope(resnet_arg_scope()): self.output, self.outputs = resnet_v2_50( self.input_tensor, num_classes=self.num_classes, is_training=self.is_training, global_pool=self.global_pool) elif self.model_name == 'InceptionV3': self.ckpt_path = slim_models_path + "inception_v3.ckpt" from nets.inception import inception_v3, inception_v3_arg_scope with slim.arg_scope(inception_v3_arg_scope()): self.output, self.outputs = inception_v3( self.input_tensor, num_classes=self.num_classes, is_training=self.is_training) elif self.model_name == 'InceptionV4': self.ckpt_path = slim_models_path + "inception_v4.ckpt" from nets.inception import inception_v4, inception_v4_arg_scope with slim.arg_scope(inception_v4_arg_scope()): self.output, self.outputs = inception_v4( self.input_tensor, num_classes=self.num_classes, is_training=self.is_training) elif self.model_name == 'pnasnet_large': self.ckpt_path = slim_models_path + "pnasnet_large_2.ckpt" from nets.nasnet.pnasnet import build_pnasnet_large, pnasnet_large_arg_scope with tf.variable_scope(self.model_name): with slim.arg_scope(pnasnet_large_arg_scope()): self.output, self.outputs = build_pnasnet_large( self.input_tensor, num_classes=self.num_classes, is_training=self.is_training) #collecting all variables related to this model #self.model_weights = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=self.model_name+'/model') self.model_weights = slim.get_model_variables(self.model_name)
def main(_): batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3] num_classes = 1001 # max_epsilon over checking # get original images origin_img_list=np.sort(glob.glob(FLAGS.origin_img_dir+"*.png")); origin_imgs=np.zeros((len(origin_img_list),FLAGS.image_height,FLAGS.image_width,3),dtype=float); for i in range(len(origin_img_list)): origin_imgs[i]=imread(origin_img_list[i],mode='RGB').astype(np.float); # get adv images adv_img_list=np.sort(glob.glob(FLAGS.input_dir+"*.png")); adv_imgs=np.zeros((len(adv_img_list),FLAGS.image_height,FLAGS.image_width,3),dtype=float); for i in range(len(adv_img_list)): adv_imgs[i]=imread(adv_img_list[i],mode='RGB').astype(np.float); epsilon_list=np.linalg.norm(np.reshape(abs(origin_imgs-adv_imgs),[-1,FLAGS.image_height*FLAGS.image_width*3]),ord=np.inf,axis=1); #print(epsilon_list);exit(1); over_epsilon_list=np.zeros((len(origin_img_list),2),dtype=object); cnt=0; for i in range(len(origin_img_list)): file_name=origin_img_list[i].split("/")[-1]; file_name=file_name.split(".")[0]; over_epsilon_list[i,0]=file_name; if(epsilon_list[i]>FLAGS.max_epsilon): over_epsilon_list[i,1]="1"; cnt+=1; tf.logging.set_verbosity(tf.logging.INFO) with tf.Graph().as_default(): # Prepare graph x_input = tf.placeholder(tf.float32, shape=batch_shape) if(FLAGS.checkpoint_file_name=="inception_v3.ckpt"): with slim.arg_scope(inception.inception_v3_arg_scope()): _, end_points = inception.inception_v3( x_input, num_classes=num_classes, is_training=False) predicted_labels = tf.argmax(end_points['Predictions'], 1) elif(FLAGS.checkpoint_file_name=="inception_v4.ckpt"): with slim.arg_scope(inception.inception_v4_arg_scope()): _, end_points = inception.inception_v4( x_input, num_classes=num_classes, is_training=False) predicted_labels = tf.argmax(end_points['Predictions'], 1) elif(FLAGS.checkpoint_file_name=="inception_resnet_v2_2016_08_30.ckpt"): with slim.arg_scope(inception.inception_resnet_v2_arg_scope()): _, end_points = inception.inception_resnet_v2( x_input, num_classes=num_classes, is_training=False) predicted_labels = tf.argmax(end_points['Predictions'], 1) elif(FLAGS.checkpoint_file_name=="resnet_v2_101.ckpt"): x_input2 = tf.image.resize_bilinear(x_input,[224,224],align_corners=False); with slim.arg_scope(resnet_utils.resnet_arg_scope()): _, end_points = resnet_v2.resnet_v2_101( x_input2, num_classes=num_classes, is_training=False) predicted_labels = tf.argmax(end_points['predictions'], 1) elif(FLAGS.checkpoint_file_name=="resnet_v2_50.ckpt"): x_input2 = tf.image.resize_bilinear(x_input,[224,224],align_corners=False); with slim.arg_scope(resnet_utils.resnet_arg_scope()): _, end_points = resnet_v2.resnet_v2_50( x_input2, num_classes=num_classes, is_training=False) predicted_labels = tf.argmax(end_points['predictions'], 1) elif(FLAGS.checkpoint_file_name=="resnet_v2_152.ckpt"): x_input2 = tf.image.resize_bilinear(x_input,[224,224],align_corners=False); with slim.arg_scope(resnet_utils.resnet_arg_scope()): _, end_points = resnet_v2.resnet_v2_152( x_input2, num_classes=num_classes, is_training=False) predicted_labels = tf.argmax(end_points['predictions'], 1) elif(FLAGS.checkpoint_file_name=="inception_v1.ckpt"): x_input2 = tf.image.resize_bilinear(x_input,[224,224],align_corners=False); with slim.arg_scope(inception.inception_v1_arg_scope()): _, end_points = inception.inception_v1( x_input2, num_classes=num_classes, is_training=False) predicted_labels = tf.argmax(end_points['Predictions'], 1) elif(FLAGS.checkpoint_file_name=="inception_v2.ckpt"): x_input2 = tf.image.resize_bilinear(x_input,[224,224],align_corners=False); with slim.arg_scope(inception.inception_v2_arg_scope()): _, end_points = inception.inception_v2( x_input2, num_classes=num_classes, is_training=False) predicted_labels = tf.argmax(end_points['Predictions'], 1) # Resnet v1 and vgg are not working now elif(FLAGS.checkpoint_file_name=="vgg_16.ckpt"): x_input_list=tf.unstack(x_input,FLAGS.batch_size,0); for i in range(FLAGS.batch_size): x_input_list[i]=vgg_preprocessing.preprocess_image(x_input_list[i],224,224); x_input2=tf.stack(x_input_list,0); with slim.arg_scope(vgg.vgg_arg_scope()): _, end_points = vgg.vgg_16( x_input2, num_classes=num_classes-1, is_training=False) predicted_labels = tf.argmax(end_points['vgg_16/fc8'], 1)+1 elif(FLAGS.checkpoint_file_name=="vgg_19.ckpt"): x_input_list=tf.unstack(x_input,FLAGS.batch_size,0); for i in range(FLAGS.batch_size): x_input_list[i]=vgg_preprocessing.preprocess_image(x_input_list[i],224,224); x_input2=tf.stack(x_input_list,0); with slim.arg_scope(vgg.vgg_arg_scope()): _, end_points = vgg.vgg_19( x_input2, num_classes=num_classes-1, is_training=False) predicted_labels = tf.argmax(end_points['vgg_19/fc8'], 1)+1 elif(FLAGS.checkpoint_file_name=="resnet_v1_50.ckpt"): x_input_list=tf.unstack(x_input,FLAGS.batch_size,0); for i in range(FLAGS.batch_size): x_input_list[i]=vgg_preprocessing.preprocess_image(x_input_list[i],224,224); x_input2=tf.stack(x_input_list,0); with slim.arg_scope(resnet_utils.resnet_arg_scope()): _, end_points = resnet_v1.resnet_v1_50( x_input, num_classes=num_classes-1, is_training=False) predicted_labels = tf.argmax(end_points['predictions'], 1)+1 elif(FLAGS.checkpoint_file_name=="resnet_v1_101.ckpt"): x_input_list=tf.unstack(x_input,FLAGS.batch_size,0); for i in range(FLAGS.batch_size): x_input_list[i]=vgg_preprocessing.preprocess_image(x_input_list[i],224,224); x_input2=tf.stack(x_input_list,0); with slim.arg_scope(resnet_utils.resnet_arg_scope()): _, end_points = resnet_v1.resnet_v1_101( x_input2, num_classes=num_classes-1, is_training=False) predicted_labels = tf.argmax(end_points['predictions'], 1)+1 elif(FLAGS.checkpoint_file_name=="resnet_v1_152.ckpt"): x_input_list=tf.unstack(x_input,FLAGS.batch_size,0); for i in range(FLAGS.batch_size): x_input_list[i]=vgg_preprocessing.preprocess_image(x_input_list[i],224,224); x_input2=tf.stack(x_input_list,0); with slim.arg_scope(resnet_utils.resnet_arg_scope()): _, end_points = resnet_v1.resnet_v1_152( x_input2, num_classes=num_classes-1, is_training=False) predicted_labels = tf.argmax(end_points['predictions'], 1)+1 # Run computation saver = tf.train.Saver(slim.get_model_variables()) session_creator = tf.train.ChiefSessionCreator( scaffold=tf.train.Scaffold(saver=saver), checkpoint_filename_with_path=FLAGS.checkpoint_path+FLAGS.checkpoint_file_name, master=FLAGS.master) f=open(FLAGS.true_label,"r"); t_label_list=np.array([i[:-1].split(",") for i in f.readlines()]); score=0; with tf.train.MonitoredSession(session_creator=session_creator) as sess: with tf.gfile.Open(FLAGS.output_file, 'w') as out_file: for filenames, images in load_images(FLAGS.input_dir, batch_shape): labels = sess.run(predicted_labels, feed_dict={x_input: images}) for filename, label in zip(filenames, labels): f_name=filename.split(".")[0]; t_label=int(t_label_list[t_label_list[:,0]==f_name,1][0]); if(t_label!=label): if(over_epsilon_list[over_epsilon_list[:,0]==f_name,1]!="1"): score+=1; #out_file.write('{0},{1}\n'.format(filename, label)) print("Over max epsilon#: "+str(cnt)); print(str(FLAGS.max_epsilon)+" max epsilon Score: "+str(score));
def GenerateTags(imagesFolder): images = glob.glob("%s/*.jpg"%(imagesFolder)) video_file_name = videoFile.rsplit("\\",1)[-1].rsplit(".",1)[-2] append_mode = False for image in images: url = '' tagx = '' probx = '' all_tags = [] file_cnt = 0 with tf.Graph().as_default(): print("Processing image :",image) image_file_name = image.rsplit('\\', 1)[-1] image_string = urllib.urlopen("file:///" + image).read() image = tf.image.decode_jpeg(image_string, channels=3) processed_image = inception_preprocessing.preprocess_image(image, image_size, image_size, is_training=False) processed_images = tf.expand_dims(processed_image, 0) # Create the model, use the default arg scope to configure the batch norm parameters. with slim.arg_scope(inception.inception_v4_arg_scope()): logits, _ = inception.inception_v4(processed_images, num_classes=1001, is_training=False,reuse=tf.AUTO_REUSE) probabilities = tf.nn.softmax(logits) init_fn = slim.assign_from_checkpoint_fn( os.path.join(checkpoints_dir, 'inception_v4.ckpt'), slim.get_model_variables('InceptionV4')) with tf.Session() as sess: init_fn(sess) np_image, probabilities = sess.run([image, probabilities]) probabilities = probabilities[0, 0:] sorted_inds = [i[0] for i in sorted(enumerate(-probabilities), key=lambda x:x[1])] # plt.figure() # plt.imshow(np_image.astype(np.uint8)) # plt.axis('off') # plt.show() names = imagenet.create_readable_names_for_imagenet_labels() for i in range(10): index = sorted_inds[i] tags = names[index].split(',') #print "debug", tags, type(tags) store_tags = [] for tagx in tags: tagx = tagx.strip() # remove white space probx = probabilities[index] if (tagx not in store_tags): # lowerbound 5% chance values = OrderedDict() values['tag_captured'] = tagx values['probability'] = probx values['image_file_name'] = image_file_name store_tags.append(tagx) all_tags.append(values) print("number of tags captured for " , image_file_name, len(all_tags)) if len(all_tags) > 0 : file_cnt = file_cnt + 1 for val in all_tags : if val['image_file_name']: write_to_csv(video_file_name, val, append_mode) append_mode = True r = glob.glob('*.csv') r = "".join(r) output_file = str(os.getcwd()) + "\\" + r df = pd.read_csv(output_file) df.sort_values(['image_file_name','probability'],ascending= [True , False],inplace=True) df.to_csv(os.path.join(imagesFolder,r), index=False) print(df.shape)
def export(): with tf.Graph().as_default(): # build inference model # imagenet labels names = imagenet.create_readable_names_for_imagenet_labels() names_tensor = tf.constant(list(names.values())) names_lookup_table = tf.contrib.lookup.index_to_string_table_from_tensor(names_tensor) # input transformation serialized_tf_example = tf.placeholder(tf.string, name="tf_example") feature_configs = { "image/encoded": tf.FixedLenFeature(shape=[], dtype=tf.string), } tf_example = tf.parse_example(serialized_tf_example, feature_configs) jpegs = tf_example["image/encoded"] images = tf.map_fn(preprocess_image, jpegs, dtype=tf.float32) # run inference with slim.arg_scope(inception.inception_v4_arg_scope()): # inception v4 models logits, end_points = inception.inception_v4(images, num_classes=NUM_CLASSES, is_training=False) # logits = tf.Print(logits, [logits]) probs = tf.nn.softmax(logits) # transform output to topk result topk_probs, topk_indices = tf.nn.top_k(probs, NUM_TOP_CLASSES) topk_names = names_lookup_table.lookup(tf.to_int64(topk_indices)) init_fn = slim.assign_from_checkpoint_fn( tf.train.latest_checkpoint(FLAGS.checkpoint_dir), slim.get_model_variables(), ) # sess config config = tf.ConfigProto( gpu_options={ "allow_growth": 1, }, allow_soft_placement=True, log_device_placement=False, ) with tf.Session(config=config) as sess: init_fn(sess) # # to print out all the tensornames in the attached layers to inception V3 # for node_tensor in tf.get_default_graph().as_graph_def().node: # if str(node_tensor.name).startswith('InceptionV4/Logits'): # print str(node_tensor.name) prelogits = sess.graph.get_tensor_by_name("InceptionV4/Logits/Predictions:0") # an optional alternative # prelogits = end_points['PreLogitsFlatten'] # export inference model. output_path = os.path.join( tf.compat.as_bytes(FLAGS.output_dir), tf.compat.as_bytes(str(FLAGS.model_version)) ) print("Exporting trained model to", output_path) builder = tf.saved_model.builder.SavedModelBuilder(output_path) # build the signature_def_map. predict_inputs_tensor_info = tf.saved_model.utils.build_tensor_info(jpegs) classes_output_tensor_info = tf.saved_model.utils.build_tensor_info(topk_names) scores_output_tensor_info = tf.saved_model.utils.build_tensor_info(topk_probs) prelogits_output_tensor_info = tf.saved_model.utils.build_tensor_info(prelogits) prediction_signature = ( tf.saved_model.signature_def_utils.build_signature_def( inputs={ "images": predict_inputs_tensor_info }, outputs={ "classes": classes_output_tensor_info, "scores": scores_output_tensor_info, "prelogits": prelogits_output_tensor_info, }, method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME ) ) legacy_init_op = tf.group(tf.tables_initializer(), name="legacy_init_op") builder.add_meta_graph_and_variables( sess, [tf.saved_model.tag_constants.SERVING], signature_def_map={ "predict_images": prediction_signature, }, legacy_init_op=legacy_init_op ) builder.save() print("Successfully exported model to %s" % FLAGS.output_dir)
image_size = inception.inception_v4.default_image_size # checkpoints_dir = '/Users/zhangxin/data_public/goolenet/v4' # inception_v4.ckpt checkpoints_dir = '/Users/zhangxin/gitlab/CarRecognition/_tmp/Train/zhangkan/inception_v4_recognition/' img_file = '/Users/zhangxin/pic/car.jpg' image_string = open(img_file, 'r').read() with tf.Graph().as_default(): # url = 'https://upload.wikimedia.org/wikipedia/commons/7/70/EnglishCockerSpaniel_simon.jpg' # image_string = urllib.urlopen(url).read() image = tf.image.decode_jpeg(image_string, channels=3) processed_image = inception_preprocessing.preprocess_image(image, image_size, image_size, is_training=False) processed_images = tf.expand_dims(processed_image, 0) # Create the model, use the default arg scope to configure the batch norm parameters. with slim.arg_scope(inception.inception_v4_arg_scope()): logits, _ = inception.inception_v4(processed_images, num_classes=1001, is_training=False) probabilities = tf.nn.softmax(logits) # init_fn = slim.assign_from_checkpoint_fn( # os.path.join(checkpoints_dir, 'inception_v4.ckpt'), # slim.get_model_variables('InceptionV4')) with tf.Session() as sess: init_fn(sess) np_image, probabilities = sess.run([image, probabilities]) probabilities = probabilities[0, 0:] sorted_inds = [i[0] for i in sorted(enumerate(-probabilities), key=lambda x:x[1])] # plt.figure()
def train(): eps=2.0*float(FLAGS.max_epsilon)/256.0; tf.logging.set_verbosity(tf.logging.INFO); with tf.Graph().as_default(): # Design architecture # input is_training = tf.placeholder(tf.bool); x_data = tf.placeholder(tf.float32, [None, FLAGS.img_height, FLAGS.img_width,3], name="x_data") y_label = tf.placeholder(tf.float32, [None, FLAGS.num_classes], name="y_label") y_label_ll = tf.placeholder(tf.float32, [None, FLAGS.num_classes], name="y_label_ll") lam = tf.placeholder(tf.float32, [], name="lambda"); # generator x_generated, g_params, bn_var_num = build_generator(x_data,is_training,FLAGS); x_generated = x_generated * eps; x_generated = x_data + x_generated; # discriminator(inception v3) with slim.arg_scope(inception.inception_v3_arg_scope()): _, end_points = inception.inception_v3( x_generated, num_classes=FLAGS.num_classes, is_training=False) predicted_labels = tf.argmax(end_points['Predictions'], 1); predicted_logits = end_points['Logits']; disc_var_list=slim.get_model_variables()[bn_var_num:]; # discriminator(resnet v2 50) x_generated2=tf.image.resize_bilinear(x_generated,[224,224],align_corners=False); with slim.arg_scope(resnet_utils.resnet_arg_scope()): _, end_points2 = resnet_v2.resnet_v2_50( x_generated2, num_classes=FLAGS.num_classes, is_training=False) predicted_labels2 = tf.argmax(end_points2['predictions'], 1); predicted_logits2 = end_points2['predictions']; disc_var_list2=slim.get_model_variables()[(bn_var_num+len(disc_var_list)):]; # discriminator(resnet v2 152) with slim.arg_scope(resnet_utils.resnet_arg_scope()): _, end_points3 = resnet_v2.resnet_v2_152( x_generated2, num_classes=FLAGS.num_classes, is_training=False) predicted_labels3 = tf.argmax(end_points3['predictions'], 1); predicted_logits3 = end_points3['predictions']; disc_var_list3=slim.get_model_variables()[(bn_var_num+len(disc_var_list)+len(disc_var_list2)):]; # discriminator(resnet v2 101) with slim.arg_scope(resnet_utils.resnet_arg_scope()): _, end_points4 = resnet_v2.resnet_v2_101( x_generated2, num_classes=FLAGS.num_classes, is_training=False) predicted_labels4 = tf.argmax(end_points4['predictions'], 1); predicted_logits4 = end_points4['predictions']; disc_var_list4=slim.get_model_variables()[(bn_var_num+len(disc_var_list)+len(disc_var_list2)+len(disc_var_list3)):]; # discriminator(inception v4) with slim.arg_scope(inception.inception_v4_arg_scope()): _, end_points5 = inception.inception_v4( x_generated, num_classes=FLAGS.num_classes, is_training=False) predicted_labels5 = tf.argmax(end_points5['Predictions'], 1); predicted_logits5 = end_points['Logits']; disc_var_list5=slim.get_model_variables()[(bn_var_num+len(disc_var_list)+len(disc_var_list2)+len(disc_var_list3)+len(disc_var_list4)):]; # average predicted_prob_avg = (end_points['Predictions']+end_points2['predictions']+end_points3['predictions']+end_points4['predictions']+end_points5['Predictions'])/5.0; predicted_labels_avg = tf.argmax((end_points['Predictions']+end_points2['predictions']+end_points3['predictions']+end_points4['predictions']+end_points5['Predictions'])/5.0, 1); # loss and optimizer gen_acc=tf.reduce_mean(tf.cast(tf.equal(predicted_labels_avg,tf.argmax(y_label,1)),tf.float32)); cross_entropy=tf.reduce_mean(-tf.reduce_sum(y_label*tf.log(predicted_prob_avg),1)); cross_entropy_ll=tf.reduce_mean(-tf.reduce_sum(y_label_ll*tf.log(predicted_prob_avg),1)); infi_norm=tf.reduce_mean(tf.norm(tf.reshape(abs(x_data-x_generated),[-1,FLAGS.img_size]),ord=np.inf,axis=1)); g_loss=-1*cross_entropy+cross_entropy_ll; optimizer = tf.train.AdamOptimizer(0.0001) g_trainer = optimizer.minimize(g_loss, var_list=g_params) # get the data and label img_list=np.sort(glob.glob(FLAGS.input_folder+"*.png")); total_data=np.zeros((len(img_list),FLAGS.img_height,FLAGS.img_width,3),dtype=float); for i in range(len(img_list)): total_data[i]=imread(img_list[i],mode='RGB').astype(np.float) / 255.0; total_data[i]=total_data[i]*2.0-1.0; # 0~1 -> -1~1 val_data=np.copy(total_data[0]); f=open(FLAGS.label_folder+"true_label","r"); total_label2=np.array([i[:-1].split(",")[1] for i in f.readlines()],dtype=int); total_label=np.zeros((len(total_data),FLAGS.num_classes),dtype=int); for i in range(len(total_data)): total_label[i,total_label2[i]]=1; f=open("logits","r"); total_logits=np.array([i[:-1].split(",") for i in f.readlines()],dtype=float); total_label_ll=np.zeros((len(total_data),FLAGS.num_classes),dtype=int); for i in range(len(total_data)): #total_logits[i,total_label2[i]]=0.0; target_idx=np.argmin(total_logits[i]); total_label_ll[i,target_idx]=1; val_label=np.copy(total_label[0]); # shuffle total_idx=range(len(total_data)); np.random.shuffle(total_idx); total_data=total_data[total_idx];total_label=total_label[total_idx]; # Run computation saver = tf.train.Saver(disc_var_list); saver2 = tf.train.Saver(disc_var_list2); saver3 = tf.train.Saver(disc_var_list3); saver4 = tf.train.Saver(disc_var_list4); saver5 = tf.train.Saver(disc_var_list5); saver_gen = tf.train.Saver(g_params); """ session_creator = tf.train.ChiefSessionCreator( scaffold=tf.train.Scaffold(saver=saver), checkpoint_filename_with_path=FLAGS.checkpoint_path, master=FLAGS.master) """ # initialization init = tf.global_variables_initializer(); with tf.Session() as sess: #with tf.train.MonitoredSession() as sess: sess.run(init) saver.restore(sess,FLAGS.checkpoint_path+"inception_v3.ckpt"); saver2.restore(sess,FLAGS.checkpoint_path+"resnet_v2_50.ckpt"); saver3.restore(sess,FLAGS.checkpoint_path+"resnet_v2_152.ckpt"); saver4.restore(sess,FLAGS.checkpoint_path+"resnet_v2_101.ckpt"); saver5.restore(sess,FLAGS.checkpoint_path+"inception_v4.ckpt"); # tf board tf.summary.scalar('reverse_cross_entropy',cross_entropy); tf.summary.scalar('training_accuracy',gen_acc); merged = tf.summary.merge_all() train_writer = tf.summary.FileWriter('/tmp/nips17/attack_gan/learn_gan5_avg2_ll_'+str(FLAGS.max_epsilon), sess.graph) # training for i in range(FLAGS.max_epoch): if(i>100): lam_value=1.0; else: lam_value=1.0; tr_ce=0; tr_infi=0; tr_gen_acc=0; for j in range(len(total_data) / FLAGS.batch_size): batch_data=total_data[j*FLAGS.batch_size:(j+1)*FLAGS.batch_size]; batch_label=total_label[j*FLAGS.batch_size:(j+1)*FLAGS.batch_size]; batch_label_ll=total_label_ll[j*FLAGS.batch_size:(j+1)*FLAGS.batch_size]; summary,tr_gen_acc_part,tr_ce_part,tr_infi_part,_=sess.run([merged,gen_acc,cross_entropy,infi_norm,g_trainer],feed_dict={is_training:True,x_data: batch_data, y_label: batch_label,y_label_ll:batch_label_ll,lam:lam_value}); tr_ce+=tr_ce_part; tr_infi+=tr_infi_part; tr_gen_acc+=tr_gen_acc_part; train_writer.add_summary(summary,i*len(total_data)+j*FLAGS.batch_size); print(str(i+1)+" Epoch Training Cross Entropy: "+str(tr_ce/(j+1))+", Infinity Norm: "+str(tr_infi/(j+1))+",Gen Acc: "+str(tr_gen_acc/(j+1))); total_idx=range(len(total_data)); np.random.shuffle(total_idx); total_data=total_data[total_idx];total_label=total_label[total_idx]; saver_gen.save(sess,"mark3_iv3_rv250_rv2152_rv2101_iv4_avg2/my-model_"+str(FLAGS.max_epsilon)+".ckpt");
val_list.append( (os.path.join(data_dir, dataset, line.strip().split(': ')[0]), int(line.strip().split(': ')[1]))) print('Length of train: %d' %len(train_list)) print('Length of val: %d' %len(val_list)) # Base network architecture if base_network == 'InceptionV3': endpoint = 'Mixed_7c' arg_scope = inception.inception_v3_arg_scope() elif base_network == 'InceptionV3SE': endpoint = 'Mixed_7c' arg_scope = inception.inception_v3_se_arg_scope() elif base_network == 'InceptionV4': endpoint = 'Mixed_7d' arg_scope = inception.inception_v4_arg_scope() elif base_network == 'InceptionResnetV2': endpoint = 'Conv2d_7b_1x1' arg_scope = inception.inception_resnet_v2_arg_scope() elif base_network == 'InceptionResnetV2SE': endpoint = 'Conv2d_7b_1x1' arg_scope = inception.inception_resnet_v2_se_arg_scope() elif base_network[:6] == 'ResNet': layers = base_network.split('ResNet')[1] base_network = 'ResNet' # Feature extraction. fea_train = np.zeros((len(train_list), fea_dim), dtype=np.float32) label_train = np.zeros((len(train_list), ), dtype=np.int32) fea_val = np.zeros((len(val_list), fea_dim), dtype=np.float32) label_val = np.zeros((len(val_list), ), dtype=np.int32)
def main(opt): config = tf.ConfigProto(allow_soft_placement=True) gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.1) config.gpu_options.allow_growth = True jpg_path = opt.image_path images_lists = [] for subdir, dirs, files in os.walk(jpg_path): for f in files: f = f.strip() images_lists.append(os.path.join(jpg_path, f)) att_dir = os.path.join(opt.out_dir, opt.att_dir) fc_dir = os.path.join(opt.out_dir, opt.fc_dir) if not tf.gfile.Exists(fc_dir): tf.gfile.MakeDirs(fc_dir) if not tf.gfile.Exists(att_dir): tf.gfile.MakeDirs(att_dir) checkpoints_dir = opt.model_path slim = tf.contrib.slim image_size = inception.inception_v4.default_image_size tf_image = tf.placeholder(tf.string, None) image = tf.image.decode_jpeg(tf_image, channels=3) processed_image = inception_preprocessing.preprocess_image( image, image_size, image_size, is_training=False) processed_images = tf.expand_dims(processed_image, 0) with slim.arg_scope(inception.inception_v4_arg_scope()): tf_feats_att, tf_feats_fc = inception.inception_v4( processed_images, num_classes=1001, is_training=False, create_aux_logits=False) init_fn = slim.assign_from_checkpoint_fn( os.path.join(checkpoints_dir, 'inception_v4.ckpt'), slim.get_model_variables('InceptionV4')) with tf.Session(config=config) as sess: init_fn(sess) for idx, image_path in enumerate(images_lists): print('{} {}'.format(idx, image_path)) image_name = os.path.basename(image_path) image_id = get_image_id(image_name) url = 'file://' + image_path image_string = urllib.request.urlopen(url).read() conv_feats, fc_feats = sess.run([tf_feats_att, tf_feats_fc], feed_dict={tf_image: image_string}) conv_feats = np.squeeze(conv_feats) fc_feats = np.squeeze(fc_feats) np.save(os.path.join(fc_dir, str(image_id)), fc_feats) np.savez_compressed(os.path.join(att_dir, str(image_id)), feat=conv_feats)
#url = ("http://img04.tooopen.com/images/20130123/tooopen_09221274.jpg") # Open specified url and load image as a string #image_string = urllib2.urlopen(url).read() # Decode string into matrix with intensity values #image = tf.image.decode_jpeg(image_string, channels=3) X = tf.placeholder(tf.float32, shape=[None, image_size, image_size, image_channel]) images, processed_images = get_data() # Create the model, use the default arg scope to configure # the batch norm parameters. arg_scope is a very conveniet # feature of slim library -- you can define default # parameters for layers -- like stride, padding etc. with slim.arg_scope(inception.inception_v4_arg_scope()): logits, _ = inception.inception_v4(X, num_classes=1001, is_training=False) # In order to get probabilities we apply softmax on the output. probabilities = tf.nn.softmax(logits) # Create a function that reads the network weights # from the checkpoint file that you downloaded. # We will run it in session later. #print slim.get_model_variables() init_fn = slim.assign_from_checkpoint_fn( os.path.join(checkpoints_dir, 'inception_v4.ckpt'), slim.get_model_variables('InceptionV4')) names = imagenet.create_readable_names_for_imagenet_labels() with tf.Session() as sess:
def process(lines, split_index_list, output_file, model, batch_size): frame_ids = [] frame_id_to_path = {} frame_id_to_image_ids = {} image_id_to_path = {} image_id_to_coordinates = {} for i in range(len(split_index_list) - 1): frame_path = lines[split_index_list[i]].rstrip() frame_id = ntpath.basename(frame_path) frame_ids.append(frame_id) frame_id_to_path[frame_id] = frame_path num_images = split_index_list[i + 1] - split_index_list[i] - 1 image_ids = [] for j in range(num_images): line = lines[split_index_list[i] + j + 1] fields = line.rstrip().split("\t") image_path = fields[0] image_id = ntpath.basename(image_path) coordinates = fields[1] + "\t" + fields[2] + "\t" + fields[ 3] + "\t" + fields[4] image_path = os.path.join(IMAGES_FOLDER, image_id) image_id_to_path[image_id] = image_path image_id_to_coordinates[image_id] = coordinates image_ids.append(image_id) frame_id_to_image_ids[frame_id] = image_ids if (len(image_id_to_path) < batch_size and i + 1 < len(split_index_list) - 1) or len(frame_ids) == 0: continue print frame_id output = open(output_file, "a") if model == 'inception_v1': tf.Graph().as_default() with tf.Session(graph=tf.Graph()) as sess: with slim.arg_scope(inception.inception_v1_arg_scope()): process_super_batch(frame_ids, frame_id_to_path, frame_id_to_image_ids, \ image_id_to_path, image_id_to_coordinates, output, model, sess) if model == 'inception_v2': tf.Graph().as_default() with tf.Session(graph=tf.Graph()) as sess: with slim.arg_scope(inception.inception_v2_arg_scope()): process_super_batch(frame_ids, frame_id_to_path, frame_id_to_image_ids, \ image_id_to_path, image_id_to_coordinates, output, model, sess) if model == 'inception_v3': tf.Graph().as_default() with tf.Session(graph=tf.Graph()) as sess: with slim.arg_scope(inception.inception_v3_arg_scope()): process_super_batch(frame_ids, frame_id_to_path, frame_id_to_image_ids, \ image_id_to_path, image_id_to_coordinates, output, model, sess) if model == 'inception_v4': tf.Graph().as_default() with tf.Session(graph=tf.Graph()) as sess: with slim.arg_scope(inception.inception_v4_arg_scope()): process_super_batch(frame_ids, frame_id_to_path, frame_id_to_image_ids, \ image_id_to_path, image_id_to_coordinates, output, model, sess) if model == 'resnet_v1_50': tf.Graph().as_default() with tf.Session(graph=tf.Graph()) as sess: with slim.arg_scope(resnet_v1.resnet_arg_scope()): process_super_batch(frame_ids, frame_id_to_path, frame_id_to_image_ids, \ image_id_to_path, image_id_to_coordinates, output, model, sess) if model == 'resnet_v1_101': tf.Graph().as_default() with tf.Session(graph=tf.Graph()) as sess: with slim.arg_scope(resnet_v1.resnet_arg_scope()): process_super_batch(frame_ids, frame_id_to_path, frame_id_to_image_ids, \ image_id_to_path, image_id_to_coordinates, output, model, sess) if model == 'resnet_v1_152': tf.Graph().as_default() with tf.Session(graph=tf.Graph()) as sess: with slim.arg_scope(resnet_v1.resnet_arg_scope()): process_super_batch(frame_ids, frame_id_to_path, frame_id_to_image_ids, \ image_id_to_path, image_id_to_coordinates, output, model, sess) elif model == 'vgg_16': tf.Graph().as_default() with tf.Session(graph=tf.Graph()) as sess: with slim.arg_scope(vgg.vgg_arg_scope()): process_super_batch(frame_ids, frame_id_to_path, frame_id_to_image_ids, \ image_id_to_path, image_id_to_coordinates, output, model, sess) output.close() frame_ids = [] frame_id_to_path = {} frame_id_to_image_ids = {} image_id_to_path = {} image_id_to_coordinates = {}
def main(_): batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3] num_classes = 1001 ensemble_type = FLAGS.ensemble_type tf.logging.set_verbosity(tf.logging.INFO) checkpoint_path_list = [ FLAGS.checkpoint_path_inception_v1, FLAGS.checkpoint_path_inception_v2, FLAGS.checkpoint_path_inception_v3, FLAGS.checkpoint_path_inception_v4, FLAGS.checkpoint_path_inception_resnet_v2, FLAGS.checkpoint_path_resnet_v1_101, FLAGS.checkpoint_path_resnet_v1_152, FLAGS.checkpoint_path_resnet_v2_101, FLAGS.checkpoint_path_resnet_v2_152, FLAGS.checkpoint_path_vgg_16, FLAGS.checkpoint_path_vgg_19 ] normalization_method = [ 'default', 'default', 'default', 'default', 'global', 'caffe_rgb', 'caffe_rgb', 'default', 'default', 'caffe_rgb', 'caffe_rgb' ] pred_list = [] for idx, checkpoint_path in enumerate(checkpoint_path_list, 1): with tf.Graph().as_default(): if int(FLAGS.test_idx) == 20 and idx in [3]: continue if int(FLAGS.test_idx) in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ] and int(FLAGS.test_idx) != idx: continue # Prepare graph if idx in [1, 2, 6, 7, 10, 11]: _x_input = tf.placeholder(tf.float32, shape=batch_shape) x_input = tf.image.resize_images(_x_input, [224, 224]) else: _x_input = tf.placeholder(tf.float32, shape=batch_shape) x_input = _x_input x_input = image_normalize(x_input, normalization_method[idx - 1]) if idx == 1: with slim.arg_scope(inception.inception_v1_arg_scope()): _, end_points = inception.inception_v1( x_input, num_classes=num_classes, is_training=False) elif idx == 2: with slim.arg_scope(inception.inception_v2_arg_scope()): _, end_points = inception.inception_v2( x_input, num_classes=num_classes, is_training=False) elif idx == 3: with slim.arg_scope(inception.inception_v3_arg_scope()): _, end_points = inception.inception_v3( x_input, num_classes=num_classes, is_training=False) elif idx == 4: with slim.arg_scope(inception.inception_v4_arg_scope()): _, end_points = inception.inception_v4( x_input, num_classes=num_classes, is_training=False) elif idx == 5: with slim.arg_scope(inception.inception_resnet_v2_arg_scope()): _, end_points = inception.inception_resnet_v2( x_input, num_classes=num_classes, is_training=False) elif idx == 6: with slim.arg_scope(resnet_v1.resnet_arg_scope()): _, end_points = resnet_v1.resnet_v1_101(x_input, num_classes=1000, is_training=False) elif idx == 7: with slim.arg_scope(resnet_v1.resnet_arg_scope()): _, end_points = resnet_v1.resnet_v1_152(x_input, num_classes=1000, is_training=False) elif idx == 8: with slim.arg_scope(resnet_v2.resnet_arg_scope()): _, end_points = resnet_v2.resnet_v2_101( x_input, num_classes=num_classes, is_training=False) elif idx == 9: with slim.arg_scope(resnet_v2.resnet_arg_scope()): _, end_points = resnet_v2.resnet_v2_152( x_input, num_classes=num_classes, is_training=False) elif idx == 10: with slim.arg_scope(vgg.vgg_arg_scope()): _, end_points = vgg.vgg_16(x_input, num_classes=1000, is_training=False) end_points['predictions'] = tf.nn.softmax( end_points['vgg_16/fc8']) elif idx == 11: with slim.arg_scope(vgg.vgg_arg_scope()): _, end_points = vgg.vgg_19(x_input, num_classes=1000, is_training=False) end_points['predictions'] = tf.nn.softmax( end_points['vgg_19/fc8']) #end_points = tf.reduce_mean([end_points1['Predictions'], end_points2['Predictions'], end_points3['Predictions'], end_points4['Predictions']], axis=0) #predicted_labels = tf.argmax(end_points, 1) # Run computation saver = tf.train.Saver(slim.get_model_variables()) session_creator = tf.train.ChiefSessionCreator( scaffold=tf.train.Scaffold(saver=saver), checkpoint_filename_with_path=checkpoint_path, master=FLAGS.master) pred_in = [] filenames_list = [] with tf.train.MonitoredSession( session_creator=session_creator) as sess: for filenames, images in load_images(FLAGS.input_dir, batch_shape): #if idx in [1,2,6,7,10,11]: # # 16x299x299x3 # images = zoom(images, (1, 0.7491638795986622, 0.7491638795986622, 1), order=2) filenames_list.extend(filenames) end_points_dict = sess.run(end_points, feed_dict={_x_input: images}) if idx in [6, 7, 10, 11]: end_points_dict['predictions'] = \ np.concatenate([np.zeros([FLAGS.batch_size, 1]), np.array(end_points_dict['predictions'].reshape(-1, 1000))], axis=1) try: pred_in.extend(end_points_dict['Predictions'].reshape( -1, num_classes)) except KeyError: pred_in.extend(end_points_dict['predictions'].reshape( -1, num_classes)) pred_list.append(pred_in) if ensemble_type == 'mean': pred = np.mean(pred_list, axis=0) labels = np.argmax( pred, axis=1 ) # model_num X batch X class_num ==(np.mean)==> batch X class_num ==(np.argmax)==> batch elif ensemble_type == 'vote': pred = np.argmax( pred_list, axis=2 ) # model_num X batch X class_num ==(np.mean)==> batch X class_num ==(np.argmax)==> batch labels = np.median(pred, axis=0) with tf.gfile.Open(FLAGS.output_file, 'w') as out_file: for filename, label in zip(filenames_list, labels): out_file.write('{0},{1}\n'.format(filename, label))
def __init__(self, options): num_classes = options.NUM_CLASSES with tf.variable_scope('input'): self.image_size = options.IMAGE_SIZE self.x_input = tf.placeholder(tf.float32, [None, self.image_size, self.image_size, 3], name='x_input') self.y_input = tf.placeholder(tf.float32, [None, num_classes], name='y_input') self.learning_rate = tf.placeholder(tf.float32, name='learning_rate') self.keep_prob = tf.placeholder(tf.float32, name='keep_prob') if options.PHASE == 'train': if train_layers == 'default': self.train_layers = self.DEFAULT_TRAIN_LAYERS else: self.train_layers = train_layers # training with arg_scope(inception.inception_v4_arg_scope(weight_decay=options.WEIGHT_DECAY, activation_fn=tf.nn.relu)): self.logits, _ = inception.inception_v4(self.x_input, num_classes=num_classes, is_training=True, reuse=tf.AUTO_REUSE, dropout_keep_prob=self.keep_prob) # validation with arg_scope(inception.inception_v4_arg_scope(weight_decay=0.0, activation_fn=tf.nn.relu)): self.logits_val, _ = inception.inception_v4(self.x_input, num_classes=num_classes, is_training=False, reuse=tf.AUTO_REUSE, dropout_keep_prob=self.keep_prob) with tf.name_scope('loss'): self.loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2( logits=self.logits, labels=self.y_input)) self.loss_val = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2( logits=self.logits_val, labels=self.y_input)) with tf.name_scope('train'): self.global_step = tf.Variable(0, name='global_step', trainable=False) update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) var_list = [v for v in tf.trainable_variables() if v.name.split('/')[-2] in self.train_layers or v.name.split('/')[-3] in self.train_layers or v.name.split('/')[1] in self.train_layers] gradients = tf.gradients(self.loss, var_list) self.grads_and_vars = list(zip(gradients, var_list)) optimizer = get_optimizer(options.OPTIMIZER, self.learning_rate) with tf.control_dependencies(update_ops): self.train_op = optimizer.apply_gradients(grads_and_vars=self.grads_and_vars, global_step=self.global_step) else: # validation with arg_scope(inception.inception_v4_arg_scope(weight_decay=0.0, activation_fn=tf.nn.relu)): self.logits_val, _ = inception.inception_v4(self.x_input, num_classes=num_classes, is_training=False, reuse=tf.AUTO_REUSE, dropout_keep_prob=self.keep_prob) with tf.name_scope('probability'): self.probability = tf.nn.softmax(self.logits_val, name='probability') with tf.name_scope('prediction'): self.prediction = tf.argmax(self.logits_val, 1, name='prediction') with tf.name_scope('accuracy'): correct_prediction = tf.equal(self.prediction, tf.argmax(self.y_input, 1)) self.accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float'), name='accuracy')