|
|
@ -1,3 +1,10 @@
|
|
|
|
|
|
|
|
# Ultralytics YOLO 🚀, GPL-3.0 license
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
Ultralytics Results, Boxes and Masks classes for handling inference results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Usage: See https://docs.ultralytics.com/predict/
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
from copy import deepcopy
|
|
|
|
from copy import deepcopy
|
|
|
|
from functools import lru_cache
|
|
|
|
from functools import lru_cache
|
|
|
|
|
|
|
|
|
|
|
@ -36,7 +43,7 @@ class Results:
|
|
|
|
self.probs = probs if probs is not None else None
|
|
|
|
self.probs = probs if probs is not None else None
|
|
|
|
self.names = names
|
|
|
|
self.names = names
|
|
|
|
self.path = path
|
|
|
|
self.path = path
|
|
|
|
self.comp = ['boxes', 'masks', 'probs']
|
|
|
|
self._keys = (k for k in ('boxes', 'masks', 'probs') if getattr(self, k) is not None)
|
|
|
|
|
|
|
|
|
|
|
|
def pandas(self):
|
|
|
|
def pandas(self):
|
|
|
|
pass
|
|
|
|
pass
|
|
|
@ -44,10 +51,8 @@ class Results:
|
|
|
|
|
|
|
|
|
|
|
|
def __getitem__(self, idx):
|
|
|
|
def __getitem__(self, idx):
|
|
|
|
r = Results(orig_img=self.orig_img, path=self.path, names=self.names)
|
|
|
|
r = Results(orig_img=self.orig_img, path=self.path, names=self.names)
|
|
|
|
for item in self.comp:
|
|
|
|
for k in self._keys:
|
|
|
|
if getattr(self, item) is None:
|
|
|
|
setattr(r, k, getattr(self, k)[idx])
|
|
|
|
continue
|
|
|
|
|
|
|
|
setattr(r, item, getattr(self, item)[idx])
|
|
|
|
|
|
|
|
return r
|
|
|
|
return r
|
|
|
|
|
|
|
|
|
|
|
|
def update(self, boxes=None, masks=None, probs=None):
|
|
|
|
def update(self, boxes=None, masks=None, probs=None):
|
|
|
@ -60,57 +65,37 @@ class Results:
|
|
|
|
|
|
|
|
|
|
|
|
def cpu(self):
|
|
|
|
def cpu(self):
|
|
|
|
r = Results(orig_img=self.orig_img, path=self.path, names=self.names)
|
|
|
|
r = Results(orig_img=self.orig_img, path=self.path, names=self.names)
|
|
|
|
for item in self.comp:
|
|
|
|
for k in self._keys:
|
|
|
|
if getattr(self, item) is None:
|
|
|
|
setattr(r, k, getattr(self, k).cpu())
|
|
|
|
continue
|
|
|
|
|
|
|
|
setattr(r, item, getattr(self, item).cpu())
|
|
|
|
|
|
|
|
return r
|
|
|
|
return r
|
|
|
|
|
|
|
|
|
|
|
|
def numpy(self):
|
|
|
|
def numpy(self):
|
|
|
|
r = Results(orig_img=self.orig_img, path=self.path, names=self.names)
|
|
|
|
r = Results(orig_img=self.orig_img, path=self.path, names=self.names)
|
|
|
|
for item in self.comp:
|
|
|
|
for k in self._keys:
|
|
|
|
if getattr(self, item) is None:
|
|
|
|
setattr(r, k, getattr(self, k).numpy())
|
|
|
|
continue
|
|
|
|
|
|
|
|
setattr(r, item, getattr(self, item).numpy())
|
|
|
|
|
|
|
|
return r
|
|
|
|
return r
|
|
|
|
|
|
|
|
|
|
|
|
def cuda(self):
|
|
|
|
def cuda(self):
|
|
|
|
r = Results(orig_img=self.orig_img, path=self.path, names=self.names)
|
|
|
|
r = Results(orig_img=self.orig_img, path=self.path, names=self.names)
|
|
|
|
for item in self.comp:
|
|
|
|
for k in self._keys:
|
|
|
|
if getattr(self, item) is None:
|
|
|
|
setattr(r, k, getattr(self, k).cuda())
|
|
|
|
continue
|
|
|
|
|
|
|
|
setattr(r, item, getattr(self, item).cuda())
|
|
|
|
|
|
|
|
return r
|
|
|
|
return r
|
|
|
|
|
|
|
|
|
|
|
|
def to(self, *args, **kwargs):
|
|
|
|
def to(self, *args, **kwargs):
|
|
|
|
r = Results(orig_img=self.orig_img, path=self.path, names=self.names)
|
|
|
|
r = Results(orig_img=self.orig_img, path=self.path, names=self.names)
|
|
|
|
for item in self.comp:
|
|
|
|
for k in self._keys:
|
|
|
|
if getattr(self, item) is None:
|
|
|
|
setattr(r, k, getattr(self, k).to(*args, **kwargs))
|
|
|
|
continue
|
|
|
|
|
|
|
|
setattr(r, item, getattr(self, item).to(*args, **kwargs))
|
|
|
|
|
|
|
|
return r
|
|
|
|
return r
|
|
|
|
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
def __len__(self):
|
|
|
|
for item in self.comp:
|
|
|
|
for k in self._keys:
|
|
|
|
if getattr(self, item) is None:
|
|
|
|
return len(getattr(self, k))
|
|
|
|
continue
|
|
|
|
|
|
|
|
return len(getattr(self, item))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
def __str__(self):
|
|
|
|
str_out = ''
|
|
|
|
return ''.join(getattr(self, k).__str__() for k in self._keys)
|
|
|
|
for item in self.comp:
|
|
|
|
|
|
|
|
if getattr(self, item) is None:
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
str_out = str_out + getattr(self, item).__str__()
|
|
|
|
|
|
|
|
return str_out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
def __repr__(self):
|
|
|
|
str_out = ''
|
|
|
|
return ''.join(getattr(self, k).__repr__() for k in self._keys)
|
|
|
|
for item in self.comp:
|
|
|
|
|
|
|
|
if getattr(self, item) is None:
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
str_out = str_out + getattr(self, item).__repr__()
|
|
|
|
|
|
|
|
return str_out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __getattr__(self, attr):
|
|
|
|
def __getattr__(self, attr):
|
|
|
|
name = self.__class__.__name__
|
|
|
|
name = self.__class__.__name__
|
|
|
@ -226,20 +211,16 @@ class Boxes:
|
|
|
|
return self.xywh / self.orig_shape[[1, 0, 1, 0]]
|
|
|
|
return self.xywh / self.orig_shape[[1, 0, 1, 0]]
|
|
|
|
|
|
|
|
|
|
|
|
def cpu(self):
|
|
|
|
def cpu(self):
|
|
|
|
boxes = self.boxes.cpu()
|
|
|
|
return Boxes(self.boxes.cpu(), self.orig_shape)
|
|
|
|
return Boxes(boxes, self.orig_shape)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def numpy(self):
|
|
|
|
def numpy(self):
|
|
|
|
boxes = self.boxes.numpy()
|
|
|
|
return Boxes(self.boxes.numpy(), self.orig_shape)
|
|
|
|
return Boxes(boxes, self.orig_shape)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def cuda(self):
|
|
|
|
def cuda(self):
|
|
|
|
boxes = self.boxes.cuda()
|
|
|
|
return Boxes(self.boxes.cuda(), self.orig_shape)
|
|
|
|
return Boxes(boxes, self.orig_shape)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def to(self, *args, **kwargs):
|
|
|
|
def to(self, *args, **kwargs):
|
|
|
|
boxes = self.boxes.to(*args, **kwargs)
|
|
|
|
return Boxes(self.boxes.to(*args, **kwargs), self.orig_shape)
|
|
|
|
return Boxes(boxes, self.orig_shape)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def pandas(self):
|
|
|
|
def pandas(self):
|
|
|
|
LOGGER.info('results.pandas() method not yet implemented')
|
|
|
|
LOGGER.info('results.pandas() method not yet implemented')
|
|
|
@ -272,8 +253,7 @@ class Boxes:
|
|
|
|
f'shape: {self.boxes.shape}\n' + f'dtype: {self.boxes.dtype}\n + {self.boxes.__repr__()}')
|
|
|
|
f'shape: {self.boxes.shape}\n' + f'dtype: {self.boxes.dtype}\n + {self.boxes.__repr__()}')
|
|
|
|
|
|
|
|
|
|
|
|
def __getitem__(self, idx):
|
|
|
|
def __getitem__(self, idx):
|
|
|
|
boxes = self.boxes[idx]
|
|
|
|
return Boxes(self.boxes[idx], self.orig_shape)
|
|
|
|
return Boxes(boxes, self.orig_shape)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __getattr__(self, attr):
|
|
|
|
def __getattr__(self, attr):
|
|
|
|
name = self.__class__.__name__
|
|
|
|
name = self.__class__.__name__
|
|
|
@ -331,20 +311,16 @@ class Masks:
|
|
|
|
return self.masks
|
|
|
|
return self.masks
|
|
|
|
|
|
|
|
|
|
|
|
def cpu(self):
|
|
|
|
def cpu(self):
|
|
|
|
masks = self.masks.cpu()
|
|
|
|
return Masks(self.masks.cpu(), self.orig_shape)
|
|
|
|
return Masks(masks, self.orig_shape)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def numpy(self):
|
|
|
|
def numpy(self):
|
|
|
|
masks = self.masks.numpy()
|
|
|
|
return Masks(self.masks.numpy(), self.orig_shape)
|
|
|
|
return Masks(masks, self.orig_shape)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def cuda(self):
|
|
|
|
def cuda(self):
|
|
|
|
masks = self.masks.cuda()
|
|
|
|
return Masks(self.masks.cuda(), self.orig_shape)
|
|
|
|
return Masks(masks, self.orig_shape)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def to(self, *args, **kwargs):
|
|
|
|
def to(self, *args, **kwargs):
|
|
|
|
masks = self.masks.to(*args, **kwargs)
|
|
|
|
return Masks(self.masks.to(*args, **kwargs), self.orig_shape)
|
|
|
|
return Masks(masks, self.orig_shape)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __len__(self): # override len(results)
|
|
|
|
def __len__(self): # override len(results)
|
|
|
|
return len(self.masks)
|
|
|
|
return len(self.masks)
|
|
|
@ -357,8 +333,7 @@ class Masks:
|
|
|
|
f'shape: {self.masks.shape}\n' + f'dtype: {self.masks.dtype}\n + {self.masks.__repr__()}')
|
|
|
|
f'shape: {self.masks.shape}\n' + f'dtype: {self.masks.dtype}\n + {self.masks.__repr__()}')
|
|
|
|
|
|
|
|
|
|
|
|
def __getitem__(self, idx):
|
|
|
|
def __getitem__(self, idx):
|
|
|
|
masks = self.masks[idx]
|
|
|
|
return Masks(self.masks[idx], self.orig_shape)
|
|
|
|
return Masks(masks, self.orig_shape)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __getattr__(self, attr):
|
|
|
|
def __getattr__(self, attr):
|
|
|
|
name = self.__class__.__name__
|
|
|
|
name = self.__class__.__name__
|
|
|
|