def create(self, images, num_classes, is_training): """See baseclass.""" with slim.arg_scope(inception.inception_v2_arg_scope()): _, endpoints = inception.inception_v2(inputs=images, num_classes=num_classes, is_training=is_training) return endpoints
def Inception_V2(input_batch): """ Reference: 1. Vasili's codes 2. https://github.com/tensorflow/models/issues/429#issuecomment-277885861 """ slim_dir = "/home/xyang/workspace/models/research/slim" checkpoints_dir = slim_dir + "/pretrain" checkpoints_file = checkpoints_dir + '/inception_v2.ckpt' import sys sys.path.append(slim_dir) from nets import inception slim = tf.contrib.slim image_size = inception.inception_v2.default_image_size cropped_images = tf.random_crop( tf.image.convert_image_dtype(input_batch, dtype=tf.float32), [tf.shape(input_batch)[0], image_size, image_size, 3]) preprocessed_images = tf.multiply(tf.subtract(cropped_images, 0.5), 2.0) # Create the model, use the default arg scope to configure # the batch norm parameters. with slim.arg_scope(inception.inception_v2_arg_scope()): logits, endpoints = inception.inception_v2(preprocessed_images, num_classes=1001, is_training=True) pool5 = endpoints['AvgPool_1a'] return tf.reshape(pool5, (-1, 1024))
def testNoBatchNormScaleByDefault(self): height, width = 224, 224 num_classes = 1000 inputs = tf.placeholder(tf.float32, (1, height, width, 3)) with slim.arg_scope(inception.inception_v2_arg_scope()): inception.inception_v2(inputs, num_classes, is_training=False) self.assertEqual(tf.global_variables('.*/BatchNorm/gamma:0$'), [])
def testModelHasExpectedNumberOfParameters(self): batch_size = 5 height, width = 224, 224 inputs = tf.random_uniform((batch_size, height, width, 3)) with slim.arg_scope(inception.inception_v2_arg_scope()): inception.inception_v2_base(inputs) total_params, _ = slim.model_analyzer.analyze_vars( slim.get_model_variables()) self.assertAlmostEqual(10173112, total_params)
def testBatchNormScale(self): height, width = 224, 224 num_classes = 1000 inputs = tf.placeholder(tf.float32, (1, height, width, 3)) with slim.arg_scope( inception.inception_v2_arg_scope(batch_norm_scale=True)): inception.inception_v2(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): """Constructs model and return probabilities for given input.""" reuse = True if self.built else None x_input = image_normalize(x_input, normalization_method[1]) x_input = tf.image.resize_images(x_input, [224, 224]) with slim.arg_scope(inception.inception_v2_arg_scope()): _, end_points = inception.inception_v2( 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 probs
def inception_v2(inputs, is_training, opts): with slim.arg_scope(inception.inception_v2_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_v2( inputs, num_classes=opts.num_classes, is_training=is_training, dropout_keep_prob=opts.dropout_keep_prob, min_depth=opts.min_depth, depth_multiplier=opts.depth_multiplier, prediction_fn=slim.softmax, spatial_squeeze=opts.spatial_squeeze, reuse=None, global_pool=opts.global_pool)
def predict(image_file, check_point, label_file): """ Predict image file with pre-trained model and print top-3 predictions. Arg: image_file: Image file to predict. check_point: Pretrained model path. label_file: Classes list file with txt format Returns: Nothing. """ with open(label_file) as f: classes = f.readlines() with tf.Graph().as_default(): with open(image_file, "rb") as image_file: image_string = image_file.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_v2_arg_scope()): logits, _ = inception.inception_v2(processed_images, num_classes=len(classes), is_training=False) probabilities = tf.nn.softmax(logits) init_fn = slim.assign_from_checkpoint_fn(check_point, slim.get_model_variables('InceptionV2')) 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])] # Show top 3 predictions for i in range(5): index = sorted_inds[i] print('Probability %0.2f%% => [%s]' % (probabilities[index] * 100, classes[index]))
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 create(self, images, num_classes, is_training): """See baseclass.""" with slim.arg_scope(inception.inception_v2_arg_scope()): _, endpoints = inception.inception_v2( inputs=images, num_classes=num_classes, is_training=is_training) return endpoints
def eval(params): # batch_size = params['batch_size'] # num_examples = len(params['test_files'][0]) with tf.Graph().as_default() as g: url = '/home/coskun/PycharmProjects/data/pose/mv_val/img/S9/Discussion 1.54138969/frame_00010.png' filename_queue = tf.train.string_input_producer( [url]) # list of files to read reader = tf.WholeFileReader() key, value = reader.read(filename_queue) # image_raw = tf.image.decode_png(value) # use png or jpg decoder based on your files. image_raw = tf.image.decode_png(value, channels=3) processed_image = human36m_preprocessing.preprocess_image( image_raw, 224, 224, is_training=is_training) processed_images = tf.expand_dims(processed_image, 0) # image, label = dut.distorted_inputs(params,is_training=is_training) with slim.arg_scope(inception.inception_v2_arg_scope()): logits, end_points = inception.inception_v2( processed_images, num_classes=params['n_output'], is_training=is_training) init_fn = ut.get_init_fn(slim, params, load_previus_cp=True) config = tf.ConfigProto() config.gpu_options.per_process_gpu_memory_fraction = params[ 'per_process_gpu_memory_fraction'] # operations = g.get_operations() # for operation in operations: # print "Operation:",operation.name features = g.get_tensor_by_name( 'InceptionV2/InceptionV2/Mixed_3b/concat:0') # features = g.get_tensor_by_name('InceptionV2/InceptionV2/MaxPool_3a_3x3/MaxPool:0') with tf.Session(config=config) as sess: sess.run(tf.initialize_local_variables()) coord = tf.train.Coordinator() threads = [] for qr in tf.get_collection(tf.GraphKeys.QUEUE_RUNNERS): threads.extend( qr.create_threads(sess, coord=coord, daemon=True, start=True)) init_fn(sess) num_iter = 1 print('%s: Model reading started.' % (datetime.now())) step = 0 loss_lst = [] # while step < num_iter and not coord.should_stop(): while step < num_iter: try: features_values = sess.run(features) except tf.errors.OutOfRangeError: print('Testing finished....%d' % step) break print features_values.shape img_arr = np.squeeze(features_values[:, :, :, 1]) print img_arr.shape img = Image.fromarray(img_arr).convert('RGB') img.save( '/home/coskun/PycharmProjects/poseft/files/temp/my.png') img.show() # joint_list=['/'.join(p1.split('/')[0:-1]).replace('joints','img').replace('.cdf','')+'/frame_'+(p1.split('/')[-1].replace('.txt','')).zfill(5)+'.png' for p1 in image_names] # print ('List equality check:') # print len(label_names) == len(set(label_names)) # print sum(joint_list==label_names)==(len(est)) # print(len(label_names))init_fn=ut.get_init_fn(slim,params,load_previus_cp=True) step += 1 coord.request_stop() coord.join(threads)
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 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 __init__(self, num_classes, train_layers=None, weights_path='DEFAULT'): """Create the graph of the inceptionv2 model. """ # Parse input arguments into class variables if weights_path == 'DEFAULT': self.WEIGHTS_PATH = "./pre_trained_models/inception_v2.ckpt" else: self.WEIGHTS_PATH = weights_path self.train_layers = train_layers with tf.variable_scope("input"): self.image_size = inception.inception_v2.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_v2_arg_scope()): self.logits, _ = inception.inception_v2(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_v2_arg_scope()): self.logits_val, _ = inception.inception_v2(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")