New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Load pretrained Imagenet models by pytorch #347
Comments
Hi @Kongsea ! If I understand the problem correctly, you are trying to load a pretrained model from The recommended approach for doing something like this would be to do the following - import torchvision.models as models
my_model = models.resnet18(pretrained=True)
from classy_vision.models import ClassyModelWrapper
my_classy_model = ClassyModelWrapper(my_model)
my_task = ClassificationTask().set_model(my_classy_model)
... Note that I'm doing two things here -
Let me know if that gives you any trouble! |
Thank you @mannatsingh for your advice.
So how to change the default head (fullyconnected layer) according to my custom dataset? |
Finally, I found we can first build the block and then set the model head using:
However, we met a new question: how to set to freeze some layer of the pretrained resnet model and train only the last layers and the fc head? |
@Kongsea unfortunately, allowing support to attach heads to a model requires a user to implement the model themselves (to propagate the output of the heads). So you will not be able to replace the original "head" with a With regard to freezing only the last later and the fc head, that isn't supported yet. What is supported is freezing the whole trunk and just training the fc head (https://classyvision.ai/tutorials/fine_tuning). Feel free to create a feature request for this! |
Hello all, I'm basically in the same situation as Kongsea: I would like to have a simple possibility to use a pretrained model ideally configurable via the config file and with the option to freeze the trunk (or any other combination layer combination). I'm using the standard
But that doesn't allow me to freeze any layers. @mannatsingh I have a couple of questions:
|
Hi @P4ppenheimer , responding to your messages inline -
This doesn't work because the heads can only be attached to
This would require us to host our own pre-trained models. We don't have an ETA for this, but I'm getting feedback from multiple people who are interested in this, so we can discuss internally to try get an ETA.
The concept of a head (
You would have to write some code to 1) support attaching heads to that model by editing its implementation 2) modify the state dictionary of the checkpoint and make it compatible with the new model.
You would need a pre-trained The big thing I'm getting from this is that pre-trained classy models would be really helpful, and we should consider providing them to our users soon. cc @aadcock , @vreis |
@P4ppenheimer , @Kongsea I've just landed a series of commits which makes what you intended to do possible - you can attach heads to any PyTorch module! This probably needs to go in a tutorial, will get to that when I get some time. But in the meantime, the following works -
See #465 for a full description. |
Hello @mannatsingh, |
@mannatsingh |
@CaiyuZhang my guess would be that you're on old version of the code. You can checkout and install the latest master and the issue should go away. If it doesn't work, can you run the following and let me know what version you see - import classy_vision
classy_vision.__version__ We're also planning to release our next version next week, so you will be able to get it using |
@mannatsingh Yeah, I thought about this reason too, but I actually update classy_vision before running my code. But this error still happened, I will check out the version, no worry, I have loaded my trained models through other methods, thanks for ur reply. |
When I load a pretrained imagenet model by pytorch using a finetune task, an
AssertionError: Checkpoint does not contain classy_state_dict
was raised. So I want to know, how to load a imagenet model to initialize the backbone of a class model?Thank you.
The text was updated successfully, but these errors were encountered: