def prepare_image(image): return vgg_preprocessing.preprocess_image(image, _IMAGE_SIZE, _IMAGE_SIZE, is_training=False)
with tf.Graph().as_default(): url = ("https://upload.wikimedia.org/wikipedia/commons/d/d9/" "First_Student_IC_school_bus_202076.jpg") # Open specified url and load image as a string image_string = urllib.request.urlopen(url).read() # Decode string into matrix with intensity values image = tf.image.decode_jpeg(image_string, channels=3) # Resize the input image, preserving the aspect ratio # and make a central crop of the resulted image. # The crop will be of the size of the default image size of # the network. processed_image = vgg_preprocessing.preprocess_image(image, image_size, image_size, is_training=False) # Networks accept images in batches. # The first dimension usually represents the batch size. # In our case the batch size is one. processed_images = tf.expand_dims(processed_image, 0) # 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(vgg.vgg_arg_scope()): logits, _ = vgg.vgg_16(processed_images, num_classes=1000, is_training=False)
#id_quantized_layer_2 = int(sys.argv[2]) id_ave_size = int(sys.argv[1]) num_layers_wei = 16 num_layers_act = 15 #### build quantized vgg graph with slim.arg_scope(vgg_arg_scope()): input_string = tf.placeholder(tf.string) input_images = tf.read_file(input_string) input_images = tf.image.decode_jpeg(input_images, channels=3) input_images = tf.cast(input_images, tf.float32) processed_images = vgg_preprocessing.preprocess_image(input_images, 224, 224, is_training=False) processed_images = tf.expand_dims(processed_images, 0) logits, _, acts = vgg_16_quant_act(processed_images, num_classes=1000, is_training=False) probabilities = tf.nn.softmax(logits) variables_to_restore = slim.get_variables_to_restore() variables_weights, valuables_codebooks = preprocessing_variables( variables_to_restore) for i in range(len(variables_weights)): print(str(i) + ': ' + variables_weights[i].name) for i in range(len(valuables_codebooks)):
def batch_prediction(image_id_to_path, model, sess): print "batch processing: " + str(len(image_id_to_path)) image_id_to_predictions = {} image_ids = [] count = 0 start_time_1 = time.time() for image_id, path in image_id_to_path.iteritems(): image_string = open(path, 'rb').read() image = tf.image.decode_jpeg(image_string, channels=3) if model == 'inception_v1' or model == 'inception_v2' or model == 'inception_v3' or model == 'inception_v4': processed_image = preprocess_for_inception(image, image_size, image_size, central_fraction=1.0) elif model == 'vgg_16' or model == 'resnet_v1_50' or model == 'resnet_v1_101' or model == 'resnet_v1_152': processed_image = vgg_preprocessing.preprocess_image( image, image_size, image_size, is_training=False) start_time = time.time() #print processed_image.shape #np_val = sess.run(processed_image) #print np_val.shape #processed_image = tf.convert_to_tensor(np_val) #print processed_image.shape #print "conversion: "+str(time.time()-start_time)+" seconds" if count == 0: processed_images = tf.expand_dims(processed_image, 0) else: local_matrix = tf.expand_dims(processed_image, 0) processed_images = tf.concat([processed_images, local_matrix], 0) image_ids.append(image_id) count = count + 1 print "Preparation: " + str(time.time() - start_time_1) + " seconds" start_time = time.time() if model == 'inception_v1': logits, _ = inception.inception_v1(processed_images, num_classes=1001, is_training=False) init_fn = slim.assign_from_checkpoint_fn( os.path.join(checkpoints_dir, 'inception_v1.ckpt'), slim.get_model_variables('InceptionV1')) elif model == 'inception_v2': logits, _ = inception.inception_v2(processed_images, num_classes=1001, is_training=False) init_fn = slim.assign_from_checkpoint_fn( os.path.join(checkpoints_dir, 'inception_v2.ckpt'), slim.get_model_variables('InceptionV2')) elif model == 'inception_v3': logits, _ = inception.inception_v3(processed_images, num_classes=1001, is_training=False) init_fn = slim.assign_from_checkpoint_fn( os.path.join(checkpoints_dir, 'inception_v3.ckpt'), slim.get_model_variables('InceptionV3')) elif model == 'inception_v4': logits, _ = inception.inception_v4(processed_images, num_classes=1001, is_training=False) init_fn = slim.assign_from_checkpoint_fn( os.path.join(checkpoints_dir, 'inception_v4.ckpt'), slim.get_model_variables('InceptionV4')) elif model == 'resnet_v1_50': logits, _ = resnet_v1.resnet_v1_50(processed_images, num_classes=1000, is_training=False) init_fn = slim.assign_from_checkpoint_fn( os.path.join(checkpoints_dir, 'resnet_v1_50.ckpt'), slim.get_model_variables('resnet_v1_50')) elif model == 'resnet_v1_101': logits, _ = resnet_v1.resnet_v1_101(processed_images, num_classes=1000, is_training=False) init_fn = slim.assign_from_checkpoint_fn( os.path.join(checkpoints_dir, 'resnet_v1_101.ckpt'), slim.get_model_variables('resnet_v1_101')) elif model == 'resnet_v1_152': logits, _ = resnet_v1.resnet_v1_152(processed_images, num_classes=1000, is_training=False) init_fn = slim.assign_from_checkpoint_fn( os.path.join(checkpoints_dir, 'resnet_v1_152.ckpt'), slim.get_model_variables('resnet_v1_152')) elif model == 'vgg_16': logits, _ = vgg.vgg_16(processed_images, num_classes=1000, is_training=False) init_fn = slim.assign_from_checkpoint_fn( os.path.join(checkpoints_dir, 'vgg_16.ckpt'), slim.get_model_variables('vgg_16')) print "Prediction2.1: " + str(time.time() - start_time) + " seconds" start_time = time.time() init_fn(sess) print "Prediction2.2: " + str(time.time() - start_time) + " seconds" probabilities = tf.nn.softmax(logits) print "Prediction1: " + str(time.time() - start_time) + " seconds" start_time = time.time() np_image, probabilities = sess.run([image, probabilities]) runtime = time.time() - start_time print "Prediction: " + str(runtime) + " seconds" for k in range(len(image_ids)): image_id = image_ids[k] predictions = [] prob = probabilities[k, 0:] sorted_inds = [ i[0] for i in sorted(enumerate(-prob), key=lambda x: x[1]) ] for i in range(5): index = sorted_inds[i] if model == 'inception_v1' or model == 'inception_v2' or model == 'inception_v3' or model == 'inception_v4': name = names[index] elif model == 'vgg_16' or model == 'resnet_v1_50' or model == 'resnet_v1_101' or model == 'resnet_v1_152': name = names[index + 1] pr = prob[index] pair = (name, pr) predictions.append(pair) image_id_to_predictions[image_id] = predictions return image_id_to_predictions, runtime, sess
def classify_image(filepath): with tf.Graph().as_default(): image = open(filepath, 'rb') # Open specified url and load image as a string image_string = image.read() # Decode string into matrix with intensity values image = tf.image.decode_jpeg(image_string, channels=3) # Resize the input image, preserving the aspect ratio # and make a central crop of the resulted image. # The crop will be of the size of the default image size of # the network. processed_image = vgg_preprocessing.preprocess_image(image, image_size, image_size, is_training=False) # Networks accept images in batches. # The first dimension usually represents the batch size. # In our case the batch size is one. processed_images = tf.expand_dims(processed_image, 0) # Create the model, use the default arg scope to configure # the batch norm parameters. arg_scope is a very convenient # feature of slim library -- you can define default # parameters for layers -- like stride, padding etc. with slim.arg_scope(alexnet.alexnet_v2_arg_scope()): logits, _ = alexnet.alexnet_v2(processed_images, num_classes=5, 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. init_fn = slim.assign_from_checkpoint_fn( os.path.join(checkpoints_dir, 'model.ckpt-500000'), slim.get_model_variables('alexnet_v2')) with tf.Session() as sess: # Load weights init_fn(sess) # We want to get predictions, image as numpy matrix # and resized and cropped piece that is actually # being fed to the network. np_image, network_input, probabilities = sess.run([image, processed_image, probabilities]) probabilities = probabilities[0, 0:] sorted_inds = [i[0] for i in sorted(enumerate(-probabilities), key=lambda x: x[1])] for i in range(5): index = sorted_inds[i] print('Probability %0.2f => [%s]' % (probabilities[index], names[index])) return sorted_inds[0], probabilities
def main(_): model_variables = model_name_to_variables.get(FLAGS.model_name) if model_variables is None: tf.logging.error("Unknown model_name provided `%s`." % FLAGS.model_name) sys.exit(-1) if tf.gfile.IsDirectory(FLAGS.checkpoint_path): checkpoint_path = tf.train.latest_checkpoint(FLAGS.checkpoint_path) else: checkpoint_path = FLAGS.checkpoint_path # 读入图像、预处理模型、网络模型 image_string = tf.placeholder(tf.string) image = tf.image.decode_jpeg(image_string, channels=3, try_recover_truncated=True, acceptable_fraction=0.3) network_fn = nets_factory.get_network_fn(FLAGS.model_name, FLAGS.num_classes, is_training=False) # 数据预处理 if FLAGS.test_image_size is None: test_image_size = network_fn.default_image_size processed_image = vgg_preprocessing.preprocess_image(image, test_image_size, test_image_size, is_training=False) processed_images = tf.expand_dims(processed_image, 0) # 获取输出 logits, _ = network_fn(processed_images) probabilities = tf.nn.softmax(logits) # 初始化 init_fn = slim.assign_from_checkpoint_fn( checkpoint_path, slim.get_model_variables(model_variables)) sess = tf.Session() init_fn(sess) start_time = time.time() result = [] test_images = os.listdir(FLAGS.infile) for test_image in test_images: # 进行推断 path = os.path.join(FLAGS.infile, test_image) content = tf.gfile.FastGFile(path, 'rb').read() probs = sess.run(probabilities, feed_dict={image_string: content}) probs = probs[0, 0:] # 保存输出 num = probs[1] border = 0.01 if num > 1 - border: num = 1 - border elif num < border: num = border result.append([test_image[:-4], "%.3f" % num]) sess.close() # 将结果按编号有小到大排序 for i in range(len(result)): for j in range(i + 1, len(result)): if int(result[i][0]) > int(result[j][0]): temp = result[i] result[i] = result[j] result[j] = temp # 将结果输出到csv文件 id_list = [] label_list = [] for num in result: id_list.append(num[0]) label_list.append(num[1]) dataframe = pd.DataFrame({'id': id_list, 'label': label_list}) dataframe.to_csv("result.csv", index=False, sep=',') print('total time cost = %.2f' % (time.time() - start_time))
def main(_): img_read_o = cv2.imread(FLAGS.image_file) shape = img_read_o.shape height = shape[0] width = shape[1] img_read = np.float32(img_read_o) with tf.Graph().as_default(): with tf.Session().as_default() as sess: X = tf.placeholder(tf.float32, shape=[None, None, 3], name='input') X = tf.cast(X, tf.uint8) # Read image data. image0 = vgg_preprocessing.preprocess_image(X, height, width, is_training=False) # Add batch dimension image = tf.expand_dims(image0, 0) #image = tf.expand_dims(image, 0) generated = model.net(image, training=False) #generated = tf.cast(generated, tf.uint8) generated = tf.cast(generated, tf.float32) # Remove batch dimension---- generated = tf.squeeze(generated, [0], name="output_new") generated1 = tf.cast(generated, tf.uint8) # Restore model variables. saver = tf.train.Saver(tf.global_variables(), write_version=tf.train.SaverDef.V1) sess.run([ tf.global_variables_initializer(), tf.local_variables_initializer() ]) # Use absolute path FLAGS.model_file = os.path.abspath(FLAGS.model_file) saver.restore(sess, FLAGS.model_file) # Make sure 'generated' directory exists. generated_file = 'generated/res4A.jpg' if os.path.exists('generated') is False: os.makedirs('generated') # Generate and write image data to file. with open(generated_file, 'wb') as img: start_time = time.time() img.write( sess.run(tf.image.encode_jpeg(generated1), feed_dict={X: img_read})) end_time = time.time() # Generated the protobuf file. output_graph_def = tf.graph_util.convert_variables_to_constants( sess, sess.graph_def, output_node_names=['output_new']) with tf.gfile.FastGFile('models/wave4A.pb', mode='wb') as f: f.write(output_graph_def.SerializeToString()) tf.logging.info('Convert done!') tf.logging.info('Elapsed time: %fs' % (end_time - start_time)) tf.logging.info('Done. Please check %s.' % generated_file)
# or for png im = tf.image.decode_png(im_as_string, channels=3) ''' ## for an image stored in our computer, we can create a queue of our filenames in the # target directory, and then read the entire image file by using tf.WholeFileReader() : filename_queue = tf.train.string_input_producer( tf.train.match_filenames_once('/home/desktop/tensorflow/images/lakeside.png')) image_reader = tf.WholeFileReader() _, image_file = image_reader.read(filename_queue) image = tf.image.decode_png(image_file) image_size = vgg.vgg_16.default_image_size processed_im = preprocess_image(image, image_size,image_size) processed_images = tf.expand_dims(processed_im, 0) with slim.arg_scope(vgg.vgg_arg_scope()): logits, _ = vgg.vgg_16(processed_images, num_classes=1000, is_training=False) probabilities = tf.nn.softmax(logits) def vgg_arg_scope(weight_decay=0.0005): with slim.arg_scope([slim.conv2d, slim.fully_connected], activation_fn=tf.nn.relu, weight_regularizer=slim.l2_regularizer(weight_decay), biases_initializer=tf.zeros.initializer): with slim.arg_scope([slim.conv2d], padding='SAME') as arg_sc:
# Build the graph g = tf.Graph() with g.as_default(): # Open specified url and load image as a string image_string = open(image_path, 'rb').read() # Decode string into matrix with intensity values image = tf.image.decode_jpeg(image_string, channels=3) # Resize the input image, preserving the aspect ratio # and make a central crop of the resulted image. # The crop will be of the size of the default image size of # the network. processed_image = vgg_preprocessing.preprocess_image(image, image_size, image_size, is_training=False) # Networks accept images in batches. # The first dimension usually represents the batch size. # In our case the batch size is one. processed_images = tf.expand_dims(processed_image, 0) # 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(vgg.vgg_arg_scope()): logits, _ = vgg.vgg_16(processed_images, num_classes=1000, is_training=False)
image = plt.imread(image_file) # pdb.set_trace() image = scipy.misc.imresize(image, (image_size, image_size)) # image = transform.resize(image, (image_size, image_size)) # pdb.set_trace() # plt.imshow(image) # plt.annotate('Something', xy = (0.05, 0.95), xycoords = 'axes fraction') # plt.show() labels = imagenet.create_readable_names_for_imagenet_labels() # Define graph with tf.Graph().as_default(): x = tf.placeholder(dtype=tf.float32, shape=(image_size, image_size, 3)) normalized_image = vgg_preprocessing.preprocess_image( x, image_size, image_size, is_training=False) normalized_images = tf.expand_dims(normalized_image, 0) with slim.arg_scope(vgg.vgg_arg_scope()): output, endpoints = vgg.vgg_16(normalized_images, num_classes=1000, is_training=False) probabilities = tf.nn.softmax(output) init_fn = slim.assign_from_checkpoint_fn( os.path.join(checkpoints_dir, 'vgg_16.ckpt'), slim.get_model_variables('vgg_16')) # Run in a session with tf.Session() as sess: init_fn(sess) probability, layers = sess.run([probabilities, endpoints], feed_dict={x: image}) layer_names, layer_activations = zip(*list(layers.items()))
def main(_): model_variables = model_name_to_variables.get(FLAGS.model_name) if model_variables is None: tf.logging.error("Unknown model_name provided `%s`." % FLAGS.model_name) sys.exit(-1) if tf.gfile.IsDirectory(FLAGS.checkpoint_path): checkpoint_path = tf.train.latest_checkpoint(FLAGS.checkpoint_path) else: checkpoint_path = FLAGS.checkpoint_path # 读入图像、预处理模型、网络模型 image_string = tf.placeholder(tf.string) image = tf.image.decode_jpeg(image_string, channels=3, try_recover_truncated=True, acceptable_fraction=0.3) network_fn = nets_factory.get_network_fn(FLAGS.model_name, FLAGS.num_classes, is_training=False) # 数据预处理 if FLAGS.test_image_size is None: test_image_size = network_fn.default_image_size processed_image = vgg_preprocessing.preprocess_image(image, test_image_size, test_image_size, is_training=False) processed_images = tf.expand_dims(processed_image, 0) # 获取输出 logits, _ = network_fn(processed_images) probabilities = tf.nn.softmax(logits) # 初始化 init_fn = slim.assign_from_checkpoint_fn( checkpoint_path, slim.get_model_variables(model_variables)) sess = tf.Session() init_fn(sess) start_time = time.time() # 进行推断 result = [] test_images = os.listdir(FLAGS.infile) for test_image in test_images: path = os.path.join(FLAGS.infile, test_image) content = tf.gfile.FastGFile(path, 'rb').read() _logits, _prob = sess.run([logits, probabilities], feed_dict={image_string: content}) sum_squares = _logits[0, 0] * _logits[0, 0] + _logits[0, 1] * _logits[ 0, 1] _prob = _prob[0, 0:] _prob = _prob[1] classes = 'cat' if 'cat' in test_image else 'dog' result.append([ path, test_image, classes, sum_squares, _prob, _logits[0, 0], _logits[0, 1] ]) sess.close() # 将结果输出到csv文件 path_list = [] name_list = [] class_list = [] sum_squares_list = [] prob_list = [] logits1_list = [] logits2_list = [] for item in result: path_list.append(item[0]) name_list.append(item[1]) class_list.append(item[2]) sum_squares_list.append(item[3]) prob_list.append(item[4]) logits1_list.append(item[5]) logits2_list.append(item[6]) dataframe = pd.DataFrame({ 'path': path_list, 'name': name_list, 'class': class_list, 'sum_squares': sum_squares_list, 'prob': prob_list, 'logits1': logits1_list, 'logits2': logits2_list }) dataframe.to_csv("outliers.csv", index=False, sep=',') if not os.path.exists(FLAGS.outliers_path): os.makedirs(FLAGS.outliers_path) # 输出sum_squares最小的部分图片 all_path = os.path.join(FLAGS.outliers_path, 'min_sum_squares') if not os.path.exists(all_path): os.makedirs(all_path) for i in range(min(500, len(result))): for j in range(i + 1, len(result)): if result[i][3] > result[j][3]: temp = result[i] result[i] = result[j] result[j] = temp shutil.copyfile( result[i][0], os.path.join(all_path, format(i, "3d") + "_" + result[i][1])) # 输出cat中最难识别的部分图片 cat_path = os.path.join(FLAGS.outliers_path, 'cat_max_logits') if not os.path.exists(cat_path): os.makedirs(cat_path) for i in range(min(250, len(result))): for j in range(i + 1, len(result)): if (result[j][2] == 'cat') and (result[i][2] == 'dog' or result[i][4] < result[j][4]): temp = result[i] result[i] = result[j] result[j] = temp shutil.copyfile( result[i][0], os.path.join(cat_path, format(result[i][4], ".3f") + "_" + result[i][1])) # 输出dog中最难识别的部分图片 dog_path = os.path.join(FLAGS.outliers_path, 'dog_min_logits') if not os.path.exists(dog_path): os.makedirs(dog_path) for i in range(min(250, len(result))): for j in range(i + 1, len(result)): if (result[j][2] == 'dog') and (result[i][2] == 'cat' or result[i][4] > result[j][4]): temp = result[i] result[i] = result[j] result[j] = temp shutil.copyfile( result[i][0], os.path.join(dog_path, format(result[i][4], ".3f") + "_" + result[i][1])) print('total time cost = %.2f' % (time.time() - start_time))
def main(_): if not FLAGS.dataset_dir: raise ValueError('必须设置 dataset_dir 参数') tf.logging.set_verbosity(tf.logging.INFO) with tf.Graph().as_default(): slim.get_or_create_global_step() # 获取验证数据集 dataset = dataset_utils.get_dataset('verify', FLAGS.dataset_dir, FLAGS.dataset_size, NUM_CLASS) provider = slim.dataset_data_provider.DatasetDataProvider( dataset, shuffle=False, common_queue_capacity=2 * FLAGS.batch_size, common_queue_min=FLAGS.batch_size) [image, label] = provider.get(['image', 'label']) # 获取网络和预处理后的图像 network_fn = nets_factory.get_network_fn( FLAGS.model_name, num_classes=dataset.num_classes, is_training=False) eval_image_size = FLAGS.eval_image_size or network_fn.default_image_size image = vgg_preprocessing.preprocess_image(image, eval_image_size, eval_image_size, is_training=False) images, labels = tf.train.batch( [image, label], batch_size=FLAGS.batch_size, num_threads=FLAGS.num_preprocessing_threads, capacity=5 * FLAGS.batch_size) # 得到网络的输出 network_fn = nets_factory.get_network_fn( FLAGS.model_name, num_classes=dataset.num_classes, is_training=False) logits, _ = network_fn(images) variables_to_restore = slim.get_variables_to_restore() predictions = tf.argmax(logits, 1) labels = tf.squeeze(labels) # 定义评价指标 names_to_values, names_to_updates = slim.metrics.aggregate_metric_map({ 'Accuracy': slim.metrics.streaming_accuracy(predictions, labels), 'Recall_5': slim.metrics.streaming_recall_at_k(logits, labels, 5), }) for name, value in names_to_values.items(): summary_name = 'eval/%s' % name op = tf.summary.scalar(summary_name, value, collections=[]) op = tf.Print(op, [value], summary_name) tf.add_to_collection(tf.GraphKeys.SUMMARIES, op) num_batches = math.ceil(dataset.num_samples / float(FLAGS.batch_size)) if tf.gfile.IsDirectory(FLAGS.checkpoint_path): checkpoint_path = tf.train.latest_checkpoint(FLAGS.checkpoint_path) else: checkpoint_path = FLAGS.checkpoint_path tf.logging.info('\r>> Evaluating %s' % checkpoint_path) # 开始验证 start_time = time.time() slim.evaluation.evaluate_once( master='', checkpoint_path=checkpoint_path, logdir=FLAGS.eval_dir, num_evals=num_batches, eval_op=list(names_to_updates.values()), variables_to_restore=variables_to_restore) print('total time cost = %.2f' % (time.time() - start_time))
sys.path.append("/home/rm/tensorflow_models/slim") target_dir = '/home/rm/tmp/TF-Slim-PretrainedVGG16/checkpoints/' url = ("http://54.68.5.226/car.jpg") IMAGE_SIZE = vgg.vgg_16.default_image_size IMAGE_FROM_COMPUTER = True if (not IMAGE_FROM_COMPUTER): #im_as_string = urllib2.urlopen(url).read() im_as_string = urllib.request.urlopen(url).read() im = tf.image.decode_jpeg(im_as_string, channels=3) processed_im = vgg_preprocessing.preprocess_image(im, IMAGE_SIZE, IMAGE_SIZE, is_training=False) else: #Hope, Tom; Resheff, Yehezkel S.; Lieder, Itay. #Learning TensorFlow: A Guide to Building Deep Learning Systems #(Kindle Locations 4920-4926). O'Reilly Media. Kindle Edition. # list_of_files = tf.train.match_filenames_once("/home/rm/Downloads/Images/Car.jpg") # file_queue = tf.train.string_input_producer(list_of_files) #https://www.tensorflow.org/api_docs/python/tf/WholeFileReader#read # image_reader = tf.WholeFileReader() # key_value_pair = image_reader.read(file_queue) # filename_at_head_of_Q = key_value_pair[0] # content_of_file_at_head_of_Q = key_value_pair[1] # im_ = tf.image.decode_jpeg(content_of_file_at_head_of_Q, channels=0) from PIL import Image # im_ = Image.open('/home/rm/tmp/Images/Car.jpg')
data = json.load(data_file) image_feature_vectors = {} tf.reset_default_graph() one_image = ndimage.imread(TRAIN_IMAGE_PATH + data["images"][0]['file_name']) #resize for vgg network resize_img = misc.imresize(one_image, [224, 224]) if len( one_image.shape ) != 3: #Check to see if the image is grayscale if True mirror colorband resize_img = np.asarray(np.dstack((resize_img, resize_img, resize_img)), dtype=np.uint8) processed_image = vgg_preprocessing.preprocess_image(resize_img, 224, 224, is_training=False) processed_images = tf.expand_dims(processed_image, 0) network, endpts = vgg.vgg_16(processed_images, is_training=False) init_fn = slim.assign_from_checkpoint_fn( os.path.join('/dli/data/mdt/mscoco/vgg_16.ckpt'), slim.get_model_variables('vgg_16')) sess = tf.Session() init_fn(sess) NETWORK, ENDPTS = sess.run([network, endpts]) sess.close() print('fc7 array for a single image') print(ENDPTS['vgg_16/fc7'][0][0][0]) # It's clear there's a lot of information there. Let's find out what's contained in the "..." by visualizing the vector as a plot instead.
def parse_record(raw_record, is_training, num_channels, dtype, use_random_crop=True, image_size=224, autoaugment_type=None, with_drawing_bbox=False, dct_method="", preprocessing_type='imagenet'): """ Parses a record containing a training example of an image. The input record is parsed into a label and image, and the image is passed through preprocessing steps. (cropping, flipping, and so on). :param raw_record: Scalar Tensor tf.string containing a serialized Example protocol buffer. :param is_training: A boolean denoting whether the input is for training. :param num_channels: The number of channels. :param dtype: Data type to use for images/features. :param use_random_crop: Whether to randomly crop a training image. :param image_size: Output image size. :param autoaugment_type: Auto augmentation type. :param with_drawing_bbox: If True, return processed image tensor including raw image tensor with bbox. :param dct_method: An optional `string`. Defaults to `""`. string specifying a hint about the algorithm used for decompression. Defaults to "" which maps to a system-specific default. Currently valid values are ["INTEGER_FAST", "INTEGER_ACCURATE"]. The hint may be ignored. (e.g., the internal jpeg library changes to a version that does not have that specific option.) :param preprocessing_type: image preprocessing type. ['imagenet', 'cub'] :return: Tuple with processed image tensor and one-hot-encoded label tensor. """ image_buffer, label, bbox = parse_example_proto(raw_record) raw_image_with_bbox = None print('preprocessing_type', preprocessing_type) if preprocessing_type == 'imagenet': image, raw_image_with_bbox = imagenet_preprocessing.preprocess_image( image_buffer=image_buffer, bbox=bbox, output_height=image_size, output_width=image_size, num_channels=num_channels, is_training=is_training, use_random_crop=use_random_crop, dct_method=dct_method, autoaugment_type=autoaugment_type, with_drawing_bbox=with_drawing_bbox) elif preprocessing_type == 'cub': image = cub_preprocessing.preprocess_image( image_buffer=image_buffer, output_height=image_size, output_width=image_size, num_channels=num_channels, is_training=is_training, dct_method=dct_method, autoaugment_type=autoaugment_type) elif preprocessing_type == 'vgg': image = vgg_preprocessing.preprocess_image( image=image_buffer, output_height=image_size, output_width=image_size, is_training=is_training, autoaugment_type=autoaugment_type) else: raise NotImplementedError image = tf.cast(image, dtype) if with_drawing_bbox and not raw_image_with_bbox: image = tf.stack([image, raw_image_with_bbox]) return image, label