Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inference made using detect.py differs than inference made using PyTorch Hub inference #6394

Closed
1 task done
edwardwang1 opened this issue Jan 22, 2022 · 4 comments
Closed
1 task done
Labels
question Further information is requested

Comments

@edwardwang1
Copy link

Search before asking

Question

I have noticed that the inference obtained through the detect.py script differs than the inference made using PyTorch Hub. At first, I thought the issue was due to a discrepancy between the model downloaded from PyTorch Hub and the local copy of the model. However, the issue persisted even when loading the local model. The model weights were generated using the train.py script. Essentially, the bounding box is tighter in the PyTorch Hub case than the detect.py case.

I found a similar issue here. #2224

Inference via detect.py
!python detect.py --img-size 288 --conf 0.01 --project /content/saveDir2/ --save-conf --save-txt --source /content/temptest/ --weights "./yolo50epoch288V2_best.pt"
results = model(image).pandas().xywhn[0]
Results (x_cent, y_cent, width, height, conf) = (0.13 0.43 0.25 0.27 0.80)

Inference via PyTorch Hub
model = torch.hub.load('ultralytics/yolov5', 'custom', path="./yolo50epoch288V2_best.pt", force_reload=True)
results = model(image).pandas().xywhn[0]
Inference via PyTorch Hub (Local)
model = torch.hub.load('.', 'custom', path="./yolo50epoch288V2_best.pt", source="local", force_reload=True)
results = model(image).pandas().xywhn[0]
Results (x_cent, y_cent, width, height, conf) = (0.13, 0.43, 0.15, 0.14, 0.59)

Environment
I am running everything in a Google Colab Environment
YOLOv5 🚀 2022-1-22 torch 1.10.0+cu111 CUDA:0 (Tesla T4, 15110MiB)
YOLOv5 install
!git clone https://github.com/ultralytics/yolov5.git
!mv ./yolov5/* ./
!pip install -r requirements.txt

The issue also persists on my local machine.
YOLOv5 v6.0-205-gbd815d4 torch 1.9.0 CUDA:0 (NVIDIA GeForce RTX 3080, 10240MiB)

Additional

No response

@edwardwang1 edwardwang1 added the question Further information is requested label Jan 22, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Jan 22, 2022

👋 Hello @edwardwang1, thank you for your interest in YOLOv5 🚀! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a 🐛 Bug Report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available.

For business inquiries or professional support requests please visit https://ultralytics.com or email Glenn Jocher at glenn.jocher@ultralytics.com.

Requirements

Python>=3.6.0 with all requirements.txt installed including PyTorch>=1.7. To get started:

git clone https://github.com/ultralytics/yolov5  # clone
cd yolov5
pip install -r requirements.txt  # install

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

CI CPU testing

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training (train.py), validation (val.py), inference (detect.py) and export (export.py) on MacOS, Windows, and Ubuntu every 24 hours and on every commit.

@glenn-jocher
Copy link
Member

glenn-jocher commented Jan 23, 2022

@edwardwang1 👋 hi, thanks for letting us know about this possible problem with YOLOv5 🚀. Your issue is not reproducible. Inference with detect.py and PyTorch Hub respectively for bus.jpg are below. If you are comparing both you should ensure all parameters are identical including batch size.

image

image

We've created a few short guidelines below to help users provide what we need in order to get started investigating a possible problem.

How to create a Minimal, Reproducible Example

When asking a question, people will be better able to provide help if you provide code that they can easily understand and use to reproduce the problem. This is referred to by community members as creating a minimum reproducible example. Your code that reproduces the problem should be:

  • Minimal – Use as little code as possible to produce the problem
  • Complete – Provide all parts someone else needs to reproduce the problem
  • Reproducible – Test the code you're about to provide to make sure it reproduces the problem

For Ultralytics to provide assistance your code should also be:

  • Current – Verify that your code is up-to-date with GitHub master, and if necessary git pull or git clone a new copy to ensure your problem has not already been solved in master.
  • Unmodified – Your problem must be reproducible using official YOLOv5 code without changes. Ultralytics does not provide support for custom code ⚠️.

If you believe your problem meets all the above criteria, please close this issue and raise a new one using the 🐛 Bug Report template with a minimum reproducible example to help us better understand and diagnose your problem.

Thank you! 😃

@edwardwang1
Copy link
Author

Hi Glenn,

Thanks for the prompt response. I tried the bus example using default yolov5x weights myself, and indeed found no discrepancy. It appears the issue is with my custom weights and custom classes.

I will update if I am able to resolve this.

@edwardwang1
Copy link
Author

I've determined that my issue was due to not specifying the image size when using PyTorch Hub inference.
Wrong
results = model(im,).pandas().xywhn[0]
Correct
results = model(im, size=288).pandas().xywhn[0]

I previously didn't include the size flag because I already resize my images to 288x288 prior to inference.
Thanks again to Glenn for putting me on the right track.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants