Skip to content

Commit a6e9f95

Browse files
committed
Fix some instance segmentation bugs
1 parent 8dfb5db commit a6e9f95

4 files changed

Lines changed: 64 additions & 67 deletions

File tree

src/Detector/tensorrt_yolo/RFDETR_bb.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class RFDETR_bb_onnx : public YoloONNX
4040
size_t ncInd = 2;
4141
size_t lenInd = 1;
4242

43-
int nc = m_outpuDims[1].d[ncInd];
43+
size_t nc = m_outpuDims[1].d[ncInd];
4444
size_t len = static_cast<size_t>(m_outpuDims[0].d[lenInd]) / m_params.explicitBatchSize;
4545
auto volume0 = len * m_outpuDims[0].d[ncInd]; // Volume(m_outpuDims[0]);
4646
dets += volume0 * imgIdx;

src/Detector/tensorrt_yolo/YoloONNXv11_instance.hpp

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include "YoloONNX.hpp"
4+
#include "../../common/defines.h"
45

56
///
67
/// \brief The YOLOv11_instance_onnx class
@@ -163,10 +164,10 @@ class YOLOv11_instance_onnx : public YoloONNX
163164
if (objectConf >= m_params.confThreshold)
164165
{
165166
// (center x, center y, width, height) to (x, y, w, h)
166-
float x = fw * (output[k] - output[k + 2] / 2 - m_resizedROI.x);
167-
float y = fh * (output[k + 1] - output[k + 3] / 2 - m_resizedROI.y);
168-
float width = fw * output[k + 2];
169-
float height = fh * output[k + 3];
167+
float x = output[k] - output[k + 2] / 2;
168+
float y = output[k + 1] - output[k + 3] / 2;
169+
float width = output[k + 2];
170+
float height = output[k + 3];
170171

171172
//auto ClampToFrame = [](float& v, float& size, int hi) -> int
172173
//{
@@ -254,7 +255,7 @@ class YOLOv11_instance_onnx : public YoloONNX
254255
cv::Rect roi(int((float)padw / INPUT_W * segWidth), int((float)padh / INPUT_H * segHeight), int(segWidth - padw / 2), int(segHeight - padh / 2));
255256
dest = dest(roi);
256257

257-
cv::resize(dest, mask, frameSize, cv::INTER_NEAREST);
258+
cv::resize(dest, mask, cv::Size(INPUT_W, INPUT_H), cv::INTER_NEAREST);
258259

259260
resBoxes[i].m_boxMask = mask(resBoxes[i].m_brect) > MASK_THRESHOLD;
260261

@@ -277,28 +278,25 @@ class YOLOv11_instance_onnx : public YoloONNX
277278
if (br.width >= 4 &&
278279
br.height >= 4)
279280
{
280-
cv::RotatedRect rr = (contour.size() < 5) ? cv::minAreaRect(contour) : cv::fitEllipse(contour);
281-
282-
br.x += resBoxes[i].m_brect.x;
283-
br.y += resBoxes[i].m_brect.y;
284-
rr.center.x += resBoxes[i].m_brect.x;
285-
rr.center.y += resBoxes[i].m_brect.y;
281+
int dx = resBoxes[i].m_brect.x;
282+
int dy = resBoxes[i].m_brect.y;
286283

287-
//std::cout << "rr: " << rr.center << ", " << rr.angle << ", " << rr.size << std::endl;
284+
cv::RotatedRect rr = (contour.size() < 5) ? cv::minAreaRect(contour) : cv::fitEllipse(contour);
285+
rr.center.x = (rr.center.x + dx - m_resizedROI.x) * fw;
286+
rr.center.y = (rr.center.y + dy - m_resizedROI.y) * fw;
287+
rr.size.width *= fw;
288+
rr.size.height *= fh;
288289

289-
if (resBoxes[i].m_boxMask.size() != br.size())
290-
{
291-
br.width = resBoxes[i].m_boxMask.cols;
292-
br.height = resBoxes[i].m_boxMask.rows;
293-
if (br.x + br.width >= frameSize.width)
294-
br.x = frameSize.width - br.width;
295-
if (br.y + br.height >= frameSize.height)
296-
br.y = frameSize.height - br.height;
297-
}
290+
br.x = cvRound((dx + br.x - m_resizedROI.x) * fw);
291+
br.y = cvRound((dy + br.y - m_resizedROI.y) * fh);
292+
br.width = cvRound(br.width * fw);
293+
br.height = cvRound(br.height * fh);
298294

299295
resBoxes[i].m_brect = br;
300296
resBoxes[i].m_rrect = rr;
301297

298+
//std::cout << "resBoxes[" << i << "] br: " << br << ", rr: (" << rr.size << " from " << rr.center << ", " << rr.angle << ")" << std::endl;
299+
302300
break;
303301
}
304302
}

src/Detector/tensorrt_yolo/YoloONNXv7_instance.hpp

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,10 @@ class YOLOv7_instance_onnx : public YoloONNX
131131
if (object_conf >= m_params.confThreshold)
132132
{
133133
// (center x, center y, width, height) to (x, y, w, h)
134-
float x = fw * (output[k] - output[k + 2] / 2 - m_resizedROI.x);
135-
float y = fh * (output[k + 1] - output[k + 3] / 2 - m_resizedROI.y);
136-
float width = fw * output[k + 2];
137-
float height = fh * output[k + 3];
134+
float x = output[k] - output[k + 2] / 2;
135+
float y = output[k + 1] - output[k + 3] / 2;
136+
float width = output[k + 2];
137+
float height = output[k + 3];
138138

139139
// Classes
140140
float class_conf = output[k + 5];
@@ -207,7 +207,7 @@ class YOLOv7_instance_onnx : public YoloONNX
207207
cv::Rect roi(int((float)padw / INPUT_W * segWidth), int((float)padh / INPUT_H * segHeight), int(segWidth - padw / 2), int(segHeight - padh / 2));
208208
dest = dest(roi);
209209

210-
cv::resize(dest, mask, frameSize, cv::INTER_NEAREST);
210+
cv::resize(dest, mask, cv::Size(INPUT_W, INPUT_H), cv::INTER_NEAREST);
211211

212212
resBoxes[i].m_boxMask = mask(resBoxes[i].m_brect) > MASK_THRESHOLD;
213213

@@ -230,28 +230,25 @@ class YOLOv7_instance_onnx : public YoloONNX
230230
if (br.width >= 4 &&
231231
br.height >= 4)
232232
{
233-
cv::RotatedRect rr = (contour.size() < 5) ? cv::minAreaRect(contour) : cv::fitEllipse(contour);
234-
235-
br.x += resBoxes[i].m_brect.x;
236-
br.y += resBoxes[i].m_brect.y;
237-
rr.center.x += resBoxes[i].m_brect.x;
238-
rr.center.y += resBoxes[i].m_brect.y;
233+
int dx = resBoxes[i].m_brect.x;
234+
int dy = resBoxes[i].m_brect.y;
239235

240-
//std::cout << "rr: " << rr.center << ", " << rr.angle << ", " << rr.size << std::endl;
236+
cv::RotatedRect rr = (contour.size() < 5) ? cv::minAreaRect(contour) : cv::fitEllipse(contour);
237+
rr.center.x = (rr.center.x + dx - m_resizedROI.x) * fw;
238+
rr.center.y = (rr.center.y + dy - m_resizedROI.y) * fw;
239+
rr.size.width *= fw;
240+
rr.size.height *= fh;
241241

242-
if (resBoxes[i].m_boxMask.size() != br.size())
243-
{
244-
br.width = resBoxes[i].m_boxMask.cols;
245-
br.height = resBoxes[i].m_boxMask.rows;
246-
if (br.x + br.width >= frameSize.width)
247-
br.x = frameSize.width - br.width;
248-
if (br.y + br.height >= frameSize.height)
249-
br.y = frameSize.height - br.height;
250-
}
242+
br.x = cvRound((dx + br.x - m_resizedROI.x) * fw);
243+
br.y = cvRound((dy + br.y - m_resizedROI.y) * fh);
244+
br.width = cvRound(br.width * fw);
245+
br.height = cvRound(br.height * fh);
251246

252247
resBoxes[i].m_brect = br;
253248
resBoxes[i].m_rrect = rr;
254249

250+
//std::cout << "resBoxes[" << i << "] br: " << br << ", rr: (" << rr.size << " from " << rr.center << ", " << rr.angle << ")" << std::endl;
251+
255252
break;
256253
}
257254
}

src/Detector/tensorrt_yolo/YoloONNXv8_instance.hpp

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include "YoloONNX.hpp"
4+
#include "../../common/defines.h"
45

56
///
67
/// \brief The YOLOv8_instance_onnx class
@@ -140,6 +141,9 @@ class YOLOv8_instance_onnx : public YoloONNX
140141
}
141142
}
142143

144+
//if (objectConf > 0.1)
145+
// std::cout << i << ": objectConf = " << objectConf << ", classId = " << classId << std::endl;
146+
143147
//if (i == 0)
144148
//{
145149
// std::cout << "without nms: mem" << i << ": ";
@@ -163,10 +167,10 @@ class YOLOv8_instance_onnx : public YoloONNX
163167
if (objectConf >= m_params.confThreshold)
164168
{
165169
// (center x, center y, width, height) to (x, y, w, h)
166-
float x = fw * (output[k] - output[k + 2] / 2 - m_resizedROI.x);
167-
float y = fh * (output[k + 1] - output[k + 3] / 2 - m_resizedROI.y);
168-
float width = fw * output[k + 2];
169-
float height = fh * output[k + 3];
170+
float x = output[k] - output[k + 2] / 2;
171+
float y = output[k + 1] - output[k + 3] / 2;
172+
float width = output[k + 2];
173+
float height = output[k + 3];
170174

171175
//auto ClampToFrame = [](float& v, float& size, int hi) -> int
172176
//{
@@ -198,9 +202,6 @@ class YOLOv8_instance_onnx : public YoloONNX
198202
//ClampToFrame(x, width, frameSize.width);
199203
//ClampToFrame(y, height, frameSize.height);
200204

201-
//if (i == 0)
202-
// std::cout << i << ": object_conf = " << object_conf << ", class_conf = " << class_conf << ", classId = " << classId << ", rect = " << cv::Rect(cvRound(x), cvRound(y), cvRound(width), cvRound(height)) << std::endl;
203-
204205
if (width > 4 && height > 4)
205206
{
206207
classIds.push_back(classId);
@@ -213,6 +214,8 @@ class YOLOv8_instance_onnx : public YoloONNX
213214
}
214215
}
215216

217+
//std::cout << "rectBoxes.size = " << rectBoxes.size() << std::endl;
218+
216219
// Non-maximum suppression to eliminate redudant overlapping boxes
217220
std::vector<int> indices;
218221
cv::dnn::NMSBoxes(rectBoxes, confidences, m_params.confThreshold, m_params.nmsThreshold, indices);
@@ -254,7 +257,7 @@ class YOLOv8_instance_onnx : public YoloONNX
254257
cv::Rect roi(int((float)padw / INPUT_W * segWidth), int((float)padh / INPUT_H * segHeight), int(segWidth - padw / 2), int(segHeight - padh / 2));
255258
dest = dest(roi);
256259

257-
cv::resize(dest, mask, frameSize, cv::INTER_NEAREST);
260+
cv::resize(dest, mask, cv::Size(INPUT_W, INPUT_H), cv::INTER_NEAREST);
258261

259262
resBoxes[i].m_boxMask = mask(resBoxes[i].m_brect) > MASK_THRESHOLD;
260263

@@ -274,31 +277,30 @@ class YOLOv8_instance_onnx : public YoloONNX
274277
{
275278
cv::Rect br = cv::boundingRect(contour);
276279

280+
//std::cout << "contour br: " << br << std::endl;
281+
277282
if (br.width >= 4 &&
278283
br.height >= 4)
279284
{
280-
cv::RotatedRect rr = (contour.size() < 5) ? cv::minAreaRect(contour) : cv::fitEllipse(contour);
281-
282-
br.x += resBoxes[i].m_brect.x;
283-
br.y += resBoxes[i].m_brect.y;
284-
rr.center.x += resBoxes[i].m_brect.x;
285-
rr.center.y += resBoxes[i].m_brect.y;
285+
int dx = resBoxes[i].m_brect.x;
286+
int dy = resBoxes[i].m_brect.y;
286287

287-
//std::cout << "rr: " << rr.center << ", " << rr.angle << ", " << rr.size << std::endl;
288+
cv::RotatedRect rr = (contour.size() < 5) ? cv::minAreaRect(contour) : cv::fitEllipse(contour);
289+
rr.center.x = (rr.center.x + dx - m_resizedROI.x) * fw;
290+
rr.center.y = (rr.center.y + dy - m_resizedROI.y) * fw;
291+
rr.size.width *= fw;
292+
rr.size.height *= fh;
288293

289-
if (resBoxes[i].m_boxMask.size() != br.size())
290-
{
291-
br.width = resBoxes[i].m_boxMask.cols;
292-
br.height = resBoxes[i].m_boxMask.rows;
293-
if (br.x + br.width >= frameSize.width)
294-
br.x = frameSize.width - br.width;
295-
if (br.y + br.height >= frameSize.height)
296-
br.y = frameSize.height - br.height;
297-
}
294+
br.x = cvRound((dx + br.x - m_resizedROI.x) * fw);
295+
br.y = cvRound((dy + br.y - m_resizedROI.y) * fh);
296+
br.width = cvRound(br.width * fw);
297+
br.height = cvRound(br.height * fh);
298298

299299
resBoxes[i].m_brect = br;
300300
resBoxes[i].m_rrect = rr;
301301

302+
//std::cout << "resBoxes[" << i << "] br: " << br << ", rr: (" << rr.size << " from " << rr.center << ", " << rr.angle << ")" << std::endl;
303+
302304
break;
303305
}
304306
}

0 commit comments

Comments
 (0)