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

Different results when using torch tensor and image path #7030

Closed
1 of 2 tasks
MrEarle opened this issue Mar 18, 2022 · 15 comments
Closed
1 of 2 tasks

Different results when using torch tensor and image path #7030

MrEarle opened this issue Mar 18, 2022 · 15 comments
Labels
bug Something isn't working Stale

Comments

@MrEarle
Copy link

MrEarle commented Mar 18, 2022

Search before asking

  • I have searched the YOLOv5 issues and found no similar bug report.

YOLOv5 Component

Detection, PyTorch Hub

Bug

I have an image in png form. I also have that same image as a tensor (obtained as shown in #36 ).

The problem is that, when I use a tensor as an input, the result is nonsensical, outputting a tensor with a shape of [18900, 85]. I suppose one of those dimensions should be 6, for the xyxy confidence and class id.

On the other hand, if I use the path to the image as an input, I get what I would expect.

Both of these cases are shown in the image below:

image

Environment

  • YOLOv5 🚀 2022-3-17 torch 1.9.1+cu102 CUDA:0 (NVIDIA GeForce GTX 1080 Ti, 11178MiB)
  • OS: Ubuntu 18.04
  • Python: 3.6.8

Minimal Reproducible Example

import torch
from PIL import Image

# Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5m')

img = Image.open('path_to_image').resize((640, 480), Image.ANTIALIAS).convert('RGB')
img = torch.tensor(np.array(img)).permute((2, 0, 1)).unsqueeze(0).float() / 255
result = model(img)
print(result.shape)

result = model('path_to_image')
print(result.xyxyn)

Additional

No response

Are you willing to submit a PR?

  • Yes I'd like to help by submitting a PR!
@MrEarle MrEarle added the bug Something isn't working label Mar 18, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Mar 18, 2022

👋 Hello @MrEarle, 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 support@ultralytics.com.

Requirements

Python>=3.7.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.

@MrEarle
Copy link
Author

MrEarle commented Mar 18, 2022

Update: I just tried it with python >= 3.7.0 and still have the same problem.

Environment:

  • YOLOv5 🚀 2022-3-17 torch 1.11.0+cu102 CUDA:0 (NVIDIA GeForce GTX 1080 Ti, 11178MiB)
  • Python 3.7.3
  • OS: Ubuntu 18.04 LTS

Edit: I also tried using numpy, and I get a completely different result. Using numpy gives no detections on the same data as the used tensor.

image

@glenn-jocher
Copy link
Member

glenn-jocher commented Mar 19, 2022

@MrEarle 👋 Hello! Thanks for asking about handling inference results. Your workflow is way overly complicated, none of your custom code is needed, just follow example below and pass im.png straight to your model.

YOLOv5 🚀 PyTorch Hub models allow for simple model loading and inference in a pure python environment without using detect.py.

Simple Inference Example

This example loads a pretrained YOLOv5s model from PyTorch Hub as model and passes an image for inference. 'yolov5s' is the YOLOv5 'small' model. For details on all available models please see the README. Custom models can also be loaded, including custom trained PyTorch models and their exported variants, i.e. ONNX, TensorRT, TensorFlow, OpenVINO YOLOv5 models.

import torch

# Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')  # or yolov5m, yolov5l, yolov5x, etc.
# model = torch.hub.load('ultralytics/yolov5', 'custom', 'path/to/best.pt')  # custom trained model

# Images
im = 'https://ultralytics.com/images/zidane.jpg'  # or file, Path, URL, PIL, OpenCV, numpy, list

# Inference
results = model(im)

# Results
results.print()  # or .show(), .save(), .crop(), .pandas(), etc.

results.xyxy[0]  # im predictions (tensor)
results.pandas().xyxy[0]  # im predictions (pandas)
#      xmin    ymin    xmax   ymax  confidence  class    name
# 0  749.50   43.50  1148.0  704.5    0.874023      0  person
# 2  114.75  195.75  1095.0  708.0    0.624512      0  person
# 3  986.00  304.00  1028.0  420.0    0.286865     27     tie

See YOLOv5 PyTorch Hub Tutorial for details.

Good luck 🍀 and let us know if you have any other questions!

@MrEarle
Copy link
Author

MrEarle commented Mar 19, 2022

I understand that I can use the path to the image.

The thing is that I don't have images saved on disk (or online). I'm using a simulator to get images, and I get a bumpy array as images.

I need to process thousands of images, so saving them to disk and then loading them for inference is not really useful for me.

@glenn-jocher
Copy link
Member

@MrEarle as the above example shows you can pass your numpy arrays directly to the model.

@MrEarle
Copy link
Author

MrEarle commented Mar 20, 2022 via email

@glenn-jocher
Copy link
Member

glenn-jocher commented Mar 20, 2022

@MrEarle see YOLOv5 PyTorch Hub Tutorial for details.

@MrEarle
Copy link
Author

MrEarle commented Mar 24, 2022

@MrEarle see YOLOv5 PyTorch Hub Tutorial for details.

Yes. The code I included in this issue is a replica of the code shown there. That's why I made this issue. I'm doing the same thing, but with a different image, and I get that nonsensical result.

I'm not getting the bounding boxes, confidence and class as I should. I get this (18900 x 85) dimensional tensor.

@glenn-jocher
Copy link
Member

@MrEarle don't pass a tensor. Pass anything else from the example. Tensors are for training only.

@github-actions
Copy link
Contributor

github-actions bot commented Apr 24, 2022

👋 Hello, this issue has been automatically marked as stale because it has not had recent activity. Please note it will be closed if no further activity occurs.

Access additional YOLOv5 🚀 resources:

Access additional Ultralytics ⚡ resources:

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 YOLOv5 🚀 and Vision AI ⭐!

@valentin-fngr
Copy link

@MrEarle don't pass a tensor. Pass anything else from the example. Tensors are for training only.

Interesting, is there a particular reason why we should not use torch tensors during inference ?
Won't I want to convert my image as a tensor and send it to the GPU to speed up inference ? (this is a noob question)
Thanks

@valentin-fngr
Copy link

valentin-fngr commented Jun 2, 2023

@MrEarle don't pass a tensor. Pass anything else from the example. Tensors are for training only.

Interesting, is there a particular reason why we should not use torch tensors during inference ? Won't I want to convert my image as a tensor and send it to the GPU to speed up inference ? (this is a noob question) Thanks

I have found this answer here : #2722

@glenn-jocher
Copy link
Member

@valentin-fngr The post you found in the discussion confirms that tensors are used during training only. During inference, you can pass images directly as opposed to using tensors. The example provided in the documentation on PyTorch Hub model loading provides an easy way of how you can use your numpy array images without the need to save them on disk. These numpy arrays can be passed directly to the tensorflow model without being converted to GPU tensors and you should get the expected results without issues.

@hoxnocha
Copy link

@glenn-jocher , may i ask a question. Since my dataloader only returns tensor describe a batch of images with shape BCHW, how could I use yolov5 to detect the object i need for further training? Should I convert this tensor to some other format.

@glenn-jocher
Copy link
Member

@hoxnocha You can convert your tensor batch of images to numpy arrays using tensor.numpy(), and then pass these numpy arrays to the YOLOv5 model for inference as shown in the provided example. This way, you can use your dataloader to prepare your data for training and then convert the tensor outputs to numpy arrays for inference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Stale
Projects
None yet
Development

No branches or pull requests

4 participants