JSONDecodeError in `onnxmltools.utils.save_text` function
See original GitHub issueHi! I am wondering about onnxmltools.utils.save_text
function. The example on title page says:
# Save as text
onnxmltools.utils.save_text(onnx_model, 'example.json')
So I expected the output file to be a valid JSON but it is not. Here is the example code copied from the title page; MWE:
import onnxmltools
from keras.layers import Input, Dense, Add
from keras.models import Model
# N: batch size, C: sub-model input dimension, D: final model's input dimension
N, C, D = 2, 3, 3
# Define a sub-model, it will become a part of our final model
sub_input1 = Input(shape=(C,))
sub_mapped1 = Dense(D)(sub_input1)
sub_model1 = Model(inputs=sub_input1, outputs=sub_mapped1)
# Define another sub-model, it will become a part of our final model
sub_input2 = Input(shape=(C,))
sub_mapped2 = Dense(D)(sub_input2)
sub_model2 = Model(inputs=sub_input2, outputs=sub_mapped2)
# Define a model built upon the previous two sub-models
input1 = Input(shape=(D,))
input2 = Input(shape=(D,))
mapped1_2 = sub_model1(input1)
mapped2_2 = sub_model2(input2)
sub_sum = Add()([mapped1_2, mapped2_2])
keras_model = Model(inputs=[input1, input2], output=sub_sum)
# Convert it! The target_opset parameter is optional.
onnx_model = onnxmltools.convert_keras(keras_model, target_opset=7)
onnxmltools.utils.save_text(onnx_model, 'example.json')
import json
with open('example.json') as f:
json.load(f)
yields error: JSONDecodeError: Expecting value: line 1 column 1 (char 0)
So… can I somehow obtain a valid JSON out of converted ONNX model?
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Python JSONDecodeError Explanation and Solution | CK
The Python JSONDecodeError indicates there is an issue with how a JSON object is formatted. To fix this error, you should read the...
Read more >How to use the onnxmltools.proto.onnx_proto function ... - Snyk
Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@Eugene-1984
onnx_model
is a protobuf object not a json, so json.load should not work. The functionsave_text
is a legacy code, we need deprecate it, just usesave_model
to save it into binary file. This library does not support the conversion to json.@Eugene-1984 The following script should help you get started! Remember to
pip install protobuf
andpip install onnx
in your environment.j_model is now a json string with the contents of model.json. If you would prefer the output as a dictionary, please include the following line:
dict_model = json.loads(j_model)
See the top voted answer here: https://stackoverflow.com/questions/19734617/protobuf-to-json-in-python