experiencor / keras-yolo2

Easy training on custom dataset. Various backends (MobileNet and SqueezeNet) supported. A YOLO demo to detect raccoon run entirely in brower is accessible at https://git.io/vF7vI (not on Windows).
MIT License
1.73k stars 784 forks source link

Stop/Start Session #356

Closed andreassilva closed 6 years ago

andreassilva commented 6 years ago

Hi there,

I have a little Kivy app which has a button to start and stop reading frames from a camera and then predict from them.

The program runs fine on the first time, but when I start the prediction the second time I get the following error: Note: on each start I init YOLO

... File "...../venv/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 900, in run run_metadata_ptr) File "...../venv/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1079, in _run 'Cannot interpret feed_dict key as Tensor: ' + e.args[0]) TypeError: Cannot interpret feed_dict key as Tensor: Tensor Tensor("Placeholder:0", shape=(3, 3, 3, 32), dtype=float32) is not an element of this graph.

Assuming that my problem is that I did not close the previous session, how do I do that with keras-yolo2?

Regards,

rodrigo2019 commented 6 years ago

Are you using threads? I think you must keep the same graphs for all inferences, or you will need to load the network everytime. If you are using threads you can pass a reference from actual session to thread function in order to keep the same session inside the thread.

something like this:

foo(net,session, **kwargs): 
    with session:
        net.predict(predict_something)
andreassilva commented 6 years ago

Hi thank you for your response.

I am using threads. I don't mind loading the network on each start, in fact I prefer that way. My problem is that I'm not being able to load it on the second start.

The following code only runs fine on the first start.

 yolo = YOLO(backend                = yolo_config['model']['backend'],
                input_size          = yolo_config['model']['input_size'],
                labels              = yolo_config['model']['labels'],
                max_box_per_image   = yolo_config['model']['max_box_per_image'],
                anchors             = yolo_config['model']['anchors'])

What and how do I free the previous one, so I can init yolo again?

rodrigo2019 commented 6 years ago
import tensorflow as tf

yolo = YOLO(backend                = yolo_config['model']['backend'],
                input_size          = yolo_config['model']['input_size'],
                labels              = yolo_config['model']['labels'],
                max_box_per_image   = yolo_config['model']['max_box_per_image'],
                anchors             = yolo_config['model']['anchors'])
graph = tf.get_default_graph()  

def foo(yolo,graph):
    with graph.as_default():
        yolo.predict(example_image)

I did something like this in my code, doing that I can pass the foo function into a thread and use the model without problems

andreassilva commented 6 years ago

I solved it by calling

from keras import backend as K
...
K.clear_session()
walterxtx commented 3 years ago

hi andreassilva can you leave your code here ?