Closed pathikg closed 1 year ago
@pathikg hello,
Thanks for providing the detailed replication steps and code.
I believe the issue you are encountering in the notebook setup may be related to the difference in version between the model used in val.py and the model used in your notebook. Additionally, since the confidence threshold
and IOU threshold
can also have an impact on the results, please ensure that you are using the same values for these parameters as in val.py.
Moreover, as you seem to be using a specific dataset, I would advise you to double-check if the paths to your files are correctly set.
I hope this helps. Please let me know if you have any further questions or issues.
Best regards!
Thanks for responding @glenn-jocher
I double checked everything and everything seems same as val.py
though, there's a once step which I did not replicate and it was the following: https://github.com/ultralytics/yolov5/blob/c3e4e94e944de3b41b3398e2f78e596384739339/val.py#L143
I did it as follows:
model = DetectMultiBackend(weights_path, device=device, dnn=False, data="dataset_7412/data.yaml", fp16=False)
So I repeated all the steps as earlier, and while doing the inference I faced this error:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
Cell In[13], line 6
4 nb, _, height, width = im.shape
5 im = im.to(device)
----> 6 preds, train_out = model(im), None
8 targets[:, 2:] *= torch.tensor((width, height, width, height), device=device)
9 lb = [targets[targets[:, 0] == i, 1:] for i in range(nb)] if save_hybrid else [] # for autolabelling
File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/torch/nn/modules/module.py:889, in Module._call_impl(self, *input, **kwargs)
887 result = self._slow_forward(*input, **kwargs)
888 else:
--> 889 result = self.forward(*input, **kwargs)
890 for hook in itertools.chain(
891 _global_forward_hooks.values(),
892 self._forward_hooks.values()):
893 hook_result = hook(self, input, result)
File /mnt/batch/tasks/shared/LS_root/mounts/clusters/pibit-ml-cpu/code/health-claim/yolov5/models/common.py:514, in DetectMultiBackend.forward(self, im, augment, visualize)
511 im = im.permute(0, 2, 3, 1) # torch BCHW to numpy BHWC shape(1,320,192,3)
513 if self.pt: # PyTorch
--> 514 y = self.model(im, augment=augment, visualize=visualize) if augment or visualize else self.model(im)
515 elif self.jit: # TorchScript
516 y = self.model(im)
File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/torch/nn/modules/module.py:889, in Module._call_impl(self, *input, **kwargs)
887 result = self._slow_forward(*input, **kwargs)
888 else:
--> 889 result = self.forward(*input, **kwargs)
890 for hook in itertools.chain(
891 _global_forward_hooks.values(),
892 self._forward_hooks.values()):
893 hook_result = hook(self, input, result)
File /mnt/batch/tasks/shared/LS_root/mounts/clusters/pibit-ml-cpu/code/health-claim/yolov5/models/yolo.py:209, in DetectionModel.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)
File /mnt/batch/tasks/shared/LS_root/mounts/clusters/pibit-ml-cpu/code/health-claim/yolov5/models/yolo.py:121, in BaseModel._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:
File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/torch/nn/modules/module.py:889, in Module._call_impl(self, *input, **kwargs)
887 result = self._slow_forward(*input, **kwargs)
888 else:
--> 889 result = self.forward(*input, **kwargs)
890 for hook in itertools.chain(
891 _global_forward_hooks.values(),
892 self._forward_hooks.values()):
893 hook_result = hook(self, input, result)
File /mnt/batch/tasks/shared/LS_root/mounts/clusters/pibit-ml-cpu/code/health-claim/yolov5/models/common.py:59, in Conv.forward_fuse(self, x)
58 def forward_fuse(self, x):
---> 59 return self.act(self.conv(x))
File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/torch/nn/modules/module.py:889, in Module._call_impl(self, *input, **kwargs)
887 result = self._slow_forward(*input, **kwargs)
888 else:
--> 889 result = self.forward(*input, **kwargs)
890 for hook in itertools.chain(
891 _global_forward_hooks.values(),
892 self._forward_hooks.values()):
893 hook_result = hook(self, input, result)
File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/torch/nn/modules/conv.py:399, in Conv2d.forward(self, input)
398 def forward(self, input: Tensor) -> Tensor:
--> 399 return self._conv_forward(input, self.weight, self.bias)
File /anaconda/envs/azureml_py38/lib/python3.8/site-packages/torch/nn/modules/conv.py:395, in Conv2d._conv_forward(self, input, weight, bias)
391 if self.padding_mode != 'zeros':
392 return F.conv2d(F.pad(input, self._reversed_padding_repeated_twice, mode=self.padding_mode),
393 weight, bias, self.stride,
394 _pair(0), self.dilation, self.groups)
--> 395 return F.conv2d(input, weight, bias, self.stride,
396 self.padding, self.dilation, self.groups)
RuntimeError: expected scalar type Byte but found Float
Do you know what can be done?
Okay the issue was solved after doing
im = im.float().to(device)
but still the predictions are not correct
e.g. when I load the torch.hub
model I get following predictions:
but when I am loading the model using snippets present in val.py on the same image, I get the following:
👋 Hello there! We wanted to give you a friendly reminder that this issue has not had any recent activity and may be closed soon, but don't worry - you can always reopen it if needed. If you still have any questions or concerns, please feel free to let us know how we can help.
For additional resources and information, please see the links below:
Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!
Thank you for your contributions to YOLO 🚀 and Vision AI ⭐
@pathikg
Glad to hear that you were able to resolve the initial issue regarding the expected scalar type. Regarding the discrepancy in predictions between the torch.hub
model and the custom model loading using the val.py
setup, I would suggest checking the following:
Ensure that the models are loaded with the same configuration and weights.
Validate that the pre-processing steps are consistent between the two methods, including input image normalization, resizing, and format.
Verify that the post-processing steps such as non-maximum suppression (NMS) and confidence thresholding are consistent across the models.
Double-check if the model input data (inference image) is the same for both methods.
If these aspects are aligned and you are still receiving inconsistent predictions, you may consider comparing the model structures and configurations between the torch.hub
model and the custom model loaded with the val.py
setup to ensure that they are indeed the same.
I hope these suggestions help. Let me know if you have further questions or require additional assistance.
Search before asking
YOLOv5 Component
Validation
Bug
My problem statement is given an image, ground truth bounding boxes and predicted bounding boxes generate a mAP score e.g.
So I tried replicating the function
run()
present in theval.py
in a separate notebook https://github.com/ultralytics/yolov5/blob/c3e4e94e944de3b41b3398e2f78e596384739339/val.py#L99I'll be adding the screenshots of my notebook with some outputs:
images/
andlabels/
batch_size=1
just to test)I ran the same code using
python val.py ...
and I got a mAP score of about 0.71 So my question is, am I missing anything in the code while running it separately in the notebook?Environment
Minimal Reproducible Example
Additional
No response
Are you willing to submit a PR?