Ignite: Does ignite support to load weights for different num_classes?

Created on 27 Feb 2020  ยท  6Comments  ยท  Source: pytorch/ignite

โ“ Questions/Help/Support

Hi @vfdev-5 ,

Does ignite Checkpoint handler support the case that pre-trained model has different number output with the model load to?
For example, the weights in model checkpoint is for 1000 classes output, and now user defines the network with 3 classes output and use ignite handler to load pretrained model for the weights.
Thanks.


enhancement help wanted

Most helpful comment

@vfdev-5 we should nevertheless think about kwargs for the load_objects function, to allow kwargs for both, torch.load (for example the map_location) and the load_state_dict (for example the strict).

All 6 comments

Hi @Nic-Ma
I think this is currently not supported, because we simply load the checkpoint and don't do any checks on the Tensor sizes.
We should allow kwargs in the load_objects function and you would have to pass everything except the last layer as to_load. I think implementing a general interface for that is not that easy, but I'll think on that.

Hi @Nic-Ma ,

yes, as @justusschock said this is not supported and probably the reason for that is related to the fact that pretrained weights loading in most of the cases provided by model definition (e.g. in torchvision resnet18(pretrained=True)). In that case, user's modification can be easily done a posteriori :

model = resnet18(pretrained=True)
model.fc = nn.Linear(512, 3)

@vfdev-5 we should nevertheless think about kwargs for the load_objects function, to allow kwargs for both, torch.load (for example the map_location) and the load_state_dict (for example the strict).

Yes, I agree!

Hi @vfdev-5 and @justusschock ,

Thanks very much for your sharing. Looking forward to this feature.
It's really useful for us, especially to provide "Transfer Learning" function in our product.
Currently, we can't exclude FC layer when loading weights, users raised this request.
Thanks.

Closed by #861

Was this page helpful?
0 / 5 - 0 ratings

Related issues

elanmart picture elanmart  ยท  4Comments

kilsenp picture kilsenp  ยท  3Comments

andreydung picture andreydung  ยท  4Comments

jphdotam picture jphdotam  ยท  4Comments

vfdev-5 picture vfdev-5  ยท  3Comments