Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

YOLOv5 PyTorch Hub results.save() method retains filenames #2194

Merged
merged 4 commits into from
Feb 12, 2021
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions models/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,10 @@ def forward(self, imgs, size=640, augment=False, profile=False):
# numpy: = np.zeros((720,1280,3)) # HWC
# torch: = torch.zeros(16,3,720,1280) # BCHW
# multiple: = [Image.open('image1.jpg'), Image.open('image2.jpg'), ...] # list of images

orig_imgs = imgs
p = next(self.model.parameters()) # for device and type
if isinstance(imgs, torch.Tensor): # torch
return self.model(imgs.to(p.device).type_as(p), augment, profile) # inference

# Pre-process
n, imgs = (len(imgs), imgs) if isinstance(imgs, list) else (1, [imgs]) # number of images, list of images
shape0, shape1 = [], [] # image and inference shapes
Expand Down Expand Up @@ -224,16 +223,17 @@ def forward(self, imgs, size=640, augment=False, profile=False):
for i in range(n):
scale_coords(shape1, y[i][:, :4], shape0[i])

return Detections(imgs, y, self.names)
return Detections(imgs, y, self.names, orig_imgs)


class Detections:
# detections class for YOLOv5 inference results
def __init__(self, imgs, pred, names=None):
def __init__(self, imgs, pred, names=None, orig_imgs=None):
super(Detections, self).__init__()
d = pred[0].device # device
gn = [torch.tensor([*[im.shape[i] for i in [1, 0, 1, 0]], 1., 1.], device=d) for im in imgs] # normalizations
self.imgs = imgs # list of images as numpy arrays
self.orig_imgs = imgs # list of original names
self.pred = pred # list of tensors pred[0] = (xyxy, conf, cls)
self.names = names # class names
self.xyxy = pred # xyxy pixels
Expand All @@ -242,7 +242,7 @@ def __init__(self, imgs, pred, names=None):
self.xywhn = [x / g for x, g in zip(self.xywh, gn)] # xywh normalized
self.n = len(self.pred)

def display(self, pprint=False, show=False, save=False, render=False, save_dir=''):
def display(self, pprint=False, show=False, save=False, render=False, save_dir='', save_orig_name=False):
colors = color_list()
for i, (img, pred) in enumerate(zip(self.imgs, self.pred)):
str = f'image {i + 1}/{len(self.pred)}: {img.shape[0]}x{img.shape[1]} '
Expand All @@ -260,7 +260,11 @@ def display(self, pprint=False, show=False, save=False, render=False, save_dir='
if show:
img.show(f'image {i}') # show
if save:
f = Path(save_dir) / f'results{i}.jpg'
if save_orig_name:
f = Path(save_dir) / f'results{orig_imgs[i]}.jpg'
else:
f = Path(save_dir) / f'results_{i}.jpg'
print(f)
img.save(f) # save
print(f"{'Saving' * (i == 0)} {f},", end='' if i < self.n - 1 else ' done.\n')
if render:
Expand All @@ -272,8 +276,8 @@ def print(self):
def show(self):
self.display(show=True) # show results

def save(self, save_dir=''):
self.display(save=True, save_dir=save_dir) # save results
def save(self, save_dir='', save_orig_name=False):
self.display(save=True, save_dir=save_dir, save_orig_name=save_orig_name) # save results

def render(self):
self.display(render=True) # render results
Expand Down