# Error: 'DataParallel' object has no attribute 'items'

**URL:** <https://discuss.pytorch.org/t/error-dataparallel-object-has-no-attribute-items/26791>\
**Category:** Uncategorized\
**Created:** [October 8, 2018, 5:40pm UTC](https://discuss.pytorch.org/t/error-dataparallel-object-has-no-attribute-items/26791 "2018-10-08T17:40:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![uhvardhan](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/u/82dd89/32.png) [@uhvardhan](https://discuss.pytorch.org/u/uhvardhan)\
**Post date:** [October 8, 2018, 5:40pm UTC](https://discuss.pytorch.org/t/error-dataparallel-object-has-no-attribute-items/26791/1 "2018-10-08T17:40:55Z")

</div>

I am trying to convert PyTorch model to Onnx model.

I keep ending up with the following error:

```auto
Traceback (most recent call last):
  File "/tmp/pycharm_project_896/utils/pytorch2onnx.py", line 15, in <module>
    for k, v in state_dict.items():
  File "/usr/local/lib/python2.7/dist-packages/torch/nn/modules/module.py", line 398, in __getattr__
    type(self). __name__ , name))
AttributeError: 'DataParallel' object has no attribute 'items'

```

I have been using the following code:

```auto
import torch
from torch.autograd import Variable

# Load the trained model from file

path = "model.pth"

#pytorch_network = torch.load(path)
state_dict = torch.load("model.pth")

#pytorch_network.state_dict()
# Create new OrderedDict that does not contain the module
from collections import OrderedDict
new_state_dict = OrderedDict()
for k, v in state_dict.items():
    name = k[7:] # remove module
    new_state_dict[name] = v

pytorch_network.load_state_dict(new_state_dict)

# Export the trained model to ONNX
#pytorch_network = torch.nn.DataParallel(pytorch_network, device_ids=range(torch.cuda.device_count()))
dummy_input = Variable(torch.randn(1, 3, 224, 224))
torch.onnx.export(pytorch_network, dummy_input, "model.onnx")

```

Can anyone tell me where I might be going wrong. I keep ended up with the same error.  
I have referred this [Issue](https://discuss.pytorch.org/t/solved-keyerror-unexpected-key-module-encoder-embedding-weight-in-state-dict/1686/3) too but I don’t seem to end with a solution.

Thanks

---

<div class="post-metadata">

**Author:** ![kaixin](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/kaixin/32/33299_2.png) [@kaixin](https://discuss.pytorch.org/u/kaixin)\
**Post date:** [October 9, 2018, 3:00am UTC](https://discuss.pytorch.org/t/error-dataparallel-object-has-no-attribute-items/26791/2 "2018-10-09T03:00:31Z")

</div>

I guess it is similar with the problem you posted [before](https://discuss.pytorch.org/t/attributeerror-dataparallel-object-has-no-attribute-items/26500/8).  
The `.pth` file saves the entire model rather than the weights. Your `state_dict` is actually not an `OrderedDict` but a `DataParallel` object. That is why you get the error.
