def _init_preprocessing(placeholder, preprocessing_type, image_size): import tensorflow as tf from preprocessing import vgg_preprocessing, inception_preprocessing from model_tools.activations.tensorflow import load_image preprocessing_types = { 'vgg': lambda image: vgg_preprocessing.preprocess_image( image, image_size, image_size, resize_side_min=image_size), 'inception': lambda image: inception_preprocessing.preprocess_for_eval( image, image_size, image_size, central_fraction=None) } assert preprocessing_type in preprocessing_types preprocess_image = preprocessing_types[preprocessing_type] preprocess = lambda image_path: preprocess_image(load_image(image_path)) preprocess = tf.map_fn(preprocess, placeholder, dtype=tf.float32) return preprocess
def freeze_model(name): checkpoints_dir = '/data/huang/behaviour/data/tfmodel' OUTPUT_PB_FILENAME = 'inception_resnet_v2_behaviour_309_4_10_{}k.pb'.format( name[0:3]) NUM_CLASSES = 309 tensorName_v4 = 'InceptionResnetV2/Logits/Predictions' image_size = inception.inception_resnet_v2.default_image_size with tf.Graph().as_default(): input_image_t = tf.placeholder(tf.string, name='input_image') image = tf.image.decode_jpeg(input_image_t, channels=3) processed_image = inception_preprocessing.preprocess_for_eval( image, image_size, image_size, central_fraction=None) processed_images = tf.expand_dims(processed_image, 0) with slim.arg_scope(inception.inception_resnet_v2_arg_scope()): logits, _ = inception.inception_resnet_v2(processed_images, num_classes=NUM_CLASSES, is_training=False) # Apply softmax function to the logits (output of the last layer of the network) probabilities = tf.nn.softmax(logits) #model_path = tf.train.latest_checkpoint(checkpoints_dir) model_path = '/data/huang/behaviour/data/tfmodel/model_backup/model.ckpt-{}'.format( name) init_fn = slim.assign_from_checkpoint_fn(model_path, slim.get_model_variables()) with tf.Session() as sess: # Now call the initialization function within the session init_fn(sess) constant_graph = convert_variables_to_constants( sess, sess.graph_def, ["input_image", "DecodeJpeg", tensorName_v4]) tf.train.write_graph(constant_graph, '.', OUTPUT_PB_FILENAME, as_text=False)
# resize input image later in the code. image_size = inception.inception_resnet_v2.default_image_size with tf.Graph().as_default(): # Inject placeholder into the graph input_image_t = tf.placeholder(tf.string, name='input_image') image = tf.image.decode_jpeg(input_image_t, 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. # I use the "preprocess_for_eval()" method instead of "inception_preprocessing()" # because the latter crops all images to the center by 85% at # prediction time (training=False). processed_image = inception_preprocessing.preprocess_for_eval( image, image_size, image_size, central_fraction=None) # 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) # Load the inception network structure with slim.arg_scope(inception.inception_resnet_v2_arg_scope()): logits, _ = inception.inception_resnet_v2(processed_images, num_classes=NUM_CLASSES, is_training=False) # Apply softmax function to the logits (output of the last layer of the network) probabilities = tf.nn.softmax(logits) model_path = tf.train.latest_checkpoint(checkpoints_dir)