神经网络模型的训练输入得到错误的形状 ((预期形状=(None, 222, 222, 3), 发现形状=(None, None, 224, 224, 3) ))

问题描述 投票:0回答:1
所以我正在尝试使用 Oxford 102 花数据集训练神经网络模型。 但由于某种未知的原因,我在训练时使用 model.fit 出现错误,以下是与代码相关的部分:

# Define the path to the dataset directory # dataset_dir = "/content/drive/MyDrive/jpg" # Define batch size and image size batch_size = 32 img_size = (224, 224) # Use image_dataset_from_directory to load the dataset train_dataset = tf.keras.utils.image_dataset_from_directory( dataset_dir, labels="inferred", label_mode="categorical", # Change to "binary" for binary classification image_size=img_size, batch_size=batch_size, validation_split=0.2, subset="training", seed=1337, ) valid_dataset = tf.keras.utils.image_dataset_from_directory( dataset_dir, labels="inferred", label_mode="categorical", image_size=img_size, batch_size=batch_size, validation_split=0.2, subset="validation", seed=1337, ) train_dataset = train_dataset.map(lambda x, y: (x / 255.0, y)) # Normalize pixel values for Train dataset valid_dataset = valid_dataset.map(lambda x, y: (x / 255.0, y)) # Normalize pixel values for Validation dataset # Batch the datasets train_dataset = train_dataset.batch(batch_size) valid_dataset = valid_dataset.batch(batch_size)
这是该单元格的输出:

Found 8189 files belonging to 1 classes. Using 6552 files for training. Found 8189 files belonging to 1 classes. Using 1637 files for validation.
使用此模型:

# Model ( Sequential Model ) model = models.Sequential() # Convolutional base ( Using Convolutional Neural network to extract information from images) model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(224, 224, 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(128, (3, 3), activation='relu')) model.add(layers.MaxPooling2D((2, 2))) # Flatten the convolutional base model.add(layers.Flatten()) # Dense layers to extract features from CNN output # We use a Dropout of 50% to prevent the model from over-fitting and for regularization model.add(layers.Dense(256, activation='relu')) model.add(layers.Dropout(0.5)) model.add(layers.Dense(102, activation='softmax')) # 102 classes for the flowers dataset # Compile the model model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) # Display the model summary model.summary()
模型总结输出:

_________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d_6 (Conv2D) (None, 222, 222, 32) 896 max_pooling2d_6 (MaxPoolin (None, 111, 111, 32) 0 g2D) conv2d_7 (Conv2D) (None, 109, 109, 64) 18496 max_pooling2d_7 (MaxPoolin (None, 54, 54, 64) 0 g2D) conv2d_8 (Conv2D) (None, 52, 52, 128) 73856 max_pooling2d_8 (MaxPoolin (None, 26, 26, 128) 0 g2D) flatten_2 (Flatten) (None, 86528) 0 dense_4 (Dense) (None, 256) 22151424 dropout_2 (Dropout) (None, 256) 0 dense_5 (Dense) (None, 102) 26214 ================================================================= Total params: 22270886 (84.96 MB) Trainable params: 22270886 (84.96 MB) Non-trainable params: 0 (0.00 Byte) _________________________________________________________________
这个列车单元将导致以下错误:

epochs = 10 Train = model.fit( train_dataset, epochs = epochs, validation_data=valid_dataset )
运行训练单元后得到的错误:

ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 222, 222, 3), found shape=(None, None, 224, 224, 3)
我看过一些类似的帖子引用相同的问题,但没有一个得到正确或体面的答案,希望这个问题得到解决,以便人们稍后使用它。

tensorflow keras deep-learning neural-network conv-neural-network
1个回答
0
投票
你应该在第一个卷积层中

layers.Conv2D(..., padding = 'same')

。该层的默认值为“有效”。 (参考 -> 
https://keras.io/api/layers/convolution_layers/convolution2d/

_________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d_22 (Conv2D) (None, 224, 224, 32) 896 max_pooling2d_21 (MaxPooli (None, 112, 112, 32) 0 ng2D)
    
© www.soinside.com 2019 - 2024. All rights reserved.