[英]ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [None, 32, 32]
import imageio
import glob
import numpy as np
import tensorflow as tf
from tensorflow.keras import datasets, layers, models
import matplotlib.pyplot as plt
trainImages = []
for imagePath in glob.glob('C:/Users/razva/*.png'):
image = imageio.imread(imagePath)
trainImages.append(image)
trainImages = np.array(trainImages)
f = open('C:/Users/razva/train.txt')
trainLabels = f.readlines()
for i in range(len(trainLabels)):
trainLabels[i] = int(trainLabels[i][11])
trainLabels = np.array(trainLabels)
validationImages = []
for imagePath in glob.glob('C:/Users/razva/*.png'):
image = imageio.imread(imagePath)
validationImages.append(image)
validationImages = np.array(validationImages)
f = open('C:/Users/razva/validation.txt')
validationLabels = f.readlines()
for i in range(len(validationLabels)):
validationLabels[i] = int(validationLabels[i][11])
validationLabels = np.array(validationLabels)
mean_image = np.mean(trainImages, axis = 0)
sd = np.std(trainImages)
trainImages = (trainImages - mean_image) / sd
mean_image1 = np.mean(validationImages, axis = 0)
sd1 = np.std(validationImages)
validationImages = (validationImages - mean_image1) / sd1
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10))
model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy'])
history = model.fit(trainImages, trainLabels, epochs=10, validation_data=(validationImages, validationLabels))
I have this cnn for image classification, trainImages and trainLabel(from 0 to 8) are training data, validationImages and validationLabels are for test.我有这个用于图像分类的 cnn,trainImages 和 trainLabel(从 0 到 8)是训练数据,validationImages 和 validationLabel 用于测试。 Images are 32 * 32. I cant make this algorithm work, tell me if u observe more errors pls.
图像是 32 * 32。我不能让这个算法工作,请告诉我你是否观察到更多错误。
I can't tell exactly where the problem is since I have no access to the loaded images, but the issue is that you are providing samples without the "channel" axis which in the specified input_shape=(32, 32, 3)
is indicated as having size 3. Each sample (image) must have 3 dimensions (width, height, channels) but, on the contrary, you are passing samples with just 2 dimensions (width and height).我无法准确说出问题出在哪里,因为我无法访问加载的图像,但问题是您提供的样本没有指定
input_shape=(32, 32, 3)
中的“通道”轴指示大小为 3。每个样本(图像)必须有 3 个维度(宽度、高度、通道),但相反,您传递的样本只有 2 个维度(宽度和高度)。
This is most likely due to the fact that you are probably loading gray-scale images with just one channel, which is not explicitly assigned an axis by numpy.这很可能是因为您可能正在加载只有一个通道的灰度图像,numpy 没有明确指定一个轴。 If this is the case, make sure that both trainImages and validationImages have shape (32, 32, 1), otherwise just expand the last dimension with
np.expand_dims(_trainImages_, axis=-1)
(same for the validation set) before feeding them to the model.如果是这种情况,请确保trainImages和validationImages都具有形状 (32, 32, 1),否则只需在喂食前使用
np.expand_dims(_trainImages_, axis=-1)
扩展最后一个维度(与验证集相同)他们到 model。 Accordingly, adjust to (32, 32, 1) the input_shape in the first Conv2D layer.因此,将第一个 Conv2D 层中的 input_shape 调整为 (32, 32, 1)。
Hope it helps, otherwise let me have further details.希望它有所帮助,否则让我有更多的细节。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.