onnx / tensorflow-onnx

Convert TensorFlow, Keras, Tensorflow.js and Tflite models to ONNX
Apache License 2.0
2.32k stars 432 forks source link

ValueError: Optimizer only accepts ModelProto, incorrect type: <class 'bytes'> #58

Closed jiangsuliji closed 6 years ago

jiangsuliji commented 6 years ago

It's just a simple CNN frozen TF model. Not sure how it happens. Tried with both onnx 1.2.1 and 1.2.2 but neither works

More details regarding the error: tensorflow ops: Counter({'Const': 12, 'BiasAdd': 5, 'Relu': 4, 'MatMul': 3, 'MaxPool': 2, 'Conv2D': 2, 'Reshape': 2, 'Placeholder': 1, 'Softmax': 1}) tensorflow attr: Counter({'T': 19, 'dtype': 13, 'value': 12, 'data_format': 9, 'strides': 4, 'padding': 4, 'transpose_b': 3, 'transpose_a': 3, 'ksize': 2, 'dilations': 2, 'Tshape': 2, 'use_cudnn_on_gpu': 2, 'shape': 1}) onnx mapped: Counter({'Const': 12, 'BiasAdd': 5, 'Relu': 4, 'MatMul': 3, 'MaxPool': 2, 'Conv2D': 2, 'Reshape': 2, 'Placeholder': 1, 'Softmax': 1}) onnx unmapped: Counter() Traceback (most recent call last): File "C:\Program Files\Python35\lib\runpy.py", line 193, in _run_module_as_main "main", mod_spec) File "C:\Program Files\Python35\lib\runpy.py", line 85, in _run_code exec(code, run_globals) File "C:\Program Files\Python35\lib\site-packages\tf2onnx\convert.py", line 63, in main() File "C:\Program Files\Python35\lib\site-packages\tf2onnx\convert.py", line 47, in main "converted from {}".format(args.input), args.inputs, args.outputs) File "C:\Program Files\Python35\lib\site-packages\tf2onnx\graph.py", line 387, in make_model "eliminate_nop_transpose"]) File "C:\Program Files\Python35\lib\site-packages\onnx\optimizer.py", line 43, in optimize raise ValueError('Optimizer only accepts ModelProto, incorrect type: {}'.format(type(model))) ValueError: Optimizer only accepts ModelProto, incorrect type: <class 'bytes'>

guschmue commented 6 years ago

in onnx-1.2 the optimizer interface changed and https://github.com/onnx/tensorflow-onnx/pull/35/files is tracking that. Somehow you might have onnx <1.2 on your box. When you call the converter it prints a line at the begining like

>using tensorflow=1.8.0, onnx=1.2.2

which is the onnx version the converter found. Is yours >= 1.2 ?

jiangsuliji commented 6 years ago

Thank you for the quick response! If running "python -m tf2onnx.convert ...", it gives "ValueError: Optimizer only accepts ModelProto, incorrect type: <class 'bytes'>" error. But if running "python D:\Downloads\tensorflow-onnx\tf2onnx\convert.py ...", it shows "using tensorflow=1.8.0, onnx=1.2.2" in the printout. But it gives error "TypeError: process_tf_graph() got an unexpected keyword argument 'opset'". I've tried adding "--opset ..." in the argument but it doesn't work.

So i guess the tf2onnx.convert should be configured to onnx 1.2.2. How should I do that? I've downloaded the latest version of tf2onnx and run the setup.py.

guschmue commented 6 years ago

hm, I tried on master (for linux and windows):

python -m tf2onnx.convert --input tests/models/fc-layers/frozen.pb --inputs X:0 --outputs output:0 --output /tmp/model.onnx --opset 5

and it is happy. Try:

pip uninstall t2onnx # maybe run this more than once until it complains
python setup.py develop
jiangsuliji commented 6 years ago

Yeah, I found that the two versions co-exist in my python lib and it will always pick 0.0.0.1 instead of the latest version. After uninstall, the error goes away. The following cmd works well: python -m tf2onnx.convert --input tests/models/fc-layers/frozen.pb --inputs X:0 --outputs output:0 --output /tmp/model.onnx --opset 5 The python setup.py test also works. So the tool should be all set.

But for my simple tf frozen model, it errors out >python -m tf2onnx.convert --input frozen.pb --inputs Placeholder --outputs Softmax --output model.onnx --verbose --opset 5 using tensorflow=1.8.0, onnx=1.2.2 2018-06-27 10:22:02.554037: I T:\src\github\tensorflow\tensorflow\tools\graph_transforms\transform_graph.cc:264] Applying fold_batch_norms 2018-06-27 10:22:02.574017: I T:\src\github\tensorflow\tensorflow\tools\graph_transforms\transform_graph.cc:264] Applying fold_old_batch_norms 2018-06-27 10:22:02.694528: I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 2018-06-27 10:22:02.939830: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:1356] Found device 0 with properties: name: Quadro M2000 major: 5 minor: 2 memoryClockRate(GHz): 1.1625 pciBusID: 0000:03:00.0 totalMemory: 4.00GiB freeMemory: 3.34GiB 2018-06-27 10:22:02.944954: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:1435] Adding visible gpu devices: 0 2018-06-27 10:22:03.221562: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:923] Device interconnect StreamExecutor with strength 1 edge matrix: 2018-06-27 10:22:03.224417: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:929] 0 2018-06-27 10:22:03.226322: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:942] 0: N 2018-06-27 10:22:03.228385: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:1053] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 3075 MB memory) -> physical GPU (device: 0, name: Quadro M2000, pci bus id: 0000:03:00.0, compute capability: 5.2) tensorflow ops: Counter({'Const': 12, 'Identity': 10, 'BiasAdd': 5, 'Relu': 4, 'MatMul': 3, 'MaxPool': 2, 'Reshape': 2, 'Conv2D': 2, 'Softmax': 1, 'Placeholder': 1}) tensorflow attr: Counter({'T': 29, 'dtype': 13, 'value': 12, '_class': 10, 'data_format': 9, 'strides': 4, 'padding': 4, 'transpose_a': 3, 'transpose_b': 3, 'ksize': 2, 'use_cudnn_on_gpu': 2, 'dilations': 2, 'Tshape': 2, 'shape': 1}) onnx mapped: Counter({'Const': 12, 'Identity': 10, 'BiasAdd': 5, 'Relu': 4, 'MatMul': 3, 'MaxPool': 2, 'Reshape': 2, 'Conv2D': 2, 'Softmax': 1, 'Placeholder': 1}) onnx unmapped: Counter()

The error happens in model_proto = g.make_model("converted from {}".format(args.input), args.inputs, args.outputs, optimize=not args.continue_on_error)

guschmue commented 6 years ago

Try:

python -m tf2onnx.convert --input frozen.pb --inputs Placeholder:0 --outputs Softmax:0 --output model.onnx --verbose --opset 5

I need to add some checking on inputs and outputs ...

jiangsuliji commented 6 years ago

More info: after tried with "continue_on_error", it generates a onnx graph with a renamed softmax node (which is renamed to Softmax:0) as output (originally it's Softmax). The input Placeholder is also renamed to "Placeholder:0". These changes seem fine. The weird part is that it has a duplicated Softmax node as output, which is isolated from the main graph. With the latest tensorflow, it seems the node names are without ":0". Would that be the cause?

I'm trying to load the generated onnx using other tools to see if it can be loaded. --So far, CNTK fails to load the model --After adding ":0" to the input/output names, there is no error. but cntk still fails to load the model. I'm trying winml --WinML can load the model, Will reopen if it can't execute it

jiangsuliji commented 6 years ago

Thanks a lot! WinML can load and execute the model. Note that the opset was 5 (7 won't work as my WinML version is not up to date, so it can load the model but cann't execute it...)

guschmue commented 6 years ago

Super! The current version of winml is opset4 as far I know, but there was nothing in opset5 that broke it I think. The next version will be based on onnx-1.2, aka opset7