Skip to content

Unable to Infer from a trained custom model #10180

@hey24sheep

Description

@hey24sheep

Search before asking

Question

Hi, I am facing an issue while trying to infer data. What am I doing wrong here? Can anyone please help me.

Trained like

!python -m torch.distributed.run --nproc_per_node 2 yolov5/train.py --data train.yaml --weights yolov5m6.pt --img 1280 --epochs 2 --device 0,1

Infering Like

!python yolov5/detect.py --weights 'last.pt' --imgsz 1280 --source 'test.jpg' --data 'train.yaml'

# I also tried without "--data" arg
# python yolov5/detect.py --weights 'last.pt' --imgsz 1280 --source 'test.jpg'

Error


detect: weights=['last.pt'], source=test.jpg, data=train.yaml, imgsz=[1280, 1280], conf_thres=0.25, iou_thres=0.45, max_det=1000, device=, view_img=False, save_txt=False, save_conf=False, save_crop=False, nosave=False, classes=None, agnostic_nms=False, augment=False, visualize=False, update=False, project=yolov5/runs/detect, name=exp, exist_ok=False, line_thickness=3, hide_labels=False, hide_conf=False, half=False, dnn=False, vid_stride=1
YOLOv5 🚀 v6.2-243-g5e03f5f Python-3.7.12 torch-1.11.0 CUDA:0 (Tesla T4, 15110MiB)

Fusing layers... 
Model summary: 276 layers, 35341272 parameters, 0 gradients, 49.1 GFLOPs
Traceback (most recent call last):
  File "yolov5/detect.py", line 258, in <module>
    main(opt)
  File "yolov5/detect.py", line 253, in main
    run(**vars(opt))
  File "/opt/conda/lib/python3.7/site-packages/torch/autograd/grad_mode.py", line 27, in decorate_context
    return func(*args, **kwargs)
  File "yolov5/detect.py", line 95, in run
    model = DetectMultiBackend(weights, device=device, dnn=dnn, data=data, fp16=half)
  File "/kaggle/working/yolov5/models/common.py", line 501, in __init__
    if names[0] == 'n01440764' and len(names) == 1000:  # ImageNet
KeyError: 0

Other infer code

model = torch.hub.load('./yolov5', 'custom', path='last.pt', source='local') 
model.to('cuda')
img = cv2.imread("test.jpg")
img = cv2.resize(img, (1280, 1280))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# I tried the below code as well, it doesn't work
# im = torch.from_numpy(img).to('cuda')
# im = im.half() if True else im.float()  # uint8 to fp16/32
# im /= 255  # 0 - 255 to 0.0 - 1.0
# if len(im.shape) == 3:
    # im = im[None]  # expand for batch dim

results = model(img)

Error

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_23/1415234167.py in <module>
     14 # print(t_img.shape)
     15 # Inference
---> 16 results = model(img)
     17 # Results, change the flowing to: results.show()
     18 results.show()  # or .show(), .save(), .crop(), .pandas(), etc

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1108         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1109                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1110             return forward_call(*input, **kwargs)
   1111         # Do not call functions when jit is used
   1112         full_backward_hooks, non_full_backward_hooks = [], []

/kaggle/working/yolov5/models/yolo.py in forward(self, x, augment, profile, visualize)
    207         if augment:
    208             return self._forward_augment(x)  # augmented inference, None
--> 209         return self._forward_once(x, profile, visualize)  # single-scale inference, train
    210 
    211     def _forward_augment(self, x):

/kaggle/working/yolov5/models/yolo.py in _forward_once(self, x, profile, visualize)
    119             if profile:
    120                 self._profile_one_layer(m, x, dt)
--> 121             x = m(x)  # run
    122             y.append(x if m.i in self.save else None)  # save output
    123             if visualize:

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1108         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1109                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1110             return forward_call(*input, **kwargs)
   1111         # Do not call functions when jit is used
   1112         full_backward_hooks, non_full_backward_hooks = [], []

/kaggle/working/yolov5/models/common.py in forward(self, x)
     55 
     56     def forward(self, x):
---> 57         return self.act(self.bn(self.conv(x)))
     58 
     59     def forward_fuse(self, x):

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1108         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1109                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1110             return forward_call(*input, **kwargs)
   1111         # Do not call functions when jit is used
   1112         full_backward_hooks, non_full_backward_hooks = [], []

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/conv.py in forward(self, input)
    445 
    446     def forward(self, input: Tensor) -> Tensor:
--> 447         return self._conv_forward(input, self.weight, self.bias)
    448 
    449 class Conv3d(_ConvNd):

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight, bias)
    442                             _pair(0), self.dilation, self.groups)
    443         return F.conv2d(input, weight, bias, self.stride,
--> 444                         self.padding, self.dilation, self.groups)
    445 
    446     def forward(self, input: Tensor) -> Tensor:

TypeError: conv2d() received an invalid combination of arguments - got (numpy.ndarray, Parameter, NoneType, tuple, tuple, tuple, int), but expected one of:
 * (Tensor input, Tensor weight, Tensor bias, tuple of ints stride, tuple of ints padding, tuple of ints dilation, int groups)
      didn't match because some of the arguments have invalid types: (!numpy.ndarray!, !Parameter!, !NoneType!, !tuple!, !tuple!, !tuple!, int)
 * (Tensor input, Tensor weight, Tensor bias, tuple of ints stride, str padding, tuple of ints dilation, int groups)
      didn't match because some of the arguments have invalid types: (!numpy.ndarray!, !Parameter!, !NoneType!, !tuple!, !tuple!, !tuple!, int)

Additional

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions