Skip to content

Pretrained yolov5 returns list of tensors instead of yolo objects #10750

@toschi23

Description

@toschi23

Search before asking

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

YOLOv5 Component

No response

Bug

I have the following code:

class yolo_mask_model(pl.LightningModule):
    def __init__(self, weight_path = '/home/my_folder/yolov5/runs/train/exp6/weights/best.pt'):
        super(yolo_mask_model, self).__init__()
        self.save_hyperparameters()
        self.pretrained_yolo = torch.hub.load('ultralytics/yolov5', 'custom', path=weight_path) 
        
    def forward(self, input):
        yolo_output = self.pretrained_yolo(input)
        print(yolo_output)
        for index,row in yolo_output.pandas().xyxy[0].iloc[::-1].iterrows():
            do something

if __name__ == '__main__':
	mymodel = yolo_mask_model()
	image = Image.open("my_image.jpeg")
	transform = transforms.Compose([transforms.Resize((320,640)),transforms.ToTensor()])
	tensor = transform(image)
	input_tensor = torch.unsqueeze(tensor, 0)
	output = mymodel(input_tensor)

When I run the code I get the following error message:

Exception has occurred: AttributeError
'list' object has no attribute 'pandas'
  File "/home/my_workspace/model/YOLO_mask_module.py", line 23, in forward
    for index,row in yolo_output.pandas().xyxy[0].iloc[::-1].iterrows():
  File "/home/my_workspace/model/YOLO_mask_module.py", line 40, in <module>
    output = mymodel(input_tensor)
AttributeError: 'list' object has no attribute 'pandas'

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

class yolo_mask_model(pl.LightningModule):
    def __init__(self, weight_path = '/home/my_folder/yolov5/runs/train/exp6/weights/best.pt'):
        super(yolo_mask_model, self).__init__()
        self.save_hyperparameters()
        self.pretrained_yolo = torch.hub.load('ultralytics/yolov5', 'custom', path=weight_path) 
        
    def forward(self, input):
        yolo_output = self.pretrained_yolo(input)
        print(yolo_output)
        for index,row in yolo_output.pandas().xyxy[0].iloc[::-1].iterrows():
            do something

if __name__ == '__main__':
	mymodel = yolo_mask_model()
	image = Image.open("my_image.jpeg")
	transform = transforms.Compose([transforms.Resize((320,640)),transforms.ToTensor()])
	tensor = transform(image)
	input_tensor = torch.unsqueeze(tensor, 0)
	output = mymodel(input_tensor)

Additional

No response

Are you willing to submit a PR?

  • Yes I'd like to help by submitting a PR!

Metadata

Metadata

Assignees

No one assigned

    Labels

    StaleStale and schedule for closing soonbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions