|
|
@ -172,19 +172,37 @@ class FocalLoss(nn.Module):
|
|
|
|
|
|
|
|
|
|
|
|
class ConfusionMatrix:
|
|
|
|
class ConfusionMatrix:
|
|
|
|
# Updated version of https://github.com/kaanakan/object_detection_confusion_matrix
|
|
|
|
# Updated version of https://github.com/kaanakan/object_detection_confusion_matrix
|
|
|
|
def __init__(self, nc, conf=0.25, iou_thres=0.45):
|
|
|
|
def __init__(self, nc, conf=0.25, iou_thres=0.45, task='detect'):
|
|
|
|
self.matrix = np.zeros((nc + 1, nc + 1))
|
|
|
|
self.task = task
|
|
|
|
|
|
|
|
self.matrix = np.zeros((nc + 1, nc + 1)) if self.task == 'detect' else np.zeros((nc, nc))
|
|
|
|
self.nc = nc # number of classes
|
|
|
|
self.nc = nc # number of classes
|
|
|
|
self.conf = conf
|
|
|
|
self.conf = conf
|
|
|
|
self.iou_thres = iou_thres
|
|
|
|
self.iou_thres = iou_thres
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def process_cls_preds(self, preds, targets):
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
Update confusion matrix for classification task
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
|
|
|
|
preds (Array[N, min(nc,5)])
|
|
|
|
|
|
|
|
targets (Array[N, 1])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
None, updates confusion matrix accordingly
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
preds, targets = torch.cat(preds)[:, 0], torch.cat(targets)
|
|
|
|
|
|
|
|
for p, t in zip(preds.cpu().numpy(), targets.cpu().numpy()):
|
|
|
|
|
|
|
|
self.matrix[t][p] += 1
|
|
|
|
|
|
|
|
|
|
|
|
def process_batch(self, detections, labels):
|
|
|
|
def process_batch(self, detections, labels):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
Return intersection-over-union (Jaccard index) of boxes.
|
|
|
|
Return intersection-over-union (Jaccard index) of boxes.
|
|
|
|
Both sets of boxes are expected to be in (x1, y1, x2, y2) format.
|
|
|
|
Both sets of boxes are expected to be in (x1, y1, x2, y2) format.
|
|
|
|
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
Arguments:
|
|
|
|
detections (Array[N, 6]), x1, y1, x2, y2, conf, class
|
|
|
|
detections (Array[N, 6]), x1, y1, x2, y2, conf, class
|
|
|
|
labels (Array[M, 5]), class, x1, y1, x2, y2
|
|
|
|
labels (Array[M, 5]), class, x1, y1, x2, y2
|
|
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Returns:
|
|
|
|
None, updates confusion matrix accordingly
|
|
|
|
None, updates confusion matrix accordingly
|
|
|
|
"""
|
|
|
|
"""
|
|
|
@ -231,7 +249,7 @@ class ConfusionMatrix:
|
|
|
|
tp = self.matrix.diagonal() # true positives
|
|
|
|
tp = self.matrix.diagonal() # true positives
|
|
|
|
fp = self.matrix.sum(1) - tp # false positives
|
|
|
|
fp = self.matrix.sum(1) - tp # false positives
|
|
|
|
# fn = self.matrix.sum(0) - tp # false negatives (missed detections)
|
|
|
|
# fn = self.matrix.sum(0) - tp # false negatives (missed detections)
|
|
|
|
return tp[:-1], fp[:-1] # remove background class
|
|
|
|
return (tp[:-1], fp[:-1]) if self.task == 'detect' else (tp, fp) # remove background class if task=detect
|
|
|
|
|
|
|
|
|
|
|
|
@TryExcept('WARNING ⚠️ ConfusionMatrix plot failure')
|
|
|
|
@TryExcept('WARNING ⚠️ ConfusionMatrix plot failure')
|
|
|
|
@plt_settings()
|
|
|
|
@plt_settings()
|
|
|
|