From 841378247f3ca0d55aced416bbf1bf8291047cc3 Mon Sep 17 00:00:00 2001 From: jbutle55 Date: Wed, 27 Jul 2022 11:03:18 -0600 Subject: [PATCH 1/4] Fix confusion matrix update when no predictions are made --- utils/metrics.py | 13 +++++++++++++ val.py | 2 ++ 2 files changed, 15 insertions(+) diff --git a/utils/metrics.py b/utils/metrics.py index 6bba4cfe2a42..781d6f9aef57 100644 --- a/utils/metrics.py +++ b/utils/metrics.py @@ -169,6 +169,19 @@ def process_batch(self, detections, labels): if not any(m1 == i): self.matrix[dc, self.nc] += 1 # background FN + def process_batch_no_detections(self, labels): + """ + Updates confusion matrix when model made zero predictions but the image contains groundtruth objects. + Every label passed in is considered a False Negative (a missed detection) + Arguments: + labels (Array[M, 1]), [class] + Returns: + None, updates confusion matrix accordingly + """ + gt_classes = labels.int() + for i, gc in enumerate(gt_classes): + self.matrix[self.nc, gc] += 1 # background FN + def matrix(self): return self.matrix diff --git a/val.py b/val.py index b0cc8e7f1577..624d22f3b7e7 100644 --- a/val.py +++ b/val.py @@ -228,6 +228,8 @@ def run( if npr == 0: if nl: stats.append((correct, *torch.zeros((2, 0), device=device), labels[:, 0])) + if plots: + confusion_matrix.process_batch_no_detections(labels[:, 0]) continue # Predictions From 36753c3854f95b8cbe6f133e570c43be7ceedd23 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Wed, 27 Jul 2022 21:33:11 +0200 Subject: [PATCH 2/4] Update metrics.py --- utils/metrics.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/utils/metrics.py b/utils/metrics.py index 781d6f9aef57..b75ec681c285 100644 --- a/utils/metrics.py +++ b/utils/metrics.py @@ -171,13 +171,13 @@ def process_batch(self, detections, labels): def process_batch_no_detections(self, labels): """ - Updates confusion matrix when model made zero predictions but the image contains groundtruth objects. - Every label passed in is considered a False Negative (a missed detection) - Arguments: - labels (Array[M, 1]), [class] - Returns: - None, updates confusion matrix accordingly - """ + Updates confusion matrix when model made zero predictions but the image contains groundtruth objects. + Every label passed in is considered a False Negative (a missed detection) + Arguments: + labels (Array[M, 1]), [class] + Returns: + None, updates confusion matrix accordingly + """ gt_classes = labels.int() for i, gc in enumerate(gt_classes): self.matrix[self.nc, gc] += 1 # background FN From 76be60fbaeceddf3b495e1a2cba2c62ed21171d9 Mon Sep 17 00:00:00 2001 From: jbutle55 Date: Wed, 27 Jul 2022 13:39:48 -0600 Subject: [PATCH 3/4] Simply confusion matrix changes --- utils/metrics.py | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/utils/metrics.py b/utils/metrics.py index 781d6f9aef57..9bf084c78854 100644 --- a/utils/metrics.py +++ b/utils/metrics.py @@ -139,6 +139,12 @@ def process_batch(self, detections, labels): Returns: None, updates confusion matrix accordingly """ + if detections is None: + gt_classes = labels.int() + for i, gc in enumerate(gt_classes): + self.matrix[self.nc, gc] += 1 # background FN + return + detections = detections[detections[:, 4] > self.conf] gt_classes = labels[:, 0].int() detection_classes = detections[:, 5].int() @@ -169,19 +175,6 @@ def process_batch(self, detections, labels): if not any(m1 == i): self.matrix[dc, self.nc] += 1 # background FN - def process_batch_no_detections(self, labels): - """ - Updates confusion matrix when model made zero predictions but the image contains groundtruth objects. - Every label passed in is considered a False Negative (a missed detection) - Arguments: - labels (Array[M, 1]), [class] - Returns: - None, updates confusion matrix accordingly - """ - gt_classes = labels.int() - for i, gc in enumerate(gt_classes): - self.matrix[self.nc, gc] += 1 # background FN - def matrix(self): return self.matrix From 2f888f375ecf1809cc4cc5f49fb26783e4e0b010 Mon Sep 17 00:00:00 2001 From: jbutle55 Date: Wed, 27 Jul 2022 13:42:50 -0600 Subject: [PATCH 4/4] Simply confusion matrix fix --- utils/metrics.py | 13 ------------- val.py | 2 +- 2 files changed, 1 insertion(+), 14 deletions(-) diff --git a/utils/metrics.py b/utils/metrics.py index 7e522609ba89..9bf084c78854 100644 --- a/utils/metrics.py +++ b/utils/metrics.py @@ -175,19 +175,6 @@ def process_batch(self, detections, labels): if not any(m1 == i): self.matrix[dc, self.nc] += 1 # background FN - def process_batch_no_detections(self, labels): - """ - Updates confusion matrix when model made zero predictions but the image contains groundtruth objects. - Every label passed in is considered a False Negative (a missed detection) - Arguments: - labels (Array[M, 1]), [class] - Returns: - None, updates confusion matrix accordingly - """ - gt_classes = labels.int() - for i, gc in enumerate(gt_classes): - self.matrix[self.nc, gc] += 1 # background FN - def matrix(self): return self.matrix diff --git a/val.py b/val.py index 624d22f3b7e7..48207a1130a6 100644 --- a/val.py +++ b/val.py @@ -229,7 +229,7 @@ def run( if nl: stats.append((correct, *torch.zeros((2, 0), device=device), labels[:, 0])) if plots: - confusion_matrix.process_batch_no_detections(labels[:, 0]) + confusion_matrix.process_batch(detections=None, labels=labels[:, 0]) continue # Predictions