简体   繁体   English

ValueError:层顺序的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=3。 收到的完整形状:[None, 32, 32]

[英]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.如果是这种情况,请确保trainImagesvalidationImages都具有形状 (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.

相关问题 ValueError:layersequence_4 的输入 0 与 layer 不兼容::预期 min_ndim=4,发现 ndim=3。 收到的完整形状:(32、224、3) - ValueError: Input 0 of layer sequential_4 is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: (32, 224, 3) ValueError:layersequential_32 的输入 0 与 layer 不兼容::预期 min_ndim=3,发现 ndim=2。 收到的完整形状:[无,256] - ValueError: Input 0 of layer sequential_32 is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: [None, 256] ValueError: 层序列 9 的输入 0 与层不兼容:预期 min_ndim=4,发现 ndim=3。 收到的完整形状:[无,无,无] - ValueError: Input 0 of layer sequential_9 is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [None, None, None] ValueError:layersequential_1 的输入 0 与 layer 不兼容::预期 min_ndim=4,发现 ndim=3。 收到的完整形状:[无、256、256] - ValueError: Input 0 of layer sequential_1 is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [None, 256, 256] ValueError:层顺序的输入 0 与层不兼容:预期 ndim=4,发现 ndim=3。 收到的完整形状:[32, 64, 3] - ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: [32, 64, 3] ValueError:顺序层的输入 0 与层不兼容::预计 min_ndim=4,发现 ndim=3。 收到完整形状:[8, 28, 28] - ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [8, 28, 28] 层序的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=2。 收到的完整形状:(无,1) - Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=2. Full shape received: (None, 1) ValueError: 层顺序的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=2。 收到完整形状:[无,1] - ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=2. Full shape received: [None, 1] ValueError:layersequential_5 的输入 0 与 layer 不兼容::预期 min_ndim=4,发现 ndim=2。 收到的完整形状:[无,953] - ValueError: Input 0 of layer sequential_5 is incompatible with the layer: : expected min_ndim=4, found ndim=2. Full shape received: [None, 953] ValueError:顺序层的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=2。 收到完整形状:[无,2584] - ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=2. Full shape received: [None, 2584]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM