Load a saved model on Pytorch AttributeError: 'dict' object has no attribute 'seek'

I trained a model, and I saved the best one, with:

# Store params at the best validation accuracy
        if save_param and accuracy_star > best_accuracy:
          torch.save({'vgg_a':nets[0].state_dict(),'classifier':nets[3].state_dict()}, f"{model_name}_best_test.pth")

SO, at the end of the training I downloaded this “federated_mnist_best_test.pth” on my local computer.
Now, i created a new notebook on google colab, and I tried din this way to upload the model:

from google.colab import files
uploaded= files.upload()

Inside uploaded I put my .pth file, and then:

state_dict = torch.load(uploaded)

gives me this error:

AttributeError: 'dict' 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.

This error is raised, if you are trying to torch.load a dict as seen here:

torch.load({})
> AttributeError: 'dict' 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.

so I assume that uploaded is not a valid path but a dict object instead.

1 Like

Yes.
Solution was this: state_dict = torch.load('federated_mnist_best_test.pth')