如何将灰度图像数据集加载到移动网络模块

2024-05-17 16:46:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将灰度图像数据集(时尚mnist)加载到MobileNet模型中,以预测手写数字,但根据this教程,只有RGB图像可以加载到模型中。当我试图提供时尚杂志的样本时,它给出了以下错误

Error when checking input: expected keras_layer_13_input to have shape (224, 224, 3) but got array with shape (224, 224, 1)

如何解决这个问题?在


Tags: 数据模型图像input教程数字rgbthis
3条回答

可能预先训练过的MobileNet不适合这个任务。你有两个不同的问题。Mobilenet是为Imagenet图像制作的,它是224x224图像,有3个颜色通道,而MNIST数据集是28x28个图像,有一个颜色通道。可以在RGB中重复颜色通道:

# data.shape [70000, 224, 224, 1] -> [70000, 224, 224, 3]
data = np.repeat(data, 3, -1)

但在此之前,您需要调整图像大小。例如,可以使用PIL调整图像大小:

^{pr2}$

这里有些小细节你应该自己弄清楚。例如,dtype的图像,如果您已经从数据集中加载为numpy。您可能需要使用np.uint8()将numpy类型转换为整数。在

试试这个,x = np.stack((x,)*3, axis=-1)。有关详细信息,请参阅此链接:https://github.com/malnakli/ML/blob/master/tf_serving_keras_mobilenetv2/main.ipynb

Mobilenet v2需要RGB。您还可以使用PIL中的convert函数。在

试试这个:

from PIL import Image
x= Image.open(input_image).resize((96,96)).convert("RGB")

文档在这里:https://pillow.readthedocs.io/en/stable/reference/Image.html

相关问题 更多 >