首页 > 解决方案 > 无法在 OpenCV 中应用 tensorflow 模型

问题描述

您好,我在 OpenCV 中应用 tensorflow 模型时遇到问题。下面的代码可以正确加载模型,但是在调用 forward 方法时会抛出 Assertion 错误。你有什么想法问题出在哪里吗?或者如何调试/找到它?

cv::dnn::Net net;
string path;
path = "graph.pb";
net = cv::dnn::readNetFromTensorflow(path);
if (net.empty())
{
    std::cerr << "Can't load network by using the given files." << std::endl;
    return ;
}
Mat image = imread(imagePath)
Mat inputBlob = cv::dnn::blobFromImage(image, 1.0, Size(512, 512), Scalar(0,0,0), true, false);
int N = inputBlob.size[0], C = inputBlob.size[1], H = inputBlob.size[2], W = inputBlob.size[3]; // [1, 3, 512, 512]
net.setInput(inputBlob);        //set the network input
Mat output = net.forward(); // <- throws error

错误

调试断言失败!

程序:C:\Workspace\ImageAnalysisPlus\x64\Debug\opencv_world3410d.dll 文件:C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\vector 行: 1789

表达式:在空向量上调用 back()

有关您的程序如何导致断言失败的信息,请参阅有关断言的 Visual C++ 文档。


OpenCV 版本:4.4.0

张量流模型: https ://drive.google.com/file/d/1aE0smAw-CyPLch6UY8blK3RreT5RrZfN/view ?usp=sharing

平台:Windows 10、Visual Studio 2017

我试图在 python 和 OpenCV 中加载模型并且它可以工作。

提前感谢您的任何建议。

标签: c++tensorflowopencv

解决方案


我不得不承认我没有尝试发布的最小代码。这是一个更大的代码的一部分。最少的代码也适用于我的机器。知道我发现我在未初始化的网络上调用了 forward 方法,这导致了错误。我在类的 init 方法中初始化了局部网络变量,而不是类属性。


推荐阅读