Replies: 2 comments
-
@ioangatop if you want classifier weights loaded into feature extraction wrapped models, you need to load weights as 'pretrained' so that they are loaded before the model is mutated. See related discussion, should work with >= 0.9 timm version https://github.com/hugginface/pytorch-image-models/discussions/1941 Although, example in that discussion should be a bit differentl, use the 'overlay' arg as in the train script Lines 463 to 468 in d4ef0b4 The overlay dict is merged with the models normal pretrained_cfg, the pretrained_cfg arg fully overrides it. Alternative to using the |
Beta Was this translation helpful? Give feedback.
-
@rwightman thanks for the response! from your reply, here is the complete solution that worked for me: feature_extractor = timm.create_model(
model_name="vit_small_patch16_224",
pretrained=True,
pretrained_cfg={
"url": "https://dl.fbaipublicfiles.com/dino/dino_deitsmall16_pretrain/dino_deitsmall16_pretrain.pth",
"num_classes": 0
},
out_indices=1,
features_only=True,
) |
Beta Was this translation helpful? Give feedback.
-
Describe the bug
Hi Ross! I'm facing a small issue with the features extractor, here are some details:
The function
create_model
supports the argument ofcheckpoint_path
which allows to load custom model weights. However, when we want to load a model as feature extractor, the model is wrapped around theFeatureGetterNet
class, and the loading fails as the keys do not much anymore; theFeatureGetterNet
stores the model underself.model
so in order to work, the state dict keys should have a prefixmodel.
, for exampleclass_token
->model.class_token
Additionally, one workaround is to do the loading of the model after the initialisation, but this also fails as some networks, like vision transformer, prune some layers and thus the state_dict has extra keys
To Reproduce
As always, thanks a lot 🙏
Beta Was this translation helpful? Give feedback.
All reactions