1
$\begingroup$

Hi I am studying tensorflow for cifar-10 image classification using the code here

$\endgroup$
2
$\begingroup$

I want to ask you that how to predict the probabilities of each class in test images.

At line 27 in the train.py you have the following code:

correct_prediction = tf.equal(y_pred_cls, tf.argmax(y, axis=1))

It tries to find whether the predicted values are the same as the real ones. You can run y_pred_cls to see the probability of each class for your desired input.

I want to use the code to predict the probabilities of new data's labels, how to save and load the model we have trained which used the train data.

for saving your model and its weights you can take a look at here. As you can see from there, you have to make a saver object:

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)

And for loading that you have to restore it:

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('./'))


# Access saved Variables directly
print(sess.run('bias:0'))
# This will print 2, which is the value of bias that we saved


# 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 


Edit: Actually the code is a bit strange, anyway. The following part:

tf.nn.softmax_cross_entropy_with_logits(logits=output, labels=y)

can help you. It outputs the probabilities for each class.

$\endgroup$
  • 1
    $\begingroup$ Nice Explanation $\endgroup$ – Aditya Mar 12 '18 at 10:50
  • $\begingroup$ Hi thanks for your answer. it seems that y_pred_cls is the label value not prob. $\endgroup$ – tktktk0711 Mar 12 '18 at 10:55
  • $\begingroup$ @tktktk0711 Updated $\endgroup$ – Media Mar 12 '18 at 11:05
  • $\begingroup$ @Media thanks for your updated answer, now I am trying to do it. You are right, the code is strange.It is difficult for me to understand. If you have time, could you test the code and get the prob. $\endgroup$ – tktktk0711 Mar 12 '18 at 11:15
  • $\begingroup$ @Media I still confused about the code that how to get the prob. $\endgroup$ – tktktk0711 Mar 12 '18 at 11:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.