GithubHelp home page GithubHelp logo

xxradon / onnxtocaffe Goto Github PK

View Code? Open in Web Editor NEW
152.0 4.0 31.0 12.35 MB

pytorch -> onnx -> caffe, pytorch to caffe, or other deep learning framework to onnx and onnx to caffe.

Python 100.00%
pytorch caffe onnx

onnxtocaffe's Introduction

Code mainly come from https://github.com/MTlab/onnx2caffe https://github.com/seanxcwang/onnx2caffe and https://github.com/205418367/onnx2caffe,thanks for their contribution.

onnx to Caffe

we can convert onnx operations to caffe layer which not only from https://github.com/BVLC/caffe but also from many other caffe modified branch like ssd-caffe,and only onnx opset_version=9 is supported.

  1. Convert pytorch to Caffe by ONNX

This tool converts pytorch model to Caffe model by ONNX
only use for inference

  1. Convert tensorflow to Caffe by ONNX

you can use this repo https://github.com/onnx/tensorflow-onnx.

  1. other deeplearning frame work to caffe bt ONNX

Dependencies

  • caffe (with python support)
  • pytorch (optional if you want to convert onnx)
  • onnx
  • onnxruntime

we recomand using protobuf 2.6.1 and install onnx from source

git clone --recursive https://github.com/onnx/onnx.git
cd onnx 
python setup.py install

or just using pip

pip install onnx

How to use

  1. To convert onnx model to caffe:
python convertCaffe.py ./model/MobileNetV2.onnx ./model/MobileNetV2.prototxt ./model/MobileNetV2.caffemodel

pytorch to onnx Tips

  1. you can refer model_generator folder to learn how to generate onnx from pytorch,or just learn from pytorch.org.
  2. in pytorch,speeding up model with fusing batch normalization and convolution,so before convert pytorch pth model to onnx fusing fusing batch normalization and convolution is a good choice.you may refer this https://learnml.today/speeding-up-model-with-fusing-batch-normalization-and-convolution-3.
  3. Sometimes you need to use onnx-simplifier to simplify onnx model and then run convertCaffe.py to convert it into caffe model.

Current support operation

  • Conv
  • Relu
  • LeakyRelu
  • PRelu
  • Transpose
  • ReduceMean
  • MatMul
  • BatchNormalization
  • Add
  • Mul
  • Add
  • Reshape
  • MaxPool
  • AveragePool
  • GlobalAveragePool
  • Dropout
  • Gemm (InnerProduct only)
  • Upsample (nearest and bilinear all supported)
  • Concat
  • ConvTranspose
  • Sigmoid
  • Flatten
  • Sqrt
  • Softmax
  • Unsqueeze
  • Slice

onnxtocaffe's People

Contributors

jacinhu avatar mtlab avatar quinnrong94 avatar seanxcwang avatar shaundai-tencent avatar xxradon avatar yukinagato 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

onnxtocaffe's Issues

TypeError: ONNX node of type Pad is not supported

when run python test.py got this error
with python3.6
pytorch==1.4
onnx==1.8.0

full log:

export broadcast_mul onnx model ...
graph(%input.1 : Float(1, 3, 4, 4),
      %conv1.0.weight : Float(128, 3, 3, 3),
      %conv1.0.bias : Float(128),
      %conv1.1.weight : Float(128),
      %conv1.1.bias : Float(128),
      %conv1.1.running_mean : Float(128),
      %conv1.1.running_var : Float(128),
      %conv1.1.num_batches_tracked : Long()):
  %8 : Float(1, 128, 4, 4) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[3, 3], pads=[1, 1, 1, 1], strides=[1, 1]](%input.1, %conv1.0.weight, %conv1.0.bias) # D:\Anaconda3\lib\site-packages\torch\nn\modules\conv.py:342:0
  %9 : Float(1, 128, 4, 4) = onnx::BatchNormalization[epsilon=1e-05, momentum=0.9](%8, %conv1.1.weight, %conv1.1.bias, %conv1.1.running_mean, %conv1.1.running_var) # D:\Anaconda3\lib\site-packages\torch\nn\functional.py:1670:0
  %10 : Float(1, 128, 4, 4) = onnx::Relu(%9) # D:\Anaconda3\lib\site-packages\torch\nn\functional.py:912:0
  %11 : Tensor = onnx::Pad[mode="constant", pads=[0, 0, 0, 0, 0, 0, 0, 0], value=0](%10)
  %12 : Float(1, 128, 1, 1) = onnx::AveragePool[kernel_shape=[4, 4], pads=[0, 0, 0, 0], strides=[4, 4]](%11) # D:\Anaconda3\lib\site-packages\torch\nn\modules\pooling.py:554:0
  %13 : Float(1, 128, 4, 4) = onnx::Mul(%10, %12) # f:\OpenGit\ONNXToCaffe\model_generator\broadcast_mul.py:23:0
  return (%13)

converting broadcast_mul to caffe ...
8 Conv
9 BatchNormalization
10 Relu
11 Pad
  File "ONNXToCaffe\test.py", line 44, in <module>
    caffe_model = convertToCaffe(graph[0], graph[1], prototxt_path, caffemodel_path)
  File "ONNXToCaffe\convertCaffe.py", line 64, in convertToCaffe
    err.unsupported_op(node)
  File "ONNXToCaffe\onnx2caffe\_error_utils.py", line 36, in unsupported_op
    "ONNX node of type {} is not supported.\n".format(node.op_type,)
TypeError: ONNX node of type Pad is not supported.

转换caffe报错Check failed: bottom[i]->shape() == bottom[0]->shape()

在将pytorch的DDRNet转换为caffe的时候报了如下错误:

F0222 06:37:17.979665 24253 eltwise_layer.cpp:34] Check failed: bottom[i]->shape() == bottom[0]->shape()

输入大小为[1,3,512,1024]应该是在upsample层出现了问题,其中有一个upsample是bilinear的 height_scale=8, width_scale=16;看到在 _operators.py _convert_upsample 将 height_scale=width_scale=2;我将这里注释掉了,并且在下面改为Deconv中的stride_w也该成了width_scale; 但依旧报错,希望可以指导一下

环境问题

onnxruntime需要python3,而protobuf2.6.1不支持python3。请问是怎么解决的呢?

covert opset v11

Does anybody knows how to convert opset v11 to caffe ?

error info:
"ONNX opset version {} is not supported,only opset 9 is supported,you can export onnx by setting opset_version like torch.onnx.export(model, '', opset_version=9, verbose=True).\n".format(version )
TypeError: ONNX opset version 11 is not supported,only opset 9 is supported,you can export onnx by setting opset_version like torch.onnx.export(model, '', opset_version=9, verbose=True).

can torch.split be support

Hi, thank for your excellent work,
but there are some err when convert model, i think it's because I use torch.split, can you give me some advice about this err.

err log like below:

GlobalAveragePool_46 GlobalAveragePool
Conv_47 Conv
Shape_48 Shape
Traceback (most recent call last):
  File "convertCaffe.py", line 122, in <module>
    convertToCaffe(graph, opset_version, prototxt_path, caffemodel_path)
  File "convertCaffe.py", line 64, in convertToCaffe
    err.unsupported_op(node)
  File "/home/liaobaoxin.lbx/ONNXToCaffe/onnx2caffe/_error_utils.py", line 36, in unsupported_op
    "ONNX node of type {} is not supported.\n".format(node.op_type,)
TypeError: ONNX node of type Shape is not supported.

look forward to your reply

AttributeError: permute_param

大佬这个问题咋解决,参考的解决办法是git caffe_plus
cp caffe_plus/include/caffe/layers/upsample_layer.hpp caffe/include/caffe/layers/
但ubuntu装的caffe1.0 没有caffe/include/caffe/layers/这个路径

Slice not Supported

Hi xxradon
I find Slice is supported in readme.
My network is created by using slice to split [C x H x W] -> [0~C/2 x H x W ] and [C/2 x H x W] :
x1, x2 = x[:,0:split_point,:,:], x[:,split_point:2*split_point,:,:]
1. Can this work for onnx2caffe ? What is the standard way to do slice on Channel?
2. When I convert network onnx to caffe, TypeError :

         Traceback (most recent call last):
      File "convertCaffe.py", line 131, in <module>
        convertToCaffe(graph, opset_version, prototxt_path, caffemodel_path)
      File "convertCaffe.py", line 105, in convertToCaffe
        err.unsupported_op(node)
      File "/mnt/d/07SubSystem/onnx2caffe/ONNXToCaffe_xxradon/onnx2caffe/_error_utils.py", line 36, in unsupported_op
        "ONNX node of type {} is not supported.\n".format(node.op_type,)
    TypeError: ONNX node of type Slice is not supported.

  Can you help me to make slice work?

嗨 !
目的: 我在使用slice操作,将tensor安装channel维分割为2的sub branch的时候,遇到了转换不支持的问题。
使用:在torch模型构建中的slice操作语法是这样使用的: x1, x2 = x[:,0:split_point,:,:], x[:,split_point:2*split_point,:,:]
错误:转换后可以生产prototxt,但是生产model失败,log显示不支持slice:
Traceback (most recent call last):
File "convertCaffe.py", line 131, in
convertToCaffe(graph, opset_version, prototxt_path, caffemodel_path)
File "convertCaffe.py", line 105, in convertToCaffe
err.unsupported_op(node)
File "/mnt/d/07SubSystem/onnx2caffe/ONNXToCaffe_xxradon/onnx2caffe/_error_utils.py", line 36, in unsupported_op
"ONNX node of type {} is not supported.\n".format(node.op_type,)
TypeError: ONNX node of type Slice is not supported.
请求:请问是否有slice操作的推荐使用方法?

AttributeError: upsample_param

博主您好,我在转换Mobilenet+fpn时都遇到了这样的错误
Traceback (most recent call last):
File "convertCaffe.py", line 122, in
convertToCaffe(graph, opset_version, prototxt_path, caffemodel_path)
File "convertCaffe.py", line 79, in convertToCaffe
layers[id] = layer._to_proto()
File "/home/dongly/ONNXToCaffe-master/MyCaffe.py", line 100, in _to_proto
assign_proto(layer, k, v)
File "/home/dongly/ONNXToCaffe-master/MyCaffe.py", line 29, in assign_proto
is_repeated_field = hasattr(getattr(proto, name), 'extend')
AttributeError: upsample_param
请求博主指导,非常感谢。

ONNX node of type Resize is not supported

@xxradon
Thank You First!
I convert YOLOv3 from mmdetection , pytorch model => onnx model => caffemodel;
I convert pytorcch model to onnx successfully, but got an error when convert onnx model to caffe model,
waht should i do?
Thank you ! looking forward to your reply!

image

Gather层的实现

您好,请问您有遇到过gather层吗?这个层能用caffe哪些层去拼凑实现呢

Clip的支持

你好,我尝试着把pytorch官方model zoo里面的mobilenetv2转换成onnx然后转换成caffe,然后发现onnx版本里面batchnorm都会带上clip操作,求问可以添加对clip的支持吗?

转换upsample层时遇到了问题

我尝试将pytorch版yolov3模型通过onnx转换为caffe模型。在转换upsample层时程序报错:
Traceback (most recent call last):
File "convertCaffe.py", line 122, in
convertToCaffe(graph, opset_version, prototxt_path, caffemodel_path)
File "convertCaffe.py", line 67, in convertToCaffe
layer = converter_fn(node,graph,err)
File "/root/ONNXToCaffe-master/onnx2caffe/_operators.py", line 362, in _convert_upsample
factor = int(node.attrs["height_scale"])
KeyError: 'height_scale'

upsample层参数如图所示:
image
于是我将_operators.py中第362行与370行注释掉来尝试解决,结果又出现如下错误:
Traceback (most recent call last):
File "convertCaffe.py", line 122, in
convertToCaffe(graph, opset_version, prototxt_path, caffemodel_path)
File "convertCaffe.py", line 79, in convertToCaffe
layers[id] = layer._to_proto()
File "/root/ONNXToCaffe-master/MyCaffe.py", line 100, in _to_proto
assign_proto(layer, k, v)
File "/root/ONNXToCaffe-master/MyCaffe.py", line 29, in assign_proto
is_repeated_field = hasattr(getattr(proto, name), 'extend')
AttributeError: upsample_param

python3.7.9 onnx1.8.0
求教该怎么解决!谢谢各位大佬!

Upsample_param bug

Traceback (most recent call last):
File "/content/ONNXToCaffe/convertCaffe.py", line 122, in
convertToCaffe(graph, opset_version, prototxt_path, caffemodel_path)
File "/content/ONNXToCaffe/convertCaffe.py", line 79, in convertToCaffe
layers[id] = layer._to_proto()
File "/content/ONNXToCaffe/MyCaffe.py", line 100, in _to_proto
assign_proto(layer, k, v)
File "/content/ONNXToCaffe/MyCaffe.py", line 29, in assign_proto
is_repeated_field = hasattr(getattr(proto, name), 'extend')
AttributeError: upsample_param

Shape not aligned

Traceback (most recent call last):
File "convertCaffe.py", line 123, in
compareOnnxAndCaffe(onnx_path,prototxt_path,caffemodel_path)
File "/home/dldev/ONNXToCaffe/modelComparator.py", line 141, in compareOnnxAndCaffe
check_results(net_results, onnx_info, caffe_info)
File "/home/dldev/ONNXToCaffe/modelComparator.py", line 95, in check_results
dot_result = np.dot(result.flatten(), caffe_results[i].flatten())
File "<array_function internals>", line 5, in dot
ValueError: shapes (4096000,) and (4029045,) not aligned: 4096000 (dim 0) != 4029045 (dim 0)

requirement.txt ?

Can you please provide a requirement.txt file with exact versions of the dependencies? Thank you

_weightloader::_conver_upsample Bug On Python3 : wrong filter weights for deconv

Hi xxrandon
I find wrong output when converting Upsample/Resize(bilinear) to Deconv_layer running on Python3.
I checked code in Caffe :
int f = ceil(blob->width() / 2.);
float c = (2 * f - 1 - f % 2) / (2. * f);
for (int i = 0; i < blob->count(); ++i) {
float x = i % blob->width();
float y = (i / blob->width()) % blob->height(); // y original type is int and then converted to float
data[i] = (1 - fabs(x / f - c)) * (1 - fabs(y / f - c));
}

 So add floor() and float() to compute y will be save for python3 :
 def bilinear_weight(shape):
        print ('bilinear_weight input : ', shape)
        weight = np.zeros(np.prod(shape), dtype='float32')
        f = np.ceil(shape[3] / 2.)
        c = (2 * f - 1 - f % 2) / (2. * f)
        for i in range(np.prod(shape)):
            x = i % shape[3]
            y = float(np.floor((i / shape[3]) % shape[2]))#y = (i / shape[3]) % shape[2]
            weight[i] = (1 - abs(x / f - c)) * (1 - abs(y / f - c))
        return weight.reshape(shape)

The output (bilinear) between Interploate in Torch and Deconv in Caffe only diffs in tensors' edge. I think this due to the zero padding in Deconv.

Question: Could you provide the torch and onnx version tested?

Thanks for your delighted code. This repo helps me a lot.
However, I found that the Upsample and MaxPooling's conversion with torch 1.8.2 and onnx 1.5.0 leads to abnormal cosine similarity.
My guess is that the torch version I used was too high. So I would like to ask if the author can provide torch and ONNX version numbers for your test environment?

Unsqueeze层支持

看到Readme.md上说Unsqueeze是支持的,实际查看代码时,发现并未支持,请问你后来支持了吗?能否更新一下代码,我学习一下,谢谢啦

ONNX node of type Shape is not supported! Shape_167 Shape

Conv_165 Conv
BatchNormalization_166 BatchNormalization
Shape_167 Shape
Traceback (most recent call last):
File "convertCaffe.py", line 122, in
convertToCaffe(graph, opset_version, prototxt_path, caffemodel_path)
File "convertCaffe.py", line 64, in convertToCaffe
err.unsupported_op(node)
File "/home/wyw/ONNXToCaffe/onnx2caffe/_error_utils.py", line 36, in unsupported_op
"ONNX node of type {} is not supported.\n".format(node.op_type,)
TypeError: ONNX node of type Shape is not supported.

onnxsim简化后
I0323 10:09:59.523739 1970 net.cpp:84] Creating Layer Mul_56
I0323 10:09:59.523746 1970 net.cpp:406] Mul_56 <- 430_Conv_47_0_split_1
I0323 10:09:59.523754 1970 net.cpp:406] Mul_56 <- 438
I0323 10:09:59.523763 1970 net.cpp:380] Mul_56 -> 439
F0323 10:09:59.523782 1970 scale_layer.cpp:92] Check failed: bottom[0]->shape(axis_ + i) == scale->shape(i) (56 vs. 1) dimension mismatch between bottom[0]->shape(2) and scale->shape(2)
*** Check failure stack trace: ***
Aborted

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.