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
783 stars 224 forks source link

None has type NoneType, but expected one of: bytes, unicode #26

Open cswwp opened 5 years ago

cswwp commented 5 years ago

convert from pytorch to caffe with own model,

WARNING: CANNOT FOUND blob 140437991457808 Traceback (most recent call last): File "conver_rgb.py", line 49, in convert(args) File "conver_rgb.py", line 42, in convert pytorch_to_caffe.trans_net(net, input_var, name) File "/home/wangwenpeng/work/FaceAntiSpoofing_readsense_pytorch_patch/pytorch2caffe_MGN/convert/pytorch_to_caffe.py", line 459, in trans_net out = net.forward(input_var) File "convert/patch_attention_net.py", line 58, in forward logit = logit.view(batch_size, -1) File "/home/wangwenpeng/work/FaceAntiSpoofing_readsense_pytorch_patch/pytorch2caffe_MGN/convert/pytorch_to_caffe.py", line 297, in _view bottom=[log.blobs(input)],top=top_blobs) File "/home/wangwenpeng/work/FaceAntiSpoofing_readsense_pytorch_patch/pytorch2caffe_MGN/Caffe/layer_param.py", line 33, in init self.bottom.extend(bottom) TypeError: None has type NoneType, but expected one of: bytes, unicode

Anyone help? seems input blob error, but i show input shape ok before NET_INITTED=True in trans_net(), and error after NET_INITTED=True

xingyueye commented 4 years ago

hi, I have met same problem, can you tell how to solve it, thank you

ivylinden commented 4 years ago

Hi ,I also have met this problem,Can you tell me how to solve it. thank you

114153 commented 4 years ago

Haaaaaa ,I also have met this problem.So, what can we do?

cswwp commented 4 years ago

please check your pytorch layer, confirm whether the layer you used is supported in this tool, some operation need change your writing, such as change torch.add to0 "torch.add"

xingyueye commented 4 years ago

That's right, I have found some layers that are not supported. The core reason for this problem is that some layers are not registered and the blobs can not be found. You must accurately locating the unsuitable layers/operations, Unfortunately ,This work would be disgusting...

peyer commented 4 years ago

@xingyueye I want to convert a model containing DCN, how should I add DCN layer register on this tools?

xingyueye commented 4 years ago

@peyer Sorry, I have no experience in converting DCN and can't give you advice.

zchrissirhcz commented 4 years ago

Same problem message here...

torch 1.3.1 torchvision 0.4.2

What I ran is the official example

python example/alexnet_pytorch_to_caffe.py

Got result output:

⚡ python example/alexnet_pytorch_to_caffe.py Starting Transform, This will take a while 140624258807272:blob1 was added to blobs torch ops name: {AlexNet( (features): Sequential( (0): Conv2d(3, 64, kernel_size=(11, 11), stride=(4, 4), padding=(2, 2)) (1): ReLU(inplace=True) (2): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False) (3): Conv2d(64, 192, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2)) (4): ReLU(inplace=True) (5): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False) (6): Conv2d(192, 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)) (7): ReLU(inplace=True) (8): Conv2d(384, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)) (9): ReLU(inplace=True) (10): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)) (11): ReLU(inplace=True) (12): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False) ) (avgpool): AdaptiveAvgPool2d(output_size=(6, 6)) (classifier): Sequential( (0): Dropout(p=0.5, inplace=False) (1): Linear(in_features=9216, out_features=4096, bias=True) (2): ReLU(inplace=True) (3): Dropout(p=0.5, inplace=False) (4): Linear(in_features=4096, out_features=4096, bias=True) (5): ReLU(inplace=True) (6): Linear(in_features=4096, out_features=1000, bias=True) ) ): '', Sequential( (0): Conv2d(3, 64, kernel_size=(11, 11), stride=(4, 4), padding=(2, 2)) (1): ReLU(inplace=True) (2): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False) (3): Conv2d(64, 192, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2)) (4): ReLU(inplace=True) (5): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False) (6): Conv2d(192, 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)) (7): ReLU(inplace=True) (8): Conv2d(384, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)) (9): ReLU(inplace=True) (10): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)) (11): ReLU(inplace=True) (12): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False) ): 'features', Conv2d(3, 64, kernel_size=(11, 11), stride=(4, 4), padding=(2, 2)): 'features.0', ReLU(inplace=True): 'features.1', MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False): 'features.2', Conv2d(64, 192, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2)): 'features.3', ReLU(inplace=True): 'features.4', MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False): 'features.5', Conv2d(192, 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)): 'features.6', ReLU(inplace=True): 'features.7', Conv2d(384, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)): 'features.8', ReLU(inplace=True): 'features.9', Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)): 'features.10', ReLU(inplace=True): 'features.11', MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False): 'features.12', AdaptiveAvgPool2d(output_size=(6, 6)): 'avgpool', Sequential( (0): Dropout(p=0.5, inplace=False) (1): Linear(in_features=9216, out_features=4096, bias=True) (2): ReLU(inplace=True) (3): Dropout(p=0.5, inplace=False) (4): Linear(in_features=4096, out_features=4096, bias=True) (5): ReLU(inplace=True) (6): Linear(in_features=4096, out_features=1000, bias=True) ): 'classifier', Dropout(p=0.5, inplace=False): 'classifier.0', Linear(in_features=9216, out_features=4096, bias=True): 'classifier.1', ReLU(inplace=True): 'classifier.2', Dropout(p=0.5, inplace=False): 'classifier.3', Linear(in_features=4096, out_features=4096, bias=True): 'classifier.4', ReLU(inplace=True): 'classifier.5', Linear(in_features=4096, out_features=1000, bias=True): 'classifier.6'} features.0 conv: blob1 conv1 was added to layers 140624258809792:conv_blob1 was added to blobs features.1 relu1 was added to layers 140624258809576:relu_blob1 was added to blobs features.2 max_pool1 was added to layers 140624258809432:max_pool_blob1 was added to blobs features.3 conv: max_pool_blob1 conv2 was added to layers 140624258807200:conv_blob2 was added to blobs features.4 relu2 was added to layers 140624258809504:relu_blob2 was added to blobs features.5 max_pool2 was added to layers 140624258809648:max_pool_blob2 was added to blobs features.6 conv: max_pool_blob2 conv3 was added to layers 140624258809720:conv_blob3 was added to blobs features.7 relu3 was added to layers 140624258890920:relu_blob3 was added to blobs features.8 conv: relu_blob3 conv4 was added to layers 140624258890704:conv_blob4 was added to blobs features.9 relu4 was added to layers 140624258890776:relu_blob4 was added to blobs features.10 conv: relu_blob4 conv5 was added to layers 140624258890560:conv_blob5 was added to blobs features.11 relu5 was added to layers 140624258890632:relu_blob5 was added to blobs features.12 max_pool3 was added to layers 140624258890416:max_pool_blob3 was added to blobs classifier.0 WARNING: CANNOT FOUND blob 140624258890272 dropout1 was added to layers 140624258890488:None was added to blobs Traceback (most recent call last): File "example/alexnet_pytorch_to_caffe.py", line 12, in pytorch_to_caffe.trans_net(net,input,name) File "./pytorch_to_caffe.py", line 759, in trans_net out = net.forward(input_var) File "/home/zz/soft/miniconda3/lib/python3.7/site-packages/torchvision/models/alexnet.py", line 48, in forward x = self.classifier(x) File "/home/zz/soft/miniconda3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 541, in call result = self.forward(*input, kwargs) File "/home/zz/soft/miniconda3/lib/python3.7/site-packages/torch/nn/modules/container.py", line 92, in forward input = module(input) File "/home/zz/soft/miniconda3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 541, in call result = self.forward(*input, *kwargs) File "/home/zz/soft/miniconda3/lib/python3.7/site-packages/torch/nn/modules/dropout.py", line 54, in forward return F.dropout(input, self.p, self.training, self.inplace) File "./pytorch_to_caffe.py", line 662, in call out=self.obj(self.raw,args,kwargs) File "./pytorch_to_caffe.py", line 230, in _dropout bottom=bottom_blobs,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

zchrissirhcz commented 4 years ago

By switching to python3.6 and related packages, I can run it corectly.

You may just conda env create -f environment.yml. Its content:

name: py36_pytorch041
channels:
  - defaults
dependencies:
  - _libgcc_mutex=0.1=main
  - ca-certificates=2019.10.16=0
  - certifi=2019.9.11=py36_0
  - libedit=3.1.20181209=hc058e9b_0
  - libffi=3.2.1=hd88cf55_4
  - libgcc-ng=9.1.0=hdf63c60_0
  - libstdcxx-ng=9.1.0=hdf63c60_0
  - ncurses=6.1=he6710b0_1
  - openssl=1.1.1d=h7b6447c_3
  - pip=19.3.1=py36_0
  - python=3.6.9=h265db76_0
  - readline=7.0=h7b6447c_5
  - setuptools=41.6.0=py36_0
  - sqlite=3.30.1=h7b6447c_0
  - tk=8.6.8=hbc83047_0
  - wheel=0.33.6=py36_0
  - xz=5.2.4=h14c3975_4
  - zlib=1.2.11=h7b6447c_3
  - pip:
    - cycler==0.10.0
    - decorator==4.4.1
    - imageio==2.6.1
    - kiwisolver==1.1.0
    - matplotlib==3.1.1
    - networkx==2.4
    - numpy==1.17.2
    - opencv-python==3.3.1.11
    - pillow==6.2.1
    - protobuf==3.10.0
    - pyparsing==2.4.5
    - python-dateutil==2.8.1
    - pywavelets==1.1.1
    - scikit-image==0.15.0
    - scipy==1.3.1
    - six==1.13.0
    - torch==0.4.1
    - torchvision==0.2.1
prefix: /media/data1/hl/software/miniconda3/envs/py36_pytorch041
maithstartup commented 4 years ago

By switching to python3.6 and related packages, I can run it corectly.

You may just conda env create -f environment.yml. Its content:

name: py36_pytorch041
channels:
  - defaults
dependencies:
  - _libgcc_mutex=0.1=main
  - ca-certificates=2019.10.16=0
  - certifi=2019.9.11=py36_0
  - libedit=3.1.20181209=hc058e9b_0
  - libffi=3.2.1=hd88cf55_4
  - libgcc-ng=9.1.0=hdf63c60_0
  - libstdcxx-ng=9.1.0=hdf63c60_0
  - ncurses=6.1=he6710b0_1
  - openssl=1.1.1d=h7b6447c_3
  - pip=19.3.1=py36_0
  - python=3.6.9=h265db76_0
  - readline=7.0=h7b6447c_5
  - setuptools=41.6.0=py36_0
  - sqlite=3.30.1=h7b6447c_0
  - tk=8.6.8=hbc83047_0
  - wheel=0.33.6=py36_0
  - xz=5.2.4=h14c3975_4
  - zlib=1.2.11=h7b6447c_3
  - pip:
    - cycler==0.10.0
    - decorator==4.4.1
    - imageio==2.6.1
    - kiwisolver==1.1.0
    - matplotlib==3.1.1
    - networkx==2.4
    - numpy==1.17.2
    - opencv-python==3.3.1.11
    - pillow==6.2.1
    - protobuf==3.10.0
    - pyparsing==2.4.5
    - python-dateutil==2.8.1
    - pywavelets==1.1.1
    - scikit-image==0.15.0
    - scipy==1.3.1
    - six==1.13.0
    - torch==0.4.1
    - torchvision==0.2.1
prefix: /media/data1/hl/software/miniconda3/envs/py36_pytorch041

I created a virtual environment with your environment.yml file, I used python 3.6.0,is there anything else i need to convert the model.

CASIATumingfei commented 4 years ago

convert from pytorch to caffe with own model, WARNING: CANNOT FOUND blob 140437991457808 Traceback (most recent call last): File "conver_rgb.py", line 49, in convert(args) File "conver_rgb.py", line 42, in convert pytorch_to_caffe.trans_net(net, input_var, name) File "/home/wangwenpeng/work/FaceAntiSpoofing_readsense_pytorch_patch/pytorch2caffe_MGN/convert/pytorch_to_caffe.py", line 459, in trans_net out = net.forward(input_var) File "convert/patch_attention_net.py", line 58, in forward logit = logit.view(batch_size, -1) File "/home/wangwenpeng/work/FaceAntiSpoofing_readsense_pytorch_patch/pytorch2caffe_MGN/convert/pytorch_to_caffe.py", line 297, in _view bottom=[log.blobs(input)],top=top_blobs) File "/home/wangwenpeng/work/FaceAntiSpoofing_readsense_pytorch_patch/pytorch2caffe_MGN/Caffe/layer_param.py", line 33, in init self.bottom.extend(bottom) TypeError: None has type NoneType, but expected one of: bytes, unicode Anyone help? seems input blob error, but i show input shape ok before NET_INITTED=True in trans_net(), and error after NET_INITTED=True

I met the same problem when using .view(), WARNING: CANNOT FOUND blob 140538514487648, should I change view() to something else? want to know how did you solve it

NickShen4Github commented 4 years ago

By switching to python3.6 and related packages, I can run it corectly. You may just conda env create -f environment.yml. Its content:

name: py36_pytorch041
channels:
  - defaults
dependencies:
  - _libgcc_mutex=0.1=main
  - ca-certificates=2019.10.16=0
  - certifi=2019.9.11=py36_0
  - libedit=3.1.20181209=hc058e9b_0
  - libffi=3.2.1=hd88cf55_4
  - libgcc-ng=9.1.0=hdf63c60_0
  - libstdcxx-ng=9.1.0=hdf63c60_0
  - ncurses=6.1=he6710b0_1
  - openssl=1.1.1d=h7b6447c_3
  - pip=19.3.1=py36_0
  - python=3.6.9=h265db76_0
  - readline=7.0=h7b6447c_5
  - setuptools=41.6.0=py36_0
  - sqlite=3.30.1=h7b6447c_0
  - tk=8.6.8=hbc83047_0
  - wheel=0.33.6=py36_0
  - xz=5.2.4=h14c3975_4
  - zlib=1.2.11=h7b6447c_3
  - pip:
    - cycler==0.10.0
    - decorator==4.4.1
    - imageio==2.6.1
    - kiwisolver==1.1.0
    - matplotlib==3.1.1
    - networkx==2.4
    - numpy==1.17.2
    - opencv-python==3.3.1.11
    - pillow==6.2.1
    - protobuf==3.10.0
    - pyparsing==2.4.5
    - python-dateutil==2.8.1
    - pywavelets==1.1.1
    - scikit-image==0.15.0
    - scipy==1.3.1
    - six==1.13.0
    - torch==0.4.1
    - torchvision==0.2.1
prefix: /media/data1/hl/software/miniconda3/envs/py36_pytorch041

I created a virtual environment with your environment.yml file, I used python 3.6.0,is there anything else i need to convert the model.

If you are using pytorch version above or equal to 1.1. The conda solution wont work. In my conputer pytorch <=1.0.0 works perfectly while I update to 1.3 it gives an identical error shown above.

xiexu666 commented 4 years ago

By switching to python3.6 and related packages, I can run it corectly. You may just conda env create -f environment.yml. Its content:

name: py36_pytorch041
channels:
  - defaults
dependencies:
  - _libgcc_mutex=0.1=main
  - ca-certificates=2019.10.16=0
  - certifi=2019.9.11=py36_0
  - libedit=3.1.20181209=hc058e9b_0
  - libffi=3.2.1=hd88cf55_4
  - libgcc-ng=9.1.0=hdf63c60_0
  - libstdcxx-ng=9.1.0=hdf63c60_0
  - ncurses=6.1=he6710b0_1
  - openssl=1.1.1d=h7b6447c_3
  - pip=19.3.1=py36_0
  - python=3.6.9=h265db76_0
  - readline=7.0=h7b6447c_5
  - setuptools=41.6.0=py36_0
  - sqlite=3.30.1=h7b6447c_0
  - tk=8.6.8=hbc83047_0
  - wheel=0.33.6=py36_0
  - xz=5.2.4=h14c3975_4
  - zlib=1.2.11=h7b6447c_3
  - pip:
    - cycler==0.10.0
    - decorator==4.4.1
    - imageio==2.6.1
    - kiwisolver==1.1.0
    - matplotlib==3.1.1
    - networkx==2.4
    - numpy==1.17.2
    - opencv-python==3.3.1.11
    - pillow==6.2.1
    - protobuf==3.10.0
    - pyparsing==2.4.5
    - python-dateutil==2.8.1
    - pywavelets==1.1.1
    - scikit-image==0.15.0
    - scipy==1.3.1
    - six==1.13.0
    - torch==0.4.1
    - torchvision==0.2.1
prefix: /media/data1/hl/software/miniconda3/envs/py36_pytorch041

I created a virtual environment with your environment.yml file, I used python 3.6.0,is there anything else i need to convert the model.

If you are using pytorch version above or equal to 1.1. The conda solution wont work. In my conputer pytorch <=1.0.0 works perfectly while I update to 1.3 it gives an identical error shown above.

Have you tried other environments? Such as 1.2 ,1.4

imistyrain commented 4 years ago

torchvision 0.3-0.5 doesn't work, roll backto 0.2 Note: You can use torch==1.4 and torchvision==0.2 sametime.

leoluopy commented 4 years ago

please check your pytorch layer, confirm whether the layer you used is supported in this tool, some operation need change your writing, such as change torch.add to0 "torch.add"

such as change torch.add to0 "torch.add" ?? some details ?? change torch.add to ???? @cswwp

applefishsky009 commented 3 years ago

torchvision 0.3-0.5 doesn't work, roll backto 0.2 Note: You can use torch==1.4 and torchvision==0.2 sametime.

谢谢,这完美的解决了我的问题。

XinyingZheng commented 3 years ago

That's right, I have found some layers that are not supported. The core reason for this problem is that some layers are not registered and the blobs can not be found. You must accurately locating the unsuitable layers/operations, Unfortunately ,This work would be disgusting...

How do you determine which layer operations are not supported,looking forward to your reply,thank you

XinyingZheng commented 3 years ago

torchvision 0.3-0.5 doesn't work, roll backto 0.2 Note: You can use torch==1.4 and torchvision==0.2 sametime.

谢谢,这完美的解决了我的问题。

我torch改成1.4 ,torchvision改成0.2 ,还是报一样的错误....

imistyrain commented 3 years ago

That's right, I have found some layers that are not supported. The core reason for this problem is that some layers are not registered and the blobs can not be found. You must accurately locating the unsuitable layers/operations, Unfortunately ,This work would be disgusting...

How do you determine which layer operations are not supported,looking forward to your reply,thank you

出现这个错误的原因一般是当前层的上一层未做转换,blob未加入列表所致,调试下断点那就能找到.

imistyrain commented 3 years ago

主要是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)

Adhithya-tech commented 3 years ago

Still getting that error with torch 1.8, torchvision 0.9

Ronales commented 2 years ago

may be you should add net.eval()

example: net = resnet() net.eval()

YarrDOpanas commented 1 year ago

@Ronales 's advice worked for me. Added net.eval() and it worked. Have torch==1.8.1 and torchvision==0.9.1

zengfanyi commented 1 year ago

您的来信已收到,将会尽早做出答复,谢谢!

cc-wangxin2020 commented 3 months ago

主要是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)

解决了!!太感谢了

zengfanyi commented 3 months ago

您的来信已收到,将会尽早做出答复,谢谢!