简体   繁体   English

CoreML中Python Tensorflow模型的图像输入

[英]Image input for Python Tensorflow model in CoreML

I'm implementing a simple image classification model with tensor flow (python). 我正在使用张量流(python)实现一个简单的图像分类模型。

Here's my image preprocessing: 这是我的图像预处理:

import glob
for filename in glob.glob('/Volumes/G-DRIVE mobile USB-C/traan/*.jpeg'): #assuming jpeg
    im=Image.open(filename)
    im = im.resize((150,120), Image.ANTIALIAS)
    print(im.size)
    training_images.append(im)

And here's my very simple model: 这是我非常简单的模型:

model = keras.Sequential([
    keras.layers.Flatten(input_shape=(120, 150, 3)),
    keras.layers.Dense(512, activation=tf.nn.relu),
    keras.layers.Dense(256, activation=tf.nn.relu),
    keras.layers.Dense(128, activation=tf.nn.relu),
    keras.layers.Dense(10, activation=tf.nn.softmax)
])

I want to load this model to CoreML something like this, 我想将此模型加载到CoreML中,

import coremltools

modelCoreML = coremltools.converters.tensorflow.convert(model, input_feature, output_feature)
modelCoreML.save("Model.mlmodel")

But how do I do this where I can input an image, and not a numpy stack? 但是如何在可以输入图像而不是numpy堆栈的位置执行此操作? Should I process the image and turn it into the right format in the app itself, then put it in the model? 我应该在应用程序本身中处理图像并将其转换为正确的格式,然后将其放入模型中吗? How would I do this? 我该怎么做?

You need to supply the image_input_names argument to coremltools.converters.keras.convert() so that coremltools knows which inputs should be treated as images. 您需要向coremltools.converters.keras.convert()提供image_input_names参数,以便coremltools知道应将哪些输入视为图像。 This is explained in the documentation . 文档中对此进行了说明

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

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