在getMemoryShapes中,OpenCV DNN断言失败

时间:2018-01-16 18:21:26

标签: python-3.x opencv object-detection

我已根据他们的指导使用Tensorflow的对象检测API训练了一个模型。我还生成了用于部署的文件,并尝试对图像进行分类。 OpenCV的DNN文件可以使用readNetFromTensorflow加载图形而不会出错,但是当我尝试设置网络输入并调用.forward()时,它会给出以下错误。我已经搜索过低和高的解决方案但是我无法找到任何指向正确方向的东西。任何帮助将不胜感激。

[INFO] loading model...
[INFO] starting video stream...
[ INFO:0] Initialize OpenCL runtime...
OpenCV Error: Assertion failed (int(numPriors * _numClasses) == inputs[1][1]) in getMemoryShapes, file /home/pi/opencv/modules/dnn/src/layers/detection_output_layer.cpp, line 202
Traceback (most recent call last):
  File "real_time_object_detection.py", line 68, in <module>
    detections = net.forward()
cv2.error: /home/pi/opencv/modules/dnn/src/layers/detection_output_layer.cpp:202: error: (-215) int(numPriors * _numClasses) == inputs[1][1] in function getMemoryShapes

1 个答案:

答案 0 :(得分:0)

我一直面临着完全相同的错误。诀窍是使用此脚本tf_text_graph_ssd来转换您用于训练的pipeline.config

python3 tf_text_graph_ssd.py \
--input=<path to the frozen inference graph> \
--config=<path to the pipeline.config used for training> \
--output=<output file>

然后使用opencv创建网络

net = cv2.dnn.readNetFromTensorflow(config=<path to generated file>, model=<path to frozen inference graph>)

并推断网络

image = cv2.imread('som image file')
blob = cv2.dnn.blobFromImage(image=image, scalefactor=1.0, size=(300, 300))
net.setInput(blob)
detections = net.forward()

检查size=(300, 300)元组,此大小来自pipeline.config文件

model {
  ssd {
    num_classes: 90
    image_resizer {
      fixed_shape_resizer {
        height: 300
        width: 300
      }
    }
...

还请检查this topic以获得更多信息。