2024-04-17 07:46:49 发布
网友
我试图将灰度图像数据集(时尚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)
如何解决这个问题?在
可能预先训练过的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调整图像大小:
PIL
这里有些小细节你应该自己弄清楚。例如,dtype的图像,如果您已经从数据集中加载为numpy。您可能需要使用np.uint8()将numpy类型转换为整数。在
dtype
np.uint8()
试试这个,x = np.stack((x,)*3, axis=-1)。有关详细信息,请参阅此链接:https://github.com/malnakli/ML/blob/master/tf_serving_keras_mobilenetv2/main.ipynb
x = np.stack((x,)*3, axis=-1)
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
可能预先训练过的MobileNet不适合这个任务。你有两个不同的问题。Mobilenet是为Imagenet图像制作的,它是224x224图像,有3个颜色通道,而MNIST数据集是28x28个图像,有一个颜色通道。可以在RGB中重复颜色通道:
但在此之前,您需要调整图像大小。例如,可以使用
^{pr2}$PIL
调整图像大小:这里有些小细节你应该自己弄清楚。例如,
dtype
的图像,如果您已经从数据集中加载为numpy。您可能需要使用np.uint8()
将numpy类型转换为整数。在试试这个,
x = np.stack((x,)*3, axis=-1)
。有关详细信息,请参阅此链接:https://github.com/malnakli/ML/blob/master/tf_serving_keras_mobilenetv2/main.ipynbMobilenet v2需要RGB。您还可以使用PIL中的convert函数。在
试试这个:
文档在这里:https://pillow.readthedocs.io/en/stable/reference/Image.html
相关问题 更多 >
编程相关推荐