tutorials icon indicating copy to clipboard operation
tutorials copied to clipboard

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

Open Z68bakh opened this issue 6 years ago • 3 comments

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?

Z68bakh avatar Apr 23 '19 08:04 Z68bakh

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

prasanthpul avatar Apr 23 '19 16:04 prasanthpul

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.

Z68bakh avatar Apr 25 '19 08:04 Z68bakh

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.

prasanthpul avatar Apr 26 '19 21:04 prasanthpul