Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ModuleNotFoundError: No module named 'models' #18325

Closed
LIMr1209 opened this issue Mar 22, 2019 · 18 comments
Closed

ModuleNotFoundError: No module named 'models' #18325

LIMr1209 opened this issue Mar 22, 2019 · 18 comments

Comments

@LIMr1209
Copy link

mdoel = torch.load('/opt/checkpoint/ResNet152.pth',map_location='cpu')

Traceback (most recent call last):
File "/home/tian/pycharm-2018.3.3/helpers/pydev/pydevd.py", line 1741, in
main()
File "/home/tian/pycharm-2018.3.3/helpers/pydev/pydevd.py", line 1735, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "/home/tian/pycharm-2018.3.3/helpers/pydev/pydevd.py", line 1135, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "/home/tian/pycharm-2018.3.3/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/opt/project/python/opalus/fff.py", line 3, in
mdoel = torch.load('/opt/checkpoint/ResNet152_quantize.pth',map_location='cpu')
File "/home/tian/.conda/envs/jiqi/lib/python3.6/site-packages/torch/serialization.py", line 368, in load
return _load(f, map_location, pickle_module)
File "/home/tian/.conda/envs/jiqi/lib/python3.6/site-packages/torch/serialization.py", line 542, in _load
result = unpickler.load()
ModuleNotFoundError: No module named 'models'

@ssnl
Copy link
Collaborator

ssnl commented Mar 22, 2019

@ssnl ssnl closed this as completed Mar 22, 2019
@cccober
Copy link

cccober commented Jun 25, 2019

I met the same issue, how to fix it?

@LIMr1209
Copy link
Author

However in this case, the serialized data is bound to the specific classes and the exact directory structure used, so it can break in various ways when used in other projects, or after some serious refactors. @cccober

@cccober
Copy link

cccober commented Jun 25, 2019

thanks!

@jhagege
Copy link

jhagege commented Jun 25, 2019

How about if I want to save multiple values with torch.save ?
I did

torch.save({'model': model.state_dict(), 'acc': best_valid_acc, 'epoch': epoch, 'network_params': network_params, 'drop_path_prob': drop_path_prob})

and encountering the same error.

state = torch.load(state_path)
ModuleNotFoundError: No module named 'cnn'

after changing the directory structure.
I thought using model.state_dict() was robust to directory structure changes..

@ericwtlin
Copy link

How about if I want to save multiple values with torch.save ?
I did

torch.save({'model': model.state_dict(), 'acc': best_valid_acc, 'epoch': epoch, 'network_params': network_params, 'drop_path_prob': drop_path_prob})

and encountering the same error.

state = torch.load(state_path)
ModuleNotFoundError: No module named 'cnn'

after changing the directory structure.
I thought using model.state_dict() was robust to directory structure changes..

I met the same problem with problem with @jhagege

@pinkhamr
Copy link

I'm having the same issue as @jhagege and @ericwtlin, any solutions?

@Ryuk17
Copy link

Ryuk17 commented Dec 24, 2019

I used
torch.save(model_object, 'model.pkl')
but I cannot load the trained model by
model = torch.load('model.pkl')

@Coderx7
Copy link
Contributor

Coderx7 commented Mar 18, 2020

Solution 1:

Try converting your model into torch script and then use that instead:

def convert_model(model, input=torch.tensor(torch.rand(size=(1,3,112,112)))):
        model = torch.jit.trace(self.model, input)
        torch.jit.save(model,'/home/Rika/Documents/models/model.tjm')

and then loaded this version instead:

# load the model 
self.model = torch.jit.load('/home/Rika/Documents/models/model.tjm')

Solution 2:

simply save the model's state_dict() again and use that instead :
I myself ended up doing :

# since my model was wrapped around by `nn.DataParallel`, I use the `module `property to access the actual model)
self.model = checkpoint['model'].module
# create the new checkpoint based on what you need 
torch.save({'state_dict' : self.model.state_dict(), 'use_se':True},
            '/home/Rika/Documents/BEST_checkpoint_r18_2.tar')

and started using the new checkpoint and so far everything has been good

@Asteria-BCSD
Copy link

Try to construct a module with the same path in your previous project.

@Pradhy729
Copy link

I'm facing the same issue, did anyone figure out a good way to resolve this? @Coderx7 's solution did not work for me.

@pinkhamr
Copy link

pinkhamr commented Jul 9, 2020

To load it, I found replicating the original file structure with a dummy file sufficed and would allow me to resave the model with a different method. After I realized this was a problem, I saved the model definition separately from the weights and as long as I loaded them in that order I didn't have the module not found issue.

@priteshgohil
Copy link

torch.load() requires model module in the same folder #3678

@azuryl
Copy link

azuryl commented Oct 9, 2020

replicating the original file structure with a dummy file
how to replicating the original file structure with a dummy file

@GoingMyWay
Copy link

torch.load() requires model module in the same folder #3678

I think you are right.

@barrelchester
Copy link

I'm getting this error when trying to load a third party checkpoint that I didn't originally save. I have the checkpoint file in the same directory as my code.

checkpoint = torch.load('model_checkpoint.pth.tar')
ModuleNotFoundError: No module named 'models'

Looking through the third party source code I can see that they saved the model this way:
torch.save({'epoch': epoch, 'model': model}, 'model_checkpoint.pth.tar')

I also tried to load it with torch.load_state_dict('model_checkpoint.pth.tar') but that didn't work either.
Any help I could get would be much appreciated.

@lunaryan
Copy link

lunaryan commented Nov 7, 2022

I solved it by adding the ckpt path to sys.path

@wyxscir
Copy link

wyxscir commented Apr 10, 2023

I solved it by adding the ckpt path to sys.path

thank you! it works!

sys.path.append('/mnt/.../my_demo')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests