xxradon / PytorchToCaffe

Pytorch model to caffe model, supported pytorch 0.3, 0.3.1, 0.4, 0.4.1 ,1.0 , 1.0.1 , 1.2 ,1.3 .notice that only pytorch 1.1 have some bugs
MIT License
782 stars 224 forks source link

run the examples error. `TypeError: None has type NoneType, but expected one of: bytes, unicode` #104

Open leo038 opened 3 years ago

leo038 commented 3 years ago

I just download the code and run python3 example/resnet_pytorch_2_caffe.py

and got the following error

WARNING: CANNOT FOUND blob 140623413128576
Traceback (most recent call last):
  File "example/resnet_pytorch_2_caffe.py", line 19, in <module>
    pytorch_to_caffe.trans_net(resnet18,input,name)
  File "./pytorch_to_caffe.py", line 786, in trans_net
    out = net.forward(input_var)
  File "/usr/local/lib/python3.6/dist-packages/torchvision/models/resnet.py", line 206, in forward
    x = self.fc(x)
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 547, in __call__
    result = self.forward(*input, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/linear.py", line 87, in forward
    return F.linear(input, self.weight, self.bias)
  File "./pytorch_to_caffe.py", line 690, in __call__
    out=self.obj(self.raw,*args,**kwargs)
  File "./pytorch_to_caffe.py", line 138, in _linear
    bottom=[log.blobs(input)],top=top_blobs)
  File "./Caffe/layer_param.py", line 33, in __init__
    self.bottom.extend(bottom)
TypeError: None has type NoneType, but expected one of: bytes, unicode

the version of my environment is: Ubuntu 18.04 x86_64 python3: 3.6.9 torch: 1.2.0 torchvision:0.4.0

WeibinKOU commented 3 years ago

I have the same issue. can someone help us to fix this issue? my log is listed as follow: ['cat_blob1'] ['relu_blob8', None] Traceback (most recent call last): File "app_py2caffe.py", line 32, in pytorch2caffe.trans_net(seg_model,input,name) File "/opt/anaconda3/envs/palm/lib/python3.6/site-packages/pytorch2caffe/pytorch2caffe.py", line 1020, in trans_net out = net.forward(input_var) File "./unet.py", line 104, in forward x = self.up4(x5, x4) File "/opt/anaconda3/envs/palm/lib/python3.6/site-packages/torch/nn/modules/module.py", line 727, in _call_impl result = self.forward(*input, *kwargs) File "./unet.py", line 66, in forward x = torch.cat([x2, x1], dim=1) File "/opt/anaconda3/envs/palm/lib/python3.6/site-packages/pytorch2caffe/pytorch2caffe.py", line 923, in call out = self.obj(self.raw, args, **kwargs) File "/opt/anaconda3/envs/palm/lib/python3.6/site-packages/pytorch2caffe/pytorch2caffe.py", line 314, in _cat top=top_blobs) File "/opt/anaconda3/envs/palm/lib/python3.6/site-packages/pytorch2caffe/layer_param.py", line 44, in init self.bottom.extend(bottom) TypeError: None has type NoneType, but expected one of: bytes, unicode

changgongcheng commented 3 years ago

the same issue

imistyrain commented 3 years ago

你的torchvision是0.4.0太高了,换成0.2.0 主要是0.2之后的版本将view改成了flatten操作,以alexnet为例 0.2的实现为

def forward(self, x):
    x = self.features(x)
    x = x.view(x.size(0), 256 * 6 * 6)
    x = self.classifier(x)
    return x

到了0.5变成了

def forward(self, x):
    x = self.features(x)
    x = self.avgpool(x)
    x = torch.flatten(x, 1)
    x = self.classifier(x)
    return x

其中的flatten并没有实现,搞清楚原因后解决就简单多了 新建一_flatten函数

def _flatten(raw , input, * args):
    x = raw(input, *args)
    if not NET_INITTED:
        return x
    layer_name=log.add_layer(name='flatten')
    top_blobs=log.add_blobs([x],name='flatten_blob')
    layer=caffe_net.Layer_param(name=layer_name,type='Reshape',
                                bottom=[log.blobs(input)],top=top_blobs)
    start_dim = args[0]
    end_dim = len(x.shape)
    if len(args) > 1:
        end_dim = args[1]
    dims = []
    for i in range(start_dim):
        dims.append(x.shape[i])
    cum = 1
    for i in range(start_dim, end_dim):
        cum = cum * x.shape[i]
    dims.append(cum)
    if end_dim != len(x.shape):
        cum = 1
        for i in range(end_dim, len(x.shape)):
            cum = cum * x.shape[i]
        dims.append(cum)
    layer.param.reshape_param.shape.CopyFrom(caffe_net.pb.BlobShape(dim=dims))
    log.cnet.add_layer(layer)
    return x

然后替换pytorch中的实现,在那一堆torch.max=Rp(torch.max,_max)等骚操作后加上 torch.flatten = Rp(torch.flatten,_flatten)

wuwuzhijing commented 2 years ago
  1. not related to vision of torchvision
  2. @imistyrain add the flatten founction, the same problem
RichardcLee commented 2 years ago

I just download the code and run python3 example/resnet_pytorch_2_caffe.py

and got the following error

WARNING: CANNOT FOUND blob 140623413128576
Traceback (most recent call last):
  File "example/resnet_pytorch_2_caffe.py", line 19, in <module>
    pytorch_to_caffe.trans_net(resnet18,input,name)
  File "./pytorch_to_caffe.py", line 786, in trans_net
    out = net.forward(input_var)
  File "/usr/local/lib/python3.6/dist-packages/torchvision/models/resnet.py", line 206, in forward
    x = self.fc(x)
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 547, in __call__
    result = self.forward(*input, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/linear.py", line 87, in forward
    return F.linear(input, self.weight, self.bias)
  File "./pytorch_to_caffe.py", line 690, in __call__
    out=self.obj(self.raw,*args,**kwargs)
  File "./pytorch_to_caffe.py", line 138, in _linear
    bottom=[log.blobs(input)],top=top_blobs)
  File "./Caffe/layer_param.py", line 33, in __init__
    self.bottom.extend(bottom)
TypeError: None has type NoneType, but expected one of: bytes, unicode

the version of my environment is: Ubuntu 18.04 x86_64 python3: 3.6.9 torch: 1.2.0 torchvision:0.4.0

It works!