|
|
|
@ -1,7 +1,5 @@ |
|
|
|
|
# Ultralytics YOLO 🚀, AGPL-3.0 license |
|
|
|
|
|
|
|
|
|
import torch |
|
|
|
|
|
|
|
|
|
from ultralytics.engine.results import Results |
|
|
|
|
from ultralytics.models.yolo.detect.predict import DetectionPredictor |
|
|
|
|
from ultralytics.utils import DEFAULT_CFG, ops |
|
|
|
@ -27,7 +25,6 @@ class SegmentationPredictor(DetectionPredictor): |
|
|
|
|
self.args.task = 'segment' |
|
|
|
|
|
|
|
|
|
def postprocess(self, preds, img, orig_imgs): |
|
|
|
|
"""TODO: filter by classes.""" |
|
|
|
|
p = ops.non_max_suppression(preds[0], |
|
|
|
|
self.args.conf, |
|
|
|
|
self.args.iou, |
|
|
|
@ -36,22 +33,20 @@ class SegmentationPredictor(DetectionPredictor): |
|
|
|
|
nc=len(self.model.names), |
|
|
|
|
classes=self.args.classes) |
|
|
|
|
results = [] |
|
|
|
|
is_list = isinstance(orig_imgs, list) # input images are a list, not a torch.Tensor |
|
|
|
|
proto = preds[1][-1] if len(preds[1]) == 3 else preds[1] # second output is len 3 if pt, but only 1 if exported |
|
|
|
|
for i, pred in enumerate(p): |
|
|
|
|
orig_img = orig_imgs[i] if isinstance(orig_imgs, list) else orig_imgs |
|
|
|
|
path = self.batch[0] |
|
|
|
|
img_path = path[i] if isinstance(path, list) else path |
|
|
|
|
orig_img = orig_imgs[i] if is_list else orig_imgs |
|
|
|
|
img_path = self.batch[0][i] |
|
|
|
|
if not len(pred): # save empty boxes |
|
|
|
|
results.append(Results(orig_img=orig_img, path=img_path, names=self.model.names, boxes=pred[:, :6])) |
|
|
|
|
continue |
|
|
|
|
if self.args.retina_masks: |
|
|
|
|
if not isinstance(orig_imgs, torch.Tensor): |
|
|
|
|
masks = None |
|
|
|
|
elif self.args.retina_masks: |
|
|
|
|
if is_list: |
|
|
|
|
pred[:, :4] = ops.scale_boxes(img.shape[2:], pred[:, :4], orig_img.shape) |
|
|
|
|
masks = ops.process_mask_native(proto[i], pred[:, 6:], pred[:, :4], orig_img.shape[:2]) # HWC |
|
|
|
|
else: |
|
|
|
|
masks = ops.process_mask(proto[i], pred[:, 6:], pred[:, :4], img.shape[2:], upsample=True) # HWC |
|
|
|
|
if not isinstance(orig_imgs, torch.Tensor): |
|
|
|
|
if is_list: |
|
|
|
|
pred[:, :4] = ops.scale_boxes(img.shape[2:], pred[:, :4], orig_img.shape) |
|
|
|
|
results.append( |
|
|
|
|
Results(orig_img=orig_img, path=img_path, names=self.model.names, boxes=pred[:, :6], masks=masks)) |
|
|
|
|
results.append(Results(orig_img, path=img_path, names=self.model.names, boxes=pred[:, :6], masks=masks)) |
|
|
|
|
return results |
|
|
|
|