Closed hedeya1980 closed 1 year ago
I have trained a YOLOv7 model on a custom dataset.
As recommended here, I performed 'Reparameterization' on the .pt file, before converting it into .onnx.
I followed your code in main.cpp file in the opencv folder, and just changed the model's .onnx file, and the image file, as follows:
#include <fstream> #include <sstream> #include <iostream> #include <opencv2/dnn.hpp> #include <opencv2/imgproc.hpp> #include <opencv2/highgui.hpp> using namespace cv; using namespace dnn; using namespace std; struct Net_config { float confThreshold; // Confidence threshold float nmsThreshold; // Non-maximum suppression threshold string modelpath; }; class YOLOV7 { public: YOLOV7(Net_config config); void detect(Mat& frame); private: int inpWidth; int inpHeight; vector<string> class_names; int num_class; float confThreshold; float nmsThreshold; Net net; void drawPred(float conf, int left, int top, int right, int bottom, Mat& frame, int classid); }; YOLOV7::YOLOV7(Net_config config) { this->confThreshold = config.confThreshold; this->nmsThreshold = config.nmsThreshold; //this->net = readNetFromONNX(config.modelpath); this->net = readNetFromONNX("yolov7.onnx"); ifstream ifs("coco.names"); string line; while (getline(ifs, line)) this->class_names.push_back(line); this->num_class = class_names.size(); size_t pos = config.modelpath.find("_"); int len = config.modelpath.length() - 6 - pos; string hxw = config.modelpath.substr(pos + 1, len); pos = hxw.find("x"); string h = hxw.substr(0, pos); len = hxw.length() - pos; string w = hxw.substr(pos + 1, len); this->inpHeight = stoi(h); this->inpWidth = stoi(w); } void YOLOV7::drawPred(float conf, int left, int top, int right, int bottom, Mat& frame, int classid) // Draw the predicted bounding box { //Draw a rectangle displaying the bounding box rectangle(frame, Point(left, top), Point(right, bottom), Scalar(0, 0, 255), 2); //Get the label for the class name and its confidence string label = format("%.2f", conf); label = this->class_names[classid] + ":" + label; //Display the label at the top of the bounding box int baseLine; Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine); top = max(top, labelSize.height); //rectangle(frame, Point(left, top - int(1.5 * labelSize.height)), Point(left + int(1.5 * labelSize.width), top + baseLine), Scalar(0, 255, 0), FILLED); putText(frame, label, Point(left, top), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0, 255, 0), 1); } void YOLOV7::detect(Mat& frame) { Mat blob = blobFromImage(frame, 1 / 255.0, Size(this->inpWidth, this->inpHeight), Scalar(0, 0, 0), true, false); this->net.setInput(blob); vector<Mat> outs; this->net.forward(outs, this->net.getUnconnectedOutLayersNames()); int num_proposal = outs[0].size[0]; int nout = outs[0].size[1]; if (outs[0].dims > 2) { num_proposal = outs[0].size[1]; nout = outs[0].size[2]; outs[0] = outs[0].reshape(0, num_proposal); } /////generate proposals vector<float> confidences; vector<Rect> boxes; vector<int> classIds; float ratioh = (float)frame.rows / this->inpHeight, ratiow = (float)frame.cols / this->inpWidth; int n = 0, row_ind = 0; ///cx,cy,w,h,box_score,class_score float* pdata = (float*)outs[0].data; for (n = 0; n < num_proposal; n++) ///ÌØÕ÷ͼ³ß¶È { float box_score = pdata[4]; if (box_score > this->confThreshold) { Mat scores = outs[0].row(row_ind).colRange(5, nout); Point classIdPoint; double max_class_socre; // Get the value and location of the maximum score minMaxLoc(scores, 0, &max_class_socre, 0, &classIdPoint); max_class_socre *= box_score; if (max_class_socre > this->confThreshold) { const int class_idx = classIdPoint.x; float cx = pdata[0] * ratiow; ///cx float cy = pdata[1] * ratioh; ///cy float w = pdata[2] * ratiow; ///w float h = pdata[3] * ratioh; ///h int left = int(cx - 0.5 * w); int top = int(cy - 0.5 * h); confidences.push_back((float)max_class_socre); boxes.push_back(Rect(left, top, (int)(w), (int)(h))); classIds.push_back(class_idx); } } row_ind++; pdata += nout; } // Perform non maximum suppression to eliminate redundant overlapping boxes with // lower confidences vector<int> indices; dnn::NMSBoxes(boxes, confidences, this->confThreshold, this->nmsThreshold, indices); for (size_t i = 0; i < indices.size(); ++i) { int idx = indices[i]; Rect box = boxes[idx]; this->drawPred(confidences[idx], box.x, box.y, box.x + box.width, box.y + box.height, frame, classIds[idx]); } } int main() { Net_config YOLOV7_nets = { 0.3, 0.5, "yolov7.onnx" }; ////choices=["models/yolov7_640x640.onnx", "models/yolov7-tiny_640x640.onnx", "models/yolov7_736x1280.onnx", "models/yolov7-tiny_384x640.onnx", "models/yolov7_480x640.onnx", "models/yolov7_384x640.onnx", "models/yolov7-tiny_256x480.onnx", "models/yolov7-tiny_256x320.onnx", "models/yolov7_256x320.onnx", "models/yolov7-tiny_256x640.onnx", "models/yolov7_256x640.onnx", "models/yolov7-tiny_480x640.onnx", "models/yolov7-tiny_736x1280.onnx", "models/yolov7_256x480.onnx"] YOLOV7 net(YOLOV7_nets); string imgpath = "frame1.png"; Mat srcimg = imread(imgpath); net.detect(srcimg); static const string kWinName = "Deep learning object detection in OpenCV"; namedWindow(kWinName, WINDOW_NORMAL); imshow(kWinName, srcimg); system("pause"); waitKey(0); destroyAllWindows(); }
However, I got the following error:
OpenCV: terminate handler is called! The last OpenCV error is: OpenCV(4.1.1) Error: Unsupported format or combination of formats (Failed to parse onnx model) in cv::dnn::dnn4_v20190621::ONNXImporter::ONNXImporter, file C:\opencv-4.1.1\modules\dnn\src\onnx\onnx_importer.cpp, line 57
Here is a link to my 'yolov7.onnx' file, and here is a link to 'frame1.png'
The model is trained to detect 1 class, which is 'Potholes' in roads.
Currently, I have visual studio 2019, and opencv 4.1.1.
Should I upgrade to another opencv version?
Pls guide me to any possible solutions, so that I can successfully deploy the YOLOv7 model using C++.
The issue was that I wasn't placing the .onnx and the image files in the same folder as the .exe file.
The following is the code in it's final status (but as I said above, the paths of the .onnx file, and the image files should be specified correctly):
#include <fstream>
#include <sstream>
#include <iostream>
#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
using namespace dnn;
using namespace std;
struct Net_config
{
float confThreshold; // Confidence threshold
float nmsThreshold; // Non-maximum suppression threshold
string modelpath;
};
class YOLOV7
{
public:
YOLOV7(Net_config config);
void detect(Mat& frame);
private:
int inpWidth;
int inpHeight;
vector<string> class_names;
int num_class;
float confThreshold;
float nmsThreshold;
Net net;
void drawPred(float conf, int left, int top, int right, int bottom, Mat& frame, int classid);
};
YOLOV7::YOLOV7(Net_config config)
{
this->confThreshold = config.confThreshold;
this->nmsThreshold = config.nmsThreshold;
//this->net = readNetFromONNX(config.modelpath);
this->net = readNetFromONNX("yolov7.onnx");
//ifstream ifs("coco.names");
ifstream ifs("Potholes.names");
string line;
while (getline(ifs, line)) this->class_names.push_back(line);
this->num_class = class_names.size();
this->inpHeight = 640;//stoi(h);
this->inpWidth = 640;//stoi(w);
}
void YOLOV7::drawPred(float conf, int left, int top, int right, int bottom, Mat& frame, int classid) // Draw the predicted bounding box
{
//Draw a rectangle displaying the bounding box
rectangle(frame, Point(left, top), Point(right, bottom), Scalar(0, 0, 255), 2);
//Get the label for the class name and its confidence
string label = format("%.2f", conf);
label = this->class_names[classid] + ":" + label;
//Display the label at the top of the bounding box
int baseLine;
Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
top = max(top, labelSize.height);
//rectangle(frame, Point(left, top - int(1.5 * labelSize.height)), Point(left + int(1.5 * labelSize.width), top + baseLine), Scalar(0, 255, 0), FILLED);
putText(frame, label, Point(left, top), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0, 255, 0), 1);
}
void YOLOV7::detect(Mat& frame)
{
Mat blob = blobFromImage(frame, 1 / 255.0, Size(this->inpWidth, this->inpHeight), Scalar(0, 0, 0), true, false);
this->net.setInput(blob);
vector<Mat> outs;
this->net.forward(outs, this->net.getUnconnectedOutLayersNames());
int num_proposal = outs[0].size[0];
int nout = outs[0].size[1];
if (outs[0].dims > 2)
{
num_proposal = outs[0].size[1];
nout = outs[0].size[2];
outs[0] = outs[0].reshape(0, num_proposal);
}
/////generate proposals
vector<float> confidences;
vector<Rect> boxes;
vector<int> classIds;
float ratioh = (float)frame.rows / this->inpHeight, ratiow = (float)frame.cols / this->inpWidth;
int n = 0, row_ind = 0; ///cx,cy,w,h,box_score,class_score
float* pdata = (float*)outs[0].data;
for (n = 0; n < num_proposal; n++) ///ÌØÕ÷ͼ³ß¶È
{
float box_score = pdata[4];
if (box_score > this->confThreshold)
{
Mat scores = outs[0].row(row_ind).colRange(5, nout);
Point classIdPoint;
double max_class_socre;
// Get the value and location of the maximum score
minMaxLoc(scores, 0, &max_class_socre, 0, &classIdPoint);
max_class_socre *= box_score;
if (max_class_socre > this->confThreshold)
{
const int class_idx = classIdPoint.x;
float cx = pdata[0] * ratiow; ///cx
float cy = pdata[1] * ratioh; ///cy
float w = pdata[2] * ratiow; ///w
float h = pdata[3] * ratioh; ///h
int left = int(cx - 0.5 * w);
int top = int(cy - 0.5 * h);
confidences.push_back((float)max_class_socre);
boxes.push_back(Rect(left, top, (int)(w), (int)(h)));
classIds.push_back(class_idx);
}
}
row_ind++;
pdata += nout;
}
// Perform non maximum suppression to eliminate redundant overlapping boxes with
// lower confidences
vector<int> indices;
dnn::NMSBoxes(boxes, confidences, this->confThreshold, this->nmsThreshold, indices);
for (size_t i = 0; i < indices.size(); ++i)
{
int idx = indices[i];
Rect box = boxes[idx];
this->drawPred(confidences[idx], box.x, box.y,
box.x + box.width, box.y + box.height, frame, classIds[idx]);
}
}
int main()try
{
int img_index = 0;
Net_config YOLOV7_nets = { 0.3, 0.5, "yolov7.onnx" }; ////choices=["models/yolov7_640x640.onnx", "models/yolov7-tiny_640x640.onnx", "models/yolov7_736x1280.onnx", "models/yolov7-tiny_384x640.onnx", "models/yolov7_480x640.onnx", "models/yolov7_384x640.onnx", "models/yolov7-tiny_256x480.onnx", "models/yolov7-tiny_256x320.onnx", "models/yolov7_256x320.onnx", "models/yolov7-tiny_256x640.onnx", "models/yolov7_256x640.onnx", "models/yolov7-tiny_480x640.onnx", "models/yolov7-tiny_736x1280.onnx", "models/yolov7_256x480.onnx"]
YOLOV7 net(YOLOV7_nets);
while (img_index <= 822)
{
string base_path = "D:/Post_Grad/STDF/Depth_estimation-master/workspace/test_vid/pngFrames/frame";
//string imgpath = "frame1.png";
string imgpath = base_path + to_string(img_index) + ".png";
Mat srcimg = imread(imgpath);
net.detect(srcimg);
static const string kWinName = "Deep learning object detection in OpenCV";
namedWindow(kWinName, WINDOW_NORMAL);
imshow(kWinName, srcimg);
waitKey(1);
img_index++;
}
destroyAllWindows();
}
catch (const std::exception& e)
{
std::cerr << e.what() << std::endl;
system("pause");
return EXIT_FAILURE;
}
你的opencv版本是4.1.1的,需要升级到opencv4.5以上的才能成功加载onnx文件的
你的opencv版本是4.1.1的,需要升级到opencv4.5以上的才能成功加载onnx文件的
Thanks a lot @hpc203 for your reply. I forgot to say that I upgraded to opencv 4.6.0. Thanks a lot for this valuable repository and for your reply.
你的opencv版本是4.1.1的,需要升级到opencv4.5以上的才能成功加载onnx文件的
您好,想问下如果项目限制死了opencv4.4.0版本需要做那些改动呢,以下为报错信息,感觉是upsample导致的
what(): 0penCV(4.4.0)_/home/***/opencv-4.4.0/modules/dnn/src/onnx/onnx importer.cpp:1410:error: (-2:Unspecified error) in function 'void cv::dnn::dnn4_v20200609::ONNXImporter::populateNet(cv::dnn::dnn4_v20200609::Net)'
(expected: 'shapes.depth() == CV_32S'), where 'shapes.depth()' is 5 (CV_32FC1) must be equal to 'CV_32S' is 4 (CV_32SC1)
I have trained a YOLOv7 model on a custom dataset.
As recommended here, I performed 'Reparameterization' on the .pt file, before converting it into .onnx.
I followed your code in main.cpp file in the opencv folder, and just changed the model's .onnx file, and the image file, as follows:
However, I got the following error:
Here is a link to my 'yolov7.onnx' file, and here is a link to 'frame1.png'
The model is trained to detect 1 class, which is 'Potholes' in roads.
Currently, I have visual studio 2019, and opencv 4.1.1.
Should I upgrade to another opencv version?
Pls guide me to any possible solutions, so that I can successfully deploy the YOLOv7 model using C++.