Closed Cxz-dev closed 1 year ago
I dowload the pretrained model from https://github.com/Westlake-AI/MogaNet/releases/download/moganet-in1k-weights/moganet_base_sz224_8xbs128_ep300.pth.tar
Hi, @Cxz-dev, thanks for using MogaNet. Could you please describe your usage in detail? The RuntimeError you met is caused by no classification head in the built model. I provide two situations of loading pre-trained models and hope these will be helpful to you.
In image classification tasks, you can build MogaNet-Base and load pre-training without any errors:
import models
from timm.models import create_model
cnn = create_model('moganet_base', pretrained=True) # build by timm
cnn = models.moganet.moganet_base(pretrained=True) # or call the model directly
For downstream tasks, you will get RuntimeError: Error(s) in loading state_dict for MogaNet
if you build the model like this. Since fork_feat=True
will remove the classification head in MogaNet and the pre-trained models in moganet-in1k-weights contain parameters of the head, it will raise the error using model.load_state_dict(checkpoint["state_dict"])
, where strict
defaults to True.
import models
from timm.models import create_model
cnn = create_model('moganet_base', pretrained=True, fork_feat=True) # fork_feat=True for dense predictions
Therefore, I suggest you load the model as follows or directly perform dense prediction tasks by our implementations in MogaNet/detection, MogaNet/segmentation, and MogaNet/pose_estimation.
import torch
import models
from timm.models import create_model
cnn = create_model('moganet_base', fork_feat=True)
checkpoint = torch.hub.load_state_dict_from_url(url='https://github.com/Westlake-AI/MogaNet/releases/download/moganet-in1k-weights/moganet_base_sz224_8xbs128_ep300.pth.tar', map_location="cpu", check_hash=True)
cnn.load_state_dict(checkpoint["state_dict"], strict=False) # allow unexpected keys
Thanks for your question. I will close this issue if you have no more questions. Please feel free to open a new issue when new problems occur.
Hi! Thanks for your code release! when I use moganet_base, I set pretrained=True, the error as follows:
RuntimeError: Error(s) in loading state_dict for MogaNet: Unexpected key(s) in state_dict: "head.weight", "head.bias" Can u give me some advice?