Coremltools 转换 caffemodel 问题

Posted

技术标签:

【中文标题】Coremltools 转换 caffemodel 问题【英文标题】:Coremltools converting caffemodel issue 【发布时间】:2020-06-22 03:56:24 【问题描述】:

Neural Style 有一个 Wonderfull 设置说明,允许将任何图像的样式应用于我的照片。

但它返回我想转换为 Core ML 格式的 .caffemodel 格式

有一个Dropbox Link to all the Caffe models

我创建了一个 python 文件:

import coremltools

    # Convert a Caffe model to a classifier in Core ML
    coreml_model = coremltools.converters.caffe.convert(
        ('nin_imagenet_conv.caffemodel', 'train_val.prototxt'), predicted_feature_name='class_labels.txt'
    )

    # Now save the model
    coreml_model.save('nin_imagenet_conv.mlmodel')

然后在终端我运行python convert.py

然后我得到一个错误:

================= Starting Conversion from Caffe to CoreML ======================
Layer 0: Type: 'Data', Name: 'data'. Output(s): 'data', 'label'.
WARNING: Skipping Data Layer 'data' of type 'Data'. It is recommended to use Input layer for deployment.
Layer 1: Type: 'Data', Name: 'data'. Output(s): 'data', 'label'.
WARNING: Skipping Data Layer 'data' of type 'Data'. It is recommended to use Input layer for deployment.
Layer 2: Type: 'Convolution', Name: 'conv1'. Input(s): 'data'. Output(s): 'conv1'.
Layer 3: Type: 'ReLU', Name: 'relu0'. Input(s): 'conv1'. Output(s): 'conv1'.
Layer 4: Type: 'Convolution', Name: 'cccp1'. Input(s): 'conv1'. Output(s): 'cccp1'.
Layer 5: Type: 'ReLU', Name: 'relu1'. Input(s): 'cccp1'. Output(s): 'cccp1'.
Layer 6: Type: 'Convolution', Name: 'cccp2'. Input(s): 'cccp1'. Output(s): 'cccp2'.
Layer 7: Type: 'ReLU', Name: 'relu2'. Input(s): 'cccp2'. Output(s): 'cccp2'.
Layer 8: Type: 'Pooling', Name: 'pool0'. Input(s): 'cccp2'. Output(s): 'pool0'.
Layer 9: Type: 'Convolution', Name: 'conv2'. Input(s): 'pool0'. Output(s): 'conv2'.
Layer 10: Type: 'ReLU', Name: 'relu3'. Input(s): 'conv2'. Output(s): 'conv2'.
Layer 11: Type: 'Convolution', Name: 'cccp3'. Input(s): 'conv2'. Output(s): 'cccp3'.
Layer 12: Type: 'ReLU', Name: 'relu5'. Input(s): 'cccp3'. Output(s): 'cccp3'.
Layer 13: Type: 'Convolution', Name: 'cccp4'. Input(s): 'cccp3'. Output(s): 'cccp4'.
Layer 14: Type: 'ReLU', Name: 'relu6'. Input(s): 'cccp4'. Output(s): 'cccp4'.
Layer 15: Type: 'Pooling', Name: 'pool2'. Input(s): 'cccp4'. Output(s): 'pool2'.
Layer 16: Type: 'Convolution', Name: 'conv3'. Input(s): 'pool2'. Output(s): 'conv3'.
Layer 17: Type: 'ReLU', Name: 'relu7'. Input(s): 'conv3'. Output(s): 'conv3'.
Layer 18: Type: 'Convolution', Name: 'cccp5'. Input(s): 'conv3'. Output(s): 'cccp5'.
Layer 19: Type: 'ReLU', Name: 'relu8'. Input(s): 'cccp5'. Output(s): 'cccp5'.
Layer 20: Type: 'Convolution', Name: 'cccp6'. Input(s): 'cccp5'. Output(s): 'cccp6'.
Layer 21: Type: 'ReLU', Name: 'relu9'. Input(s): 'cccp6'. Output(s): 'cccp6'.
Layer 22: Type: 'Pooling', Name: 'pool3'. Input(s): 'cccp6'. Output(s): 'pool3'.
Layer 23: Type: 'Dropout', Name: 'drop'. Input(s): 'pool3'. Output(s): 'pool3'.
WARNING: Skipping training related layer 'drop' of type 'Dropout'.
Layer 24: Type: 'Convolution', Name: 'conv4-1024'. Input(s): 'pool3'. Output(s): 'conv4'.
Layer 25: Type: 'ReLU', Name: 'relu10'. Input(s): 'conv4'. Output(s): 'conv4'.
Layer 26: Type: 'Convolution', Name: 'cccp7-1024'. Input(s): 'conv4'. Output(s): 'cccp7'.
Layer 27: Type: 'ReLU', Name: 'relu11'. Input(s): 'cccp7'. Output(s): 'cccp7'.
Layer 28: Type: 'Convolution', Name: 'cccp8-1024'. Input(s): 'cccp7'. Output(s): 'cccp8'.
Layer 29: Type: 'ReLU', Name: 'relu12'. Input(s): 'cccp8'. Output(s): 'cccp8'.
Layer 30: Type: 'Pooling', Name: 'pool4'. Input(s): 'cccp8'. Output(s): 'pool4'.
Layer 31: Type: 'Accuracy', Name: 'accuracy'. Input(s): 'pool4', 'label'. Output(s): 'accuracy'.
WARNING: Skipping training related layer 'accuracy' of type 'Accuracy'.
Layer 32: Type: 'SoftmaxWithLoss', Name: 'loss'. Input(s): 'pool4', 'label'. WARNING: Skipping training related layer 'loss' of type 'SoftmaxWithLoss'.

================= Summary of the conversion: ===================================
Traceback (most recent call last):
  File "convert.py", line 5, in <module>
    ('nin_imagenet_conv.caffemodel', 'train_val.prototxt'), predicted_feature_name='class_labels.txt'
  File "/Users/pavel.tarasevich/Library/Python/2.7/lib/python/site-packages/coremltools/converters/caffe/_caffe_converter.py", line 192, in convert
    predicted_feature_name)
  File "/Users/pavel.tarasevich/Library/Python/2.7/lib/python/site-packages/coremltools/converters/caffe/_caffe_converter.py", line 260, in _export
    predicted_feature_name)
RuntimeError: Unable to infer input name and dimensions. Please provide a .prototxt file with 'Input' layer and dimensions defined.

【问题讨论】:

错误信息说明了问题所在:您需要更改 prototxt 文件以为其提供具有图像宽度和高度的输入层。看起来您使用的是训练 prototxt 而不是部署。 @MatthijsHollemans 我提供了带有另一个 prototxt 文件的 Dropbox 链接,但对我来说它也不起作用。在另一种情况下,我不知道添加具有图像宽度和高度的输入层请您在示例中提供帮助。如果有帮助,我可以开始赏金 我添加了如何做到这一点作为答案。 【参考方案1】:

在 prototxt 文件的顶部,添加以下内容:

input: "data"
input_dim: 1
input_dim: 3
input_dim: 224
input_dim: 224

维度的顺序是:batch size、channel、height、width。您需要为模型中的每个输入添加这些部分之一。

输入的名称(此处为"data")必须与第一层中的bottom 字段相同。

【讨论】:

以上是关于Coremltools 转换 caffemodel 问题的主要内容,如果未能解决你的问题,请参考以下文章

将 .caffemodel 转换为 .mlmodel 失败

从coremltools4.0转换的mlmodel比从tfcoreml转换的mlmodel慢很多

构建一个 sklearn 文本分类器并使用 coremltools 进行转换

尝试使用 coremltools 4.1 将模型转换为 coreml 不工作

Coremltools 无法成功将输出更改为图像

CoreMLTools 转换为 MLModel:reName_Feature 不起作用