sebjwallace
sebjwallace

Reputation: 795

sess.run within a loop

I have a graph that computes a series of operations based on an input tensor. This input tensor will be derived from the webcam. In this case I can have sess.run(...) inside a while loop.

However, is this optimal? Upon every iteration of the while loop are the tensors (ie weights) in the graph maintained in the GPU's memory? I read that the most expensive part of GPU computation is migrating tensors from RAM to GPU memory. Is this migration happening every time I call sess.run(...) or does it only occur once and I can run sess.run(...) as much as I want? Then I suppose the tensors get released from the GPU when sess.close() is called?

Upvotes: 0

Views: 334

Answers (1)

gorjan
gorjan

Reputation: 5555

The migration will happen every time you execute sess.run(...). However, I am pretty sure that something can be done to accelerate that. I see that you can use a gpu version of opencv. Moreover, you can use opencv to access the webcam.

However, I also read that this will require migration all the time as well, and sometimes it can be even slower that executing everything on cpu.

Another thing that I can think of is reducing the number of images you read from the camera. Using opencv you can specify how many frames per second you will read. If you ask me, keeping the computation on cpu and reading only few frames from the webcam should be the best option you got.

Here is a patch of code to do that (I know is out of the scope of the question but I hope that it will be useful for you):

cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FPS, num_of_frames_per_second)
model = YourModel()
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    while True:
        ret, frame = cap.read()
        prediction = sess.run(..., frame)
        if cv2.waitKey(1) & 0xFF == ord("q"):
            break
        sleep(1)

Like this, you will read one frame per second and perform inference using that frame as input.

Upvotes: 1

Related Questions