|
|
|
@ -22,6 +22,7 @@ import json
|
|
|
|
|
import time
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
import torch
|
|
|
|
|
from tqdm import tqdm
|
|
|
|
|
|
|
|
|
@ -199,6 +200,33 @@ class BaseValidator:
|
|
|
|
|
LOGGER.info(f"Results saved to {colorstr('bold', self.save_dir)}")
|
|
|
|
|
return stats
|
|
|
|
|
|
|
|
|
|
def match_predictions(self, pred_classes: torch.Tensor, true_classes: torch.Tensor,
|
|
|
|
|
iou: torch.Tensor) -> torch.Tensor:
|
|
|
|
|
"""
|
|
|
|
|
Matches predictions to ground truth objects (pred_classes, true_classes) using IoU.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
pred_classes (torch.Tensor): Predicted class indices of shape(N,).
|
|
|
|
|
true_classes (torch.Tensor): Target class indices of shape(M,).
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
(torch.Tensor): Correct tensor of shape(N,10) for 10 IoU thresholds.
|
|
|
|
|
"""
|
|
|
|
|
correct = np.zeros((pred_classes.shape[0], self.iouv.shape[0])).astype(bool)
|
|
|
|
|
correct_class = true_classes[:, None] == pred_classes
|
|
|
|
|
for i in range(len(self.iouv)):
|
|
|
|
|
x = torch.where((iou >= self.iouv[i]) & correct_class) # IoU > threshold and classes match
|
|
|
|
|
if x[0].shape[0]:
|
|
|
|
|
# Concatenate [label, detect, iou]
|
|
|
|
|
matches = torch.cat((torch.stack(x, 1), iou[x[0], x[1]][:, None]), 1).cpu().numpy()
|
|
|
|
|
if x[0].shape[0] > 1:
|
|
|
|
|
matches = matches[matches[:, 2].argsort()[::-1]]
|
|
|
|
|
matches = matches[np.unique(matches[:, 1], return_index=True)[1]]
|
|
|
|
|
# matches = matches[matches[:, 2].argsort()[::-1]]
|
|
|
|
|
matches = matches[np.unique(matches[:, 0], return_index=True)[1]]
|
|
|
|
|
correct[matches[:, 1].astype(int), i] = True
|
|
|
|
|
return torch.tensor(correct, dtype=torch.bool, device=pred_classes.device)
|
|
|
|
|
|
|
|
|
|
def add_callback(self, event: str, callback):
|
|
|
|
|
"""Appends the given callback."""
|
|
|
|
|
self.callbacks[event].append(callback)
|
|
|
|
|