predict.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. # Ultralytics YOLO 🚀, AGPL-3.0 license
  2. import torch
  3. from ultralytics.engine.predictor import BasePredictor
  4. from ultralytics.engine.results import Results
  5. from ultralytics.utils import ops
  6. class NASPredictor(BasePredictor):
  7. def postprocess(self, preds_in, img, orig_imgs):
  8. """Postprocess predictions and returns a list of Results objects."""
  9. # Cat boxes and class scores
  10. boxes = ops.xyxy2xywh(preds_in[0][0])
  11. preds = torch.cat((boxes, preds_in[0][1]), -1).permute(0, 2, 1)
  12. preds = ops.non_max_suppression(preds,
  13. self.args.conf,
  14. self.args.iou,
  15. agnostic=self.args.agnostic_nms,
  16. max_det=self.args.max_det,
  17. classes=self.args.classes)
  18. results = []
  19. is_list = isinstance(orig_imgs, list) # input images are a list, not a torch.Tensor
  20. for i, pred in enumerate(preds):
  21. orig_img = orig_imgs[i] if is_list else orig_imgs
  22. if is_list:
  23. pred[:, :4] = ops.scale_boxes(img.shape[2:], pred[:, :4], orig_img.shape)
  24. img_path = self.batch[0][i]
  25. results.append(Results(orig_img, path=img_path, names=self.model.names, boxes=pred))
  26. return results