该模型在 Tensorflow 到 CoreML 模型中没有有效的图像类型输入特征

Posted

技术标签:

【中文标题】该模型在 Tensorflow 到 CoreML 模型中没有有效的图像类型输入特征【英文标题】:The model does not have a valid input feature of type image in Tensorflow to CoreML model 【发布时间】:2021-11-19 06:19:49 【问题描述】:

我使用 Tensorflow 做了一个简单的图像分类 CNN。我想把它转换成 Core ML。我使用了 Core ML Tools 的通用转换 API。但是我的代码给了我以下错误:

模型没有图像类型的有效输入特征

这是 Xcode 中的模型详细信息。如何将模型的输入指定为图像而不是 MultiArray?

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.preprocessing import image
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import numpy as np
import matplotlib.pyplot as plt

train_gen = ImageDataGenerator(rescale=1./255,shear_range=0.2,zoom_range=0.2,horizontal_flip=True)

test_gen = ImageDataGenerator(rescale=1./255,shear_range=0.2,zoom_range=0.2,horizontal_flip=True)

train_data = train_gen.flow_from_directory(directory='/Users/saibalaji/Documents/TensorFlowProjects/data/train',target_size=(256,256),batch_size=32)

train_data.class_indices

class_labels = []
for class_label,class_mode in train_data.class_indices.items():
    print(class_label)
    class_labels.append(class_label)


test_data = test_gen.flow_from_directory(directory='/Users/saibalaji/Documents/TensorFlowProjects/data/val',target_size=(256,256),batch_size=32)

model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv2D(filters=32,kernel_size=(3,3),input_shape=(256,256,3),activation='relu'))
model.add(tf.keras.layers.MaxPool2D(pool_size=(2,2)))
model.add(tf.keras.layers.Conv2D(filters=64,kernel_size=(3,3),activation='relu'))
model.add(tf.keras.layers.MaxPool2D(pool_size=(2,2)))
model.add(tf.keras.layers.Conv2D(filters=128,kernel_size=(3,3),activation='relu'))
model.add(tf.keras.layers.MaxPool2D(pool_size=(2,2)))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(units=256,activation='relu'))
model.add(tf.keras.layers.Dense(units=4,activation='softmax'))


model.summary()

model.compile(optimizer = 'adam',loss = 'categorical_crossentropy', metrics = ['accuracy'])

model.fit(x=train_data,validation_data=test_data,epochs=12)

def predict(filepath):
    test_image = image.load_img(path=filepath,target_size=(256,256))
    image_array = image.img_to_array(test_image)
    image_array = image_array / 255
    print(image_array.shape)
    image_array_exp = np.expand_dims(image_array,axis=0)
    result = model.predict(image_array_exp)
    print(class_labels[np.argmax(result)])
    plt.imshow(test_image)

predict('/Users/saibalaji/Documents/TensorFlowProjects/data/test/diseased cotton leaf/dis_leaf (322).jpg')

predict('/Users/saibalaji/Documents/TensorFlowProjects/data/test/fresh cotton leaf/d (341).jpg')


predict('/Users/saibalaji/Documents/TensorFlowProjects/data/test/diseased cotton plant/dd (367).jpg')

predict('/Users/saibalaji/Documents/TensorFlowProjects/data/test/diseased cotton plant/dd (706).jpg')

predict('/Users/saibalaji/Documents/TensorFlowProjects/data/test/fresh cotton plant/dsd (600).jpg')

model.save('cottoncnntwo.h5')

from tensorflow.keras.models import load_model
savedmodel = load_model('cottoncnn.h5')

savedmodel.summary()

import coremltools as ct

coremlmodel = ct.convert(savedmodel)


coremlmodel.save('cotton')

【问题讨论】:

【参考方案1】:

请尝试:

image_input = ct.converters.mil.input_types.ImageType(name='input_image', shape=(256, 256, 3)) 
mlmodel_image = ct.convert(coremlmodel, inputs=[image_input])

【讨论】:

以上是关于该模型在 Tensorflow 到 CoreML 模型中没有有效的图像类型输入特征的主要内容,如果未能解决你的问题,请参考以下文章

如何将 CoreML 模型转换为 TensorFlow 模型?

Tensorflow .pb 文件到 coreml 模型:'Unsupported Ops of type: AddV2'

如何在 TensorFlow、Keras 或 PyTorch 中部署 CoreML 模型?

将模型从 tensorflow 转换为 Coreml (4.0) 时出现实例归一化错误

如何在 Tensorflow Lite 上为 CoreML 委托设置填充类型以修复未设置“PoolingLayerBuilder (MEAN)_1”。

如何正确使用 tf-coreml?