-
Notifications
You must be signed in to change notification settings - Fork 21.7k
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
Comments
I met the same issue, how to fix it? |
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 |
thanks! |
How about if I want to save multiple values with torch.save ?
and encountering the same error.
after changing the directory structure. |
I met the same problem with problem with @jhagege |
I'm having the same issue as @jhagege and @ericwtlin, any solutions? |
I used |
Solution 1:Try converting your model into 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 : # 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 |
Try to construct a module with the same path in your previous project. |
I'm facing the same issue, did anyone figure out a good way to resolve this? @Coderx7 's solution did not work for me. |
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. |
torch.load() requires model module in the same folder #3678 |
|
I think you are right. |
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') Looking through the third party source code I can see that they saved the model this way: I also tried to load it with torch.load_state_dict('model_checkpoint.pth.tar') but that didn't work either. |
I solved it by adding the ckpt path to sys.path |
thank you! it works! sys.path.append('/mnt/.../my_demo') |
This had to be done due to pytorch/pytorch#18325
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'
The text was updated successfully, but these errors were encountered: