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

conversion of models from ultralytics/yolov8 to yours #8

Open
mahaling opened this issue Dec 13, 2023 · 3 comments
Open

conversion of models from ultralytics/yolov8 to yours #8

mahaling opened this issue Dec 13, 2023 · 3 comments

Comments

@mahaling
Copy link

Can you share insights/code to convert ultralytics models to your version of yolo. I am specifically looking for conversion of yolov8. These are different from the base models you have provided.

@jahongir7174
Copy link
Owner

def port_weights(model1, model2):
    model1.eval()
    model2.eval()
    with torch.no_grad():
        m1_std = model1.state_dict().values()
        m2_std = model2.state_dict().values()
        for m1, m2 in zip(m1_std, m2_std):
            m1.copy_(m2)
    state = {'model': model1.half()}
    torch.save(state, 'weights/bestpt')

model1 is the dst model
model2 is the src model

@mahaling
Copy link
Author

Thanks for this piece of code. But it looks like the architecture of ultralytics' YOLO is different from what your code has. Can you please confirm?

@ItayMeiri
Copy link

ItayMeiri commented Jan 25, 2025

Hello @jahongir7174
I really appreciate your work - could you please provide a minimal example of porting? For instance, if I download the v8 weights for COCO from ultralytics and try to do the following:

import torch
from ultralytics import YOLO
from nets.nn import yolo_v8_n


downloaded_weights = "yolov8n.pt"
model = YOLO(downloaded_weights)
model_nn = yolo_v8_n(num_classes=80)

def port_weights(model1, model2):
    model1.eval()
    model2.eval()
    with torch.no_grad():
        m1_std = model1.state_dict().values()
        m2_std = model2.state_dict().values()
        for m1, m2 in zip(m1_std, m2_std):
            m1.copy_(m2)
    state = {'model': model1.half()}
    torch.save(state, 'weights/bestpt')

port_weights(model_nn, model)

I would get an error RuntimeError: The size of tensor a (32) must match the size of tensor b (16) at non-singleton dimension 0
I'm wondering how you ported the weights?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants