predict.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # Ultralytics YOLO 🚀, AGPL-3.0 license
  2. import torch
  3. from ultralytics.data.augment import LetterBox
  4. from ultralytics.engine.predictor import BasePredictor
  5. from ultralytics.engine.results import Results
  6. from ultralytics.utils import ops
  7. class RTDETRPredictor(BasePredictor):
  8. """
  9. A class extending the BasePredictor class for prediction based on an RT-DETR detection model.
  10. Example:
  11. ```python
  12. from ultralytics.utils import ASSETS
  13. from ultralytics.models.rtdetr import RTDETRPredictor
  14. args = dict(model='rtdetr-l.pt', source=ASSETS)
  15. predictor = RTDETRPredictor(overrides=args)
  16. predictor.predict_cli()
  17. ```
  18. """
  19. def postprocess(self, preds, img, orig_imgs):
  20. """Postprocess predictions and returns a list of Results objects."""
  21. nd = preds[0].shape[-1]
  22. bboxes, scores = preds[0].split((4, nd - 4), dim=-1)
  23. results = []
  24. is_list = isinstance(orig_imgs, list) # input images are a list, not a torch.Tensor
  25. for i, bbox in enumerate(bboxes): # (300, 4)
  26. bbox = ops.xywh2xyxy(bbox)
  27. score, cls = scores[i].max(-1, keepdim=True) # (300, 1)
  28. idx = score.squeeze(-1) > self.args.conf # (300, )
  29. if self.args.classes is not None:
  30. idx = (cls == torch.tensor(self.args.classes, device=cls.device)).any(1) & idx
  31. pred = torch.cat([bbox, score, cls], dim=-1)[idx] # filter
  32. orig_img = orig_imgs[i] if is_list else orig_imgs
  33. oh, ow = orig_img.shape[:2]
  34. if is_list:
  35. pred[..., [0, 2]] *= ow
  36. pred[..., [1, 3]] *= oh
  37. img_path = self.batch[0][i]
  38. results.append(Results(orig_img, path=img_path, names=self.model.names, boxes=pred))
  39. return results
  40. def pre_transform(self, im):
  41. """Pre-transform input image before inference.
  42. Args:
  43. im (List(np.ndarray)): (N, 3, h, w) for tensor, [(h, w, 3) x N] for list.
  44. Notes: The size must be square(640) and scaleFilled.
  45. Returns:
  46. (list): A list of transformed imgs.
  47. """
  48. return [LetterBox(self.imgsz, auto=False, scaleFill=True)(image=x) for x in im]