ultralytics / ultralytics

NEW - YOLOv8 🚀 in PyTorch > ONNX > OpenVINO > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
23.75k stars 4.74k forks source link

.pth #11643

Open 2029686068 opened 1 week ago

2029686068 commented 1 week ago

Search before asking

Question

TypeError: model='epoch_20SSD.pth' should be a *.pt PyTorch model to run this method, but is a different format. PyTorch models can train, val, predict and export, i.e. 'model.train(data=...)', but exported formats like ONNX, TensorRT etc. only support 'predict' and 'val' modes, i.e. 'yolo predict model=yolov8n.onnx'. To run CUDA or MPS inference please pass the device argument directly in your inference command, i.e. 'model.predict(source=..., device=0)',我该怎么转换成.pth

Additional

No response

github-actions[bot] commented 1 week ago

👋 Hello @2029686068, thank you for your interest in Ultralytics YOLOv8 🚀! We recommend a visit to the Docs for new users where you can find many Python and CLI usage examples and where many of the most common questions may already be answered.

If this is a 🐛 Bug Report, please provide a minimum reproducible example to help us debug it.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset image examples and training logs, and verify you are following our Tips for Best Training Results.

Join the vibrant Ultralytics Discord 🎧 community for real-time conversations and collaborations. This platform offers a perfect space to inquire, showcase your work, and connect with fellow Ultralytics users.

Install

Pip install the ultralytics package including all requirements in a Python>=3.8 environment with PyTorch>=1.8.

pip install ultralytics

Environments

YOLOv8 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

Ultralytics CI

If this badge is green, all Ultralytics CI tests are currently passing. CI tests verify correct operation of all YOLOv8 Modes and Tasks on macOS, Windows, and Ubuntu every 24 hours and on every commit.

glenn-jocher commented 1 week ago

Hello! It seems like you're trying to use a .pth file with YOLOv8, which expects a .pt file format for its model files. If you have a PyTorch checkpoint in .pth format, you'll first need to load it properly and save it in .pt format, suitable for using with YOLOv8. Here's a quick example on how to do that:

import torch
from ultralytics import YOLO

# Load your .pth file
model_state = torch.load('epoch_20SSD.pth')

# You may need to initialize your model architecture here if necessary
# For example:
# model = SomeModelClass(*args, **kwargs)
# model.load_state_dict(model_state)

# Now, create a YOLO model instance
# You might need the correct model config file or use a predefined model
model = YOLO('yolov8n.yaml')

# Load the state dict into the YOLO model
model.model.load_state_dict(model_state)
# or if the state dict was saved with the Ultralytics format:
# model.load_state_dict(model_state['model'])

# Finally, save it in .pt format
torch.save(model.model.state_dict(), 'epoch_20SSD.pt')

This converts your .pth file to the .pt format expected by YOLOv8. Make sure to customize the model architecture loading part as per your specific model setup. Let me know if there's anything else you need help with! 😊