简体   繁体   English

从 pytorch 模型转换而来的 coreML 模型给出了错误的预测概率

[英]coreML model converted from pytorch model giving the wrong prediction probabilities

I have a pytorch binary classification model that I converted to coreML.我有一个转换为 coreML 的 pytorch 二进制分类模型。 I converted my model directly and indirectly through onnx using the following tutorials/documentation respectively https://coremltools.readme.io/docs/pytorch-conversion , and https://github.com/onnx/onnx-docker/blob/master/onnx-ecosystem/inference_demos/resnet50_modelzoo_onnxruntime_inference.ipynb .我使用以下教程/文档分别通过 onnx 直接和间接转换了我的模型https://coremltools.readme.io/docs/pytorch-conversionhttps://github.com/onnx/onnx-docker/blob/master /onnx-ecosystem/inference_demos/resnet50_modelzoo_onnxruntime_inference.ipynb

The output prior to the softmax function and the probabilities are similar for both the original pytorch and the onnx model converted from PyTorch. softmax 函数之前的输出和概率对于原始 pytorch 和从 PyTorch 转换而来的 onnx 模型是相似的。 But the output for the coreML model converted from PyTorch via the tutorial documentation is completely incorrect.但是通过教程文档从 PyTorch 转换而来的 coreML 模型的输出是完全不正确的。 I had no errors compiling the coreML method from either method.我从任一方法编译 coreML 方法都没有错误。

Checking the weights of the last layer for coreML and Pytorch seem to be the same.检查 coreML 和 Pytorch 的最后一层的权重似乎是一样的。 the output of the coreML model prior to softmax gives me {'classLabel': '_xx', 'classLabelProbs': {'_xx': 29.15625, 'xx': -22.53125}}在 softmax 之前的 coreML 模型的输出给了我 {'classLabel': '_xx', 'classLabelProbs': {'_xx': 29.15625, 'xx': -22.53125}}

while the output from the pytorch model give me [-3.2185674 3.4477997]而 pytorch 模型的输出给了我 [-3.2185674 3.4477997]

The output of the conversion from onnx to coreML looks like...从 onnx 到 coreML 的转换输出看起来像......

58/69: Converting Node Type Add
59/69: Converting Node Type Relu
60/69: Converting Node Type Conv
61/69: Converting Node Type BatchNormalization
62/69: Converting Node Type Relu
63/69: Converting Node Type Conv
64/69: Converting Node Type BatchNormalization
65/69: Converting Node Type Add
66/69: Converting Node Type Relu
67/69: Converting Node Type GlobalAveragePool
68/69: Converting Node Type Flatten
69/69: Converting Node Type Gemm
Translation to CoreML spec completed. Now compiling the CoreML model.
Model Compilation done.

While the output of the pytorch model when I print looks like this for the final layer....虽然我打印时 pytorch 模型的输出对于最后一层来说是这样的......

(layer4): Sequential(
(0): BasicBlock(
(conv1): Conv2d(256, 512, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(downsample): Sequential(
(0): Conv2d(256, 512, kernel_size=(1, 1), stride=(2, 2), bias=False)
(1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
(1): BasicBlock(
(conv1): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
(avgpool): AdaptiveAvgPool2d(output_size=(1, 1))
(fc): Linear(in_features=512, out_features=2, bias=True).

How do I go about resolving the quantitative errors produced from my coreML model that was converted from PyTorch?我如何解决从 PyTorch 转换而来的 coreML 模型产生的定量错误?

这可能是您的图像预处理选项的问题: https : //machinethink.net/blog/help-core-ml-gives-wrong-output/

update:更新:

using the coreml unified api I have added a scaled layer.使用 coreml 统一 api 我添加了一个缩放层。 My outputs are not giving any probabilities for my classifier.我的输出没有为我的分类器提供任何概率。

![last couple layers of converted pytorch model][1] [1]: https://i.stack.imgur.com/9bzd2.png ![转换后的 pytorch 模型的最后几层][1] [1]: https://i.stack.imgur.com/9bzd2.png

the last layer prints out a tensor instead of probabilities.最后一层打印出张量而不是概率。 So I added a softmax function via the network.builder所以我通过 network.builder 添加了一个 softmax 函数

builder.add_softmax(name="softmax", input_name="305", output_name="307:labelProbabilityLayerName")

the previous last node had the output name equal to "307:labelProbabilityLayerName" and I changed it to "305", prior to me adding the softmax().上一个节点的输出名称等于“307:labelProbabilityLayerName”,在添加 softmax() 之前,我将其更改为“305”。 this way the previous last node's output is the input to my softmax.这样前一个节点的输出就是我的 softmax 的输入。 Also, now the output can be passed to my softmax can now connect to the original string class printing out the intended probabilites.此外,现在可以将输出传递给我的 softmax,现在可以连接到原始字符串类,打印出预期的概率。 I am still getting an error saying...我仍然收到一个错误说...

"RuntimeError: Error compiling model: "Error reading protobuf spec. “运行时错误:编译模型时出错:”读取 protobuf 规范时出错。 validator error: Layer 'softmax' consumes an input named '307' which is not present in this network."."验证器错误:层“softmax”消耗了一个名为“307”的输入,该输入不存在于该网络中。”。

Which doesnt make sense because I defined my softmax to consume '305' and also updated that last layer which is an innerproduct layer to output 305.这是没有意义的,因为我定义了我的 softmax 来消耗 '305' 并且还更新了最后一层(内积层)以输出 305。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM