-
-
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
Pretrained yolov5 returns list of tensors instead of yolo objects #10750
Comments
Further analysis has shown that this behavior is triggert by the input being a tensor.
with the following code results in the expected behavior.
Providing a Tensor should however give the desired output aswell. |
👋 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 ⭐! |
@toschi23 hi there! It looks like you're encountering a behavior where the input being a tensor triggers the unexpected result. The desired output should still be achievable with the input being a tensor. We appreciate your patience as we look into this further. Thank you for sharing your findings with us. We'll investigate and work towards resolving this issue for you. Let me know if there's anything else I can assist you with! |
Is there any fix for this? |
@COWI-MI Thank you for your patience. The YOLOv5 model's input tensor format is already supported, and we are constantly working to improve and address any issues. Make sure to check the input formats, as YOLOv5 supports input in the form of a list of paths, PIL images, numpy arrays, or a torch tensor. If you have any further questions or need assistance with the tensor input, feel free to ask. We're here to help! |
@glenn-jocher Hello! Thnk you for all your replies here and there. Can you please maybe help me too? I've trained yolov5 segmentation model on custom dataset and trying use this model in my own pyton script. I use model as folowing:
And it gives me following error
as it can`t take default python list. But maybe numpy or Tensor. When I use Tensor created as follows
and paste it to model via
What am I doing wrong and how to get results as simple as in examples and tutorials? UPD: I've tryied to export model to torchscript format with segment\export.py script. Now when passing string |
Hello @Amodion, Thank you for reaching out and for your kind words! Let's address the issues you're encountering with your YOLOv5 segmentation model. Issue 1: AttributeError with List InputWhen you use a list of image paths, the model should handle it correctly. However, if you're encountering an Issue 2: Tensor Input Returning ListWhen you pass a tensor directly to the model, it should return a import torch
import cv2
# Load model
model = torch.hub.load('ultralytics/yolov5', 'custom', path='path/to/best.pt', force_reload=True)
# Prepare image
im = cv2.imread('path/to/image.jpg')
im = cv2.resize(im, (640, 640), interpolation=cv2.INTER_AREA)
im = im.transpose(2, 0, 1) # HWC to CHW
im = torch.from_numpy(im).float().div(255.0).unsqueeze(0) # Add batch dimension and normalize
# Inference
results = model(im)
# Check results
print(results)
print(results.pandas().xyxy[0]) # Convert to pandas DataFrame Issue 3: Incorrect Class LabelsIf you see incorrect class labels (e.g., '9' when you only have classes '1' and '2'), it might be due to a mismatch in the class configuration. Ensure that your model's class names are correctly set up in your custom dataset and during training. Additional Steps
Example with Image PathIf you prefer using image paths, here's how you can do it: # Load model
model = torch.hub.load('ultralytics/yolov5', 'custom', path='path/to/best.pt', force_reload=True)
# Inference with image path
results = model(['path/to/image.jpg'])
# Check results
print(results)
print(results.pandas().xyxy[0]) # Convert to pandas DataFrame I hope this helps! If you have any further questions or need additional assistance, feel free to ask. We're here to help! |
@glenn-jocher, thank you for fast response! Here is my set up provied by
Detection inferenceFirts of all, I can manage inference of the deafault detection model from my ipynb notebook: import torch
import cv2
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')
results = model('data/images/zidane.jpg')
im_pred = cv2.cvtColor(results.render()[0], cv2.COLOR_BGR2RGB)
cv2.imwrite('path/to/yolo_test.jpg', im_pred) It works well. But I found out that there is no segmentation model on torch hub, is that right? Can it be the issue? Custom dataset trainingFor training I use custom dataset loaded from Roboflow as follow: from roboflow import Roboflow
rf = Roboflow(api_key="MY_API_KEY")
project = rf.workspace("WORKSPACE").project("PROJECT")
version = project.version(2)
dataset = version.download("yolov5") Contet of data.yaml file in dataset includes
After that I create
After that I initiate logger and start trainig with
command. I`ve got fine results and run prediction with
command. Prediction worked well. It detects only two defined classes and creates fine images with segmentation. DeployNow I am trying to write code in the same notebook to check if loading model works due to further usage in separete script. Just as in Your example. import torch
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
model = torch.hub.load('ultralytics/yolov5', 'custom', path='Roads/roads_results2/weights/best.pt', force_reload=True)
# Prepare image
im = cv2.imread('path/to/image.jpg')
im = cv2.resize(im, (640, 640), interpolation=cv2.INTER_AREA)
im = im.transpose(2, 0, 1) # HWC to CHW
im = torch.from_numpy(im).float().div(255.0).unsqueeze(0) # Add batch dimension and normalize
im = im.to(device)
# Inference
results = model(im)
# Check results
print(results)
print(results.pandas().xyxy[0]) # Convert to pandas DataFrame But all I got is the list of Tensors from first print like that:
And an Error from second print:
Which means that model returns list for some reason. I hope i provide enough information for You. It seems like something wrong with segmentation models but it should`t be. |
Hello @Amodion, Thank you for providing such a detailed description of your setup and the issues you're encountering. It’s great to see your thorough approach to training and deploying your YOLOv5 segmentation model. Let's address the issues step-by-step. Issue 1: Segmentation Model on Torch HubYou are correct that the segmentation models are not directly available on Torch Hub. This can indeed be a source of confusion. However, you can still load your custom segmentation model using the Issue 2: Model Returning List of TensorsWhen you pass a tensor directly to the model, it should return a import torch
import cv2
# Load model
model = torch.hub.load('ultralytics/yolov5', 'custom', path='Roads/roads_results2/weights/best.pt', force_reload=True)
# Prepare image
im = cv2.imread('path/to/image.jpg')
im = cv2.resize(im, (640, 640), interpolation=cv2.INTER_AREA)
im = im.transpose(2, 0, 1) # HWC to CHW
im = torch.from_numpy(im).float().div(255.0).unsqueeze(0) # Add batch dimension and normalize
# Inference
results = model(im)
# Check results
print(results)
print(results.pandas().xyxy[0]) # Convert to pandas DataFrame Issue 3: Incorrect Class LabelsIf you see incorrect class labels (e.g., '9' when you only have classes '1' and '2'), it might be due to a mismatch in the class configuration. Ensure that your model's class names are correctly set up in your custom dataset and during training. Additional Steps
Example with Image PathIf you prefer using image paths, here's how you can do it: # Load model
model = torch.hub.load('ultralytics/yolov5', 'custom', path='Roads/roads_results2/weights/best.pt', force_reload=True)
# Inference with image path
results = model(['path/to/image.jpg'])
# Check results
print(results)
print(results.pandas().xyxy[0]) # Convert to pandas DataFrame I hope this helps! If you have any further questions or need additional assistance, feel free to ask. We're here to help! |
Search before asking
YOLOv5 Component
No response
Bug
I have the following code:
When I run the code I get the following error message:
upon further inspection yolo_output is a list of the tensors. (I am guessing there are 3 objects in the image)
for tensor in yolo_output: print(tensor.size())
torch.Size([1, 3, 40, 80, 8])
torch.Size([1, 3, 20, 40, 8])
torch.Size([1, 3, 10, 20, 8])
According the the documentation I understand, that the model output should be some kind of yolo object.
Environment
OS: debian 11
Python: 3.10.6
Minimal Reproducible Example
Additional
No response
Are you willing to submit a PR?
The text was updated successfully, but these errors were encountered: