Skip to content
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

How to load model from local directory #193

Open
maryamag85 opened this issue Aug 11, 2022 · 6 comments
Open

How to load model from local directory #193

maryamag85 opened this issue Aug 11, 2022 · 6 comments
Labels
enhancement New feature or request

Comments

@maryamag85
Copy link

I can find the download_and_load_checkpoint class which is downloading the pretrianed model directly from the source page but I can not find a function that gets a local path and load the pretrianed saved model. I have solved this situation in my code but I think it would be helpful to have an integrated class for doing this instead.

@mufeili mufeili added the enhancement New feature or request label Aug 12, 2022
@mufeili
Copy link
Contributor

mufeili commented Aug 12, 2022

Thank you for the suggestions.

@REMUU
Copy link

REMUU commented Sep 5, 2022

Hi maryamag85 and mufeili, great question and great package. I am also trapped by this problem that I cannot find a function to load the model from the saved model.pth in local.

Would you mind introducing related documents or codes so that we can write our codes to load the pre-trained model from the local path? I have tried to search in this repository, however, I do not yet find a clear solution to this problem. For instance, on this page, 'https://github.com/awslabs/dgl-lifesci/blob/d781b80793dc84de5c0c4ac674091299c9b95cb0/examples/property_prediction/pubchem_aromaticity/utils.py', the AttentiveFP is loaded by initializing a new model with known parameters, rather than a pre-trained model.

Thank you very much!

@mufeili
Copy link
Contributor

mufeili commented Sep 6, 2022

https://github.com/awslabs/dgl-lifesci/blob/d781b80793dc84de5c0c4ac674091299c9b95cb0/examples/property_prediction/pubchem_aromaticity/utils.py

Hi @REMUU , thank you for raising the issue. Is there a particular pre-trained model that you are looking for?

@REMUU
Copy link

REMUU commented Sep 7, 2022

Hi @mufeili, thank you for your reply. I used the AttentiveFP to train my own model and save it as 'model.pth' in my local path. My problem is I cannot find a function to load this model. So I try to write the following code to load the model, however, this error has been raised.

My code is modified from 'https://github.com/awslabs/dgl-lifesci/blob/2fbf5fd6aca92675b709b6f1c3bc3c6ad5434e96/python/dgllife/model/pretrain/__init__.py' between line 51 to 55.

model = nn.Module()
checkpoint = torch.load('model.pth', map_location='cpu')
model = model.load_state_dict(checkpoint['model_state_dict']).eval()

PLWHC5W6 Z8}99)(00DP6YC

Version Information:
Ubuntu 20.04 LTS
Python 3.7.0
dgllife 0.2.8
dgl 0.7.1
pytorch 1.9.0 (cpu only)

I am not sure why this error happens, could you have a look at it? Thank you very much.

@mufeili
Copy link
Contributor

mufeili commented Sep 8, 2022

You still need to initialize a model object which has the same architecture as the AttentiveFP model you trained like model = AttentiveFP(num_layers = ..., dropout = ..., ...). With model = nn.Module(), it's an empty model architecture.

@REMUU
Copy link

REMUU commented Sep 8, 2022

You still need to initialize a model object which has the same architecture as the AttentiveFP model you trained like model = AttentiveFP(num_layers = ..., dropout = ..., ...). With model = nn.Module(), it's an empty model architecture.

Thank you very much! The model can be loaded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants