5
$\begingroup$

I am new to tensorflow. I have manged to train and validate a CNN, saved the session through the Saver object into a CPKT file and loaded it back. Now I'd like to use the trained model in order to check how it performs against a photo I took myself. I can't figure out how to do it.

$\endgroup$
2
  • $\begingroup$ What's your task? Do you want to detect object in your photos or something else? Your question is not clearly enough, please add more details. $\endgroup$
    – Icyblade
    Feb 13 '17 at 7:41
  • $\begingroup$ Please check out sample for Tensorflow estimator local prediction github.com/AshutoshDongare/… $\endgroup$ Mar 6 '18 at 10:41
7
$\begingroup$

Look at this blog. Mainly you have saved operations as a part of your computational graph. So after you load your model, you can restore the session and call the predict operation that you created for training and validating your data, and run it on the new data hy feeding into the feed_dict. Make sure it is in the same format and same shape as your training data. I am adding a small snippet of code, maybe that will help.

checkpoint_file=tf.train.latest_checkpoint(checkpoint_directory)
graph=tf.Graph()

 with graph.as_default():
     session_conf = tf.ConfigProto(allow_safe_placement=True, log_device_placement =False)
     sess = tf.Session(config = session_conf)
     with sess.as_default():
          saver =     tf.train.import_meta_graph("{}.meta".format(checkpoint_file))
          saver.restore(sess,checkpoint_file)
          input = graph.get_operation_by_name("input").outputs[0]
          prediction=graph.get_operation_by_name("prediction").outputs[0]
          #newdata=put your data here
          print sess.run(prediction,feed_dict={input:newdata})

To use operation_by_name, you will have to name the operations and variables in the original model. Secondly, in the feed dict you will have to give all the parameters needed to perform the operation(prediction here), so if you are using dropout you will have to give that too. There might be more simpler ways of doing this. This is the way I did it.

$\endgroup$
1
  • $\begingroup$ It says Traceback (most recent call last): File "predict.py", line 19, in <module> print(sess.run(prediction,feed_dict={input:images})) File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/client/session.py", line 767, in run run_metadata_ptr) File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/client/session.py", line 922, in _run + e.args[0]) TypeError: Cannot interpret feed_dict key as Tensor: Can not convert a builtin_function_or_method into a Tensor. $\endgroup$
    – Jack
    Mar 15 '17 at 14:28

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.