Improve accuracy of NMS export with quantization (#22487)

Signed-off-by: Mohammed Yasin <32206511+Y-T-G@users.noreply.github.com>
Co-authored-by: Ultralytics Assistant <135830346+UltralyticsAssistant@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
This commit is contained in:
Mohammed Yasin 2025-10-28 02:29:35 +08:00 committed by GitHub
parent b2da9ce85f
commit 4d80622b9b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1510,17 +1510,16 @@ class NMSModel(torch.nn.Module):
box, score, cls, extra = box[mask], score[mask], cls[mask], extra[mask]
nmsbox = box.clone()
# `8` is the minimum value experimented to get correct NMS results for obb
multiplier = 8 if self.obb else 1
multiplier = (8 if self.obb else 1) / max(len(self.model.names), 1)
# Normalize boxes for NMS since large values for class offset causes issue with int8 quantization
if self.args.format == "tflite": # TFLite is already normalized
nmsbox *= multiplier
else:
nmsbox = multiplier * nmsbox / torch.tensor(x.shape[2:], **kwargs).max()
if not self.args.agnostic_nms: # class-specific NMS
nmsbox = multiplier * (nmsbox / torch.tensor(x.shape[2:], **kwargs).max())
if not self.args.agnostic_nms: # class-wise NMS
end = 2 if self.obb else 4
# fully explicit expansion otherwise reshape error
# large max_wh causes issues when quantizing
cls_offset = cls.reshape(-1, 1).expand(nmsbox.shape[0], end)
cls_offset = cls.view(cls.shape[0], 1).expand(cls.shape[0], end)
offbox = nmsbox[:, :end] + cls_offset * multiplier
nmsbox = torch.cat((offbox, nmsbox[:, end:]), dim=-1)
nms_fn = (