Skip to content
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

rtdetr weight problem #12663

Closed
1 task done
S-7-12 opened this issue May 13, 2024 · 10 comments
Closed
1 task done

rtdetr weight problem #12663

S-7-12 opened this issue May 13, 2024 · 10 comments
Labels
question Further information is requested

Comments

@S-7-12
Copy link

S-7-12 commented May 13, 2024

Search before asking

Question

For the rtdetr model, I would like to train the model using the backbones of resnet50 and 101, but have not found the corresponding pre-training weights.

Additional

no

@S-7-12 S-7-12 added the question Further information is requested label May 13, 2024
Copy link

👋 Hello @S-7-12, 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
Copy link
Member

Hello! For training RT-DETR models with ResNet backbones, pre-trained weights might not be directly available from the Ultralytics repository. You can utilize pre-trained ResNet models from PyTorch repository as an alternative. Here's a code snippet on how you can do this:

import torch
from ultralytics import YOLO

# Load RT-DETR model and specify the backbone
model = YOLO('rtdetr.yaml', backbone='resnet50')  # Change to 'resnet101' as needed

# Load pre-trained weights from PyTorch
pretrained_weights = torch.load('resnet50.pth')  # Adjust path and filename as necessary
model.load_state_dict(pretrained_weights, strict=False)

# Continue with your training setup

Replace 'resnet50.pth' with the file path to your downloaded PyTorch weights, and ensure your YAML configuration file (rtdetr.yaml) correctly configures the model to use the desired ResNet backbone. Hope this helps! 😊

@S-7-12
Copy link
Author

S-7-12 commented May 15, 2024

Traceback (most recent call last):
model = YOLO('rtdetr.yaml', backbone='resnet50') # Change to 'resnet101' as needed
TypeError: init() got an unexpected keyword argument 'backbone'
The parameter backbone does not seem to be set when the model is initialized

@S-7-12
Copy link
Author

S-7-12 commented May 15, 2024

from ultralytics import RTDETR

# Load RT-DETR model and specify the backbone
model = RTDETR('ultralytics/cfg/models/rt-detr/rtdetr-resnet50.yaml') 

# Train the model on the COCO8 example dataset for 100 epochs
model.train(

)

I noticed that the rtdetr r-50.yaml file already exists in the ultralytics repository and can be trained that way, but the weighting issue is still not resolved.

@glenn-jocher
Copy link
Member

Hi there! It's great to see that you've found the appropriate rtdetr-resnet50.yaml file for training. If you're encountering issues with loading the specific weights for the ResNet-50 backbone in the RT-DETR model, you should first ensure that you have the correct pre-trained weights available. If they're not present, you might need to download them separately.

For training with pre-trained ResNet weights, you'll have to load these weights manually before the training starts. Here's a simple way to do it using PyTorch:

import torch
from ultralytics import RTDETR

# Load the RTDETR model configuration
model = RTDETR('ultralytics/cfg/models/rt-detr/rtdetr-resnet50.yaml')

# Load pre-trained weights
weights = torch.load('path_to_resnet50_weights.pth')
model.model.load_state_dict(weights, strict=False)

# Train the model
model.train(data='path_to_coco128.yaml', epochs=100)

Make sure to replace 'path_to_resnet50_weights.pth' with the path to your ResNet-50 PyTorch weights file, and adjust the data path and epochs as necessary. This should resolve the issue with the weights not being loaded properly. Let us know if you need further assistance! 😊

@yingyu13
Copy link

@glenn-jocher ,hi, the pre-trained ResNet weights cannot be loaded correctly, as the parameter names of pre-trained ResNet models from PyTorch repository is consistent with backbone defined in rtdetr r-50.yaml of ultralytics repository. How to resolve this issue?

@S-7-12
Copy link
Author

S-7-12 commented May 20, 2024

Hi there! It's great to see that you've found the appropriate rtdetr-resnet50.yaml file for training. If you're encountering issues with loading the specific weights for the ResNet-50 backbone in the RT-DETR model, you should first ensure that you have the correct pre-trained weights available. If they're not present, you might need to download them separately.

For training with pre-trained ResNet weights, you'll have to load these weights manually before the training starts. Here's a simple way to do it using PyTorch:

import torch
from ultralytics import RTDETR

# Load the RTDETR model configuration
model = RTDETR('ultralytics/cfg/models/rt-detr/rtdetr-resnet50.yaml')

# Load pre-trained weights
weights = torch.load('path_to_resnet50_weights.pth')
model.model.load_state_dict(weights, strict=False)

# Train the model
model.train(data='path_to_coco128.yaml', epochs=100)

Make sure to replace 'path_to_resnet50_weights.pth' with the path to your ResNet-50 PyTorch weights file, and adjust the data path and epochs as necessary. This should resolve the issue with the weights not being loaded properly. Let us know if you need further assistance! 😊

#12663 (comment)
There is indeed this problem, there is indeed a problem with the code you provided, it really doesn't load the backbone weights successfully, hopefully you can give a solution for this

@glenn-jocher
Copy link
Member

@S-7-12 hi! Thanks for pointing out the issue with loading the backbone weights. It seems like the parameter names in the pre-trained ResNet model might not match directly with those expected by the RT-DETR model configuration. To resolve this, you can map the ResNet weights to the expected names in the RT-DETR model.

Here’s a quick way to adapt the weights:

import torch
from ultralytics import RTDETR

# Load the RTDETR model configuration
model = RTDETR('ultralytics/cfg/models/rt-detr/rtdetr-resnet50.yaml')

# Load pre-trained weights
resnet_weights = torch.load('path_to_resnet50_weights.pth')
adapted_weights = {k if 'backbone' in k else 'backbone.' + k: v for k, v in resnet_weights.items()}

# Load adapted weights into the model, ignoring incompatible keys
model.model.load_state_dict(adapted_weights, strict=False)

# Now you can proceed with training
model.train(data='path_to_coco128.yaml', epochs=100)

This script prepends 'backbone.' to the keys of the ResNet weights, aligning them with the expected format in the RT-DETR model. Adjust the string manipulation as necessary based on the exact mismatch pattern. Let me know if this helps or if you need further adjustments! 😊

@S-7-12
Copy link
Author

S-7-12 commented May 20, 2024

``> @S-7-12 hi! Thanks for pointing out the issue with loading the backbone weights. It seems like the parameter names in the pre-trained ResNet model might not match directly with those expected by the RT-DETR model configuration. To resolve this, you can map the ResNet weights to the expected names in the RT-DETR model.

Here’s a quick way to adapt the weights:

import torch
from ultralytics import RTDETR

# Load the RTDETR model configuration
model = RTDETR('ultralytics/cfg/models/rt-detr/rtdetr-resnet50.yaml')

# Load pre-trained weights
resnet_weights = torch.load('path_to_resnet50_weights.pth')
adapted_weights = {k if 'backbone' in k else 'backbone.' + k: v for k, v in resnet_weights.items()}

# Load adapted weights into the model, ignoring incompatible keys
model.model.load_state_dict(adapted_weights, strict=False)

# Now you can proceed with training
model.train(data='path_to_coco128.yaml', epochs=100)

This script prepends 'backbone.' to the keys of the ResNet weights, aligning them with the expected format in the RT-DETR model. Adjust the string manipulation as necessary based on the exact mismatch pattern. Let me know if this helps or if you need further adjustments! 😊

import warnings
import torch
warnings.filterwarnings('ignore')

from ultralytics import RTDETR

# Load RT-DETR model and specify the backbone
model = RTDETR('ultralytics/cfg/models/rt-detr/rtdetr-resnet101.yaml') # Change to 'resnet101' as needed
# Display model information (optional)


# Train the model on the COCO8 example dataset for 100 epochs
model.train(data='/home/rendoudou/swx/pycharm/v5-ours/yolov5-master/data/our.yaml', 
                      imgsz=640,
                      cache=False,
                      epochs=300,
                      pretrained='resnet101.pth',
                      batch=16,
                      device='1',
                      project='runs-test/rtdetr-r101-train',
                      name='rtdetr-ours-',
                      patience=30,
                      )

Thanks, I found out that I can introduce weights this way, the code is currently running with no error messages, do you see any errors in the code?

@glenn-jocher
Copy link
Member

Hey @S-7-12-12! Glad to hear that you've managed to get your code running smoothly! 🚀 From the snippet you've shared, everything looks in order. Just ensure that the path to your dataset and the pretrained weights are correctly specified. Also, keep an eye on the device configuration to match your available hardware. If any issues arise during training or if you need further assistance, feel free to reach out. Happy training! 😊

@S-7-12 S-7-12 closed this as completed May 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants