awslabs / dgl-lifesci

Python package for graph neural networks in chemistry and biology
Apache License 2.0
696 stars 144 forks source link

How to load model from local directory #193

Open maryamag85 opened 1 year ago

maryamag85 commented 1 year ago

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 commented 1 year ago

Thank you for the suggestions.

REMUU commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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.