longcw / pytorch2caffe Goto Github PK
View Code? Open in Web Editor NEWConvert PyTorch model to Caffemodel
Convert PyTorch model to Caffemodel
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
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
Hi, all
It seems that the newest code does not support pytorch 0.4
. So is there any plan for that?
I wonder if it's possible to convert a pth model file into caffemodel file. how shall i do??
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
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
RT
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
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?
Segnet neutral network has nn.MaxUnpool2d layers which is uesd to upsample.Does one model with
nn.MaxUnpool2d layers in pytorch can be transformed to caffe model?
Thank you
.....pytorch2caffe.py", line 382, in plot_graph (graph,) = pydot.graph_from_dot_file(fname) TypeError: 'Dot' object is not iterable
I met error when trying to transfer SSD, it says runtime error.
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'
}
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。
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代码的头文件,加载类。
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时出错
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?
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
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?
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 !!!
"dot" not found in path,whats the problem?
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
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.
[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'
hi, I just run your code, appeared this problem. what is wrong with the code?
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?
when I run "from pytorch2caffe import pytorch2caffe", I always got error: Segmentation fault (core dumped). Anyone know about why? Thanks!
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!
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?
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.
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.
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?
Could you please add nn.ConvTranspose2d into the transformation? Because the ConvTranspose2d operation is now very common in neural networks.
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.
running_mean = func.running_mean
AttributeError: 'CudnnBatchNormBackward' object has no attribute 'running_mean'
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.