将 ONNX 模型转换为 Caffe2 时出现问题

Issue when converting ONNX model to Caffe2

我将一个 TF 模型转换为 ONNX,然后将 ONNX 模型转换为 Caffe2。转换成功发生。但是,我在尝试从获得的模型加载和推断时遇到运行时错误。

这是我收到的错误。如何将属性 'is_true' 添加到 SpatialBN 节点?

我浏览了 pytorch 存储库并看到了这个 issue,但是,它尚未解决。 在 ONNX here 的代码库中,它为 opset >=7 添加了 is_test 属性,我使用的是 8。但是,它仍然给出错误。

[W common_gpu.cc:35] Insufficient cuda driver. Cannot use cuda.
[W init.h:137] Caffe2 GlobalInit should be run before any other API calls.
[W init.h:137] Caffe2 GlobalInit should be run before any other API calls.
[W predictor_config.cc:90] Caffe2 is compiled without optimization passes.
[E operator_schema.cc:101] Argument 'is_test' is required for Operator 'SpatialBN'.
Traceback (most recent call last):
  File "main.py", line 91, in <module>
    test_caffe("mod-caffe-net.pb", "mod-caffe-init-net.pb", "../data/mouth")
  File "main.py", line 70, in test_caffe
    predictor = workspace.Predictor(param_values, model_net)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/caffe2/python/workspace.py", line 187, in Predictor
    return C.Predictor(StringifyProto(init_net), StringifyProto(predict_net))
RuntimeError: [enforce fail at operator.cc:199] schema->Verify(operator_def). Operator def did not pass schema checking: input: "conv1/Relu:0" input: "batchNorm1/gamma/read/_1__cf__1:0" input: "batchNorm1/beta/read/_0__cf__0:0" input: "batchNorm2/moving_mean/read/_6__cf__6:0" input: "batchNorm1/moving_variance/read/_3__cf__3:0" output: "batchNorm1/FusedBatchNorm:0" name: "batchNorm1/FusedBatchNorm" type: "SpatialBN" arg { name: "epsilon" f: 0.001 } device_option { device_type: 0 device_id: 0 }

问题已解决。我正在使用他们 README. However, it points to their tutorial in deprecated version of the code.

上建议的命令行实用程序

命令行实用程序(使用 pip install onnx-caffe2 安装)仍然具有 _known_opset_version = 3。这是导致错误的原因。在我通过导入 PyTorch 库中的 Python API 使用转换实用程序后,

from caffe2.python.onnx.backend import Caffe2Backend as c2

我成功地运行 推断了转换后的模型。