Skip to content

Cannot run model with URL as argument #2246

@ju-w

Description

@ju-w

Hello,

Bug

#2194 broke model.forward() with URLs as argument and subsequently the Colab example https://colab.research.google.com/github/pytorch/pytorch.github.io/blob/master/assets/hub/ultralytics_yolov5.ipynb

Reproduce

e.g. run Colab example.

Trace

ValueError                                Traceback (most recent call last)
<ipython-input-1-86a3d667ec69> in <module>()
      9 
     10 # Inference
---> 11 results = model(imgs)
     12 
     13 # Results

2 frames
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    725             result = self._slow_forward(*input, **kwargs)
    726         else:
--> 727             result = self.forward(*input, **kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),

/root/.cache/torch/hub/ultralytics_yolov5_master/models/common.py in forward(self, imgs, size, augment, profile)
    201             if isinstance(im, str):  # filename or uri
    202                 im = Image.open(requests.get(im, stream=True).raw if im.startswith('http') else im)  # open
--> 203             files.append(Path(im.filename).with_suffix('.jpg').name if isinstance(im, Image.Image) else f'image{i}.jpg')
    204             im = np.array(im)  # to numpy
    205             if im.shape[0] < 5:  # image in CHW

/usr/lib/python3.6/pathlib.py in with_suffix(self, suffix)
    835         name = self.name
    836         if not name:
--> 837             raise ValueError("%r has an empty name" % (self,))
    838         old_suffix = self.suffix
    839         if not old_suffix:

ValueError: PosixPath('.') has an empty name

Reason

yolov5/models/common.py

Lines 201 to 203 in d2e754b

if isinstance(im, str): # filename or uri
im = Image.open(requests.get(im, stream=True).raw if im.startswith('http') else im) # open
files.append(Path(im.filename).with_suffix('.jpg').name if isinstance(im, Image.Image) else f'image{i}.jpg')

An Image created from requests.get download will not have im.filename property set, thus Path(im.filename) will fail.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions