question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JSONDecodeError in `onnxmltools.utils.save_text` function

See original GitHub issue

Hi! 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:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
jiafatomcommented, Apr 8, 2019

@Eugene-1984 onnx_model is a protobuf object not a json, so json.load should not work. The function save_text is a legacy code, we need deprecate it, just use save_model to save it into binary file. This library does not support the conversion to json.

0reactions
vinitra-zzcommented, Apr 9, 2019

@Eugene-1984 The following script should help you get started! Remember to pip install protobuf and pip install onnx in your environment.

from google.protobuf.json_format import MessageToJson
import onnx
import json

model = onnx.load('model.onnx')
jsonObj = MessageToJson(model)

with open('model.json', 'w') as outfile:
    json.dump(jsonObj, outfile)

with open('model.json') as infile:
    j_model = json.load(infile)

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found