122 lines
4.9 KiB
Python
122 lines
4.9 KiB
Python
# Ultralytics YOLO 🚀, AGPL-3.0 license
|
|
import torch
|
|
from ultralytics.engine.predictor import BasePredictor
|
|
from ultralytics.engine.results import Results
|
|
from ultralytics.utils import ops
|
|
|
|
|
|
class DetectionPredictor(BasePredictor):
|
|
"""
|
|
A class extending the BasePredictor class for prediction based on a detection model.
|
|
|
|
Example:
|
|
```python
|
|
from ultralytics.utils import ASSETS
|
|
from ultralytics.models.yolo.detect import DetectionPredictor
|
|
|
|
args = dict(model='yolov8n.pt', source=ASSETS)
|
|
predictor = DetectionPredictor(overrides=args)
|
|
predictor.predict_cli()
|
|
```
|
|
"""
|
|
|
|
def postprocess(self, preds, img, orig_imgs):
|
|
"""Post-processes predictions and returns a list of Results objects."""
|
|
# if isinstance(preds, (list, tuple)): # YOLOv8 model in validation model, output = (inference_out, loss_out)
|
|
# preds = preds[0]
|
|
# cls_max = torch.max(preds[:, 4:8, :], dim=1, keepdim=True).values
|
|
# idx = (cls_max > self.args.conf)
|
|
# preds[:, -1:, :][idx] = 0
|
|
|
|
# preds = ops.non_max_suppression(
|
|
# preds,
|
|
# self.args.conf,
|
|
# self.args.iou,
|
|
# agnostic=self.args.agnostic_nms,
|
|
# max_det=self.args.max_det,
|
|
# classes=self.args.classes,
|
|
# )
|
|
|
|
if isinstance(preds, (list, tuple)): # YOLOv8 model in validation model, output = (inference_out, loss_out)
|
|
preds = preds[0]
|
|
preds_c = preds.clone()
|
|
|
|
nms_cls = ops.non_max_suppression(
|
|
preds[:, :8, :],
|
|
self.args.conf,
|
|
self.args.iou,
|
|
agnostic=self.args.agnostic_nms,
|
|
max_det=self.args.max_det,
|
|
classes=self.args.classes,
|
|
)
|
|
|
|
nms_obj = ops.non_max_suppression(
|
|
torch.cat((preds_c[:, :4, :], preds_c[:, -1:, :]), dim=1),
|
|
self.args.conf,
|
|
self.args.iou,
|
|
agnostic=self.args.agnostic_nms,
|
|
max_det=self.args.max_det,
|
|
classes=self.args.classes,
|
|
)
|
|
|
|
preds = [n.clone() for n in nms_cls]
|
|
for i in range(len(nms_cls)):
|
|
nms_obj[i][:, 5] = len(self.model.names) - 1
|
|
if nms_obj[i].size(0) == 0: # Skip if there are no objects
|
|
continue
|
|
if nms_cls[i].size(0) == 0:
|
|
preds[i] = nms_obj[i]
|
|
continue
|
|
iou = self.box_iou(nms_cls[i][:, :4], nms_obj[i][:, :4])
|
|
idxs = []
|
|
# 将多分类置信度低、二分类置信度高,且IOU较大的框替换
|
|
matchs = torch.ones_like(iou)
|
|
matchs[nms_cls[i][:, 4] > 0.3, :] = 0 # new_track_thresh
|
|
matchs[:, nms_obj[i][:, 4] < 0.3] = 0
|
|
matchs[iou < self.args.iou] = 0
|
|
rows, cols = torch.where(matchs == 1)
|
|
preds[i][rows, :] = nms_obj[i][cols, :]
|
|
idxs.append(cols)
|
|
|
|
# 将二分类无对应多分类结果的框附后
|
|
max_values = torch.max(iou, dim=0)[0]
|
|
idxs = torch.where(max_values < self.args.iou)[0]
|
|
idxs = torch.tensor([i for i in idxs if i not in cols]).to(torch.int64)
|
|
preds[i] = torch.cat((preds[i], nms_obj[i][idxs, :]), dim=0)
|
|
|
|
# for m in range(len(nms_obj[i])):
|
|
# if (torch.max(iou[:, m]) < self.args.iou) and (m not in idxs):
|
|
# preds[i] = torch.cat((preds[i], nms_obj[i][m, :]), dim=0)
|
|
|
|
if not isinstance(orig_imgs, list): # input images are a torch.Tensor, not a list
|
|
orig_imgs = ops.convert_torch2numpy_batch(orig_imgs)
|
|
|
|
results = []
|
|
for i, pred in enumerate(preds):
|
|
orig_img = orig_imgs[i]
|
|
pred[:, :4] = ops.scale_boxes(img.shape[2:], pred[:, :4], orig_img.shape)
|
|
img_path = self.batch[0][i]
|
|
results.append(Results(orig_img, path=img_path, names=self.model.names, boxes=pred))
|
|
return results
|
|
|
|
def box_iou(self, box1, box2, eps=1e-7):
|
|
"""
|
|
Calculate intersection-over-union (IoU) of boxes. Both sets of boxes are expected to be in (x1, y1, x2, y2) format.
|
|
Based on https://github.com/pytorch/vision/blob/master/torchvision/ops/boxes.py
|
|
|
|
Args:
|
|
box1 (torch.Tensor): A tensor of shape (N, 4) representing N bounding boxes.
|
|
box2 (torch.Tensor): A tensor of shape (M, 4) representing M bounding boxes.
|
|
eps (float, optional): A small value to avoid division by zero. Defaults to 1e-7.
|
|
|
|
Returns:
|
|
(torch.Tensor): An NxM tensor containing the pairwise IoU values for every element in box1 and box2.
|
|
"""
|
|
|
|
# inter(N,M) = (rb(N,M,2) - lt(N,M,2)).clamp(0).prod(2)
|
|
(a1, a2), (b1, b2) = box1.unsqueeze(1).chunk(2, 2), box2.unsqueeze(0).chunk(2, 2)
|
|
inter = (torch.min(a2, b2) - torch.max(a1, b1)).clamp_(0).prod(2)
|
|
|
|
# IoU = inter / (area1 + area2 - inter)
|
|
return inter / ((a2 - a1).prod(2) + (b2 - b1).prod(2) - inter + eps)
|