ONNX to Caffe2 in C++

I saved a pytorch model using onnx, following the instructions shown here

Now I would like to load this model in C++. I’ve spent a whole day googling and trying out different things without any luck.

I’ve tried CAFFE_ENFORCE(ReadProtoFromFile(path, &init_net));, where path is a std::string pointing to the onnx file, but this throws an error which I assume is because ReadProtoFromFile is expecting path to a protobuf file.

Ideally I would like to get the init_net and run_net set and use the Predictor API for inference. If someone could give me a pointer, that’d be great.

Any help is highly appreciated. Help!


you may run the following command:

convert-onnx-to-caffe2 model.onnx --output predict_net.pb --init-net-output init_net.pb

to convert an onnx model to caffe model’s files

Thanks for your answer but is there any possibility to obtain caffe2 in text format because as I know TensorRT parser requires TXT format?