PytorchToCaffe: pytorch2caffe error??
my env is pytorch 1.0.1.post2
and i want to convert pytorch model to caffe;
1 run python3 example/alexnet_pytorch_to_caffe.py
140558256725592:view_blob1 was added to blobs Add blob view_blob1 : torch.Size([1, 9216]) Traceback (most recent call last): File "example/alexnet_pytorch_to_caffe.py", line 12, in <module> pytorch_to_caffe.trans_net(net,input,name) File "./pytorch_to_caffe.py", line 612, in trans_net out = net.forward(input_var) File "/usr/local/lib/python3.5/dist-packages/torchvision/models/alexnet.py", line 46, in forward x = x.view(x.size(0), 256 * 6 * 6) File "./pytorch_to_caffe.py", line 410, in _view bottom=[log.blobs(input)],top=top_blobs) File "./pytorch_to_caffe.py", line 88, in blobs print("{}:{} getting".format(var, self._blobs[var])) File "./pytorch_to_caffe.py", line 31, in __getitem__ return self.data[key] KeyError: 140558256725160
2 run python3 example/resnet_pytorch_2_caffe.py
Traceback (most recent call last): File "example/resnet_pytorch_2_caffe.py", line 11, in <module> checkpoint = torch.load("/home/luna/mmdnn/imagenet_resnet18.pth") File "/usr/local/lib/python3.5/dist-packages/torch/serialization.py", line 368, in load return _load(f, map_location, pickle_module) File "/usr/local/lib/python3.5/dist-packages/torch/serialization.py", line 542, in _load result = unpickler.load() UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 1105: ordinal not in range(128)
About this issue
- Original URL
- State: open
- Created 5 years ago
- Comments: 35 (5 by maintainers)
@AshishSardana, according to @strawberryfg’s comments, you could first include
from torch.nn.modules.utils import _list_with_default
and then addand
F.adaptive_avg_pool2d=Rp(F.adaptive_avg_pool2d, _adaptive_avg_pool2d)
in pytorch_to_caffe.py. At least, according to my experience, it solves the alexnet problem.@lunalulu @strawberryfg Hi, I am trying to convert shufflenetv2 from pythorch to caffemodel. I have the same keyError. Any help will be hugely appreciated. I thought it might be caused by channel_shuffle() layer x = x.view(batchsize, -1, height, width) .
def channel_shuffle(x, groups): batchsize, num_channels, height, width = x.data.size() channels_per_group = num_channels // groups #reshape x = x.view(batchsize, groups, channels_per_group, height, width) #transpose x = torch.transpose(x, 1, 2).contiguous() #flatten x = x.view(batchsize, -1, height, width) return x
####error information ------------pytorch_to_caffe.py-----------------------------input: torch.Size([1, 24, 2, 48, 80]) ------------pytorch_to_caffe.py-----------------------------x: torch.Size([1, 48, 48, 80]) view2 was added to layers 139668331934488:view_blob2 was added to blobs Add blob view_blob2 : torch.Size([1, 48, 48, 80]) Traceback (most recent call last): File “msra_pytorch_to_caffe.py”, line 46, in <module> pytorch_to_caffe.trans_net(net, input, name) File “…/pytorch_to_caffe.py”, line 628, in trans_net out = net.forward(input_var) File “/home/yanan/zongmu/PytorchToCaffe/example/v16_d4_msra_resnet.py”, line 336, in forward x = channel_shuffle(x, 2) File “/home/yanan/zongmu/PytorchToCaffe/example/v16_d4_msra_resnet.py”, line 49, in channel_shuffle x = x.view(batchsize, -1, height, width) File “…/pytorch_to_caffe.py”, line 418, in _view print(“------------pytorch_to_caffe.py-----------------------------log.blobs(input):”, log.blobs(input)) File “…/pytorch_to_caffe.py”, line 89, in blobs print(“------------pytorch_to_caffe.py-----------------------------var:”, self._blobs[var]) File “…/pytorch_to_caffe.py”, line 33, in getitem return self.data[key] KeyError: 139668331925080
@lunalulu Hi lunalulu, I worked it out. It turns out that the current version lacks of the transformation of “AdaptiveAvgPool2d” which is needed in https://github.com/pytorch/vision/blob/master/torchvision/models/resnet.py. And so the _view cannot find log.blobs(input) since the global average pooling has not been registered yet.
See the code below for details. It should work. ` #Mar 26, 2019 def _adaptive_avg_pool2d(raw, input, output_size): _output_size = _list_with_default(output_size, input.size()) x = raw(input, _output_size)
`
#Added Mar 26, 2019 F.adaptive_avg_pool2d=Rp(F.adaptive_avg_pool2d, _adaptive_avg_pool2d)
Don’t forget to include
from torch.nn.modules.utils import _list_with_default
For reference, the adaptive function is borrowed from PyTorch implementation https://pytorch.org/docs/stable/_modules/torch/nn/functional.html.
I am not sure if PyTorch 0.3, 0.4 or whatever < 1.0 can work nicely.