ValueError: Tensor Tensor("mrcnn_detection/Reshape_1:0", shape=(1, 400, 6), dtype=float32) is not an

在使用mask-rcnn配合flask使用的时候出现了以下错误:

Traceback (most recent call last):
  File "/home/ubuntu/anaconda3/envs/tensorflow1.3/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/ubuntu/anaconda3/envs/tensorflow1.3/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/ubuntu/anaconda3/envs/tensorflow1.3/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/ubuntu/anaconda3/envs/tensorflow1.3/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/home/ubuntu/anaconda3/envs/tensorflow1.3/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/ubuntu/anaconda3/envs/tensorflow1.3/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/ubuntu/DNN_Projection/Mask_RCNN-Producting/manager (copy).py", line 389, in postdata
    r = model.detect([srcimg], verbose=0)[0]
  File "/home/ubuntu/DNN_Projection/Mask_RCNN-Producting/mrcnn/model.py", line 2525, in detect
    self.keras_model.predict([molded_images, image_metas, anchors], verbose=0)
  File "/home/ubuntu/anaconda3/envs/tensorflow1.3/lib/python3.6/site-packages/keras/engine/training.py", line 1710, in predict
    self._make_predict_function()
  File "/home/ubuntu/anaconda3/envs/tensorflow1.3/lib/python3.6/site-packages/keras/engine/training.py", line 999, in _make_predict_function
    **kwargs)
  File "/home/ubuntu/anaconda3/envs/tensorflow1.3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 2297, in function
    return Function(inputs, outputs, updates=updates, **kwargs)
  File "/home/ubuntu/anaconda3/envs/tensorflow1.3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 2246, in __init__
    with tf.control_dependencies(self.outputs):
  File "/home/ubuntu/anaconda3/envs/tensorflow1.3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3936, in control_dependencies
    return get_default_graph().control_dependencies(control_inputs)
  File "/home/ubuntu/anaconda3/envs/tensorflow1.3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3665, in control_dependencies
    c = self.as_graph_element(c)
  File "/home/ubuntu/anaconda3/envs/tensorflow1.3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2708, in as_graph_element
    return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
  File "/home/ubuntu/anaconda3/envs/tensorflow1.3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2787, in _as_graph_element_locked
    raise ValueError("Tensor %s is not an element of this graph." % obj)
ValueError: Tensor Tensor("mrcnn_detection/Reshape_1:0", shape=(1, 400, 6), dtype=float32) is not an element of this graph.

对应代码段:

config = NucleusInferenceConfig()
model = modellib.MaskRCNN(mode="inference", config=config, model_dir="./logs")
model.load_weights("./data/weights/RBC_PLT.h5", by_name=True)


@app.route('/postdata', methods=['POST'])
def postdata():
    initialData()
    # Read dataset
    dataset = NucleusDataset()
    dataset.load_nucleus("./data/cell", "stage1_test")
    dataset.prepare()
    # Detect objects
    srcimg = cv2.imread(uploadPath)
    r = model.detect([srcimg], verbose=0)[0]
    # Save image with masks
    cls_num = visualize.display_instances(
        srcimg, r['rois'], r['masks'], r['class_ids'],
        dataset.class_names, r['scores'],
        show_bbox=False, show_mask=False,
        title="Predictions")
    print(cls_num)
    global resPath
    resPath = "/static/resImg/" + str(uuid.uuid1()) + ".jpg"
    plt.savefig("." + resPath)

具体是什么原因我不是很理解,但是网上的参考意思是要在全局区域加上检测语句,我这里对应的是model.detect

所以在model.load_weights("./data/weights/RBC_PLT.h5", by_name=True)后面加上

im = cv2.imread("./static/srcImg/1.jpg")
r = model.detect([im], verbose=0)[0]

参考链接:http://www.bubuko.com/infodetail-2576311.html

你可能感兴趣的:(tf-mask-rcnn)