python - 为什么我的 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_graph
,tf.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.
.
谢谢。
解决方案
推荐阅读
- python - 使用线程的 Python 重复 IP 扫描
- ios - 向上缩放 UIVIew
- javascript - 无法从 redux 存储中存储和检索状态
- java - 定义为 JSON 接口的字段的 JAXB/Moxy 编组
- sql - SQL 获取 svn 修订版
- python - 在python中将dict转换为json时出错
- laravel - 我想重新加载我的侧边栏组件(vue js + laravel)
- bash - 我的 .sh 将配置值视为命令
- docker - 如何将服务名称解析为超级账本作曲家的 docker swarm 模式?
- web-testing - Visual Studio-Web 测试失败并显示消息“在测试上下文中找不到上下文参数'$HIDDEN1.NAP'”并且 Web 测试重定向到登录页面