Attribute Error: collections.OrderedDict' object has no attribute 'eval'

I save my model using the following code.

Path = “Model/model.pth”
torch.save(Network.state_dict(), Path)

Now, i am trying to load the trained model using following code,

model = torch.load(“Model/model.pth”)
model.eval()

But the following error occurred.
AttributeError: ‘collections.OrderedDict’ object has no attribute ‘eval’

You have to do something like this:
Save:

torch.save(model.state_dict(), PATH)

Load:

model = TheModelClass(*args, **kwargs)
model.load_state_dict(torch.load(PATH))
model.eval()

For more information: https://pytorch.org/tutorials/beginner/saving_loading_models.html

1 Like

It worked.
Thank you so much :slight_smile:

I save my model using the following code.

def save(self, file_path, e):
torch.save(self.spatial_stream.state_dict(), os.path.join(file_path, ‘spt_{:03d}.pth’.format(e)))
torch.save(self.temporal_stream.state_dict(), os.path.join(file_path, ‘tmp_{:03d}.pth’.format(e)))

load:
def load(self, file_path):
self.load_state_dict(torch.load(file_path))

But I meet this problem,can you help me?Thank you very much !!!
Traceback (most recent call last):
File “/home/wuting/anaconda3/envs/python=3.8/lib/python3.8/site-packages/torch/serialization.py”, line 308, in _check_seekable
f.seek(f.tell())
AttributeError: ‘EasyDict’ object has no attribute ‘seek’

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “test.py”, line 109, in
main()
File “test.py”, line 64, in main
epoch_loop.main()
File “/home/wuting/Spatialtem-multi/tools/epoch_loop.py”, line 70, in main
self.setup_net()
File “/home/wuting/Spatialtem-multi/tools/epoch_loop.py”, line 78, in setup_net
self.net = TwoStreamNet(self.device)
File “/home/wuting/Spatialtem-multi/lib/netwrapper/two_stream_net.py”, line 39, in init
self.create_load(device)
File “/home/wuting/Spatialtem-multi/lib/netwrapper/two_stream_net.py”, line 53, in create_load
self.load(cfg.CUS_PT_PATH)
File “/home/wuting/Spatialtem-multi/lib/netwrapper/two_stream_net.py”, line 207, in load
self.load_state_dict(torch.load(file_path))
File “/home/wuting/anaconda3/envs/python=3.8/lib/python3.8/site-packages/torch/serialization.py”, line 581, in load
with _open_file_like(f, ‘rb’) as opened_file:
File “/home/wuting/anaconda3/envs/python=3.8/lib/python3.8/site-packages/torch/serialization.py”, line 235, in _open_file_like
return _open_buffer_reader(name_or_buffer)
File “/home/wuting/anaconda3/envs/python=3.8/lib/python3.8/site-packages/torch/serialization.py”, line 220, in init
_check_seekable(buffer)
File “/home/wuting/anaconda3/envs/python=3.8/lib/python3.8/site-packages/torch/serialization.py”, line 311, in _check_seekable
raise_err_msg([“seek”, “tell”], e)
File “/home/wuting/anaconda3/envs/python=3.8/lib/python3.8/site-packages/torch/serialization.py”, line 304, in raise_err_msg
raise type(e)(msg)
AttributeError: ‘EasyDict’ object has no attribute ‘seek’. You can only torch.load from a file that is seekable. Please pre-load the data into a buffer like io.BytesIO and try to load from it instead.

I change torch.oad as this,like that it is work.

def load(self, file_path):
self.load_state_dict(torch.load(‘PATH’),strict=False)

note:PATH is Specific weight paths。But I don not know why?maybe it needs the real path?

but in this way,the loss is ery biggest.Why?can you help me ?