Giter VIP home page Giter VIP logo

pytorch2caffe's Introduction

PyTorch2Caffe

Require Pytorch < 0.4

Ported from pytorch-caffe-darknet-convert.

Add support for

  • Dilated Convolution Layer
  • Concat Layer
  • Upsampling (converted to Deconvolution with bilinear initialization)
  • Eltwise Product
  • Sigmoid Layer
# We can obtain almost the same output from caffe except Upsampling
# for inception_v3: 
# diff between pytorch and caffe: min: 0.0, max: 1.76429748535e-05, mean: 2.14079022953e-06
# see more in demo.py

import torch
from torch.autograd import Variable
import torchvision

import os
from pytorch2caffe import pytorch2caffe, plot_graph

m = torchvision.models.inception_v3(pretrained=True, transform_input=False)
m.eval()
print(m)

input_var = Variable(torch.rand(1, 3, 299, 299))
output_var = m(input_var)

output_dir = 'demo'
# plot graph to png
plot_graph(output_var, os.path.join(output_dir, 'inception_v3.dot'))

pytorch2caffe(input_var, output_var, 
              os.path.join(output_dir, 'inception_v3-pytorch2caffe.prototxt'),
              os.path.join(output_dir, 'inception_v3-pytorch2caffe.caffemodel'))

pytorch2caffe's People

Contributors

longcw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pytorch2caffe's Issues

builtins.KeyError: 'NoneType'

File "/home/dyt/ssd.pytorch/pytorch2caffe/pytorch2caffe.py", line 165, in add_layer
layer['type'] = layer_dict[parent_type]

builtins.KeyError: 'NoneType'

I want convert ssd net to caffe ,this error how can I process?

undefined symbol: _ZN3MPI8Datatype4FreeEv

from torch._C import *
ImportError: /root/anaconda3/envs/test_py2/lib/python2.7/site-packages/torch/_C.so: undefined symbol: _ZN3MPI8Datatype4FreeEv

python demo.py occur a error.

运行demo.py时出错

'Dot' object is not iterable

I run "demo.py" ,and got the error message as follows:
Traceback (most recent call last):
File "/home/software/pycharm/helpers/pydev/pydevd.py", line 1580, in
globals = debugger.run(setup['file'], None, None, is_module)
File "/home/software/pycharm/helpers/pydev/pydevd.py", line 964, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "/home/RNN/crnn.pytorch-master_demo/demo_caffe.py", line 50, in
plot_graph(output_var, os.path.join(caffemodel_dir, 'pytorch_graph.dot'))
File "/home/RNN/crnn.pytorch-master_demo/pytorch2caffe.py", line 380, in plot_graph
(graph,) = pydot.graph_from_dot_file(fname)
TypeError: 'Dot' object is not iterable

AttributeError: tuple object has no attribute 'grad_fn'

I want to convert pytorch model(hopenet) to caffemodel, However, met the following error:
AttributeError: 'tuple' object has no attribute 'grad_fn'
model want to convert:
pytorch: 0.2.0
torchvision:0.1.8

And hopenet's forward function will return more than one tensor:
return yaw, pitch, roll # all are 1*66's tensor
Might need to feed more than one tensor to output_var, but tuple has no attribute 'grad_fn'.
I try to edit the return value, just return one value, e.g. yaw, it can works, but has errors with size mismatching.

Dose anyone have similar error? Suggesstions needed. Thank you so much.

training error

fcos_core.trainer INFO: Start training
2019-10-21 09:19:59,224 fcos_core.trainer INFO: Total training time: 0:00:00.644866 (0.0000 s / it)
just it ,dont have any other error information。

ExpandBackward

layer_dict = {'ConvNdBackward': 'Convolution',
'ThresholdBackward': 'ReLU',
'MaxPool2dBackward': 'Pooling',
'AvgPool2dBackward': 'Pooling',
'DropoutBackward': 'Dropout',
'AddmmBackward': 'InnerProduct',
'BatchNormBackward': 'BatchNorm',
'AddBackward': 'Eltwise',
'ViewBackward': 'Reshape',
'ConcatBackward': 'Concat',
'UpsamplingNearest2d': 'Deconvolution',
'UpsamplingBilinear2d': 'Deconvolution',
'SigmoidBackward': 'Sigmoid',
'LeakyReLUBackward': 'ReLU',
'NegateBackward': 'Power',
'MulBackward': 'Eltwise',
'SpatialCrossMapLRNFunc': 'LRN',

          'ExpandBackward': 'Dropout',
          'ThnnConv2DBackward': 'Dropout',
          'ThnnBatchNormBackward': 'Dropout',
          'ThMulBackward': 'Dropout',
          'ConstantPadNdBackward': 'Dropout',
          'SliceBackward': 'Dropout',
          'CloneBackward': 'Dropout',
          'CatBackward': 'Dropout',
          'AdaptiveAvgPool2DBackward': 'Dropout',
          'ThAddBackward': 'Dropout',
          'SqueezeBackward1': 'Dropout',
          'TBackward': 'Dropout',
          'ThAddmmBackward': 'Dropout'
          }

ResNet size issue -> not accepting 224

Hi,
I want to convert Resnet50 from Pytorch to caffe, but can not do it with original size of 224*224.
Have to use smaller size. Do you know what the issue is?

Thanks

CudnnBatchNormBackward

running_mean = func.running_mean
AttributeError: 'CudnnBatchNormBackward' object has no attribute 'running_mean'

why the special layer of the converted caffe model always output 0 array

I converted pytorch model to caffe model.
The pytorch model code:
**flow1 = self.flow_estimator(input1,input2)self.alph
flow2 = self.flow_estimator(input2,input1)self.belta
cat1 = self.context_extractor(input1,flow1)

In self.context_extractor(),i use flow_warp operation between **"input1" and "flow1",**why "cat1"of caffe is always zero array,i don't know why,can anybody tell me that,i confused me for several days.

Thank you for advanced!

pytorch2caffe doesn't work for the last two layyers's convertion

Hi @longcw , thanks for your extraordinary work for the tool: pytorch2caffe at first.

And I used it to convert svhn\ hopenet and other pytorch model successfully. However, when converting HeadPose, one of my own network for predicting roll,pitch, yaw degree of human face, the last two layers(220: scale+bn, 221:relu,), it didn't write the weights into .caffemodel like .prototxt describes. cannot get blobs data using: net.blobs[output_name].data after net.forward()
As I debug the python code for using caffemodel, it shows no layer 220 and 221 in headpose net created by .prototxt and .caffemodel we got by convertion.
Do you have any advice? Thanks

Conversion doesn't work for DenseNet architectures

Hi @longcw,

I wanted to use the tool for other networks and I found the following problem when I tried to use one of the DenseNet architectures from Pytorch. I wanted to convert densenet121 model by changing
m = torchvision.models.inception_v3(pretrained=True, transform_input=False) to m = torchvision.models.densenet121() in your example from pytorch2caffe.py script. Until classification layer the conversion seems to work, but then and got the following error (just copy-pasting bottom part of the output log):

...
converting AvgPool2dBackward483
converting ViewBackward484
converting AddmmBackward484
((1000L,), (1000L, 1024L))
(1000,)
(1000, 4096)
Traceback (most recent call last):
  File "pytorch2caffe.py", line 398, in <module>
    os.path.join(output_dir, 'densenet121-pytorch2caffe.caffemodel'))
  File "pytorch2caffe.py", line 95, in pytorch2caffe
    convert_layer(output_var.grad_fn)
  File "pytorch2caffe.py", line 91, in convert_layer
    save_fc2caffe(weights, biases, params[parent_name])
  File "pytorch2caffe.py", line 111, in save_fc2caffe
    fc_param[0].data[...] = weights.numpy()
ValueError: could not broadcast input array from shape (1000,1024) into shape (1000,4096)

Somehow the 2nd dimension of the array to paste in weights is 4x larger than its substitute (this is also true for other DenseNet architectures tested - 161, 169 and 201). Any ideas on what goes wrong?

Thanks in advance for any help.

Installation requirements

Hi, I am thinking to use this to convert resnet-18/34 models to caffe. Does your port need complicated installation from source or can I just pip install the libraries needed in your demo file?

operator of CudnnConvolutionBackward(ThnnConv2DBackward), ThresholdBackward1 Not found

Hi, @longcw, thanks for your work. Recently, I need to transform a pytorch model(RFBNet) which has two sibling output to caffe's format. When I used your pytorch2caffe function, it shown that CudnnConvolutionBackward not in dict of layer_dict, when I set torch.backends.cudnn.enabled==False, it shown that ThnnConv2DBackward not in dict of layer_dict. Additionly, MulBackward0,AddBackward1 as the same. How should I parse the
operator of CudnnConvolutionBackward(ThnnConv2DBackward), ThresholdBackward1, ThresholdBackward0, MulBackward0, AddBackward1?
The version of torch I used is 0.4.0

convert KeyError: 'MkldnnConvolutionBackward'

[root@greenet tools]# python torch2caffe.py
Traceback (most recent call last):
File "torch2caffe.py", line 29, in
pytorch2caffe(input_var, output_var, './tc-pytorch2caffe.prototxt', './tc-pytorch2caffe.caffemodel')
File "/home/yyh/tools/pytorch2caffe/pytorch2caffe.py", line 37, in pytorch2caffe
net_info = pytorch2prototxt(input_var, output_var)
File "/home/yyh/tools/pytorch2caffe/pytorch2caffe.py", line 325, in pytorch2prototxt
add_layer(output_var.grad_fn)
File "/home/yyh/tools/pytorch2caffe/pytorch2caffe.py", line 153, in add_layer
top_name = add_layer(u[0])
File "/home/yyh/tools/pytorch2caffe/pytorch2caffe.py", line 153, in add_layer
top_name = add_layer(u[0])
File "/home/yyh/tools/pytorch2caffe/pytorch2caffe.py", line 153, in add_layer
top_name = add_layer(u[0])
[Previous line repeated 58 more times]
File "/home/yyh/tools/pytorch2caffe/pytorch2caffe.py", line 165, in add_layer
layer['type'] = layer_dict[parent_type]
KeyError: 'MkldnnConvolutionBackward'

'Dot' object is not iterable

.....pytorch2caffe.py", line 382, in plot_graph (graph,) = pydot.graph_from_dot_file(fname) TypeError: 'Dot' object is not iterable

Add ConvTranspose2d

Could you please add nn.ConvTranspose2d into the transformation? Because the ConvTranspose2d operation is now very common in neural networks.

'MaxPool2DBackward' object has no attribute 'stride'

Some trouble happen when i use the tool for squeezenet.

First , it looks like names of some layers_grad had been changed,:
MaxPool2dBackward=>MaxPool2DBackward

Then, after added the new "MaxPool2DBackward",
it caused another error,seems like:
"'MaxPool2DBackward' object has no attribute 'stride" or "'MaxPool2DBackward' object has no attribute 'kernel_size"

corresspoding to the code:
pooling_param['kernel_size'] = func.kernel_size[0]
pooling_param['stride'] = func.stride[0]

Does anyone could help?

IndexBackward error

KeyError: 'IndexBackward'

How could we fix this error?

File "/home/zq/PROJECTS/projfog/pytorch2caffe/pytorch2caffe.py", line 166, in add_layer 
layer['type'] = layer_dict[parent_type]
KeyError: 'IndexBackward'
[1]    27580 segmentation fault (core dumped)  python resnetdemo.py 

bias=False in nn.Linear error

I've traced down this error:

in add_layer
    inner_product_param['num_output'] = func.next_functions[0][0].variable.size(0)
AttributeError: 'NoneType' object has no attribute 'variable'

to the use of bias=False for a nn.Linear layer in Pytorch. Note that this parameter for nn.Conv2d works fine. Any idea for a quick fix or do I have to do surgery on my Pytorch pretrained model and add some zero biases?

keyError:'IndexBackward'

When i convert my model from pytorch to caffe,it meet with the following error:
keyError:'IndexBackward'
What's wrong with it ,anybody can tell me?which layer's problem?

pytorch 0.4 support

Hi, all
It seems that the newest code does not support pytorch 0.4. So is there any plan for that?

"func.running_mean" doesn't equal to the corrsponding "base.bn1.running_mean" in resnet?

hi, I have tranformed a caffe model from a pytorch model, after transformation, I compared all the correspoding parameters between caffe and pytorch. Conv layer's(weights, bias) and BN layer's scale parameters are all right, but the running_mean, running_var in pytorch are not corresponding to base.bn1.running_mean, base.bn1.running_var, may I ask do you know why, thank you @longcw !!!

torch.autograd.function.PReLUBackward's attribution

I convert pytorch model to caffe,now for "PReLUBackward" layer,I need yo get the "a" value of the following equation:
PReLU(x) = max(0,x) + a * min(0,x)\

so,what is the key?
i using the following method to get the "a" value:
output.grad_fn.?

what is "?" should be?

"TypeError: argument 0 is not a Variable", torch version:0.2.0

torch version:0.2.0
since I met the error "convert KeyError: 'MkldnnConvolutionBackward' " , I reinstall my torch to version 0.2.0. But I get another error as below:

output_var = model(input_var)
File "/usr/local/python3.7.5/lib/python3.7/site-packages/torch/nn/modules/module.py", line 224, in call
result = self.forward(*input, **kwargs)
File "/usr/local/python3.7.5/lib/python3.7/site-packages/torchvision/models/resnet.py", line 139, in forward
x = self.conv1(x)
File "/usr/local/python3.7.5/lib/python3.7/site-packages/torch/nn/modules/module.py", line 224, in call
result = self.forward(*input, **kwargs)
File "/usr/local/python3.7.5/lib/python3.7/site-packages/torch/nn/modules/conv.py", line 254, in forward
self.padding, self.dilation, self.groups)
File "/usr/local/python3.7.5/lib/python3.7/site-packages/torch/nn/functional.py", line 52, in conv2d
return f(input, weight, bias)
TypeError: argument 0 is not a Variable

which version of pytorch and torchvision should be used? advice needed

which version of pytorch and torchvision should be used? advice needed.
I tried torch 1.0.0 and 0.3.1
torchvision: 0.2.2, 0.2.0
errors occured: keyError: 'data'
net.blobs['data'].data[...] = input_data

dose anyone have advice?
And I have own model trained by torch 0.3.1, torchvision 0.2.0
want to convert to caffemodel for other engine to accelerate.

KeyError: 'ExpandBackward'

I tried "python pytorch2caffe" and I've got below error message.

Traceback (most recent call last):
File "pytorch2caffe.py", line 408, in
os.path.join(output_dir, 'inception_v3-pytorch2caffe.caffemodel'))
File "pytorch2caffe.py", line 37, in pytorch2caffe
net_info = pytorch2prototxt(input_var, output_var)
File "pytorch2caffe.py", line 328, in pytorch2prototxt
add_layer(output_var.grad_fn)
File "pytorch2caffe.py", line 154, in add_layer
top_name = add_layer(u[0])
File "pytorch2caffe.py", line 168, in add_layer
layer['type'] = layer_dict[parent_type]
KeyError: 'ExpandBackward'

My pytorch version is the latest one, http://download.pytorch.org/whl/cu80/torch-0.3.0.post4-cp27-cp27mu-linux_x86_64.whl

layer_dict的对应关系

layer_dict = {'ConvNdBackward': 'Convolution',
              'ThresholdBackward': 'ReLU',
              'MaxPool2dBackward': 'Pooling',
              'AvgPool2dBackward': 'Pooling',
              'DropoutBackward': 'Dropout',
              'AddmmBackward': 'InnerProduct',
              'BatchNormBackward': 'BatchNorm',
              'AddBackward': 'Eltwise',
              'ViewBackward': 'Reshape',
              'ConcatBackward': 'Concat',
              'UpsamplingNearest2d': 'Deconvolution',
              'UpsamplingBilinear2d': 'Deconvolution',
              'SigmoidBackward': 'Sigmoid',
              'LeakyReLUBackward': 'ReLU',
              'NegateBackward': 'Power',
              'MulBackward': 'Eltwise',
              'SpatialCrossMapLRNFunc': 'LRN'}

我想知道 layer_dict的对应关系是如何确定的?

我看了pytorch的代码torch/nn/_functions/thnn/auto.py会读取C代码的头文件,加载类。

How to feed a tuple of inputs?

For pytorch2caffe.py, I was wondering whether I can input a tuple of inputs to the input_var? Would it be as simple as commenting this line, because I cannot find where the input_dim is being used again.

If what I am thinking is wrong, how do I feel multiple inputs to the model from pytorch2caffe(input_var, output_var, 'somemodel-pytorch2caffe.prototxt', 'somemodel-pytorch2caffe.caffemodel')?

I am trying to use this for Faster-RCNN.

Thanks in advance.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.