简体   繁体   English

将Caffe模型转换为CoreML

[英]Converting Caffe model to CoreML

I am working to understand CoreML. 我正在努力了解CoreML。 For a starter model, I've downloaded Yahoo's Open NSFW caffemodel. 对于初学者模型,我已经下载了Yahoo的Open NSFW caffemodel。 You give it an image, it gives you a probability score (between 0 and 1) that the image contains unsuitable content. 你给它一个图像,它给你一个概率分数(0到1之间),图像包含不合适的内容。

Using coremltools , I've converted the model to a .mlmodel and brought it into my app. 使用coremltools ,我已将模型转换为.mlmodel并将其带入我的应用程序。 It appears in Xcode like so: 它像Xcode一样出现在:

在此输入图像描述

In my app, I can successfully pass an image, and the output appears as a MLMultiArray . 在我的应用程序中,我可以成功传递图像,输出显示为MLMultiArray Where I am having trouble is understanding how to use this MLMultiArray to obtain my probability score. 我遇到麻烦的地方是了解如何使用这个MLMultiArray来获得我的概率分数。 My code is like so: 我的代码是这样的:

func testModel(image: CVPixelBuffer) throws {

    let model = myModel()
    let prediction = try model.prediction(data: image)
    let output = prediction.prob // MLMultiArray
    print(output[0]) // 0.9992402791976929
    print(output[1]) // 0.0007597212097607553
}

For reference, the CVPixelBuffer is being resized to the required 224x224 that the model asks (I'll get into playing with Vision once I can figure this out). 作为参考,CVPixelBuffer被调整为模型要求的所需224x224(一旦我能想到这一点,我将开始玩Vision)。

The two indexes I've printed to the console do change if I provide a different image, but their scores are wildly different than the result I get if I run the model in Python. 如果我提供不同的图像,我打印到控制台的两个索引确实会改变,但是如果我在Python中运行模型,它们的得分与我得到的结果大不相同。 The same image passed into the model when tested in Python gives me an output of 0.16, whereas my CoreML output, per the example above, is far different (and a dictionary, unlike Python's double output) than what I'm expecting to see. 在Python中测试时传递到模型中的相同图像给出了0.16的输出,而根据上面的示例,我的CoreML输出与我期望看到的差别很大(和字典不同,Python的双输出)。

Is more work necessary to get a result like I am expecting? 是否需要更多工作来获得我期待的结果?

It seems like you are not transforming the input image in the same way the model expects. 您似乎没有像模型所期望的那样转换输入图像。
Most caffe models expects "mean subtracted" images as input, so does this model. 大多数caffe模型都希望“平均减去”图像作为输入,这个模型也是如此。 If you inspect the python code provided with Yahoo's Open NSFW ( classify_nsfw.py ): 如果你检查雅虎的Open NSFWclassify_nsfw.py )提供的python代码:

# Note that the parameters are hard-coded for best results
caffe_transformer = caffe.io.Transformer({'data': nsfw_net.blobs['data'].data.shape})
caffe_transformer.set_transpose('data', (2, 0, 1))  # move image channels to outermost
caffe_transformer.set_mean('data', np.array([104, 117, 123]))  # subtract the dataset-mean value in each channel
caffe_transformer.set_raw_scale('data', 255)  # rescale from [0, 1] to [0, 255]
caffe_transformer.set_channel_swap('data', (2, 1, 0))  # swap channels from RGB to BGR

Also there is a specific way an image is resized to 256x256 and then cropped to 224x224 . 还有一种特定的方式将图像大小调整为256x256,然后裁剪为224x224

To obtain exactly the same results, you'll need to transform your input image in exactly the same way on both platforms. 要获得完全相同的结果,您需要在两个平台上以完全相同的方式转换输入图像。

See this thread for additional information. 有关其他信息,请参阅此主题

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

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