3

There are many online tutorial to guide how to predict one image for one time in Keras using pretrained model. For my case, I use VGG16 model in Keras, I need to predict images continuously, so I use for loop to load images, then pass it to predict function, it works well, but one prediction time is too long(~800ms in my machine, CPU only), here is code:

    
    # one full prediction function cost 800ms
    def predict(image):
        image = img_to_array(image)
        image = image / 255
        image = np.expand_dims(image, axis=0)
        # build the VGG16 network, this line code cost 400~500ms
        model = keras.applications.VGG16(include_top=True, weights='imagenet')
        # Do predication
        prediction = model.predict(image)
        '''
        Process predication results
        '''

    '''
    some preprocess
    '''
    for img in imgs_list:
        predict(img)

The above code can work well, but it spend too much time for each prediction, whole function need 800ms, and build VGG network need 500ms, it cost too much. I want to remove this 500ms for each prediction for continuous predicting mode.

I try to put "model = keras.applications.VGG16(include_top=True, weights='imagenet')" this line code to the outside of predict function, define it globally or pass "model" as argument to function, but program will return error and end after first successful prediction.

Traceback (most recent call last):
  File "/home/zi/venv/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1075, in _run
    subfeed, allow_tensor=True, allow_operation=False)
  File "/home/zi/venv/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 3590, in as_graph_element
    return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
  File "/home/zi/venv/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 3669, in _as_graph_element_locked
    raise ValueError("Tensor %s is not an element of this graph." % obj)
ValueError: Tensor Tensor("input_1:0", shape=(?, ?, ?, 3), dtype=float32) is not an element of this graph.
During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "multi_classifier.py", line 256, in 
    predict(current_file_path)
  File "multi_classifier.py", line 184, in predict
    bottleneck_prediction = model_1.predict(image)
  File "/home/zi/venv/lib/python3.5/site-packages/keras/engine/training.py", line 1835, in predict
    verbose=verbose, steps=steps)
  File "/home/zi/venv/lib/python3.5/site-packages/keras/engine/training.py", line 1331, in _predict_loop
    batch_outs = f(ins_batch)
  File "/home/zi/venv/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py", line 2482, in __call__
    **self.session_kwargs)
  File "/home/zi/venv/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 900, in run
    run_metadata_ptr)
  File "/home/zi/venv/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1078, in _run
    'Cannot interpret feed_dict key as Tensor: ' + e.args[0])
TypeError: Cannot interpret feed_dict key as Tensor: Tensor Tensor("input_1:0", shape=(?, ?, ?, 3), dtype=float32) is not an element of this graph.


It seems like that I need to instantiate a VGG model for every prediction, how do I change code to save the model build time? Thanks.

stop-cran
  • 4,229
  • 2
  • 30
  • 47
zcsd
  • 31
  • 2
  • It could be more helpful if you share your GitHub repo or whole code. The code looks fine here. Maybe there is some error in Tensor Shape. – Madhi Jul 17 '18 at 09:24

0 Answers0