1

I am new to tensorflow and I am trying to build an image classifier. I have successfully created the model and I am trying to predict a single image after restoring the model. I have gone through various tutorials (https://github.com/sankit1/cv-tricks.com/blob/master/Tensorflow-tutorials/tutorial-2-image-classifier/predict.py) but I can't figure out the feed-dict thing in my code. I am stuck at predict fnction after loading the saved model. Can someone please help me and tell me what to do after loading all the variables from the saved model?

This is the train function which returns the parameters and save them in a model.

def trainModel(train, test, learning_rate=0.0001, num_epochs=2, minibatch_size=32, graph_filename='costs'):
"""
Implements a three-layer tensorflow neural network: LINEAR->RELU->LINEAR->RELU->LINEAR->SOFTMAX.

Input:
    train : training set
    test : test set
    learning_rate : learning rate 
    num_epochs : number of epochs 
    minibatch_size : size of minibatch
    print_cost : True to print the cost every epoch

Returns:
    parameters : parameters learnt by the model
"""

    ops.reset_default_graph() #for rerunning the model without resetting tf vars

# input and output shapes
    (n_x, m) = train.images.T.shape
    n_y = train.labels.T.shape[0]

    costs = [] #var for storing the costs for later use

    # create placeholders
    X, Y = placeholderCreator(n_x, n_y)

    parameters = paramInitializer()

    # Forward propagation
    Z3 = forwardPropagation(X, parameters)
    # Cost function
    cost = costCalc(Z3, Y)
    #Backpropagation using adam optimizer
    optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)
    # Initialize tf variables
    init = tf.global_variables_initializer()
    minibatch_size = 32
    # Start session to compute Tensorflow graph
    with tf.Session() as sess:
    # Run initialization
        sess.run(init)

        for epoch in range(num_epochs): # Training loop
            epoch_cost = 0.
            num_minibatches = int(m / minibatch_size)
            for i in range(num_minibatches):
                minibatch_X, minibatch_Y = train.next_batch(minibatch_size)  # Get next batch of training data and labels
                _, minibatch_cost = sess.run([optimizer, cost], feed_dict={X: minibatch_X.T, Y: minibatch_Y.T}) # Execute optimizer and cost function
                epoch_cost += minibatch_cost / num_minibatches # Update epoch cost

        saver = tf.train.Saver()
        # Save parameters
        parameters = sess.run(parameters)
        saver.save(sess, "~/trained-model.ckpt")
        return parameters

And this is my predict function where I am trying to predict an image. I have converted that image into MNIST format for ease of use (predicting_data). I load the model that I saved, use a softmax function on the output of 3rd layer (final output).

def predict():

train = predicting_data.train
(n_x, m) = train.images.T.shape
n_y = train.labels.T.shape[0]
X, Y = placeholderCreator(n_x, n_y)
with tf.Session() as sess:
    new_saver = tf.train.import_meta_graph('~/trained-model.ckpt.meta')
    new_saver.restore(sess, '~/trained-model.ckpt')
    W1 = tf.get_default_graph().get_tensor_by_name('W1:0')
    b1 = tf.get_default_graph().get_tensor_by_name('b1:0')
    W2 = tf.get_default_graph().get_tensor_by_name('W2:0')
    b2 = tf.get_default_graph().get_tensor_by_name('b2:0')
    W3 = tf.get_default_graph().get_tensor_by_name('W3:0')
    b3 = tf.get_default_graph().get_tensor_by_name('b3:0')
    # forward propagation     
    Z1 = tf.add(tf.matmul(W1,X), b1)     
    A1 = tf.nn.relu(Z1)                  
    Z2 = tf.add(tf.matmul(W2,A1), b2)    
    A2 = tf.nn.relu(Z2)                  
    Z3 = tf.add(tf.matmul(W3,A2), b3) 
    y_pred = tf.nn.softmax(Z3) ####what to do after this????
    cost = sess.run(y_pred, feed_dict={X: train.images.T}) 

Thank you in advance!

Rachit
  • 208
  • 4
  • 19
  • if I print `y_pred` in last second line of `predict` function, the output is empty – Rachit May 26 '18 at 11:06
  • Your predict part is not right, you need to get the input and predict tensors from the saved graph using `get_tensor_by_name()` and then use it in your `sess.run`. Check:https://stackoverflow.com/questions/50484676/restoring-model-take-to-long/50488160#50488160 – Vijay Mariappan May 26 '18 at 12:23
  • Thank you for your answer. But isn't that what I am doing? @vijaym – Rachit May 26 '18 at 13:30
  • Nope. In the code, X and y_pred are not obtained from the graph. – Vijay Mariappan May 26 '18 at 14:46
  • I have changed the code a bit. https://trinket.io/python/4e4ead175c Please tell me what is wrong now? – Rachit May 26 '18 at 15:26
  • The above code is right for y_pred, where is the get_tensor_by_name for X?. you need to get the tensor X from the graph. – Vijay Mariappan May 26 '18 at 18:49
  • did that in the newer version. The value of output_labels doesn't change even if the input image is changed. https://codeshare.io/2jO4oR – Rachit May 26 '18 at 18:58

2 Answers2

0

As vijay says in his comment:

Your predict part is not right, you need to get the input and predict tensors from the saved graph using the get_tensor_by_name() function and then use it in your sess.run

If you look at this post, it covers a similar problem and has some code examples.

R.F. Nelson
  • 2,254
  • 2
  • 12
  • 24
0

In your code, you can pass 1 to the next_batch method and get just one image.

minibatch_X, minibatch_Y = train.next_batch(1)