WongKinYiu / yolov9

Implementation of paper - YOLOv9: Learning What You Want to Learn Using Programmable Gradient Information
GNU General Public License v3.0
9k stars 1.43k forks source link

Error while loading custom trained model #538

Open plutus123 opened 4 months ago

plutus123 commented 4 months ago

I have trained my Yolov9 model for instance segmentation on my custom dataset, now when I am trying to load my model then I am getting an error.

from ultralytics import YOLO

model = YOLO("runs/train-seg/gelan-c-seg15/weights/best.pt")

img = 'WALL-INSTANCEE-
2/test/images/5a243513a69b150001f56c31_emptyroom6_jpeg_jpg.rf.7aa8f6a9aefbb1c76adc60a7b392dcd6.jpg'

res = model.predict(img , save = True , save_txt = True)

The error I am getting is :

TypeError: BaseModel.fuse() got an unexpected keyword argument 'verbose'

I even tried to use this code


import torch

model = torch.hub.load('.', 'custom', path='runs/train-seg/gelan-c-seg15/weights/best.pt', source='local') 

# Image
img = 'WALL-INSTANCEE-2/test/images/5a243513a69b150001f56c31_emptyroom6_jpeg_jpg.rf.7aa8f6a9aefbb1c76adc60a7b392dcd6.jpg'

# Inference
results = model(img)

But even this is not working

aziziselma commented 4 months ago

Have you faced this issue? I'm dealing with the same problem right now. Did you manage to find a solution? If so, could you please share it? I'd really appreciate any help you can offer!

plutus123 commented 4 months ago

Have you faced this issue? I'm dealing with the same problem right now. Did you manage to find a solution? If so, could you please share it? I'd really appreciate any help you can offer!

Hello @aziziselma so rightnow yolov9 int is not Autoshape compatible so I just transposed the image from (height, width, channels) to (channels, height, width). Although I was able to resolve this issue but I was getting a warning message stating that I won't be able to run inference with this model.

The code that I have had written is:

import torch
import cv2
import numpy as np
from pathlib import Path

# Load the custom PyTorch model from local path
model_path = 'runs/train-seg/gelan-c-seg15/weights/best.pt'
model = torch.hub.load('.', 'custom', path=model_path, source='local')

# Check if GPU is available and move model to GPU
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = model.to(device)

# Image path
img_path = 'WALL-INSTANCEE-2/test/images/5a243513a69b150001f56c31_emptyroom6_jpeg_jpg.rf.7aa8f6a9aefbb1c76adc60a7b392dcd6.jpg'

# Read the image using OpenCV
img = cv2.imread(img_path)
if img is None:
    raise FileNotFoundError(f"Failed to load image from path: {img_path}")
print(img.shape)

# Convert from BGR to RGB
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# Normalize the image (optional, depending on your model's requirements)
img = img.astype(np.float32) / 255.0

# Transpose the image from (height, width, channels) to (channels, height, width)
img = np.transpose(img, (2, 0, 1))

# Convert to a PyTorch tensor and add a batch dimension
img_tensor = torch.from_numpy(img).unsqueeze(0).to(device)

# Perform inference
model.eval()
with torch.no_grad():
    res = model(img_tensor)
YOLO 🚀 v0.1-104-g5b1ea9a Python-3.10.12 torch-2.1.0+cu118 CUDA:0 (NVIDIA RTX A5000, 24248MiB)

Fusing layers... 
gelan-c-seg-custom summary: 414 layers, 27364441 parameters, 0 gradients, 144.2 GFLOPs
WARNING ⚠️ YOLO SegmentationModel is not yet AutoShape compatible. You will not be able to run inference with this model.

So that's why I am now using yolov8-seg. If you get any solution for this problem please do share it with me.

aziziselma commented 4 months ago

Have you faced this issue? I'm dealing with the same problem right now. Did you manage to find a solution? If so, could you please share it? I'd really appreciate any help you can offer!

Hello @aziziselma so rightnow yolov9 int is not Autoshape compatible so I just transposed the image from (height, width, channels) to (channels, height, width). Although I was able to resolve this issue but I was getting a warning message stating that I won't be able to run inference with this model.

The code that I have had written is:

import torch
import cv2
import numpy as np
from pathlib import Path

# Load the custom PyTorch model from local path
model_path = 'runs/train-seg/gelan-c-seg15/weights/best.pt'
model = torch.hub.load('.', 'custom', path=model_path, source='local')

# Check if GPU is available and move model to GPU
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = model.to(device)

# Image path
img_path = 'WALL-INSTANCEE-2/test/images/5a243513a69b150001f56c31_emptyroom6_jpeg_jpg.rf.7aa8f6a9aefbb1c76adc60a7b392dcd6.jpg'

# Read the image using OpenCV
img = cv2.imread(img_path)
if img is None:
    raise FileNotFoundError(f"Failed to load image from path: {img_path}")
print(img.shape)

# Convert from BGR to RGB
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# Normalize the image (optional, depending on your model's requirements)
img = img.astype(np.float32) / 255.0

# Transpose the image from (height, width, channels) to (channels, height, width)
img = np.transpose(img, (2, 0, 1))

# Convert to a PyTorch tensor and add a batch dimension
img_tensor = torch.from_numpy(img).unsqueeze(0).to(device)

# Perform inference
model.eval()
with torch.no_grad():
    res = model(img_tensor)
YOLO 🚀 v0.1-104-g5b1ea9a Python-3.10.12 torch-2.1.0+cu118 CUDA:0 (NVIDIA RTX A5000, 24248MiB)

Fusing layers... 
gelan-c-seg-custom summary: 414 layers, 27364441 parameters, 0 gradients, 144.2 GFLOPs
WARNING ⚠️ YOLO SegmentationModel is not yet AutoShape compatible. You will not be able to run inference with this model.

So that's why I am now using yolov8-seg. If you get any solution for this problem please do share it with me.

Thank you for your response. I am currently working on this issue as well. If I find a solution, I will definitely share it with you.

plutus123 commented 4 months ago

Have you faced this issue? I'm dealing with the same problem right now. Did you manage to find a solution? If so, could you please share it? I'd really appreciate any help you can offer!

Hello @aziziselma so rightnow yolov9 int is not Autoshape compatible so I just transposed the image from (height, width, channels) to (channels, height, width). Although I was able to resolve this issue but I was getting a warning message stating that I won't be able to run inference with this model. The code that I have had written is:

import torch
import cv2
import numpy as np
from pathlib import Path

# Load the custom PyTorch model from local path
model_path = 'runs/train-seg/gelan-c-seg15/weights/best.pt'
model = torch.hub.load('.', 'custom', path=model_path, source='local')

# Check if GPU is available and move model to GPU
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = model.to(device)

# Image path
img_path = 'WALL-INSTANCEE-2/test/images/5a243513a69b150001f56c31_emptyroom6_jpeg_jpg.rf.7aa8f6a9aefbb1c76adc60a7b392dcd6.jpg'

# Read the image using OpenCV
img = cv2.imread(img_path)
if img is None:
    raise FileNotFoundError(f"Failed to load image from path: {img_path}")
print(img.shape)

# Convert from BGR to RGB
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# Normalize the image (optional, depending on your model's requirements)
img = img.astype(np.float32) / 255.0

# Transpose the image from (height, width, channels) to (channels, height, width)
img = np.transpose(img, (2, 0, 1))

# Convert to a PyTorch tensor and add a batch dimension
img_tensor = torch.from_numpy(img).unsqueeze(0).to(device)

# Perform inference
model.eval()
with torch.no_grad():
    res = model(img_tensor)
YOLO 🚀 v0.1-104-g5b1ea9a Python-3.10.12 torch-2.1.0+cu118 CUDA:0 (NVIDIA RTX A5000, 24248MiB)

Fusing layers... 
gelan-c-seg-custom summary: 414 layers, 27364441 parameters, 0 gradients, 144.2 GFLOPs
WARNING ⚠️ YOLO SegmentationModel is not yet AutoShape compatible. You will not be able to run inference with this model.

So that's why I am now using yolov8-seg. If you get any solution for this problem please do share it with me.

Thank you for your response. I am currently working on this issue as well. If I find a solution, I will definitely share it with you.

Thanks!! All the Best Happy Coding :)