Skip to content

Commit 5da3704

Browse files
committed
get class-length from output-size instead of class_names.length
1 parent cc89fe3 commit 5da3704

File tree

4 files changed

+13
-9
lines changed

4 files changed

+13
-9
lines changed

yolov5.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,16 @@ bool Yolov5::Detect(Mat& SrcImg, Net& net, vector<OutputSeg>& output) {
5252
std::vector<cv::Rect> boxes;//每个id矩形框
5353
float ratio_h = (float)netInputImg.rows / _netHeight;
5454
float ratio_w = (float)netInputImg.cols / _netWidth;
55-
int net_width = _className.size() + 5; //输出的网络宽度是类别数+5
56-
int net_out_width = netOutputImg[0].size[2];
57-
assert(net_out_width == net_width, "Error Wrong number of _className"); //模型类别数目不对
55+
int net_width = netOutputImg[0].size[2]; //输出的网络宽度是类别数+5
56+
//int net_out_width = netOutputImg[0].size[2];
57+
//assert(net_out_width == net_width, "Error Wrong number of _className"); //模型类别数目不对
5858
float* pdata = (float*)netOutputImg[0].data;
5959
int net_height = netOutputImg[0].size[1];
60+
int score_length = net_width - 5;
6061
for (int r = 0; r < net_height; ++r) {
6162
float box_score = pdata[4]; ;//获取每一行的box框中含有某个物体的概率
6263
if (box_score >= _classThreshold) {
63-
cv::Mat scores(1, _className.size(), CV_32FC1, pdata + 5);
64+
cv::Mat scores(1, score_length, CV_32FC1, pdata + 5);
6465
Point classIdPoint;
6566
double max_class_socre;
6667
minMaxLoc(scores, 0, &max_class_socre, 0, &classIdPoint);

yolov5_onnx.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,15 @@ bool Yolov5Onnx::OnnxBatchDetect(std::vector<cv::Mat>& srcImgs, std::vector<std:
198198
int64_t one_output_length = VectorProduct(_outputTensorShape) / _outputTensorShape[0];
199199
int net_width = _outputTensorShape[2];
200200
int net_height = _outputTensorShape[1];
201+
int score_length = net_width - 5;
201202
for (int img_index = 0; img_index < srcImgs.size(); ++img_index) {
202203
std::vector<int> class_ids;//结果id数组
203204
std::vector<float> confidences;//结果每个id对应置信度数组
204205
std::vector<cv::Rect> boxes;//每个id矩形框
205206
for (int r = 0; r < net_height; r++) { //stride
206207
float box_score = pdata[4]; ;//box-confidence
207208
if (box_score >= _classThreshold) {
208-
cv::Mat scores(1, _className.size(), CV_32FC1, pdata + 5);
209+
cv::Mat scores(1, score_length, CV_32FC1, pdata + 5);
209210
Point classIdPoint;
210211
double max_class_socre;
211212
minMaxLoc(scores, 0, &max_class_socre, 0, &classIdPoint);

yolov5_seg.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,19 @@ bool Yolov5Seg::Detect(Mat& srcImg, Net& net, vector<OutputSeg>& output) {
6565
std::vector<vector<float>> picked_proposals; //output0[:,:, 5 + _className.size():net_width]===> for mask
6666
int net_width = net_output_img[0].size[2];
6767
int net_height = net_output_img[0].size[1];
68+
int score_length = net_width - 37;
6869
float* pdata = (float*)net_output_img[0].data;
6970
for (int r = 0; r < net_height; r++) { //lines
7071
float box_score = pdata[4];
7172
if (box_score >= _classThreshold) {
72-
cv::Mat scores(1, _className.size(), CV_32FC1, pdata + 5);
73+
cv::Mat scores(1, score_length, CV_32FC1, pdata + 5);
7374
Point classIdPoint;
7475
double max_class_socre;
7576
minMaxLoc(scores, 0, &max_class_socre, 0, &classIdPoint);
7677
max_class_socre = (float)max_class_socre;
7778
if (max_class_socre >= _classThreshold) {
7879

79-
vector<float> temp_proto(pdata + 5 + _className.size(), pdata + net_width);
80+
vector<float> temp_proto(pdata + 5 + score_length, pdata + net_width);
8081
picked_proposals.push_back(temp_proto);
8182
//rect [x,y,w,h]
8283
float x = (pdata[0] - params[2]) / params[0]; //x

yolov5_seg_onnx.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ bool Yolov5SegOnnx::OnnxBatchDetect(std::vector<cv::Mat>& srcImgs, std::vector<s
225225
int64_t one_output_length = VectorProduct(_outputTensorShape) / _outputTensorShape[0];
226226
int net_width = _outputTensorShape[2];
227227
int net_height = _outputTensorShape[1];
228+
int score_length = net_width - 37;
228229
for (int img_index = 0; img_index < srcImgs.size(); ++img_index) {
229230
std::vector<int> class_ids;//结果id数组
230231
std::vector<float> confidences;//结果每个id对应置信度数组
@@ -233,13 +234,13 @@ bool Yolov5SegOnnx::OnnxBatchDetect(std::vector<cv::Mat>& srcImgs, std::vector<s
233234
for (int r = 0; r < net_height; r++) { //stride
234235
float box_score = pdata[4]; ;//box-confidence
235236
if (box_score >= _classThreshold) {
236-
cv::Mat scores(1, _className.size(), CV_32FC1, pdata + 5);
237+
cv::Mat scores(1, score_length, CV_32FC1, pdata + 5);
237238
Point classIdPoint;
238239
double max_class_socre;
239240
minMaxLoc(scores, 0, &max_class_socre, 0, &classIdPoint);
240241
max_class_socre = (float)max_class_socre;
241242
if (max_class_socre >= _classThreshold) {
242-
vector<float> temp_proto(pdata + 5 + _className.size(), pdata + net_width);
243+
vector<float> temp_proto(pdata + 5 + score_length, pdata + net_width);
243244
picked_proposals.push_back(temp_proto);
244245
//rect [x,y,w,h]
245246
float x = (pdata[0] - params[img_index][2]) / params[img_index][0]; //x

0 commit comments

Comments
 (0)