OlafenwaMoses / ImageAIDocumentation

Official English Documentation of ImageAI
MIT License
23 stars 30 forks source link

Need help to re-use the loaded model #2

Open zithiat opened 5 years ago

zithiat commented 5 years ago

Hi,

I'm using your amazing code, and able to make a prediction. However, I cannot re-use the loaded model from global scope. It told me to define the image path.

If I load model each request, the 1st request passed, and the 2nd request failed: [2019-06-19 00:04:23,160] ERROR in app: Exception on /detection [POST] Traceback (most recent call last): File "/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1092, in _run subfeed, allow_tensor=True, allow_operation=False) File "/opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3478, in as_graph_element return self._as_graph_element_locked(obj, allow_tensor, allow_operation) File "/opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3557, in _as_graph_element_locked raise ValueError("Tensor %s is not an element of this graph." % obj) ValueError: Tensor Tensor("Placeholder:0", shape=(7, 7, 3, 64), dtype=float32) is not an element of this graph. During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/opt/conda/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app response = self.full_dispatch_request() File "/opt/conda/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request rv = self.handle_user_exception(e) File "/opt/conda/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception reraise(exc_type, exc_value, tb) File "/opt/conda/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise raise value File "/opt/conda/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request rv = self.dispatch_request() File "/opt/conda/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "/notebook/flask_app/App_fruit_detection.py", line 38, in dectect detector.loadModel(detection_speed="fast") File "/opt/conda/lib/python3.6/site-packages/imageai/Detection/__init__.py", line 185, in loadModel model.load_weights(self.modelPath) File "/opt/conda/lib/python3.6/site-packages/Keras-2.2.4-py3.6.egg/keras/engine/network.py", line 1166, in load_weights f, self.layers, reshape=reshape) File "/opt/conda/lib/python3.6/site-packages/Keras-2.2.4-py3.6.egg/keras/engine/saving.py", line 1059, in load_weights_from_hdf5_group K.batch_set_value(weight_value_tuples) File "/opt/conda/lib/python3.6/site-packages/Keras-2.2.4-py3.6.egg/keras/backend/tensorflow_backend.py", line 2470, in batch_set_value get_session().run(assign_ops, feed_dict=feed_dict) File "/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 929, in run run_metadata_ptr) File "/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1095, 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=(7, 7, 3, 64), dtype=float32) is not an element of this graph. 209.152.96.166 - - [19/Jun/2019 00:04:23] "POST /detection HTTP/1.1" 500 -

I use global, it also prints error: Running on http://0.0.0.0:5000/ (Press CTRL+C to quit) /notebook/flask_app/upload/car_1560902791.jpg /notebook/flask_app/output/car_1560902791.jpg [2019-06-19 00:06:31,660] ERROR in app: Exception on /detection [POST] Traceback (most recent call last): File "/opt/conda/lib/python3.6/site-packages/imageai/Detection/__init__.py", line 327, in detectObjectsFromImage _, _, detections = model.predict_on_batch(np.expand_dims(image, axis=0)) File "/opt/conda/lib/python3.6/site-packages/Keras-2.2.4-py3.6.egg/keras/engine/training.py", line 1273, in predict_on_batch self._make_predict_function() File "/opt/conda/lib/python3.6/site-packages/Keras-2.2.4-py3.6.egg/keras/engine/training.py", line 554, in _make_predict_function **kwargs) File "/opt/conda/lib/python3.6/site-packages/Keras-2.2.4-py3.6.egg/keras/backend/tensorflow_backend.py", line 2744, in function return Function(inputs, outputs, updates=updates, **kwargs) File "/opt/conda/lib/python3.6/site-packages/Keras-2.2.4-py3.6.egg/keras/backend/tensorflow_backend.py", line 2546, in __init__ with tf.control_dependencies(self.outputs): File "/opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 5028, in control_dependencies return get_default_graph().control_dependencies(control_inputs) File "/opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 4528, in control_dependencies c = self.as_graph_element(c) File "/opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3478, in as_graph_element return self._as_graph_element_locked(obj, allow_tensor, allow_operation) File "/opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3557, in _as_graph_element_locked raise ValueError("Tensor %s is not an element of this graph." % obj) ValueError: Tensor Tensor("regression/concat:0", shape=(?, ?, 4), dtype=float32) is not an element of this graph. During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/opt/conda/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app response = self.full_dispatch_request() File "/opt/conda/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request rv = self.handle_user_exception(e) File "/opt/conda/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception reraise(exc_type, exc_value, tb) File "/opt/conda/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise raise value File "/opt/conda/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request rv = self.dispatch_request() File "/opt/conda/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "/notebook/flask_app/App_fruit_detection.py", line 40, in dectect detections = detector.detectObjectsFromImage(input_image=inputname, output_image_path=outputname) File "/opt/conda/lib/python3.6/site-packages/imageai/Detection/__init__.py", line 517, in detectObjectsFromImage raise ValueError("Ensure you specified correct input image, input type, output type and/or output image path ") ValueError: Ensure you specified correct input image, input type, output type and/or output image path

My code is below:

import base64
from flask import Flask, request
from PIL import Image
from io import BytesIO
from imageai.Detection import ObjectDetection
import os, json

app = Flask(__name__)
#counter = 0

#Global mode object
detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath("/notebook/fruit-detection/resnet50_coco_best_v2.0.1.h5")
detector.loadModel(detection_speed="fast")

@app.route('/detection', methods=['POST'])
def dectect():
    # Request
    data = request.get_json(force=True)
    # Content of the image, Base64 format
    picture = data["image"]
    # This is input image name
    inputname = '/notebook/flask_app/upload/' + data["name"] + '.jpg'
    # This is for the response
    outputname = '/notebook/flask_app/output/' + data["name"] + '.jpg'
    # Decoding from Base64
    im = Image.open(BytesIO(base64.b64decode(picture)))
    # Save input image into JPEG format
    im.save(inputname, 'JPEG')
    print(inputname)
    print(outputname)

    # Detect and save the result as image
    #detector = ObjectDetection()
    #detector.setModelTypeAsRetinaNet()
    #detector.setModelPath("/notebook/fruit-detection/resnet50_coco_best_v2.0.1.h5")
    #detector.loadModel(detection_speed="fast")
    global detector
    detections = detector.detectObjectsFromImage(input_image=inputname, output_image_path=outputname)

    print(detections)
    detectionlist = []
    for eachObject in detections:
        dict={}        
        dict["name"] = eachObject["name"]
        dict["percentage_probability"] = eachObject["percentage_probability"]
        detectionlist.append(dict)
    print(detectionlist)

    with open(outputname, "rb") as img_file:
        imageString = base64.b64encode(img_file.read())

    outputdata = {}
    outputdata['image'] = str(imageString, 'utf-8')
    outputdata['content'] = detectionlist
    print(outputdata)
    result = json.dumps(outputdata)
    #del detector, detections, im, inputname, outputname, picture, data
    return result

@app.route('/', methods=['GET','POST'])
@app.route('/test', methods=['GET','POST'])
def test():
    #global counter
    #print('Counter: ', counter)
    #counter += 1
    return "OK"

if __name__ == '__main__':
    app.run(host='0.0.0.0')
bharath5673 commented 5 years ago
210     model_config = json.loads(model_config.decode('utf-8'))
211     model = model_config_lib.model_from_config(model_config,

ValueError: No model found in config file.``

please help... im not able to reuse :(((