-
-
Notifications
You must be signed in to change notification settings - Fork 16.6k
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
How to "finish" raw inference output (with respect to anchors), to get the bounding boxes #6136
Comments
Hi @glenn-jocher, Wish you a Happy Year! This is an update of my experiment.
output is: torch.Size([1, 25200, 85])
The result is an instance of Detections class, but the result is wrong when for example using: The detections_instance is: detections_instance.print() image 1/1: Detected 1 objects of class personx1 , 1 objects of class carx1 , 18 objects of class traffic lightx18 , 1 objects of class tiex1 , 46 objects of class sports ballx46 , 1 objects of class bottlex1 , 1 objects of class cupx1 , 30 objects of class bowlx30 , 1 objects of class bananax1 , 13 objects of class applex13 , 9 objects of class broccolix9 , 24 objects of class dining tablex24 , 3 objects of class mousex3 , 58 objects of class clockx58 , The forward() of class AutoShape is modified to the following:
imgs id an image tensor (img_tensor) of size: torch.Size([1, 3, 640, 640]) Best regards, |
@hamedmh 👋 Hello! Thanks for asking about handling inference results. YOLOv5 🚀 PyTorch Hub models allow for simple model loading and inference in a python environment. 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, custom
# Images
img = 'https://ultralytics.com/images/zidane.jpg' # or file, Path, PIL, OpenCV, numpy, list
# Inference
results = model(img)
# Results
results.print # or .show(), .save(), .crop(), .pandas(), etc.
results.pandas().xyxy[0]
# xmin ymin xmax ymax confidence class name
# 0 749.50 43.50 1148.0 704.5 0.874023 0 person
# 1 433.50 433.50 517.5 714.5 0.687988 27 tie
# 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! |
Hi @glenn-jocher, Thank you for your answer! model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True) and get a Detections instance as an output (now when I modified forward() to take a single image tensor as an input, as explained in my post above). Best regards, |
@hamedmh good news 😃! Your original issue may now be fixed ✅ in PR #6195. This PR adds support for YOLOv5 CoreML inference. !python export.py --weights yolov5s.pt --include coreml # CoreML export
!python detect.py --weights yolov5s.mlmodel # CoreML inference (MacOS-only)
!python val.py --weights yolov5s.mlmodle # CoreML validation (MacOS-only)
model = torch.hub.load('ultralytics/yolov5', 'custom', 'yolov5s.mlmodel') # CoreML PyTorch Hub model To receive this update:
Thank you for spotting this issue and informing us of the problem. Please let us know if this update resolves the issue for you, and feel free to inform us of any other issues you discover or feature requests that come to mind. Happy trainings with YOLOv5 🚀! |
@glenn-jocher Thank you for the update! I'll test it. |
Hi @glenn-jocher, Now I have a more specific question regarding the same issue (post processing of predictions).
However, when I print the result:
My question is: How to understand and visualize this "raw" result? We have only one class (0). All the best, |
@hamedmh detect.py inference with trained weights is simple:
|
hi, there. I've got the same question as you, and I don't find a solution. The output is three tensor, how can I transfer them to bounding boxes? |
@DeepLearnerYe you can post-process the raw model output using the from models.yolo import Model
from utils.general import non_max_suppression
# Load the model
model = Model('path/to/yolov5s.yaml', ch=3, nc=80) # Replace with actual config and class number
ckpt = torch.load('path/to/checkpoint.pt') # Replace with actual checkpoint path
model.load_state_dict(ckpt['model'])
# Perform inference
img = torch.randn(1, 3, 640, 640) # Replace with actual input image
pred = model(img)
# Post-process the output
pred = non_max_suppression(pred, conf_thres=0.4, iou_thres=0.5)
# Output the bounding boxes
print(pred) Please replace the placeholder paths and input image with your actual data. Let me know if you encounter any more issues! |
Hi @glenn-jocher,
I use a "pure" Yolov5s model which outputs three tensors, such as: torch.Size([1, 3, 48, 80, 85]) , torch.Size([1, 3, 24, 40, 85]) , and torch.Size([1, 3, 12, 20, 85]).
I would like to convert them to bounding boxes.
I need to know which functions or equations can be used to get the bounding boxes.
Thanks!
The original issue:
@Kieran31 see PyTorch Hub tutorial for full inference examples on trained custom models.
Simple Example
This example loads a pretrained YOLOv5s model from PyTorch Hub as
model
and passes an image for inference.'yolov5s'
is the lightest and fastest YOLOv5 model. For details on all available models please see the README.YOLOv5 Tutorials
Originally posted by @glenn-jocher in #5304 (comment)
The text was updated successfully, but these errors were encountered: