Skip to content

Instantly share code, notes, and snippets.

@warmspringwinds
Created January 24, 2017 18:31
Show Gist options
  • Save warmspringwinds/71599ba1cf3494d40cfdd49e2f973e60 to your computer and use it in GitHub Desktop.
Save warmspringwinds/71599ba1cf3494d40cfdd49e2f973e60 to your computer and use it in GitHub Desktop.
On how to perform inferece with loading the weights only once
%matplotlib inline
from __future__ import division
import os
import sys
import tensorflow as tf
import skimage.io as io
import numpy as np
sys.path.append("tf-image-segmentation/")
sys.path.append("/home/dpakhom1/workspace/my_models/slim/")
fcn_16s_checkpoint_path = '/home/dpakhom1/tf_projects/segmentation/model_fcn8s_final.ckpt'
os.environ["CUDA_VISIBLE_DEVICES"] = '1'
slim = tf.contrib.slim
from tf_image_segmentation.models.fcn_8s import FCN_8s
from tf_image_segmentation.utils.inference import adapt_network_for_any_size_input
from tf_image_segmentation.utils.pascal_voc import pascal_segmentation_lut
number_of_classes = 21
image_filename_1 = 'me.jpg'
image_filename_2 = 'small_cat.jpg'
image_filename_placeholder = tf.placeholder(tf.string)
# feed_dict_to_use = {image_filename_placeholder: image_filename}
image_tensor = tf.read_file(image_filename_placeholder)
image_tensor = tf.image.decode_jpeg(image_tensor, channels=3)
# Fake batch for image and annotation by adding
# leading empty axis.
image_batch_tensor = tf.expand_dims(image_tensor, axis=0)
# Be careful: after adaptation, network returns final labels
# and not logits
FCN_8s = adapt_network_for_any_size_input(FCN_8s, 32)
pred, fcn_16s_variables_mapping = FCN_8s(image_batch_tensor=image_batch_tensor,
number_of_classes=number_of_classes,
is_training=False)
# The op for initializing the variables.
initializer = tf.local_variables_initializer()
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(initializer)
saver.restore(sess, "/home/dpakhom1/tf_projects/segmentation/model_fcn8s_final.ckpt")
image_np_1, pred_np_1 = sess.run([image_tensor, pred], feed_dict={image_filename_placeholder: image_filename_1})
image_np_2, pred_np_2 = sess.run([image_tensor, pred], feed_dict={image_filename_placeholder: image_filename_2})
io.imshow(image_np_1)
io.show()
io.imshow(pred_np_1.squeeze())
io.show()
io.imshow(image_np_2)
io.show()
io.imshow(pred_np_2.squeeze())
io.show()
@nicks165
Copy link

how can i process multiple images in one single run? Like different batch size?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment