# Buggy code
def forward(self, imgs, size=640, augment=False, profile=False):
    # Inference from various sources. For height=720, width=1280, RGB images example inputs are:
    #   filename:   imgs = 'data/samples/zidane.jpg'
    #   URI:             = 'https://github.com/ultralytics/yolov5/releases/download/v1.0/zidane.jpg'
    #   OpenCV:          = cv2.imread('image.jpg')[:,:,::-1]  # HWC BGR to RGB x(720,1280,3)
    #   PIL:             = Image.open('image.jpg')  # HWC x(720,1280,3)
    #   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

    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, files = [], [], []  # image and inference shapes, filenames
    for i, im in enumerate(imgs):
        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"
        )
        im = np.array(im)  # to numpy
        if im.shape[0] < 5:  # image in CHW
            im = im.transpose((1, 2, 0))  # reverse dataloader .transpose(2, 0, 1)
        im = (
            im[:, :, :3] if im.ndim == 3 else np.tile(im[:, :, None], 3)
        )  # enforce 3ch input
        s = im.shape[:2]  # HWC
        shape0.append(s)  # image shape
        g = size / max(s)  # gain
        shape1.append([y * g for y in s])
        imgs[i] = im  # update
    shape1 = [
        make_divisible(x, int(self.stride.max())) for x in np.stack(shape1, 0).max(0)
    ]  # inference shape
    x = [letterbox(im, new_shape=shape1, auto=False)[0] for im in imgs]  # pad
    x = np.stack(x, 0) if n > 1 else x[0][None]  # stack
    x = np.ascontiguousarray(x.transpose((0, 3, 1, 2)))  # BHWC to BCHW
    x = torch.from_numpy(x).to(p.device).type_as(p) / 255.0  # uint8 to fp16/32

    # Inference
    with torch.no_grad():
        y = self.model(x, augment, profile)[0]  # forward
    y = non_max_suppression(
        y, conf_thres=self.conf, iou_thres=self.iou, classes=self.classes
    )  # NMS

    # Post-process
    for i in range(n):
        scale_coords(shape1, y[i][:, :4], shape0[i])

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


# Traceback (ValueError)
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

# Fixed code
def forward(self, imgs, size=640, augment=False, profile=False):
    # Inference from various sources. For height=720, width=1280, RGB images example inputs are:
    #   filename:   imgs = 'data/samples/zidane.jpg'
    #   URI:             = 'https://github.com/ultralytics/yolov5/releases/download/v1.0/zidane.jpg'
    #   OpenCV:          = cv2.imread('image.jpg')[:,:,::-1]  # HWC BGR to RGB x(720,1280,3)
    #   PIL:             = Image.open('image.jpg')  # HWC x(720,1280,3)
    #   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

    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, files = [], [], []  # image and inference shapes, filenames
    for i, im in enumerate(imgs):
        if isinstance(im, str):  # filename or uri
            im, f = (
                Image.open(
                    requests.get(im, stream=True).raw if im.startswith("http") else im
                ),
                im,
            )  # open
            im.filename = f  # for uri
        files.append(
            Path(im.filename).with_suffix(".jpg").name
            if isinstance(im, Image.Image)
            else f"image{i}.jpg"
        )
        im = np.array(im)  # to numpy
        if im.shape[0] < 5:  # image in CHW
            im = im.transpose((1, 2, 0))  # reverse dataloader .transpose(2, 0, 1)
        im = (
            im[:, :, :3] if im.ndim == 3 else np.tile(im[:, :, None], 3)
        )  # enforce 3ch input
        s = im.shape[:2]  # HWC
        shape0.append(s)  # image shape
        g = size / max(s)  # gain
        shape1.append([y * g for y in s])
        imgs[i] = im  # update
    shape1 = [
        make_divisible(x, int(self.stride.max())) for x in np.stack(shape1, 0).max(0)
    ]  # inference shape
    x = [letterbox(im, new_shape=shape1, auto=False)[0] for im in imgs]  # pad
    x = np.stack(x, 0) if n > 1 else x[0][None]  # stack
    x = np.ascontiguousarray(x.transpose((0, 3, 1, 2)))  # BHWC to BCHW
    x = torch.from_numpy(x).to(p.device).type_as(p) / 255.0  # uint8 to fp16/32

    # Inference
    with torch.no_grad():
        y = self.model(x, augment, profile)[0]  # forward
    y = non_max_suppression(
        y, conf_thres=self.conf, iou_thres=self.iou, classes=self.classes
    )  # NMS

    # Post-process
    for i in range(n):
        scale_coords(shape1, y[i][:, :4], shape0[i])

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