首页 > 解决方案 > 为什么我的 Mask R-CNN 模型在 Flask 中不起作用

问题描述

我使用了来自:https ://github.com/matterport/Mask_RCNN 的 Mask RCNN并添加了我自己的自定义类。它可以通过 Pycharm 正常工作——无论是输入图像还是通过 OpenCV 使用我的网络摄像头。

当我通过 Flask 部署它时,虽然一切正常,但模型检测到 0 或 100 个类——即使图像中有大约 25 个正实例。当这些掩码被绘制时,它们只是随机的彩色点,它们去的地方没有图案。(可视化功能在非 Flask 脚本上运行良好,所以我很有信心不是这样)。

我相信这与张量流图有关,但由于我使用的是 TF2.6 和 Python 3.8.5,它似乎比 TF 1.X 中的要困难得多

def gen(video):
    weights_path = "ModelWeights/mask_rcnn_culture_0010.h5"

    model = modellib.MaskRCNN(mode="inference", model_dir=weights_path, config=InferenceConfig())
    model.load_weights(weights_path, by_name=True)

    #########
    graph1 = Graph()
    with graph1.as_default():
        session1 = tf.compat.v1.Session(graph=graph1)
        with session1.as_default():
    #########
            while True:
                success, image = video.read()

                # If ESC pressed, stop the loop
                if keyboard.is_pressed('esc'):
                    # ESC pressed
                    print("Escape hit, closing...")
                    break

                # If Space pressed, take a photo and process.
                if keyboard.is_pressed('space'):
                    # OpenCV returns images as BGR, convert to RGB
                    image = image[..., ::-1]
                    # Detect objects
                    r = model.detect([image], verbose=0)[0]

在阅读了一些 SO 帖子后,我在 ##### 之间添加了代码,但它们是关于从图像或文本目录获取输入数据的香草 keras 模型。该image = image[... 行工作正常,后面的代码r = model.detect...也工作正常,它只是一些关于何时显示图像或根据用户键盘输入中断的条件语句。

我尝试过的事情: tf.compat.v1.get_default_graphtf.compat.v1.disable_eager_execution()

上述所有 3 次尝试都会导致此错误:ValueError: Tensor Tensor("mrcnn_detection/Reshape_1:0", shape=(1, 100, 6), dtype=float32, device=/device:CPU:0) is not an element of this graph..

谢谢。

标签: pythonopencvflask

解决方案


推荐阅读