Giter VIP home page Giter VIP logo

onnx2caffe's People

Contributors

mtlab 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

onnx2caffe's Issues

Have you tried to convert resnet50? I failed.

The test.py has been ran correctly.
I used pytorch 0.4.0 to convert the pretrained resnet50 to onnx, but when I tried to convert it to the caffe model with this tool, I failed and I am not happy.

`I030818:14:05.266976 24239 layer_factory.hpp:77] Creating layer 0

I0308 18:14:05.267001 24239 net.cpp:100] Creating Layer 0

I0308 18:14:05.267019 24239 net.cpp:408] 0 -> 0

I0308 18:14:05.267062 24239 net.cpp:150] Setting up 0

I0308 18:14:05.267076 24239 net.cpp:157] Top shape: 10 3 224 224 (1505280)

I0308 18:14:05.267081 24239 net.cpp:165] Memory required for data: 6021120

I0308 18:14:05.267096 24239 layer_factory.hpp:77] Creating layer 268

I0308 18:14:05.267110 24239 net.cpp:100] Creating Layer 268

I0308 18:14:05.267117 24239 net.cpp:434] 268 <- 0

I0308 18:14:05.267127 24239 net.cpp:408] 268 -> 268

F0308 18:14:05.395298 24239 cudnn.hpp:113] Check failed: status == CUDNN_STATUS_SUCCESS (3 vs. 0) CUDNN_STATUS_BAD_PARAM

*** Check failure stack trace: ***
Aborted`

Question about 'Use only for inference'

Hello,

What do you mean exactly by 'use only for inference'? I had a Pytorch .pth model and converted it to .onnx by performing - before conversion - the model.eval() operation. Is this what you are referring to?

KeyError: when run the demo

Hi, when running

python convertCaffe.py ./model/MobileNetV2.onnx ./model/MobileNetV2.prototxt ./model/MobileNetV2.caffemodel

it returns a KeyError.

  File "convertCaffe.py", line 108, in <module>
    convertToCaffe(graph, prototxt_path, caffemodel_path)
  File "convertCaffe.py", line 88, in convertToCaffe
    converter_fn(net, node, graph, err)
  File "/root/projects/onnx2caffe/onnx2caffe/_weightloader.py", line 44, in _convert_BatchNorm
    np.copyto(net.params[node_name + '_bn'][0].data, mean, casting='same_kind')
KeyError: u'287_bn'

And my settings are as follow:

python=2.7.14
onnx= 1.12.1
torch=0.4.0
caffe=1.1.6

Eorror in caffe.Net initialiazation

When run the project I got the prototxt but got the
Check failed: status == CUDNN_STATUS_SUCCESS (4 vs. 0) CUDNN_STATUS_INTERNAL_ERROR
in the line caffe.Net() initialization.
I create a script to write only one line for initialization and successed. Why?

Floating point exception

ONNX2caffe
run in the docker of caffe

I0119 09:22:36.791309 249 layer_factory.hpp:77] Creating layer input
I0119 09:22:36.791831 249 net.cpp:84] Creating Layer input
I0119 09:22:36.792078 249 net.cpp:380] input -> input
I0119 09:22:36.792362 249 net.cpp:122] Setting up input
I0119 09:22:36.792376 249 net.cpp:129] Top shape: 0 1 1408 512 (0)
I0119 09:22:36.792385 249 net.cpp:137] Memory required for data: 0
I0119 09:22:36.792389 249 layer_factory.hpp:77] Creating layer 23
I0119 09:22:36.792400 249 net.cpp:84] Creating Layer 23
I0119 09:22:36.792404 249 net.cpp:406] 23 <- input
I0119 09:22:36.792410 249 net.cpp:380] 23 -> 23
I0119 09:22:36.794325 249 net.cpp:122] Setting up 23
I0119 09:22:36.794389 249 net.cpp:129] Top shape: 0 32 1408 512 (0)
I0119 09:22:36.794399 249 net.cpp:137] Memory required for data: 0
I0119 09:22:36.794409 249 layer_factory.hpp:77] Creating layer 24
I0119 09:22:36.794420 249 net.cpp:84] Creating Layer 24
I0119 09:22:36.794445 249 net.cpp:406] 24 <- 23
I0119 09:22:36.794453 249 net.cpp:380] 24 -> 24
I0119 09:22:36.794474 249 net.cpp:122] Setting up 24
I0119 09:22:36.794484 249 net.cpp:129] Top shape: 0 32 1408 512 (0)
I0119 09:22:36.794493 249 net.cpp:137] Memory required for data: 0
I0119 09:22:36.794497 249 layer_factory.hpp:77] Creating layer 25
I0119 09:22:36.794533 249 net.cpp:84] Creating Layer 25
I0119 09:22:36.794847 249 net.cpp:406] 25 <- 24
I0119 09:22:36.794958 249 net.cpp:380] 25 -> 25
I0119 09:22:36.796164 249 net.cpp:122] Setting up 25
I0119 09:22:36.796231 249 net.cpp:129] Top shape: 0 32 1408 512 (0)
I0119 09:22:36.796241 249 net.cpp:137] Memory required for data: 0
I0119 09:22:36.796248 249 layer_factory.hpp:77] Creating layer 26
I0119 09:22:36.796257 249 net.cpp:84] Creating Layer 26
I0119 09:22:36.796260 249 net.cpp:406] 26 <- 25
I0119 09:22:36.796264 249 net.cpp:380] 26 -> 26
I0119 09:22:36.796273 249 net.cpp:122] Setting up 26
I0119 09:22:36.796301 249 net.cpp:129] Top shape: 0 32 1408 512 (0)
I0119 09:22:36.796308 249 net.cpp:137] Memory required for data: 0
I0119 09:22:36.796311 249 layer_factory.hpp:77] Creating layer 27_bn
I0119 09:22:36.796319 249 net.cpp:84] Creating Layer 27_bn
I0119 09:22:36.796353 249 net.cpp:406] 27_bn <- 26
I0119 09:22:36.796365 249 net.cpp:380] 27_bn -> 27
Floating point exception

Load caffe model failed.

Hi, My test code is as follows:
const String model_desc = "resnet18-pytorch2caffe.prototxt";
const String model_binary = "resnet18-pytorch2caffe.caffemodel";

int test_caffe()
{
// init model
Net net = readNetFromCaffe(model_desc, model_binary);
return 0;
}

The error message from is as follows:

OpenCV Error: Assertion failed (pbBlob.raw_data_type() == caffe::FLOAT16) in cv::dnn::experimental_dnn_v3::`anonymous-namespace'::CaffeImporter::blobFromProto, file C:\build\master_winpack-build-win64-vc15\opencv\modules\dnn\src\caffe\caffe_importer.cpp, line 251

RuntimeError: Inferred shape and existing shape differ in dimension 1: (4) vs (1)

File "/home/hdsx/anaconda3/envs/caffe/lib/python3.6/site-packages/onnx-1.7.0-py3.6-linux-x86_64.egg/onnx/shape_inference.py", line 35, in infer_shapes
inferred_model_str = C.infer_shapes(model_str, check_type)
RuntimeError: Inferred shape and existing shape differ in dimension 1: (4) vs (1)

The file is from onnx model zoo's faster r-cnn. I have used https://github.com/daquexian/onnx-simplifier to do the processing

ONNX node of type Transpose is not supported.

We are facing issues in converting model which has transpose convolution. Here are the details

Traceback (most recent call last): File "convertCaffe.py", line 108, in <module> convertToCaffe(graph, prototxt_path, caffemodel_path) File "convertCaffe.py", line 52, in convertToCaffe err.unsupported_op(node) File "/home/nithish/code_space/python/onnx2caffe/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 Transpose is not supported.

When I test like this "$:python test.py", show "TypeError: ONNX node of type Pad is not supported.

Hello:
I have an error when I test the test.py. Details:

root@524c09ef713d:/home/onnx2caffe/onnx2caffe-master# python test.py
export broadcast_mul onnx model ...
graph(%0 : Float(1, 3, 4, 4)
      %1 : Float(128, 3, 3, 3)
      %2 : Float(128)
      %3 : Float(128)
      %4 : Float(128)
      %5 : Float(128)
      %6 : Float(128)
      %7 : 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]](%0, %1, %2), scope: broadcast_mul/Sequential[conv1]/Conv2d[0]
  %9 : Float(1, 128, 4, 4) = onnx::BatchNormalization[epsilon=1e-05, momentum=1](%8, %3, %4, %5, %6), scope: broadcast_mul/Sequential[conv1]/BatchNorm2d[1]
  %10 : Float(1, 128, 4, 4) = onnx::Relu(%9), scope: broadcast_mul/Sequential[conv1]/ReLU[2]
  %11 : Tensor = onnx::Pad[mode="constant", pads=[0, 0, 0, 0, 0, 0, 0, 0], value=0](%10), scope: broadcast_mul/AvgPool2d[poo1]
  %12 : Float(1, 128, 1, 1) = onnx::AveragePool[kernel_shape=[4, 4], pads=[0, 0, 0, 0], strides=[4, 4]](%11), scope: broadcast_mul/AvgPool2d[poo1]
  %13 : Float(1, 128, 4, 4) = onnx::Mul(%10, %12), scope: broadcast_mul
  return (%13);
}

converting broadcast_mul to caffe ...
Traceback (most recent call last):
  File "test.py", line 44, in <module>
    caffe_model = convertToCaffe(graph, prototxt_path, caffemodel_path)
  File "/home/onnx2caffe/onnx2caffe-master/convertCaffe.py", line 54, in convertToCaffe
    err.unsupported_op(node)
  File "/home/onnx2caffe/onnx2caffe-master/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.**

How can I fix this error?
Thanks.

TypeError: ONNX node of type Pad is not supported.

Traceback (most recent call last):
File "convertCaffe.py", line 110, in
convertToCaffe(graph, prototxt_path, caffemodel_path)
File "convertCaffe.py", line 54, in convertToCaffe
err.unsupported_op(node)
File "E:\mishang\workspace\CE2P\onnx2caffe\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.

IndexError: tuple index out of range 报这个错误呢

Traceback (most recent call last):
File "convertCaffe.py", line 108, in
convertToCaffe(graph, prototxt_path, caffemodel_path)
File "convertCaffe.py", line 34, in convertToCaffe
graph.channel_dims[edge_name] = graph.shape_dict[edge_name][1]
IndexError: tuple index out of range

converting face-alignment model to caffe

Hello,I have a simple question. I have to use OpenCV Dnn Module for Face Alignment in my application for which i am planning to use this approach https://github.com/1adrianb/face-alignmen So I am wondering is there any way to convert these weights (models with .pth extension are separately downloaded to pc) to corresponding caffe model ? Any help or advice will be highly appreciated. Thanks in advance.

3dconv not supported?

Hi:
I have an error when convert onnx(3d cnn net):
"F0521 17:11:40.998694 94226 base_conv_layer.cpp:27] Check failed: num_spatial_axes_ == 2 (3 vs. 2) kernel_h & kernel_w can only be used for 2D convolution."
How can I fix it?
Thanks

Set ceil_mode=True cannot solve the shape error.

Hi dude, you said this in another issue:
Hi, this is caused by the difference of pooling between caffe and pytorch. Caffe's pooling use "ceil" mode while pytorch's use "floor" mode by default. When the output shapes are not integers, they behaves different. For example, the output of Pooling(2, 2)(1, 3, 129, 129) will be (1, 3, 65, 65) in caffe and (1, 3, 64, 64) in pytorch. Unfortunately, there are several such kind of pooling in pytorch's offical resnet model. You need to set ceil_mode=True in pytorch when converting this kind of pooling to caffe. Check #3 for detail.
But when I tried this ,I cannot convert the model from pytorch to onnx with torch0.4.0, it said this:
ONNX export failed: Couldn't export operator aten::avg_pool2d
Although I can do it with torch0.4.1 and torch1.0, this project cannot support the operator 'ATEN' and 'GlobalAvgPool'.
I am trying to add the 'GlobalAvgPool' to your project, or do you have a better approach?
Thanks a lot!

Error when converting

I1109 12:26:11.506861 7257 net.cpp:379] This network produces output 171
I1109 12:26:11.506953 7257 net.cpp:402] Top memory (TEST) required for data: 53798656 diff: 53798656
I1109 12:26:11.506961 7257 net.cpp:405] Bottom memory (TEST) required for data: 53796608 diff: 53796608
I1109 12:26:11.506968 7257 net.cpp:408] Shared (in-place) memory (TEST) by data: 10498560 diff: 10498560
I1109 12:26:11.506973 7257 net.cpp:411] Parameters memory (TEST) required for data: 48940960 diff: 48940960
I1109 12:26:11.506979 7257 net.cpp:414] Parameters shared memory (TEST) by data: 0 diff: 0
I1109 12:26:11.506984 7257 net.cpp:420] Network initialization done.
Traceback (most recent call last):
File "convertCaffe.py", line 119, in
convertToCaffe(graph, prototxt_path, caffemodel_path)
File "convertCaffe.py", line 92, in convertToCaffe
converter_fn(net, node, graph, err)
File "/mnt/diskc/even/Car_DR/Onnx2caffe/onnx2caffe/_weightloader.py", line 89, in _convert_gemm
net.params[node_name][0].data[...] = W
ValueError: could not broadcast input array from shape (512,512) into shape (512,2048)

Hi,
I have already exported onnx file of resnet18(last FC layer modified to 512, 512), how can i fix this problem?

MobileNetV2.onnx转换失败

F0619 15:41:18.124826 21734 cudnn_conv_layer.cpp:53] 
Check failed: status == CUDNN_STATUS_SUCCESS (4 vs. 0)  CUDNN_STATUS_INTERNAL_ERROR

import caffe error

I meet error about 'import caffe'. Please 'import caffe' before 'import onnx'.

ConvTranspose层的channel问题

在使用onnx2caffe项目转换ConvTranspose2d层到caffe的Deconvolution层时,_operators.py的代码
num_output=W.shape[1],
但我在转的onnx模型中,ConvTranspose2d层的weight的shape是[256, 1, 2, 2],模型源码的定义是
self.deconv = nn.ConvTranspose2d(c1, c1, kernel_size=k, stride=s, padding=0, output_padding=0, groups=c1, bias=False)

想请教大大佬,这个是什么原因导致onnx模型中ConvTranspose2d层weight的shape定义不一样。

pip install caffe not work

Hello
in google colab(python 3.10.12)
Traceback (most recent call last):
File "/content/ONNXToCaffe/convertCaffe.py", line 10, in
import caffe
ModuleNotFoundError: No module named 'caffe'

!pip install caffe
RROR: Could not find a version that satisfies the requirement caffe (from versions: none)
ERROR: No matching distribution found for caffe

!pip install caffe2
Collecting caffe2
Using cached caffe2-0.5.0a0.dev100.tar.gz (10.7 MB)
error: subprocess-exited-with-error

× python setup.py egg_info did not run successfully.
│ exit code: 1
╰─> See above for output.

note: This error originates from a subprocess, and is likely not a problem with pip.
Preparing metadata (setup.py) ... error
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.

Fails if bias = False

Hi, I've noticed the export fails if a Layer has zero bias.

For others having this problem, this function sets the bias to zero.

def search_set_bias(model):
    for m in model.children():
        if isinstance(m,torch.nn.Linear) or isinstance(m, torch.nn.Conv2d):
            if m.bias is None:
                m.bias = torch.nn.Parameter(torch.zeros(m.weight.size(0)))
        search_set_bias(m)

onnx2caffe segfaults

This Framework segfaults when I try to convert my onnx model to a caffemodel. Any leads on how to progress with the situation?

I tried converting a LeNet ONNX model to a protoxt and caffemodel equivalent

unsupport

ONNX node of type Pad is not supported.

KeyError: u'broadcast' when convert onnx to caffe for resnet18

the resnet18 in onnx is converted from mxnet. I want to convert this r18 in onnx to caffe. the error is listed below. Anything I can do to remove this error?

Traceback (most recent call last):
File "convertCaffe.py", line 108, in
convertToCaffe(graph, prototxt_path, caffemodel_path)
File "convertCaffe.py", line 55, in convertToCaffe
layer = converter_fn(node,graph,err)
File "/home/zhen/Downloads/onnx2caffe/onnx2caffe/_operators.py", line 255, in _convert_gemm
if node.attrs["broadcast"] != 1 or node.attrs["transB"] != 1:
KeyError: u'broadcast'

How did you copy the params?

Sorry to bother you again~ After I converted the model to caffe, I tested it and the result is so low.
So I checked the code, and there are some questions there, for example, how did you copy the params in BN layer?
The code is that:
` def _convert_BatchNorm(node,graph,err):

epsilon = node.attrs.get("epsilon", 1e-5)
scale = node.input_tensors[node.inputs[1]]
bias = node.input_tensors[node.inputs[2]]
mean = node.input_tensors[node.inputs[3]]
var = node.input_tensors[node.inputs[4]]
node_name = node.name

input_name = str(node.inputs[0])
output_name = str(node.outputs[0])

if input_name==output_name:
    inplace = True
else:
    inplace = False

bn_layer = myf("BatchNorm", node_name+"_bn",[input_name],[output_name],eps = epsilon, use_global_stats = True, in_place=inplace)
scale_layer = myf("Scale", node_name, [output_name],[output_name],in_place=True,bias_term=True)

graph.channel_dims[output_name] = graph.channel_dims[input_name]

return bn_layer,scale_layer

`

And the "myf" is:
` def init(self, type_name, layer_name, inputs,outputs, **params):

    self.type_name = type_name
    self.inputs = inputs
    self.outputs = outputs
    self.params = params
    self.layer_name = layer_name
    self.ntop = self.params.get('ntop', 1)
    # use del to make sure kwargs are not double-processed as layer params
    if 'ntop' in self.params:
        del self.params['ntop']
    self.in_place = self.params.get('in_place', False)
    if 'in_place' in self.params:
        del self.params['in_place']

`
I found that there is no any codes to copy the params, so how did you do that?

ValueError: Unsupported ONNX attribute: name: "then_branch"

Does this error mean that if?

ValueError: Unsupported ONNX attribute: name: "then_branch" g { node { input: "1910" output: "1917" name: "Squeeze_1533" op_type: "Squeeze" attribute { name: "axes" ints: -1 type: INTS } } name: "torch-jit-export1" output { name: "1917" } } type: GRAPH

shufflenet v2 model onnx to caffe failed

Traceback (most recent call last):
File "convertCaffe.py", line 112, in
convertToCaffe(graph, prototxt_path, caffemodel_path)
File "convertCaffe.py", line 58, in convertToCaffe
layer = converter_fn(node,graph,err)
File "/data00/huqingwen.huqwen/workspace/npu/caffe2pb2tflite/gesture/hand-gestures/apu/pytorch2onnx2caffe2tf/onnx2caffe/_operators.py", line 194, in _convert_Reshape
return err.unsupported_op_configuration(node, "Reshape dimention number shall be 2 or 4")
File "/data00/huqingwen.huqwen/workspace/npu/caffe2pb2tflite/gesture/hand-gestures/apu/pytorch2onnx2caffe2tf/onnx2caffe/_error_utils.py", line 46, in unsupported_op_configuration
"Error while converting op of type: {}. Error message: {}\n".format(node.op_type, err_message, )
TypeError: Error while converting op of type: Reshape. Error message: Reshape dimention number shall be 2 or 4

KeyError: u'height_scale'

Hi,thank you very much for this tool.

I want to do a conversion from pytorch to caffe.

My settings are as follow:

onnx 1.6.0(pip install onnx)
pytorch 0.4.0
protobuf 2.6.1
python 2.7.17

There is no error in running test.py

root@ubuntn:/home/merz/onnx/onnx2caffe-master# python convertCaffe.py hourglass.onnx hourglass.prototxt hourglass.caffemodel
[libprotobuf WARNING google/protobuf/io/coded_stream.cc:537] Reading dangerously large protocol message.  If the message turns out to be larger than 2147483647 bytes, parsing will be halted for security reasons.  To increase the limit (or to disable these warnings), see CodedInputStream::SetTotalBytesLimit() in google/protobuf/io/coded_stream.h.
[libprotobuf WARNING google/protobuf/io/coded_stream.cc:78] The total number of bytes read was 765562426
Traceback (most recent call last):
  File "convertCaffe.py", line 108, in <module>
    convertToCaffe(graph, prototxt_path, caffemodel_path)
  File "convertCaffe.py", line 55, in convertToCaffe
    layer = converter_fn(node,graph,err)
  File "/home/merz/onnx/onnx2caffe-master/onnx2caffe/_operators.py", line 277, in _convert_upsample
    factor = int(node.attrs["height_scale"])
KeyError: u'height_scale'

What should I do with this error?
Please guide me.

doesn't support Dilated Conv?

My encoder network need 2 streams:

  • one use common conv to get shallow features;
  • the other one use dilated conv to get deep features with high-resolution as shallow features.

However,

  • shallow one works fine get 1/8 features (60, 80)
  • deep one works wrong get 1/8 features (61, 81)

Are there any solutions?

I'll be very appreciated.

Thanks, 美图.

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.