RuntimeError: Error(s) in loading state_dict for ResNet: size mismatch for conv1.weight

#1
by Devrim - opened

Getting an error while loading the pretrained model with timm.

Versions:
timm: 0.9.2
torch: 1.12.1

>>> import timm
>>> model = timm.create_model("hf_hub:edadaltocg/resnet50_cifar100", pretrained=True)
Downloading (…)lve/main/config.json: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 885/885 [00:00<00:00, 299kB/s]
Downloading pytorch_model.bin: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 95.1M/95.1M [00:01<00:00, 73.7MB/s]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/devrim/miniconda3/envs/std/lib/python3.9/site-packages/timm/models/_factory.py", line 114, in create_model
    model = create_fn(
  File "/home/devrim/miniconda3/envs/std/lib/python3.9/site-packages/timm/models/resnet.py", line 1276, in resnet50
    return _create_resnet('resnet50', pretrained, **dict(model_args, **kwargs))
  File "/home/devrim/miniconda3/envs/std/lib/python3.9/site-packages/timm/models/resnet.py", line 547, in _create_resnet
    return build_model_with_cfg(ResNet, variant, pretrained, **kwargs)
  File "/home/devrim/miniconda3/envs/std/lib/python3.9/site-packages/timm/models/_builder.py", line 393, in build_model_with_cfg
    load_pretrained(
  File "/home/devrim/miniconda3/envs/std/lib/python3.9/site-packages/timm/models/_builder.py", line 233, in load_pretrained
    model.load_state_dict(state_dict, strict=strict)
  File "/home/devrim/miniconda3/envs/std/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1604, in load_state_dict
    raise RuntimeError('Error(s) in loading state_dict for {}:\n\t{}'.format(
RuntimeError: Error(s) in loading state_dict for ResNet:
    size mismatch for conv1.weight: copying a param with shape torch.Size([64, 3, 3, 3]) from checkpoint, the shape in current model is torch.Size([64, 3, 7, 7]).

Hi! You should pip install detectors first, or manually change the first convolutional layer to have filters of size 3 and not 7 as the model trained on Imagenet.
Best.

Sign up or log in to comment