mirror of
https://github.com/THU-MIG/yolov10.git
synced 2025-05-23 05:24:22 +08:00
45 lines
1.7 KiB
Python
45 lines
1.7 KiB
Python
from ultralytics.models.yolo.detect import DetectionValidator, PGTDetectionValidator
|
|
from ultralytics.utils import ops
|
|
import torch
|
|
|
|
class YOLOv10DetectionValidator(DetectionValidator):
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
self.args.save_json |= self.is_coco
|
|
|
|
def postprocess(self, preds):
|
|
if isinstance(preds, dict):
|
|
preds = preds["one2one"]
|
|
|
|
if isinstance(preds, (list, tuple)):
|
|
preds = preds[0]
|
|
|
|
# Acknowledgement: Thanks to sanha9999 in #190 and #181!
|
|
if preds.shape[-1] == 6:
|
|
return preds
|
|
else:
|
|
preds = preds.transpose(-1, -2)
|
|
boxes, scores, labels = ops.v10postprocess(preds, self.args.max_det, self.nc)
|
|
bboxes = ops.xywh2xyxy(boxes)
|
|
return torch.cat([bboxes, scores.unsqueeze(-1), labels.unsqueeze(-1)], dim=-1)
|
|
|
|
class YOLOv10PGTDetectionValidator(PGTDetectionValidator):
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
self.args.save_json |= self.is_coco
|
|
|
|
def postprocess(self, preds):
|
|
if isinstance(preds, dict):
|
|
preds = preds["one2one"]
|
|
|
|
if isinstance(preds, (list, tuple)):
|
|
preds = preds[0]
|
|
|
|
# Acknowledgement: Thanks to sanha9999 in #190 and #181!
|
|
if preds.shape[-1] == 6:
|
|
return preds
|
|
else:
|
|
preds = preds.transpose(-1, -2)
|
|
boxes, scores, labels = ops.v10postprocess(preds, self.args.max_det, self.nc)
|
|
bboxes = ops.xywh2xyxy(boxes)
|
|
return torch.cat([bboxes, scores.unsqueeze(-1), labels.unsqueeze(-1)], dim=-1) |