pytorchonnxcaffe2

Operator translate error occurs when I try to convert onnx file to caffe2


I train a boject detection model on pytorch, and I have exported to onnx file.

And I want to convert it to caffe2 model :

import onnx
import caffe2.python.onnx.backend as onnx_caffe2_backend


# Load the ONNX ModelProto object. model is a standard Python protobuf object
model = onnx.load("CPU4export.onnx")

# prepare the caffe2 backend for executing the model this converts the ONNX model into a
# Caffe2 NetDef that can execute it. Other ONNX backends, like one for CNTK will be
# availiable soon.
prepared_backend = onnx_caffe2_backend.prepare(model)

# run the model in Caffe2

# Construct a map from input names to Tensor data.
# The graph of the model itself contains inputs for all weight parameters, after the input image.
# Since the weights are already embedded, we just need to pass the input image.
# Set the first input.
W = {model.graph.input[0].name: x.data.numpy()}

# Run the Caffe2 net:
c2_out = prepared_backend.run(W)[0]

# Verify the numerical correctness upto 3 decimal places
np.testing.assert_almost_equal(torch_out.data.cpu().numpy(), c2_out, decimal=3)

print("Exported model has been executed on Caffe2 backend, and the result looks good!")

I always got this error :

RuntimeError: ONNX conversion failed, encountered 1 errors:

Error while processing node: input: "90"
input: "91"
output: "92"
op_type: "Resize"
attribute {
  name: "mode"
  s: "nearest"
  type: STRING
}
. Exception: Don't know how to translate op Resize

How can I solve it ?


Solution

  • The problem is that the Caffe2 ONNX backend does not yet support the export of the Resize operator.

    Please raise an issue on the Caffe2 / PyTorch github -- there's an active community of developers who should be able to address this use case.