如何在 python 中将我的图像更改为所需的形状?

How to change my image into a desired shape in python?

我正在为 MNIST 时尚数据集开发 CNN 模型。我创建了一个成功的 CNN 模型。但我想测试该模型对我从互联网上下载的另一张图片的分类。

我所有的训练集和测试集都是形状 (28, 28, 1)。但是现在对于我想要预测的图像,我将它的大小调整为 (28,28) 并使用

将其变成一个 RGB 通道
cv2.cvtColor(load_img_rz, cv2.COLOR_BGR2GRAY)

现在图像的形状是(28, 28)。我试图将它输入到模型中,但它显示错误

ValueError: Input 0 of layer sequential_6 is incompatible with the layer: : expected min_ndim=4, 
found ndim=3. Full shape received: (None, 28, 3)

我认为形状是问题所在。那么,如果这是问题所在,我该如何将其转换为形状 (28,28,1)。

CNN 在单通道 RGB 中的效果是否优于 3 通道 RGB?

因为你没有包含你的代码,我假设你的输入层有问题。因此,您需要先在输入层中指定单元数和 inpt dim:

model = Sequential()
model.add(Dense(X.shape[1], activation='something you desired', input_dim=X.shape[1]))

等等

很难理解你在处理什么以及你想要实现什么,因为你没有指定/共享任何东西,甚至没有代码。

对我来说深度学习中一个非常有用的命令是 expand_dims from numpy.

your_image.shape
>>> (28, 28)

your_new_array = np.expand_dims(your_image, axis=-1)
your_new_array.shape
>>> (28, 28, 1)

您可以尝试使用 axis 参数来更好地了解这里发生的事情。