我目前正在从事一个机器学习项目,该项目涉及在图像数据集上训练卷积神经网络(CNN)。我正在使用 Python 和一个流行的深度学习库来构建和训练 CNN。但是,我在训练过程中遇到了错误,我正在寻求帮助来解决它。
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
# Load the image dataset (example: Cats vs. Dogs)
# Assuming the dataset is organized with 'train' and 'test' directories
# And each directory contains 'cat' and 'dog' subdirectories with corresponding images
train_data_dir = 'path_to_train_directory'
test_data_dir = 'path_to_test_directory'
image_size = (150, 150)
batch_size = 32
# Data augmentation and preprocessing for training set
train_datagen = tf.keras.preprocessing.image.ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True
)
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=image_size,
batch_size=batch_size,
class_mode='binary'
)
# Build the CNN architecture
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 3)))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# Compile the model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Train the model
model.fit(train_generator, epochs=10) # Error occurs in this line
ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=4, found ndim=5. Full shape received: (None, 150, 150, 3, 32)
错误信息中已明确错误
ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim = 4, found ndim = 5. Full shape received: (None, 150, 150, 3, 32)
张量流 CNN 层中的输入形状应为
(Batch,h,w,channels)
。
因为您使用了增强层,所以它将尺寸更改为 (batch_size, height, width, channels, augmentations)
修改代码以按batch_size划分数据
STEP_SIZE_TRAIN=train_generator.n//train_generator.batch_size
model.fit(train_generator, epochs=10, steps_per_epoch=STEP_SIZE_TRAIN)
Step_per_epoch 是您告诉模型完成一个 epoch 需要多少批次(步骤)的方式。这将解决维度问题并允许您的 CNN 在图像数据集上正确训练。