Yolov5: How to load an existing weight to a model?

Created on 18 Nov 2020  ·  5Comments  ·  Source: ultralytics/yolov5

❔Question

I try to load an existing weight to my model by using the code

PATH = 'runs/train/exp/weights/best.pt'
model = torch.hub.load('ultralytics/yolov5', 'yolov5l', pretrained=False)
model.load_state_dict(torch.load(PATH))

Instead, I got this error
Screenshot from 2020-11-19 01-20-25

question

All 5 comments

I solved this problem - just call .fuse() in your code:

model = model.fuse()

Thank you very much for your reply. Can you explain what does fuse()
please?

On Thu, Nov 19, 2020, 16:32 Semihal notifications@github.com wrote:

I solved this problem - just call .fuse() in your code:

model = model.fuse()


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/ultralytics/yolov5/issues/1441#issuecomment-730246968,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/APZU5XN3MQR7ZNFV3RMBJNTSQTQ2NANCNFSM4T2KWZ6A
.

Thank you very much for your reply. Can you explain what does fuse() please?

On Thu, Nov 19, 2020, 16:32 Semihal @.*> wrote: I solved this problem - just call .fuse() in your code: model = model.fuse() — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub <#1441 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/APZU5XN3MQR7ZNFV3RMBJNTSQTQ2NANCNFSM4T2KWZ6A .

I understand it blocks the main layers (excludes their change).

I've updated the PyTorch Hub tutorial now with an example of state dict loading.
https://github.com/ultralytics/yolov5#tutorials

Load a State Dict

To load a custom YOLOv5s state dict, first load a PyTorch Hub model trained on the same number of classes:

model = torch.hub.load('ultralytics/yolov5', 'yolov5s', classes=10)
model.load_state_dict(torch.load('yolov5s_10cls.pt')['model'].state_dict())

I really appreciate all your helps. I'm really new to this field and don't
really know which questions are considered to be trivial. Thank for taking
your time to reply.

On Thu, Nov 19, 2020 at 6:18 PM Glenn Jocher notifications@github.com
wrote:

I've updated the PyTorch Hub tutorial now with an example of state dict
loading.
https://github.com/ultralytics/yolov5#tutorials
Load a State Dict

To load a custom YOLOv5s state dict, first load a PyTorch Hub model
trained on the same number of classes:

model = torch.hub.load('ultralytics/yolov5', 'yolov5s', classes=10)model.load_state_dict(torch.load('yolov5s_10cls.pt')['model'].state_dict())


You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
https://github.com/ultralytics/yolov5/issues/1441#issuecomment-730305145,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/APZU5XJUKKLDQFC2UTFMPL3SQT5KBANCNFSM4T2KWZ6A
.

Was this page helpful?
0 / 5 - 0 ratings