Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用.onnx模型的OpenCV(Ptyhon)DNN模块中的正向方法错误_Python_Opencv_Conv Neural Network - Fatal编程技术网

Python 使用.onnx模型的OpenCV(Ptyhon)DNN模块中的正向方法错误

Python 使用.onnx模型的OpenCV(Ptyhon)DNN模块中的正向方法错误,python,opencv,conv-neural-network,Python,Opencv,Conv Neural Network,我想测试从这里下载的预训练模型,以执行ocr任务,它的名字是CRNN_VGG_BiLSTM_CTC.onnx。此模型是从中提取的。sample-image.png可以从下载(参见下面的代码) 当我对blob中的神经网络进行正向预测(ocr)时,我得到以下错误: 错误:OpenCV(4.4.0)/tmp/pip-req-build-xgme2194/OpenCV/modules/dnn/src/layers/convolution\u layer.cpp:348:错误:(-215:断言失败)函数“

我想测试从这里下载的预训练模型,以执行ocr任务,它的名字是CRNN_VGG_BiLSTM_CTC.onnx。此模型是从中提取的。sample-image.png可以从下载(参见下面的代码)

当我对blob中的神经网络进行正向预测(ocr)时,我得到以下错误:

错误:OpenCV(4.4.0)/tmp/pip-req-build-xgme2194/OpenCV/modules/dnn/src/layers/convolution\u layer.cpp:348:错误:(-215:断言失败)函数“getMemoryShapes”中的ngroups>0&&inpCn%ngroups==0&&outCn%ngroups==0

请随意阅读下面的代码。我尝试了很多东西,这很奇怪,因为这个模型不需要预先确定的输入形状。如果您知道如何阅读此模型并进行转发,这也会很有帮助,但我宁愿使用OpenCV解决

import cv2 as cv
# The model is downloaded from here https://drive.google.com/drive/folders/1cTbQ3nuZG-EKWak6emD_s8_hHXWz7lAr
# model path 
modelRecognition = os.path.join(MODELS_PATH,'CRNN_VGG_BiLSTM_CTC.onnx')
# read net 
recognizer = cv.dnn.readNetFromONNX(modelRecognition)

# Download sample_image.png from https://i.ibb.co/fMmCB7J/sample-image.png  (image host website)
sample_image = cv.imread('sample-image.png')
# Height , Width and number of channels of the image
H, W, C = sample_image.shape

# Create a 4D blob from cropped image
blob = cv.dnn.blobFromImage(sample_image, size = (H, W))

recognizer.setInput(blob)

# Here is where i get the errror that I mentioned before 
result = recognizer.forward()

提前非常感谢。

您的问题实际上是,您提供给模型的输入数据与模型训练时使用的数据形状不匹配

我曾经检查过你的onnx模型,它似乎希望输入shape
(1,1,32,100)
。我修改了您的代码,将图像的形状改为
1 x 32 x 100
像素,推理实际上运行时没有错误

编辑 我添加了一些代码来解释推断的结果。现在显示图像和推断的OCR文本。 这似乎不起作用,但在阅读中,应该有两种模式:

  • 一种检测图像中文本位置的方法。此网络接受各种大小的图像,它返回图像中文本的位置,然后将图像的裁剪部分(大小为100x32)传递给第二个网络
  • 一个实际执行“读取”和给定图像补丁的程序返回字符。为此,有一个文件
    alphabet_36.txt
    ,与预先训练的模型一起提供
  • 我不清楚使用哪个网络进行文本检测。希望下面编辑的代码能帮助您进一步开发应用程序

    import cv2 as cv
    import os
    import numpy as np
    import matplotlib.pyplot as plt
    # The model is downloaded from here https://drive.google.com/drive/folders/1cTbQ3nuZG-EKWak6emD_s8_hHXWz7lAr
    # model path 
    MODELS_PATH = './'
    modelRecognition = os.path.join(MODELS_PATH,'CRNN_VGG_BiLSTM_CTC.onnx')
    
    # read net 
    recognizer = cv.dnn.readNetFromONNX(modelRecognition)
    
    # Download sample_image.png from https://i.ibb.co/fMmCB7J/sample-image.png  (image host website)
    sample_image = cv.imread('sample-image.png', cv.IMREAD_GRAYSCALE)
    sample_image = cv.resize(sample_image, (100, 32))
    sample_image = sample_image[:,::-1].transpose()
    
    # Height and Width of the image
    H,W = sample_image.shape
    
    # Create a 4D blob from image
    blob = cv.dnn.blobFromImage(sample_image, size=(H,W))
    recognizer.setInput(blob)
    
    # network inference
    result = recognizer.forward()
    
    # load alphabet
    with open('alphabet_36.txt') as f:
        alphabet = f.readlines()
    alphabet = [f.strip() for f in alphabet]
    
    # interpret inference results
    res = []
    for i in range(result.shape[0]):
        ind = np.argmax(result[i,0])
        res.append(alphabet[ind])
    ocrtxt = ''.join(res)
    
    # show image and detected OCR characters
    plt.imshow(sample_image)
    plt.title(ocrtxt)
    plt.show()
    
    希望能有帮助。
    干杯

    你好,克里斯汀,非常感谢你的回答。我很有信心,这个网络的架构不需要固定的输入大小(因为其中包含卷积运算),但我错了。这样的错误!你肯定解决了我发布的问题,因此应该得到正确的答案。然而,这个网络是用于OCR模型的,你知道为什么调整到100,32会有意义吗?我认为这种ocr模式适用于文字。但也许这个模型只适用于角色,它需要的是以前的分割模型?如果你想分享你的想法,那将非常有用。提前非常感谢!你好,汤姆,不客气!嗯,我不熟悉这个特定的网络(你有它的架构吗,在某个地方,从一篇论文或者其他地方),但是如果它在卷积层之后有完全连接的层,那么网络实际上取决于输入的形状。关于调整大小是否有意义,这是一个好问题。如果调整大小后的角色大小与训练时的角色大小大致相同,这是有意义的。如果没有,网络可能很难返回正确的字符。嗨,汤姆,在我的答案中看到我的编辑,我希望它能进一步帮助你