AttributeError: 'collections.OrderedDict' object has no attribute 'eval'

后端 未结 2 1949
我在风中等你
我在风中等你 2021-02-20 07:00

I have a model file which looks like this

OrderedDict([(\'inp.conv1.conv.weight\', 
          (0 ,0 ,0 ,.,.) = 
           -1.5073e-01  6.4760e-02  1.9156e-01
           


        
2条回答
  •  你的背包
    2021-02-20 07:35

    It is not a model file, instead, this is a state file. In a model file, the complete model is stored, whereas in a state file only the parameters are stored.
    So, your OrderedDict are just values for your model. You will need to create the model and then need to load these values into your model. So, the process will be something in form of

    import torch
    import torch.nn as nn
    
    class TempModel(nn.Module):
        def __init__(self):
            self.conv1 = nn.Conv2d(3, 5, (3, 3))
        def forward(self, inp):
            return self.conv1(inp)
    
    model = TempModel()
    model.load_state_dict(torch.load(file_path))
    model.eval()
    

    You'll need to define your model properly. The one given in the example above is just a dummy. If you construct your model yourself, you might need to update the keys of the saved dict file as mentioned here. The best course of action is to define your model in exactly the same way from when the state_dict was saved and then directly executing model.load_state_dict will work.

提交回复
热议问题