onnx / tutorials

Tutorials for creating and using ONNX models
Apache License 2.0
3.39k stars 630 forks source link

convert ONNX to caffe2: error in onnx.backend.prepare #119

Open Z68bakh opened 5 years ago

Z68bakh commented 5 years ago

I am trying to convert an onnx model to caffe2 but I got the following error:

RuntimeError Traceback (most recent call last)

in ----> 1 caffe2.python.onnx.backend.prepare(onnx_model, device="CPU") ~/anaconda3/lib/python3.7/site-packages/caffe2/python/onnx/backend.py in prepare(cls, model, device, raw_values_dict, **kwargs) 695 if opset_version is None: 696 if model.ir_version >= 0x00000003: --> 697 raise RuntimeError("Model with IR version >= 3 did not specify ONNX operator set version (onnx-caffe2 requires it)") 698 else: 699 opset_version = 1 RuntimeError: Model with IR version >= 3 did not specify ONNX operator set version (onnx-caffe2 requires it)

I have checked the backend.py and it seems that there is something wrong with model.ir_version, it is always None.

Is anyone facing the same issue? Any idea how to solve it?

prasanthpul commented 5 years ago

How did you generate the ONNX model you are using? The error indicates the model file is missing the opset version, which is required

Z68bakh commented 5 years ago

The onnx model is generated by converting scikit learn SVC model to onnx:

loaded_model = pickle.load(open('svc.pkl', 'rb')) # svc is the model I had already

initial_type = [('float_input', FloatTensorType([1, 4]))] onx = convert_sklearn(loaded_model, initial_types=initial_type, target_opset=9) with open("loaded_model.onnx", "wb") as f: f.write(onx.SerializeToString())

model = onnx.load('loaded_model.onnx') model.ir_version=4

onnx.save(model, 'model_new.onnx')

onnx_model = onnx.load('model_new.onnx')

print('The model is:\n{}'.format(onnx_model))

Check the model

onnx.checker.check_model(onnx_model) print('The model is checked!')

And the model is checked.

prasanthpul commented 5 years ago

You do not have to set "model.ir_version=4". The converter will take care of it. (make sure you are using latest version)

Caffe2 does not support models converted from scikitlearn. You should use ONNX Runtime instead.