# Buggy code
def __init__(self, imgs, pred, names=None):
    super(Detections, self).__init__()
    self.imgs = imgs  # list of images as numpy arrays
    self.pred = pred  # list of tensors pred[0] = (xyxy, conf, cls)
    self.names = names  # class names
    self.xyxy = pred  # xyxy pixels
    self.xywh = [xyxy2xywh(x) for x in pred]  # xywh pixels
    gn = [
        torch.Tensor([*[im.shape[i] for i in [1, 0, 1, 0]], 1.0, 1.0]) for im in imgs
    ]  # normalization gains
    self.xyxyn = [x / g for x, g in zip(self.xyxy, gn)]  # xyxy normalized
    self.xywhn = [x / g for x, g in zip(self.xywh, gn)]  # xywh normalized
    self.n = len(self.pred)


# Traceback (RuntimeError)
Using cache found in /home/appuser/.cache/torch/hub/ultralytics_yolov5_master

from  n    params  module                                  arguments
0                -1  1      7040  models.common.Focus                     [3, 64, 3]
1                -1  1     73984  models.common.Conv                      [64, 128, 3, 2]
2                -1  1    161152  models.common.BottleneckCSP             [128, 128, 3]
3                -1  1    295424  models.common.Conv                      [128, 256, 3, 2]
4                -1  1   1627904  models.common.BottleneckCSP             [256, 256, 9]
5                -1  1   1180672  models.common.Conv                      [256, 512, 3, 2]
6                -1  1   6499840  models.common.BottleneckCSP             [512, 512, 9]
7                -1  1   4720640  models.common.Conv                      [512, 1024, 3, 2]
8                -1  1   2624512  models.common.SPP                       [1024, 1024, [5, 9, 13]]
9                -1  1  10234880  models.common.BottleneckCSP             [1024, 1024, 3, False]
10                -1  1    525312  models.common.Conv                      [1024, 512, 1, 1]
11                -1  1         0  torch.nn.modules.upsampling.Upsample    [None, 2, 'nearest']
12           [-1, 6]  1         0  models.common.Concat                    [1]
13                -1  1   2823680  models.common.BottleneckCSP             [1024, 512, 3, False]
14                -1  1    131584  models.common.Conv                      [512, 256, 1, 1]
15                -1  1         0  torch.nn.modules.upsampling.Upsample    [None, 2, 'nearest']
16           [-1, 4]  1         0  models.common.Concat                    [1]
17                -1  1    707328  models.common.BottleneckCSP             [512, 256, 3, False]
18                -1  1    590336  models.common.Conv                      [256, 256, 3, 2]
19          [-1, 14]  1         0  models.common.Concat                    [1]
20                -1  1   2561536  models.common.BottleneckCSP             [512, 512, 3, False]
21                -1  1   2360320  models.common.Conv                      [512, 512, 3, 2]
22          [-1, 10]  1         0  models.common.Concat                    [1]
23                -1  1  10234880  models.common.BottleneckCSP             [1024, 1024, 3, False]
24      [17, 20, 23]  1    457725  models.yolo.Detect                      [80, [[10, 13, 16, 30, 33, 23], [30, 61, 62, 45, 59, 119], [116, 90, 156, 198, 373, 326]], [256, 512, 1024]]
Model Summary: 499 layers, 47818749 parameters, 47818749 gradients

Fusing layers...
Model Summary: 400 layers, 47790077 parameters, 47790077 gradients
Adding autoShape...

RuntimeError                              Traceback (most recent call last)
<ipython-input-3-5dc3294ee725> in <module>
3 image_path = 'data/images/bus.jpg'
4 img = cv2.imread(image_path)
----> 5 r = model(img)

~/.conda/envs/pytorch/lib/python3.7/site-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(),

~/.cache/torch/hub/ultralytics_yolov5_master/models/common.py in forward(self, imgs, size, augment, profile)
172                 y[i][:, :4] = scale_coords(shape1, y[i][:, :4], shape0[i])
173
--> 174         return Detections(imgs, y, self.names)
175
176

~/.cache/torch/hub/ultralytics_yolov5_master/models/common.py in __init__(self, imgs, pred, names)
185         self.xywh = [xyxy2xywh(x) for x in pred]  # xywh pixels
186         gn = [torch.Tensor([*[im.shape[i] for i in [1, 0, 1, 0]], 1., 1.]) for im in imgs]  # normalization gains
--> 187         self.xyxyn = [x / g for x, g in zip(self.xyxy, gn)]  # xyxy normalized
188         self.xywhn = [x / g for x, g in zip(self.xywh, gn)]  # xywh normalized
189         self.n = len(self.pred)

~/.cache/torch/hub/ultralytics_yolov5_master/models/common.py in <listcomp>(.0)
185         self.xywh = [xyxy2xywh(x) for x in pred]  # xywh pixels
186         gn = [torch.Tensor([*[im.shape[i] for i in [1, 0, 1, 0]], 1., 1.]) for im in imgs]  # normalization gains
--> 187         self.xyxyn = [x / g for x, g in zip(self.xyxy, gn)]  # xyxy normalized
188         self.xywhn = [x / g for x, g in zip(self.xywh, gn)]  # xywh normalized
189         self.n = len(self.pred)

RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!

# Fixed code
def __init__(self, imgs, pred, names=None):
    super(Detections, self).__init__()
    self.imgs = imgs  # list of images as numpy arrays
    self.pred = pred  # list of tensors pred[0] = (xyxy, conf, cls)
    self.names = names  # class names
    self.xyxy = pred  # xyxy pixels
    self.xywh = [xyxy2xywh(x) for x in pred]  # xywh pixels
    d = pred[0].device  # device
    gn = [
        torch.tensor([*[im.shape[i] for i in [1, 0, 1, 0]], 1.0, 1.0], device=d)
        for im in imgs
    ]  # normalizations
    self.xyxyn = [x / g for x, g in zip(self.xyxy, gn)]  # xyxy normalized
    self.xywhn = [x / g for x, g in zip(self.xywh, gn)]  # xywh normalized
    self.n = len(self.pred)