-
-
Notifications
You must be signed in to change notification settings - Fork 15.9k
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
Comments
👋 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. RequirementsPython>=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 EnvironmentsYOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):
StatusIf 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. |
Update: I just tried it with python >= 3.7.0 and still have the same problem. Environment:
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. |
@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 Simple Inference ExampleThis example loads a pretrained YOLOv5s model from PyTorch Hub as 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! |
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. |
@MrEarle as the above example shows you can pass your numpy arrays directly to the model. |
Yes, but I get different results. Unlike the example shown on the issue I
linked.
Saludos!
Benjamin
…On Sat, Mar 19, 2022, 5:47 PM Glenn Jocher ***@***.***> wrote:
@MrEarle <https://github.com/MrEarle> as the above example shows you can
pass your numpy arrays directly to the model.
—
Reply to this email directly, view it on GitHub
<#7030 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AHJ6OCODT7ULTFRITH3OWDTVAY4NLANCNFSM5RAL7QPA>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
@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. |
@MrEarle don't pass a tensor. Pass anything else from the example. Tensors are for training only. |
👋 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 ⭐! |
Interesting, is there a particular reason why we should not use torch tensors during inference ? |
I have found this answer here : #2722 |
@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. |
@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. |
@hoxnocha You can convert your tensor batch of images to numpy arrays using |
Search before asking
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:
Environment
Minimal Reproducible Example
Additional
No response
Are you willing to submit a PR?
The text was updated successfully, but these errors were encountered: