4

Problem:

I am very new to Tensorflow. My specific question is what particular arguments should I put inside sess.run(fetches, feed_dict) function. For instance, how could find out what the values of the arguments?

Steps:

Here is my understanding of the steps after looking at other posts.

  1. Save tranied tensorflow model, it should consists of 4 files, below are my outputs:
  • checkpoint
  • Inception_resnet_v2.ckpt.data-00000-of-00001
  • Inception_resnet_v2.ckpt.index
  • Inception_resnet_v2.ckpt.meta
  1. Resize the input image to whatever format required by the neural network.

  2. Start tensorflow session.

  3. Retrive the Graph and associated parameters, tensors...

  4. Predict the input image.

Code:

Traning code:

https://github.com/taki0112/SENet-Tensorflow/blob/master/SE_Inception_resnet_v2.py

[Solved] Test code:

import tensorflow as tf
import numpy as np
import cv2

labels = ["airplane","automobile","bird","cat","deer","dog","frog","horse","ship","truck"]

# Load graph and parameters, etc.
sess=tf.Session()
saver = tf.train.import_meta_graph('./model/Inception_resnet_v2.ckpt.meta')
saver.restore(sess, tf.train.latest_checkpoint("./model/"))
graph = tf.get_default_graph()

# Get tensor names
x = graph.get_tensor_by_name("Placeholder:0")
training_flag = graph.get_tensor_by_name("Placeholder_2:0")
op_to_restore = graph.get_tensor_by_name("final_fully_connected/dense/BiasAdd:0")

# Preprocess imgae imput
src = cv2.imread("./input/car3.jpg")
dst = cv2.resize(src, (32, 32), interpolation=cv2.INTER_CUBIC)
b,g,r = cv2.split(dst)
b = (b - np.mean(b)) / np.std(b) * .1
g = (g - np.mean(g)) / np.std(g) * .1
r = (r - np.mean(r)) / np.std(r) * .1
src = cv2.merge((b,g,r))

picture = dst.reshape(1, 32, 32, 3)
feed_dict ={x: picture, training_flag:False}

result_index = sess.run(op_to_restore,feed_dict)
print(result_index)
print (labels[np.argmax(result_index)])
Leo
  • 86
  • 1
  • 7
  • 1
    I simply print out the tensor name for "logits" in the training code to find the corresponding output layer. And now it correctly prints out the predicted labels for me. Hope this helpful for new users of tensorflow like me. – Leo Aug 10 '18 at 03:14
  • Please don't put the solution inside the question. If you found something that worked for you, please add it as an answer. – pault Aug 17 '18 at 00:22

1 Answers1

2

the arguments actually depend on what you're doing, but mostly the first argument is the weights and placeholders. Whenever you are working with Tensorflow, you define a graph which is fed examples(training data) and some hyperparameters like learning rate, global step etc. It’s a standard practice to feed all the training data and hyperparameters using placeholders. when you build a network using placeholders and save it the network is saved, however, values of the placeholders are not saved.

Let's see a toy example:

import tensorflow as tf

#Prepare to feed input, i.e. feed_dict and placeholders
w1 = tf.placeholder("float", name="w1")
w2 = tf.placeholder("float", name="w2")
b1= tf.Variable(2.0,name="bias")
feed_dict ={w1:4,w2:8}

#Define a test operation that we will restore
w3 = tf.add(w1,w2)
w4 = tf.multiply(w3,b1,name="op_to_restore")
sess = tf.Session()
sess.run(tf.global_variables_initializer())

#Create a saver object which will save all the variables
saver = tf.train.Saver()

#Run the operation by feeding input
print sess.run(w4,feed_dict)
#Prints 24 which is sum of (w1+w2)*b1 

#Now, save the graph
saver.save(sess, 'my_test_model',global_step=1000)

Now, when we want to restore it, we not only have to restore the graph and weights, but also prepare a new feed_dict that will feed the new training data to the network. We can get reference to these saved operations and placeholder variables via graph.get_tensor_by_name() method. So if you want to train the same model with further new data, then you would have to utilize those weigtages, if however you just want to get the prediction from the model you trained, you could utilize the op_to_restore and the feed_dict as new data. Something like this, if you follow the above example:

import tensorflow as tf

sess=tf.Session()    
#First let's load meta graph and restore weights
saver = tf.train.import_meta_graph('my_test_model-1000.meta')
saver.restore(sess,tf.train.latest_checkpoint('./'))


# Now, let's access and create placeholders variables and
# create feed-dict to feed new data

graph = tf.get_default_graph()
w1 = graph.get_tensor_by_name("w1:0")
w2 = graph.get_tensor_by_name("w2:0")
feed_dict ={w1:13.0,w2:17.0}

#Now, access the op that you want to run. 
op_to_restore = graph.get_tensor_by_name("op_to_restore:0")

print sess.run(op_to_restore,feed_dict)
#This will print 60 which is calculated 
#using new values of w1 and w2 and saved value of b1. 

So, this is how it works, in your case, since you're trying to load the Inception model, your op_to_restore should depend on what you're trying to restore if you could tell us what you're trying to do, then only it's possible to suggest something. However in the other parameter feed_dict , it's just the numpy array of image pixel, of you, you're trying to classify/predict or whatever you're doing.

I took the code from the following article. This will help you as well. http://cv-tricks.com/tensorflow-tutorial/save-restore-tensorflow-models-quick-complete-tutorial/

Update: For your particular case, you may like to try the following code to predict the classes in the new images.

import tensorflow as tf
slim = tf.contrib.slim
from inception_resnet_v2 import *

#Well, since you're using resnet_v2, this may be equivalent to you. 
checkpoint_file = 'inception_resnet_v2_2016_08_30.ckpt'
sample_images = ['dog.jpg', 'panda.jpg']
#Load the model
sess = tf.Session()
arg_scope = inception_resnet_v2_arg_scope()
with slim.arg_scope(arg_scope):
  logits, end_points = inception_resnet_v2(input_tensor, is_training=False)

#With this, you could consider the op_variable with the following
predict_values, logit_values = sess.run([end_points['Predictions'], logits], feed_dict={input_tensor: im})

#Here im is the normalized numpy array of the image pixels.

Furthermore, the following resources may help you even more: Using pre-trained inception_resnet_v2 with Tensorflow

https://github.com/tensorflow/tensorflow/issues/7172

user2906838
  • 1,178
  • 9
  • 20
  • Thanks for your quick reponse, these information are very helpful. I have added my traning code in previous post. This model should be tranied on CIFAR-10 dataset, so the output should be score for each of the 10 classifications (I think). This is actually what I want to know for a new input image. But I coudln't find the name of that operation. – Leo Aug 07 '18 at 14:25
  • Ok, I updated my answer, you could try it, also the resources pointed may help you. – user2906838 Aug 07 '18 at 16:18
  • Still no luck, the code I have used seems a lot different than the one you posted. – Leo Aug 08 '18 at 07:26
  • Well, maybe using `slim` may help you as well. Can you please check this link: https://github.com/tensorflow/models/blob/master/research/slim/slim_walkthrough.ipynb especially the loading pre-trained model Inception. – user2906838 Aug 08 '18 at 14:49
  • Also, can you please tell me, what error did you encounter while trying the code I had put in the answer? – user2906838 Aug 08 '18 at 14:51
  • ModuleNotFoundError: No module named 'inception_resnet_v2', actually I am implementating SEnet that based on 'inception_resnet_v2', so I think I can't use that code directly. – Leo Aug 09 '18 at 02:43
  • https://github.com/tensorflow/models/blob/master/research/slim/nets/inception_resnet_v2.py this code is what you need to import inception_resnet_v2. First copy the code and paste it in your project dir, you should be able to run the code above. – user2906838 Aug 09 '18 at 03:47
  • 1
    Thanks for your help again: ) I ve solved it already, check my edited post. – Leo Aug 10 '18 at 03:15