Exclude low confidence detections in OBB confusion matrix (#21352)

Signed-off-by: Mohammed Yasin <32206511+Y-T-G@users.noreply.github.com>
Co-authored-by: Laughing-q <1185102784@qq.com>
Co-authored-by: Jing Qiu <61612323+Laughing-q@users.noreply.github.com>
This commit is contained in:
Mohammed Yasin 2025-07-12 10:44:22 +06:00 committed by GitHub
parent 2a2ac603da
commit cd10bf6946
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -330,7 +330,7 @@ class ConfusionMatrix(DataExportMixin):
"""
self.task = task
self.nc = len(names) # number of classes
self.matrix = np.zeros((self.nc + 1, self.nc + 1)) if self.task == "detect" else np.zeros((self.nc, self.nc))
self.matrix = np.zeros((self.nc, self.nc)) if self.task == "classify" else np.zeros((self.nc + 1, self.nc + 1))
self.names = names # name of classes
def process_cls_preds(self, preds, targets):
@ -360,8 +360,9 @@ class ConfusionMatrix(DataExportMixin):
conf (float, optional): Confidence threshold for detections.
iou_thres (float, optional): IoU threshold for matching detections to ground truth.
"""
conf = 0.25 if conf in {None, 0.001} else conf # apply 0.25 if default val conf is passed
gt_cls, gt_bboxes = batch["cls"], batch["bboxes"]
is_obb = gt_bboxes.shape[1] == 5 # check if boxes contains angle for OBB
conf = 0.25 if conf in {None, 0.01 if is_obb else 0.001} else conf # apply 0.25 if default val conf is passed
no_pred = len(detections["cls"]) == 0
if gt_cls.shape[0] == 0: # Check if labels is empty
if not no_pred:
@ -380,7 +381,6 @@ class ConfusionMatrix(DataExportMixin):
gt_classes = gt_cls.int().tolist()
detection_classes = detections["cls"].int().tolist()
bboxes = detections["bboxes"]
is_obb = bboxes.shape[1] == 5 # check if detections contains angle for OBB
iou = batch_probiou(gt_bboxes, bboxes) if is_obb else box_iou(gt_bboxes, bboxes)
x = torch.where(iou > iou_thres)
@ -422,7 +422,7 @@ class ConfusionMatrix(DataExportMixin):
tp = self.matrix.diagonal() # true positives
fp = self.matrix.sum(1) - tp # false positives
# fn = self.matrix.sum(0) - tp # false negatives (missed detections)
return (tp[:-1], fp[:-1]) if self.task == "detect" else (tp, fp) # remove background class if task=detect
return (tp, fp) if self.task == "classify" else (tp[:-1], fp[:-1]) # remove background class if task=detect
@TryExcept(msg="ConfusionMatrix plot failure")
@plt_settings()