Code Monkey home page Code Monkey logo

onnx-keras's People

Contributors

leodestiny avatar xdyllx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

xdyllx

onnx-keras's Issues

ONNX Error: Unrecognized attribute: consumed_inputs for model with BatchNormalization

Hello!
I just found that following code raises ONNX error

import sys
PATH_TO_ONNX_KERAS = 'path_to_this_repo'
sys.path.append(PATH_TO_ONNX_KERAS)
import frontend as onnxkeras
import keras
import onnx
import tensorflow as tf

if __name__ == '__main__':
    print(tf.__version__)
    print(keras.__version__)
    kerasModel = keras.models.Sequential([
        keras.layers.Dense(10, input_shape=(10,)),
        keras.layers.BatchNormalization()
    ])
    onnxModel = onnxkeras.keras_model_to_onnx_model(kerasModel)
    onnxkeras.save(onnxModel, 'tmp.proto')

Output of this example with my env.

/usr/local/lib/python3.6/dist-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
Using TensorFlow backend.
1.7.0
2.1.5
[1, 10]
2018-06-18 14:40:26.198448: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2018-06-18 14:40:26.287828: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:898] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2018-06-18 14:40:26.288054: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1344] Found device 0 with properties: 
name: GeForce GTX 1080 major: 6 minor: 1 memoryClockRate(GHz): 1.8225
pciBusID: 0000:01:00.0
totalMemory: 7.93GiB freeMemory: 6.67GiB
2018-06-18 14:40:26.288067: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1423] Adding visible gpu devices: 0
2018-06-18 14:40:26.455561: I tensorflow/core/common_runtime/gpu/gpu_device.cc:911] Device interconnect StreamExecutor with strength 1 edge matrix:
2018-06-18 14:40:26.455590: I tensorflow/core/common_runtime/gpu/gpu_device.cc:917]      0 
2018-06-18 14:40:26.455596: I tensorflow/core/common_runtime/gpu/gpu_device.cc:930] 0:   N 
2018-06-18 14:40:26.455732: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1041] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 6437 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1080, pci bus id: 0000:01:00.0, compute capability: 6.1)
Traceback (most recent call last):
  File "batch_norm_test.py", line 16, in <module>
    onnxModel = onnxkeras.keras_model_to_onnx_model(kerasModel)
  File "/home/daiver/coding/onnx-keras/frontend.py", line 104, in keras_model_to_onnx_model
    check_model(model)
  File "/usr/local/lib/python3.6/dist-packages/onnx/checker.py", line 82, in check_model
    C.check_model(model.SerializeToString())
onnx.onnx_cpp2py_export.checker.ValidationError: Unrecognized attribute: consumed_inputs

==> Context: Bad node spec: input: "dense_1/BiasAdd:0" input: "batch_normalization_1/gamma:0" input: "batch_normalization_1/beta:0" input: "batch_normalization_1/moving_mean:0" input: "batch_normalization_1/moving_variance:0" output: "batch_normalization_1/cond/Merge:0" name: "batch_normalization_1" op_type: "BatchNormalization" attribute { name: "consumed_inputs" ints: 0 ints: 0 ints: 0 ints: 1 ints: 1 type: INTS } attribute { name: "epsilon" f: 0.001 type: FLOAT } attribute { name: "momentum" f: 0.99 type: FLOAT } attribute { name: "spatial" i: 1 type: INT }

I have similar error with my own big models with BatchNorms.

NotImplementedError: Constant op is not implemented

code:

import sys
PATH_TO_ONNX_KERAS = 'path to onnx-keras'
sys.path.append(PATH_TO_ONNX_KERAS)
import backend as onnxkeras
import keras
import onnx
import tensorflow as tf

if __name__ == '__main__':
    print(tf.__version__)
    print(keras.__version__)
    onnxModel = onnx.load('resnet18.onnx')
    kerasModel = onnxkeras.prepare(onnxModel)

error:

NotImplementedError                       Traceback (most recent call last)
<ipython-input-1-fd279174cb04> in <module>
     11     print(keras.__version__)
     12     onnxModel = onnx.load('resnet18.onnx')
---> 13     kerasModel = onnxkeras.prepare(onnxModel)

~/onnx-keras-master/backend.py in prepare(cls, model, device, **kwargs)
    305 
    306         original_input_dict, predict_net = (
--> 307             cls.onnx_graph_to_keras_net(model.graph))
    308 
    309         initialized = {init.name for init in model.graph.initializer}

~/onnx-keras-master/backend.py in onnx_graph_to_keras_net(cls, graph_def)
    288             print(cnt)
    289 
--> 290             output_ops = cls._onnx_node_to_keras_op(node, input_dict)
    291             curr_node_output_map = list(zip(node.outputs, output_ops))
    292             input_dict = dict(list(input_dict.items()) +

~/onnx-keras-master/backend.py in _onnx_node_to_keras_op(cls, node, input_dict)
    349             return tmp
    350         else:
--> 351             raise NotImplementedError("{} op is not implemented.".format(node.op_type))
    352 
    353     @classmethod

NotImplementedError: Constant op is not implemented.

Converting ONNX to Keras

Hi,
I couldn't find any relevant documentation for the ONNX -> Keras conversion. It would be great if you help me understand how I could do the same.

Thanks

Cannot convert ResNet50 to onnx model (ValueError: setting an array element with a sequence in in create_zero_padding)

Hello! First of all thank you for your project!

But i have a problem with converting default ResNet50 model to onnx
Here a minimal example

import sys
PATH_TO_ONNX_KERAS = '<path to repo>'
sys.path.append(PATH_TO_ONNX_KERAS)
import frontend as onnxkeras
import keras
import onnx

if __name__ == '__main__':
    kerasModel = keras.applications.ResNet50()
    onnxModel = onnxkeras.keras_model_to_onnx_model(kerasModel)
    onnxkeras.save(onnxModel, 'keras_resnet.proto')

It gives me following output

/usr/local/lib/python3.6/dist-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
Using TensorFlow backend.
2018-05-30 11:09:32.717600: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2018-05-30 11:09:32.822003: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:898] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2018-05-30 11:09:32.822255: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1344] Found device 0 with properties: 
name: GeForce GTX 1080 major: 6 minor: 1 memoryClockRate(GHz): 1.8225
pciBusID: 0000:01:00.0
totalMemory: 7.93GiB freeMemory: 7.00GiB
2018-05-30 11:09:32.822268: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1423] Adding visible gpu devices: 0
2018-05-30 11:09:32.976717: I tensorflow/core/common_runtime/gpu/gpu_device.cc:911] Device interconnect StreamExecutor with strength 1 edge matrix:
2018-05-30 11:09:32.976745: I tensorflow/core/common_runtime/gpu/gpu_device.cc:917]      0 
2018-05-30 11:09:32.976750: I tensorflow/core/common_runtime/gpu/gpu_device.cc:930] 0:   N 
2018-05-30 11:09:32.976910: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1041] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 6763 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1080, pci bus id: 0000:01:00.0, compute capability: 6.1)
[1, 3, 224, 224]
Traceback (most recent call last):
  File "main.py", line 10, in <module>
    onnxModel = onnxkeras.keras_model_to_onnx_model(kerasModel)
  File "/home/daiver/coding/onnx-keras/frontend.py", line 100, in keras_model_to_onnx_model
    model = make_model(cls.keras_graph_to_onnx_graph(model, name=model_name),
  File "/home/daiver/coding/onnx-keras/frontend.py", line 145, in keras_graph_to_onnx_graph
    graph_input_list, weight_list, node_list = handler(layer)
  File "/home/daiver/coding/onnx-keras/frontend.py", line 998, in create_zero_padding2D
    return cls.create_zero_padding(layer, 2)
  File "/home/daiver/coding/onnx-keras/frontend.py", line 837, in create_zero_padding
    pads = np.asarray(padding).transpose().flatten().tolist()
  File "/home/daiver/.local/lib/python3.6/site-packages/numpy/core/numeric.py", line 492, in asarray
    return array(a, dtype, copy=False, order=order)
ValueError: setting an array element with a sequence.

PS Sorry for my poor English

Keras to ONNX

Hello @leodestiny @xdyllx

Great work!
I had few questions:

  1. Are there any plan to convert Keras-ONNX. So that we enable people using Keras to move to PyTorch or MXNet for example? If not, if you can share some learnings, I am happy to jump in and contribute along with my friend (@roywei https://github.com/roywei)
  2. Based on your current work, are there any list of missing/incompatible operators/layers between ONNX/Keras?

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.