apache / mxnet

Lightweight, Portable, Flexible Distributed/Mobile Deep Learning with Dynamic, Mutation-aware Dataflow Dep Scheduler; for Python, R, Julia, Scala, Go, Javascript and more
https://mxnet.apache.org
Apache License 2.0
20.77k stars 6.79k forks source link

Keras model to ONNX to mxnet error #17395

Open lilipj opened 4 years ago

lilipj commented 4 years ago

Description

I'm trying to import a keras model converted in onnx format with onnx_mxnet import module. I've built a classic mnist digit classification with keras : def define_model(): model = Sequential() model.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', input_shape=(28, 28, 1), name="name_1")) model.add(MaxPooling2D((2, 2), name="name_2")) model.add(Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_uniform', name="name_3")) model.add(Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_uniform', name="name_4")) model.add(MaxPooling2D((2, 2), name="name_5")) model.add(Flatten(name="name_6")) model.add(Dense(100, activation='relu', kernel_initializer='he_uniform', name="name_7")) model.add(Dense(10, activation='softmax', name="name_8"))

compile model

opt = SGD(lr=0.01, momentum=0.9)
model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])
return model

I've used keras2onnx to convert my model and save it in a .onnx file When i import the model with onnx_mxnet.import_model() function, I've got the following error Traceback (most recent call last): File "WORK/mnist_handwritten_digit_classification/onnx_to_mxnet.py", line 37, in sym, arg, aux = onnx_mxnet.import_model(onnx_model_file) File "anaconda3/envs/onnx/lib/python3.7/site-packages/mxnet/contrib/onnx/onnx2mx/import_model.py", line 59, in import_model sym, arg_params, aux_params = graph.from_onnx(model_proto.graph) File "anaconda3/envs/onnx/lib/python3.7/site-packages/mxnet/contrib/onnx/onnx2mx/import_onnx.py", line 115, in from_onnx inputs = [self._nodes[i] for i in node.input] File "/anaconda3/envs/onnx/lib/python3.7/site-packages/mxnet/contrib/onnx/onnx2mx/import_onnx.py", line 115, in inputs = [self._nodes[i] for i in node.input] KeyError: 'name_1/kernel:0'

Error Message

/anaconda3/envs/onnx/bin/python3.7 /WORK/mnist_handwritten_digit_classification/onnx_to_mxnet.py Traceback (most recent call last): File "/WORK/mnist_handwritten_digit_classification/onnx_to_mxnet.py", line 37, in sym, arg, aux = onnx_mxnet.import_model(onnx_model_file) File "/anaconda3/envs/onnx/lib/python3.7/site-packages/mxnet/contrib/onnx/onnx2mx/import_model.py", line 59, in import_model sym, arg_params, aux_params = graph.from_onnx(model_proto.graph) File "/anaconda3/envs/onnx/lib/python3.7/site-packages/mxnet/contrib/onnx/onnx2mx/import_onnx.py", line 115, in from_onnx inputs = [self._nodes[i] for i in node.input] File "anaconda3/envs/onnx/lib/python3.7/site-packages/mxnet/contrib/onnx/onnx2mx/import_onnx.py", line 115, in inputs = [self._nodes[i] for i in node.input] KeyError: 'name_1/kernel:0'

To Reproduce

The digit model comes from https://machinelearningmastery.com/how-to-develop-a-convolutional-neural-network-from-scratch-for-mnist-handwritten-digit-classification/ I've added the following lines onnx_model = keras2onnx.convert_keras(model, model.name) # converts the keras model to ONNX object. onnx.save_model(onnx_model, 'model.onnx')# save the ONNX object into .onnx file.

And in another script python I import the onnx model import mxnet as mx import mxnet.contrib.onnx as onnx_mxnet from mxnet.test_utils import download

onnx_model_file = download("model.onnx") sym, arg, aux = onnx_mxnet.import_model(onnx_model_file)

Steps to reproduce

(Paste the commands you ran that produced the error.)

1. 2.

What have you tried to solve it?

  1. I tried onnxmltools to convert keras to onnx. When I precise the target_opset parameter to 8, it works, 9 fails

Environment

Keras : 2.3.1 keras2onnx : 1.6.0 tf : 2.0 We recommend using our script for collecting the diagnositc information. Run the following command and paste the outputs below:

curl --retry 10 -s https://raw.githubusercontent.com/dmlc/gluon-nlp/master/tools/diagnose.py | python

# paste outputs here

----------Python Info---------- Version : 3.7.5 Compiler : GCC 7.3.0 Build : ('default', 'Oct 25 2019 15:51:11') Arch : ('64bit', '') ------------Pip Info----------- Version : 19.3.1 Directory : /anaconda3/envs/onnx/lib/python3.7/site-packages/pip ----------MXNet Info----------- Version : 1.5.1 Directory : anaconda3/envs/onnx/lib/python3.7/site-packages/mxnet Num GPUs : 0 Commit Hash : c9818480680f84daa6e281a974ab263691302ba8 ----------System Info---------- Platform : Linux-3.10.0-957.10.1.el7.x86_64-x86_64-with-centos-7.6.1810-Core system : Linux node : centos7io01 release : 3.10.0-957.10.1.el7.x86_64 version : #1 SMP Mon Mar 18 15:06:45 UTC 2019 ----------Hardware Info---------- machine : x86_64 processor : x86_64 Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian CPU(s): 8 On-line CPU(s) list: 0-7 Thread(s) per core: 2 Core(s) per socket: 4 Socket(s): 1 NUMA node(s): 1 Vendor ID: GenuineIntel CPU family: 6 Model: 60 Model name: Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz Stepping: 3 CPU MHz: 3730.371 CPU max MHz: 3900.0000 CPU min MHz: 800.0000 BogoMIPS: 6783.62 Virtualization: VT-x L1d cache: 32K L1i cache: 32K L2 cache: 256K L3 cache: 8192K NUMA node0 CPU(s): 0-7 Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm epb ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt dtherm ida arat pln pts spec_ctrl intel_stibp flush_l1d ----------Network Test---------- Setting timeout: 10 Timing for MXNet: https://github.com/apache/incubator-mxnet, DNS: 0.0225 sec, LOAD: 0.5508 sec. Timing for GluonNLP GitHub: https://github.com/dmlc/gluon-nlp, DNS: 0.0011 sec, LOAD: 0.8648 sec. Error open GluonNLP: http://gluon-nlp.mxnet.io, <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1076)>, DNS finished in 0.05342268943786621 sec. Timing for D2L: http://d2l.ai, DNS: 0.0145 sec, LOAD: 0.6090 sec. Timing for D2L (zh-cn): http://zh.d2l.ai, DNS: 0.0151 sec, LOAD: 0.2190 sec. Error open FashionMNIST: https://repo.mxnet.io/gluon/dataset/fashion-mnist/train-labels-idx1-ubyte.gz, <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1076)>, DNS finished in 0.051079750061035156 sec. Timing for PYPI: https://pypi.python.org/pypi/pip, DNS: 0.0135 sec, LOAD: 0.5829 sec. Error open Conda: https://repo.continuum.io/pkgs/free/, <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1076)>, DNS finished in 0.00898432731628418 sec.

bmistral commented 4 years ago

I am facing the same issue with 3 different onnx models from keras .... Did you solve the problem ? Anyone ?

lilipj commented 4 years ago

I've used the onnxmltool.convert_keras function, it works with target_opset=8 (9 fails)

TristonC commented 3 years ago

@szha Has this issue been solved?

usamec commented 2 years ago

It seems that MXNet loader expects graph nodes to be sorted from input to output.