Skip to content

how to use the GearNet to extract the protein feature? #23

Closed

Description

@onlyonewater

hi, authors, a great work, I want to use the GearNet as a feature extractor to extract the protein features, how to use it?

thanks!!!

Activity

  1. Oxer11 commented on Apr 22, 2023

    @Oxer11
    Collaborator

    Hi! If you want to extract protein features with GearNet, I suggest to read this tutorial carefully. Basically, you only need to write your customized dataset and then use GearNet as your encoder.

  2. onlyonewater commented on Apr 23, 2023

    @onlyonewater
    Author

    ok, got it, I will have a try. thanks!

  3. pearl-rabbit commented on Apr 24, 2023

    @pearl-rabbit

    Hello, I haven't used pretrained models before, and after reading the documentation, I still feel a bit confused.
    I want to use my own PDB dataset, but I don't know how to load it and apply the pretrained model to obtain its representation

  4. Oxer11 commented on Apr 24, 2023

    @Oxer11
    Collaborator

    Hi, to use the pre-trained models, please check the last section of this tutorial for pre-training and fine-tuning. You will find how to load the pre-trained model and fine-tune on your own dataset.

  5. pearl-rabbit commented on Apr 25, 2023

    @pearl-rabbit

    Hello, I'm sorry to bother you again.
    I hope to use GearNet loaded with pre-trained weights to extract protein features, but the program has neither output nor termination. What's the problem?

    # protein
    protein = data.Protein.from_pdb(pdb_file, atom_feature="position", bond_feature="length", residue_feature="symbol")
    _protein = data.Protein.pack([protein])
    protein = graph_construction_model(_protein)
    
    # model
    gearnet_edge = models.GearNet(input_dim=21, hidden_dims=[512, 512, 512, 512, 512, 512],
                                  num_relation=7, edge_input_dim=59, num_angle_bin=8,
                                  batch_norm=True, concat_hidden=True, short_cut=True, readout="sum")
    pthfile = 'models/angle_gearnet_edge.pth'
    net = torch.load(pthfile)
    gearnet_edge.load_state_dict(net)
    
    #output
    with torch.no_grad():
        output = gearnet_edge(protein, protein.node_feature.float(), all_loss=None, metric=None)
    
  6. Oxer11 commented on Apr 25, 2023

    @Oxer11
    Collaborator

    Hi, the code looks good to me.

    Could you provide more information about the bug? What does it mean by no output from the program? It seems that you haven't included a print in your code. If the code runs without termination, could you please show which part the code will stuck at?

  7. pearl-rabbit commented on Apr 25, 2023

    @pearl-rabbit

    The program has been running the last line of code:

    output = gearnet_edge(protein, protein.node_feature.float(), all_loss=None, metric=None)
    
  8. Oxer11 commented on Apr 25, 2023

    @Oxer11
    Collaborator

    I think it's simply because the model hasn't finished yet, since you're running the code on CPU without putting the model on GPU.

  9. pearl-rabbit commented on Apr 25, 2023

    @pearl-rabbit

    I tried to replace 'utils. sparse_coo_tensor' with 'torch. sparse_coo_tensor' in line 802 of "layers.conv.py" , and the program was able to continue executing (although an error was reported later).

  10. Oxer11 commented on Apr 25, 2023

    @Oxer11
    Collaborator

    This may be due to the compilation problem of torch_ext. You can check this issue. DeepGraphLearning/torchdrug#8 (comment)

  11. Oxer11 commented on Apr 27, 2023

    @Oxer11
    Collaborator

    BTW, to generate the embeddings, don't forget to switch the model to .eval() mode by calling gearnet_edge.eval().

  12. pearl-rabbit commented on Apr 27, 2023

    @pearl-rabbit

    Is it written like this?

    gearnet_edge.eval()
    output = gearnet_edge(graph=protein, input=protein.node_feature.float())
    
  13. Oxer11 commented on Apr 27, 2023

    @Oxer11
    Collaborator

    Yes!

  14. jinzhuwei commented on Jun 23, 2023

    @jinzhuwei

    Hello, I haven't used pretrained models before, and after reading the documentation, I still feel a bit confused. I want to use my own PDB dataset, but I don't know how to load it and apply the pretrained model to obtain its representation

    Hello, I want to consult about you using your own PDB dataset to build Graph, whether to implement, I also want to build a protein graph on my own dataset, I want to ask you about the implementation of this part of the sale, I hope to get your sharing, thank you very much.

  15. Tizzzzy commented on Aug 24, 2024

    @Tizzzzy

    Is it written like this?

    gearnet_edge.eval()
    output = gearnet_edge(graph=protein, input=protein.node_feature.float())
    

    Hi pearl-rabbit:
    I am also trying to figure out how to use GearNet loaded with pre-trained weights to extract protein features. I am writing the same code as you, and below is my code:

    import os
    import sys
    import argparse
    import torch
    from torchdrug import core
    sys.path.append(os.path.dirname(os.path.dirname(__file__)))
    from gearnet.model import GearNetIEConv
    from torchdrug.data import Protein
    from torchdrug.core import Registry as R
    from torchdrug import data, utils
    from torchdrug import layers
    from torchdrug.layers import geometry
    from torchdrug import models
    
    pdb_file = utils.download("https://files.rcsb.org/download/2LWZ.pdb", "./")
    
    graph_construction_model = layers.GraphConstruction(node_layers=[geometry.AlphaCarbonNode()], 
                                                        edge_layers=[geometry.SpatialEdge(radius=10.0, min_distance=5),
                                                                     geometry.KNNEdge(k=10, min_distance=5),
                                                                     geometry.SequentialEdge(max_distance=2)],
                                                        edge_feature="gearnet")
    
    # protein
    protein = Protein.from_pdb(pdb_file, atom_feature="position", bond_feature="length", residue_feature="symbol")
    _protein = Protein.pack([protein])
    protein = graph_construction_model(_protein)
    
    # model
    gearnet_edge = models.GearNet(input_dim=21, hidden_dims=[512, 512, 512, 512, 512, 512],
                                  num_relation=7, edge_input_dim=59, num_angle_bin=8,
                                  batch_norm=True, concat_hidden=True, short_cut=True, readout="sum")
    pthfile = '/content/angle_gearnet_edge.pth'
    net = torch.load(pthfile, map_location=torch.device('cpu'))
    gearnet_edge.load_state_dict(net)
    
    #output
    with torch.no_grad():
        gearnet_edge.eval()
        print(protein)
        output = gearnet_edge(protein, protein.node_feature.float(), all_loss=None, metric=None)
        print(output)
    

    However, I am getting following error:

    Traceback (most recent call last):
      File "/content/GearNet-main/script/test.py", line 40, in <module>
        output = gearnet_edge(protein, protein.node_feature.float(), all_loss=None, metric=None)
      File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1532, in _wrapped_call_impl
        return self._call_impl(*args, **kwargs)
      File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1541, in _call_impl
        return forward_call(*args, **kwargs)
      File "/usr/local/lib/python3.10/dist-packages/torchdrug/models/gearnet.py", line 95, in forward
        hidden = self.layers[i](graph, layer_input)
      File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1532, in _wrapped_call_impl
        return self._call_impl(*args, **kwargs)
      File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1541, in _call_impl
        return forward_call(*args, **kwargs)
      File "/usr/local/lib/python3.10/dist-packages/torchdrug/layers/conv.py", line 91, in forward
        update = self.message_and_aggregate(graph, input)
      File "/usr/local/lib/python3.10/dist-packages/torchdrug/layers/conv.py", line 813, in message_and_aggregate
        return update.view(graph.num_node, self.num_relation * self.input_dim)
    RuntimeError: shape '[57, 147]' is invalid for input of size 1197
    

    Can you please take a look at my code? Thank you so much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions