nanodet icon indicating copy to clipboard operation
nanodet copied to clipboard

NCNN - Bounding box is slightly off for NanoDet-m but is correct for NanoDet-Plus-m

Open dpetersonVT23 opened this issue 2 years ago • 2 comments

When running the demo_ncnn script with a model, I observe NanoDet-Plus-m performs as expected, but NanoDet-m has a bounding box that is slightly to the left and slightly higher than it should be every single time (the .pth performs fine though). I removed one of the strides in the header file, is there anything else I need to do for NanoDet-m specifically in the demo_ncnn module?

dpetersonVT23 avatar Aug 01 '22 20:08 dpetersonVT23

The non-plus model has a 0.5 offset in the output grid. This may not be accounted for in that inference script's output decoding. See this part of the code in the regular Nanodet: https://github.com/RangiLyu/nanodet/blob/0f4d8f114431d80729c900c158708b7d70ee2384/nanodet/model/head/gfl_head.py#L650 vs this part in plus: https://github.com/RangiLyu/nanodet/blob/0f4d8f114431d80729c900c158708b7d70ee2384/nanodet/model/head/nanodet_plus_head.py#L492

stiansel avatar Aug 17 '22 06:08 stiansel

The non-plus model has a 0.5 offset in the output grid. This may not be accounted for in that inference script's output decoding. See this part of the code in the regular Nanodet:

https://github.com/RangiLyu/nanodet/blob/0f4d8f114431d80729c900c158708b7d70ee2384/nanodet/model/head/gfl_head.py#L650

vs this part in plus: https://github.com/RangiLyu/nanodet/blob/0f4d8f114431d80729c900c158708b7d70ee2384/nanodet/model/head/nanodet_plus_head.py#L492

Thx, stiansel!

I solved this bbox shifting problem by following steps:

Step 1:

change code in nanodet/nanodet/model/head/gfl_head.py around line 656-657 from

x_range = (torch.arange(w, dtype=dtype, device=device) + 0.5) * stride
y_range = (torch.arange(h, dtype=dtype, device=device) + 0.5) * stride

to

# x_range = (torch.arange(w, dtype=dtype, device=device) + 0.5) * stride
# y_range = (torch.arange(h, dtype=dtype, device=device) + 0.5) * stride
x_range = (torch.arange(w, dtype=dtype, device=device)) * stride
y_range = (torch.arange(h, dtype=dtype, device=device)) * stride

Step 2:

train model

Step 3:

convert pytorch model to onnx, then to ncnn, then problem solved!

cyizhuo avatar Nov 29 '23 07:11 cyizhuo