OpenCV(4.5.5) 错误:cv::dnn::dnn4_v20211220::Net::forward C++ 中断言失败 (!empty())
我最近创建了自己的 TensorFlow 对象检测模型。当我将模型加载到 OpenCV 的 DNN 中时,它在 net.forward() 处出现错误。我不知道是模型训练不当还是我设置的 DNN 模型不正确。
我尝试过其他模型,例如咖啡模型,这似乎有效。
代码 :
#include <iostream>
#include <fstream>
#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>
#include <opencv2/dnn/all_layers.hpp>
using namespace std;
using namespace cv;
using namespace dnn;
int main(int, char**) {
string file_path = "C:/Users/Daniel/source/repos/AR PROJECT/AR PROJECT/TF/";
vector<string> class_names;
ifstream ifs(string(file_path + "class.txt").c_str());
string line;
// Load in all the classes from the file
while (getline(ifs, line))
{
cout << line << endl;
class_names.push_back(line);
}
// Read in the neural network from the files
auto net = readNetFromTensorflow("F:/models-master/research/object_detection/output_inference_graph/frozen_inference_graph.pb",
"F:/models-master/research/object_detection/training/object-detection.pbtxt");
// Open up the webcam
VideoCapture cap(1);
// Run on either CPU or GPU
net.setPreferableBackend(DNN_BACKEND_CUDA);
net.setPreferableTarget(DNN_TARGET_CUDA_FP16);
// Set a min confidence score for the detections
float min_confidence_score = 0.5;
// Loop running as long as webcam is open and "q" is not pressed
while (cap.isOpened()) {
// Load in an image
Mat image;
bool isSuccess = cap.read(image);
// Check if image is loaded in correctly
if (!isSuccess){
cout << "Could not load the image!" << endl;
break;
}
int image_height = image.cols;
int image_width = image.rows;
auto start = getTickCount();
// Create a blob from the image
Mat blob = blobFromImage(image, 1.0, Size(300, 300));
// Set the blob to be input to the neural network
net.setInput(blob);
// Forward pass of the blob through the neural network to get the predictions
Mat output = net.forward();
auto end = getTickCount();
// Matrix with all the detections
Mat results(output.size[2], output.size[3], CV_32F, output.ptr<float>());
imshow("image", image);
int k = waitKey(10);
if (k == 113){
break;
}
}
cap.release();
destroyAllWindows();
}
I recently created my own TensorFlow Object Detection Model. When I load the model into OpenCV's DNN it has an error at net.forward(). I don't know if the model is improperly trained or if I setup the DNN model incorrectly.
I have tried other models such as a caffe model and that seemed to work.
Code :
#include <iostream>
#include <fstream>
#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>
#include <opencv2/dnn/all_layers.hpp>
using namespace std;
using namespace cv;
using namespace dnn;
int main(int, char**) {
string file_path = "C:/Users/Daniel/source/repos/AR PROJECT/AR PROJECT/TF/";
vector<string> class_names;
ifstream ifs(string(file_path + "class.txt").c_str());
string line;
// Load in all the classes from the file
while (getline(ifs, line))
{
cout << line << endl;
class_names.push_back(line);
}
// Read in the neural network from the files
auto net = readNetFromTensorflow("F:/models-master/research/object_detection/output_inference_graph/frozen_inference_graph.pb",
"F:/models-master/research/object_detection/training/object-detection.pbtxt");
// Open up the webcam
VideoCapture cap(1);
// Run on either CPU or GPU
net.setPreferableBackend(DNN_BACKEND_CUDA);
net.setPreferableTarget(DNN_TARGET_CUDA_FP16);
// Set a min confidence score for the detections
float min_confidence_score = 0.5;
// Loop running as long as webcam is open and "q" is not pressed
while (cap.isOpened()) {
// Load in an image
Mat image;
bool isSuccess = cap.read(image);
// Check if image is loaded in correctly
if (!isSuccess){
cout << "Could not load the image!" << endl;
break;
}
int image_height = image.cols;
int image_width = image.rows;
auto start = getTickCount();
// Create a blob from the image
Mat blob = blobFromImage(image, 1.0, Size(300, 300));
// Set the blob to be input to the neural network
net.setInput(blob);
// Forward pass of the blob through the neural network to get the predictions
Mat output = net.forward();
auto end = getTickCount();
// Matrix with all the detections
Mat results(output.size[2], output.size[3], CV_32F, output.ptr<float>());
imshow("image", image);
int k = waitKey(10);
if (k == 113){
break;
}
}
cap.release();
destroyAllWindows();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过将tensorflow SavedModel 转换为.onnx 使其工作。
这对我有用:
--saved-model
:是保存模型的文件夹而不是 .pb 文件。--opset
参数是可选的,但是对于我使用 opencv 4.6 来说,版本 11 工作正常,而版本 15> 则工作正常。没有。在opencv-python中:
https://github.com/onnx/tensorflow-onnx
I got it working by converting tensorflow
SavedModel
to .onnx.That is what worked for me:
--saved-model
: is the folder of saved model not the .pb file.--opset
parameter is optional, however for me with opencv 4.6., version 11 worked fine, while version 15> did not.In opencv-python:
https://github.com/onnx/tensorflow-onnx