2

I have a realtime application where I'm trying to classify images that come from a feed as they arrive. The classify_images example has:

image_data = tf.gfile.FastGFile(image, 'rb').read()
#...
with tf.Session() as sess:
    predictions = sess.run(softmax_tensor,
        {'DecodeJpeg/contents:0': image_data})

Where image is the path to the image file. However, I'm not reading images from a file, I'm receiving them as numpy arrays. What's the best way to run a Tensorflow session on a previously-acquired image? Also, is the best practice to create one session and one graph beforehand, and whenever a new frame is acquired, run the existing session on the new image, instead of creating a new graph and a new session?

Edit:

I tried:

images_placeholder = tensorflow.placeholder(tensorflow.int32)
predictions = sess.run(softmax_tensor,
                       {images_placeholder: image})

and it worked! Thanks sygi!

Edit 2:

This crashes after a while with no error message, and also every frame has the same labels predicted. I even create a new images_place object at each frame, yet I still get the same label.

7
  • Btw, it is a good practice to give placeholders predefined shape (if you know it) -- debugging shapes will be easier then. Commented Nov 15, 2016 at 15:37
  • What is your image? You should feed different (batch of) images each time. Commented Nov 15, 2016 at 16:41
  • It's a new image, the code snippet is called in a loop and each frame from the video feed is being put into the variable image Commented Nov 15, 2016 at 16:45
  • Then model receives different images. It seems the problem is somewhere in the model if it gives the same predictions. As far as error is concerned, I have no idea (especially as there's no stacktrace), but I also suspect the reason is somewhere outside of these two lines. Commented Nov 15, 2016 at 16:49
  • I used softmax_tensor = sess.graph.get_tensor_by_name('softmax:0') every iteration in the loop, should that change per iteration? Commented Nov 15, 2016 at 17:01

1 Answer 1

1

What's the best way to run a Tensorflow session on a previously-acquired image?

I think the best way is to create a tf.placeholder, use it in your model and at the end pass the numpy array in a feed dict.

is the best practice to create one session and one graph beforehand, and whenever a new frame is acquired, run the existing session on the new image, instead of creating a new graph and a new session?

It is better to reuse one graph and one session. When you create a graph, it is "compiled" to change the code you write to an efficient, GPU implementation. You create many graphs -- you lose a lot of time doing the same "compilation". Furthermore, when you reuse one session, it is possible for you to reuse variables, preventing from passing them from RAM to GPU memory back-and-forth.

Sign up to request clarification or add additional context in comments.

2 Comments

This worked, thanks! I edited the OP with the solution included.
Actually, this crashes after a while. Doesn't give an error, and it stops working. Also, it gives me the same labels every time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.