层 conv2d 的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 1,但收到的输入形状为 [None, 64, 64, 3]

时间:2021-05-16 05:14:36

标签: python tensorflow machine-learning keras deep-learning

我正在 EMNIST 上运行一个模型(128x128 灰度图像),但我无法理解如何将数据正确加载到 Tensorflow 以进行建模。

我一直在关注 TensorFlow 提供的花朵示例 (https://www.tensorflow.org/hub/tutorials/image_feature_vector) 除了 CNN 结构,直到突然 model.fit() 失败并出现错误
{{1} }

加载数据集

Input 0 of layer conv2d_120 is incompatible with the layer: expected axis -1 of input shape to have value 1 but received input with shape [None, 64, 64, 3]
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential

batch_size = 32
image_w = 64
image_h = 64
seed = 123
<块引用>

找到属于 10 个类的 10160 个文件。
使用 8128 个文件进行训练。
找到属于 10 个类的 10160 个文件。
使用 2032 文件进行验证。

确认数据加载正确

data_dir = 'B:\Datasets\EMNIST Digital Number & Digits\OriginalDigits'

train_df = tf.keras.preprocessing.image_dataset_from_directory(
  data_dir,
  validation_split=0.2,
  subset="training",
  seed=seed,
  image_size=(image_w,image_h),
  batch_size=batch_size)

val_df = tf.keras.preprocessing.image_dataset_from_directory(
  data_dir,
  validation_split=0.2,
  subset="validation", #Same exact code block ... this is the only line of difference
  seed=seed,
  image_size=(image_w,image_h),
  batch_size=batch_size)

将数据集处理成 tf.data.Dataset 对象

import matplotlib.pyplot as plt
plt.figure(figsize=(10, 10))
for images, labels in train_df.take(1): #Take subsets the dataset into at most __1__ element (Seems to randomly create it)
    for i in range(9):
        ax = plt.subplot(3, 3, i + 1)
        plt.imshow(images[i].numpy().astype("uint8"))
        plt.title(labels[i].numpy().astype("str"))
        plt.axis("off")
<块引用>

['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] 10

class_labels = train_df.class_names
num_classes = len(class_labels)
print(class_labels,num_classes)

定义模型

AUTOTUNE = tf.data.experimental.AUTOTUNE

train_df_modeling = train_df.cache().shuffle(len(train_df)) #Load training data into memory cache + shuffle all 10160 images
val_df_modeling = val_df.cache().shuffle(len(train_df)) #Load validation data into memory cache
<块引用>

模型:“顺序”
_________________________________________________________________
层(类型)输出形状参数#
================================================== ================
重新缩放 (Rescaling) (None, 64, 64, 1) 0
_________________________________________________________________
conv2d (Conv2D) (无, 64, 64, 64) 640
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 32, 32, 64) 0
_________________________________________________________________
conv2d_1 (Conv2D)(无、32、32、128)73856
_________________________________________________________________
conv2d_2 (Conv2D)(无、32、32、128)147584
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 16, 16, 128) 0
_________________________________________________________________
conv2d_3 (Conv2D)(无、16、16、128)147584
_________________________________________________________________
conv2d_4 (Conv2D) (None, 16, 16, 128) 147584
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 8, 8, 128) 0
_________________________________________________________________
展平(Flatten)(无,8192)0
_________________________________________________________________
密集(Dense)(无,256)2097408
_________________________________________________________________
辍学(辍学)(无,256)0
_________________________________________________________________
密集_1(密集)(无,128)32896
_________________________________________________________________
dropout_1(辍学)(无,128)0
_________________________________________________________________
密集_2(密集)(无,64)8256
_________________________________________________________________
dropout_2(辍学)(无,64)0
_________________________________________________________________
密集_3(密集)(无,10)650
================================================== ================
总参数:2,656,458
可训练参数:2,656,458
不可训练的参数:0


训练模型

#Model from https://www.kaggle.com/henseljahja/simple-tensorflow-cnn-98-8
model = keras.models.Sequential([

    layers.experimental.preprocessing.Rescaling(1./255, input_shape=(image_h, image_w, 1)), #(64,64,1)
    layers.Conv2D(64, 7, padding='same', activation='relu'),    
    layers.GaussianNoise(0.2),
    layers.MaxPooling2D(pool_size=2),
    layers.Conv2D(filters=128, kernel_size=3, activation='relu', padding="SAME"),
    layers.Conv2D(filters=128, kernel_size=3, activation='relu', padding="SAME"),
    layers.MaxPooling2D(pool_size=2),
    layers.Conv2D(filters=128, kernel_size=3, activation='relu', padding="SAME"),
    layers.Conv2D(filters=128, kernel_size=3, activation='relu', padding="SAME"),
    layers.MaxPooling2D(pool_size=2),
    layers.Flatten(),
    layers.Dense(units=256, activation='relu'),
    layers.Dropout(0.5),
    layers.Dense(units=128, activation='relu'),
    layers.Dropout(0.5),
    layers.Dense(units=64, activation='relu'),
    layers.Dropout(0.5),    
    keras.layers.Dense(num_classes, activation='softmax'), #10 outputs [0,1,2,3,4,5,6,7,8,9]
])

model.summary()
<块引用>

ValueError: 层 conv2d 的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 1 但接收到形状为 [None, 64, 64, 3] 的输入

我知道我的问题与形状有关,并且 [None, 64, 64, 3] 是 [batch_size, width, height, channels] 但我有以下问题:

  1. 为什么它期望输入形状为 model.compile( loss="sparse_categorical_crossentropy", optimizer = 'nadam', metrics=['accuracy'] ) result = model.fit(train_df_modeling, validation_data=val_df_modeling, epochs=20, verbose=1) ? Conv2D 层不应该期待图像吗?
  2. 为什么我的输入有 3 个通道?我告诉它输入只有 1 个通道。
    注意:尝试删除重新缩放层并简单地将 Conv2D 作为初始层仍然会给出相同的错误消息,期望值为 1 但得到 64x64x3

1 个答案:

答案 0 :(得分:0)

嗯……在输入关于我遇到的问题的最后一部分的过程中,我找到了问题 2 的解决方案。

我的数据(虽然是灰度数据)被 Tensorflow 读取为 RGB,因为我从未指定。

解决方案

读取灰度数据

文档:https://www.tensorflow.org/api_docs/python/tf/keras/preprocessing/image_dataset_from_directory

感兴趣的参数:color_mode='grayscale'

修改我的代码以使其正常工作:

只需要更改 1 块代码(2 个变量)

data_dir = 'B:\Datasets\EMNIST Digital Number & Digits\OriginalDigits'

train_df = tf.keras.preprocessing.image_dataset_from_directory(
  data_dir,
  validation_split=0.2,
  subset="training",
  seed=seed,
  image_size=(image_w,image_h),
  batch_size=batch_size,
  color_mode='grayscale') #<---- This is was the missing link

val_df = tf.keras.preprocessing.image_dataset_from_directory(
  data_dir,
  validation_split=0.2,
  subset="validation",
  seed=seed,
  image_size=(image_w,image_h),
  batch_size=batch_size,
  color_mode='grayscale') #<---- This is was the missing link

虽然这个解决方案修复了模型并允许代码执行......有人能回答问题 #1 吗?我仍然很好奇为什么它认为它需要输入 have value 1当我认为输入应该是图像时。