zhenghao977 / FCOS-PyTorch-37.2AP

A pure torch implement of FCOS 37.2AP
162 stars 39 forks source link

Error in loading resnet model #25

Open sparshgarg23 opened 2 years ago

sparshgarg23 commented 2 years ago

I am using model_zoo.load_url to load resnet50 model,however when I use the downloaded model,I end up recieivng the error message as shown below

loading annotations into memory...
Done (t=18.10s)
creating index...
index created!
INFO====>check annos, filtering invalid data......
Traceback (most recent call last):
  File "train_coco.py", line 39, in <module>
    model=FCOSDetector(mode="training").cuda()
  File "/content/fcos/model/fcos.py", line 217, in __init__
    self.fcos_body=FCOS(config=config)
  File "/content/fcos/model/fcos.py", line 22, in __init__
    self.backbone=resnet34(pretrained=True,if_include_top=False)
  File "/content/fcos/model/backbone/resnet.py", line 194, in resnet34
    model.load_state_dict(model_zoo.load_url(model_urls['resnet34']))
  File "/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py", line 1483, in load_state_dict
    self.__class__.__name__, "\n\t".join(error_msgs)))
RuntimeError: Error(s) in loading state_dict for ResNet:
    Unexpected key(s) in state_dict: "fc.weight", "fc.bias"

Would it be possible for you to share the resnet50.pth file,if not then any suggestions on what can be done to resolve this.

chaowentao commented 2 years ago

将strict的值设为False即可

def _resnet(arch, block, layers, pretrained, progress, **kwargs):
  model = ResNet(block, layers, **kwargs)#自己定义的model
  if pretrained:
    state_dict = load_state_dict_from_url(model_urls[arch], progress=progress)#预训练的model
    model.load_state_dict(state_dict, strict=False)

    """如果是加载本地下载好的预训练模型,就用torch.load(),再把strict设为False
    model.load_state_dict(torch.load("./resnet50-19c8e357.pth"),strict=False)
    """
  return model
zhenghao977 commented 2 years ago

这是来自QQ邮箱的假期自动回复邮件。   您好,我最近正在休假中,无法亲自回复您的邮件。我将在假期结束后,尽快给您回复。