"AttributeError: 'collections.OrderedDict' object has no attribute 'state_dict"

Platform:
ubuntu 16.04
Python version:
3.5
Source framework with version (like Tensorflow 1.4.1 with GPU):
1.8

Actuaally , I use docker providing by

I can successfully run the restnet18 and restnet34 downloaded by “mmdownload”
but error occurs converting my own model:
File “/usr/local/lib/python3.5/dist-packages/torch/jit/init.py”, line 259, in _unique_state_dict
state_dict = module.state_dict(keep_vars=keep_vars)
AttributeError: ‘collections.OrderedDict’ object has no attribute ‘state_dict’

So maybe the environment is OK,There must be something wrong with my pth ,In my python code :
net.load(model_path)
torch.save(net.state_dict(),‘mb_v1_ssd.pth’)

here, model_path is a pth path(pth file) pretrained by others

Both the model_path’s pth and the mb_v1_ssd.pth output the same error
“AttributeError: ‘collections.OrderedDict’ object has no attribute 'state_dict”

Could someone please help ?

Hi,

Isn’t the problem that you give it a state dict instead of a full model?
You save only the state dict, so when you load, you load only the state dict.

1 Like

Thank you for your reply.
I tried to save the full model by adding:
torch.save(net,‘mb_v1_ssd_all.pth’)
only to find this make an error:
File “/home/hong/anaconda3/envs/PyTorch1.0_GPU_ONNX/lib/python3.7/site-packages/torch/serialization.py”, line 291, in _save
pickler.dump(obj)
TypeError: can’t pickle module objects

so,it can not be saved in this way?

Where is your net defined in your code? Could you share the code?

P.S: MMdnn indeed requires PyTorch to save models fully, as stated here in the docs.

hi, thank you for your reply.
I used this repo https://github.com/qfgaohao/pytorch-ssd
and I added my own code to save the net in one of the main python files (eg, run_ssd_example.py)
Some code are :
29 if net_type == ‘vgg16-ssd’:
30 net = create_vgg_ssd(len(class_names), is_test=True)
31 elif net_type == ‘mb1-ssd’:
32 net = create_mobilenetv1_ssd(len(class_names), is_test=True)
33 elif net_type == ‘mb1-ssd-lite’:
34 net = create_mobilenetv1_ssd_lite(len(class_names), is_test=True)
35 elif net_type == ‘mb2-ssd-lite’:
36 net = create_mobilenetv2_ssd_lite(len(class_names), is_test=True)
37 elif net_type == ‘sq-ssd-lite’:
38 net = create_squeezenet_ssd_lite(len(class_names), is_test=True)
39 else:
40 print(“The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite.”)
41 sys.exit(1)
42
43
44 print(net)
_ 45 net.load(model_path)_
_ 46 _
_ 47 torch.save(net.state_dict(),‘mb_v1_ssd.pth’)_
_ 48 _
_ 49 _
_ 50 #net.load(model_path,map_location=‘cpu’)_
_ 51 _
_ 52 torch.save(net,‘mb_v1_ssd_all.pth’)_
53
54 if net_type == ‘vgg16-ssd’:
55 predictor = create_vgg_ssd_predictor(net, candidate_size=200)
56 elif net_type == ‘mb1-ssd’:
57 predictor = create_mobilenetv1_ssd_predictor(net, candidate_size=200)
58 elif net_type == ‘mb1-ssd-lite’:
59 predictor = create_mobilenetv1_ssd_lite_predictor(net, candidate_size=200)
60 elif net_type == ‘mb2-ssd-lite’:
61 predictor = create_mobilenetv2_ssd_lite_predictor(net, candidate_size=200)
62 elif net_type == ‘sq-ssd-lite’:
63 predictor = create_squeezenet_ssd_lite_predictor(net, candidate_size=200)
64 else:
65 print(“The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite.”)
66 sys.exit(1)

line 44-52 is added by myself

Could you try replacing net.load(model_path) by net.load_state_dict(model_path) ?

Although I’m puzzled by the fact that net.load(...) didn’t throw an error…

Hi,
thank you very much.
I tried as you told me.
It seems both
net.load_state_dict(torch.load(model_path))
and
net.load(model_path)
works well(inference correctly)
but still , I don’t know how to save a whole model in the case.

using “net.load_state_dict(torch.load(model_path))” and torch.save(net,‘mb_v1_ssd_all.pth’)
still give en error:
File “/home/hong/anaconda3/envs/PyTorch1.0_GPU_ONNX/lib/python3.7/site-packages/torch/serialization.py”, line 291, in _save
pickler.dump(obj)
TypeError: can’t pickle module objects

Once you have loaded the model (with net = torch.load(file)) or the model weights (with net.load_state_dict(torch.load(file))) correctly, then you can save the model simply by calling torch.save(net, path), as stated in the tutorial here.

Yes, I did so by :
torch.save(net,‘mb_v1_ssd_all.pth’)
but it seems can not save the mb_v1_ssd_all.pth file with error information:
File “/home/hong/anaconda3/envs/PyTorch1.0_GPU_ONNX/lib/python3.7/site-packages/torch/serialization.py”, line 291, in _save
pickler.dump(obj)
TypeError: can’t pickle module objects

I’m puzzled …