In the previous section, we converted a Caffe2 model to ONNX format so that it could be used with other DL frameworks. In this section, we will learn how to use an ONNX model exported from other DL frameworks into Caffe2 for inference.
The backend module provided in the Caffe2 ONNX package enables this import of the ONNX model to Caffe2. This can be seen in the backend.py file in the python/onnx directory in the Caffe2 source code.
The ch5/run_onnx_model.py script provided along with this book's source code demonstrates how to load an ONNX model to Caffe2, and run an inference on an input image using that model.
The script first imports the Python modules necessary to work with the images (PIL.Image), Caffe2, and ONNX (caffe2.python.onnx.backend) as follows:
# Std import PIL.Image import json import sys # Ext import numpy as np from caffe2...