tutorials icon indicating copy to clipboard operation
tutorials copied to clipboard

How do we save a onnx model converted from caffe2 in python?

Open jjiang2cal opened this issue 7 years ago • 4 comments

I followed the tutorial https://github.com/onnx/tutorials/blob/master/tutorials/Caffe2OnnxExport.ipynb to export a caffe2 squeezenet model to onnx, but how do we save the onnx model to a file in python? Suppose I cannot use a shell script and have to use python.

jjiang2cal avatar Sep 14 '18 19:09 jjiang2cal

@jjiang2cal did you find the answer?

Hkathuria avatar Jan 11 '19 10:01 Hkathuria

Add the below line in your code to save the model

onnx.save(onnx_model, 'path/to/the/model.onnx')

Hkathuria avatar Jan 11 '19 10:01 Hkathuria

Hi, anyone knows in which version was save() introduced? I'm seeing "AttributeError: module 'onnx' has no attribute 'save'"

I'm on:

(py36) PS C:\Users\dimension> conda list onnx
// packages in environment at C:\Users\xxx\conda\envs\py36:
//
// Name                    Version                   Build  Channel
onnx                      1.1.1                    py36_0    conda-forge

I'm running in Conda:

(py36) PS C:\Users\dimension> python .\re_generate.py
Traceback (most recent call last):
  File ".\re_generate.py", line 9, in <module>
    onnx.save(new_model, 'test_3_withDynamicDimension.onnx')
AttributeError: module 'onnx' has no attribute 'save'

Here's my script:

import onnx
from google.protobuf.json_format import Parse

json_file = 'test.onnx.json'
with open(json_file, 'r') as f:
    json_str = f.read()
    # Convert String to a new onnx model
    new_model = Parse(json_str, onnx.ModelProto())
    onnx.save(new_model, 'test.onnx')

oreoero avatar Aug 20 '20 20:08 oreoero

Hi, anyone knows in which version was save() introduced? I'm seeing "AttributeError: module 'onnx' has no attribute 'save'"

I'm on:

(py36) PS C:\Users\dimension> conda list onnx
// packages in environment at C:\Users\xxx\conda\envs\py36:
//
// Name                    Version                   Build  Channel
onnx                      1.1.1                    py36_0    conda-forge

I'm running in Conda:

(py36) PS C:\Users\dimension> python .\re_generate.py
Traceback (most recent call last):
  File ".\re_generate.py", line 9, in <module>
    onnx.save(new_model, 'test_3_withDynamicDimension.onnx')
AttributeError: module 'onnx' has no attribute 'save'

Here's my script:

import onnx
from google.protobuf.json_format import Parse

json_file = 'test.onnx.json'
with open(json_file, 'r') as f:
    json_str = f.read()
    # Convert String to a new onnx model
    new_model = Parse(json_str, onnx.ModelProto())
    onnx.save(new_model, 'test.onnx')

Ok, I unblocked myself by running the same scripts outside conda, pip installing onnx is good enough to run my script.

oreoero avatar Aug 21 '20 16:08 oreoero