为什么 Dense 层的形状是 (None, 50176)?

Why is the Dense layer getting a shape of (None, 50176)?

我正在构建一个可以检测数字和加减符号的 CNN。

我正在关注 CNN 上的 DeepLizards 教程。

我想使用我自己的测试图像,但我在进行预测时总是遇到此错误:

ValueError: Input 0 of layer dense_10 is incompatible with the layer: expected axis -1 of input shape to have value 53760 but received input with shape (None, 50176)

我使用 Keras 的图像生成器通过 VGG16 模型的预处理功能创建了我的训练集和测试集。

train_batch = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input).flow_from_directory(directory=train_path,target_size=(224,244),classes=['+','-','0','1','2','3','4','5','6','7','8','9'],batch_size=30)

test_batch = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input).flow_from_directory(directory=test_path,target_size=(224,244),classes=['+','-','0','1','2','3','4','5','6','7','8','9'],batch_size=30)

我更新的模型:

def model():
    model = Sequential()
    
    model.add(Conv2D(filters=32,kernel_size=(3,3),padding='same',input_shape=(224,244,3)))
    model.add(LeakyReLU(alpha=0.1))
    model.add(MaxPooling2D(pool_size=(2,2),strides=2))
    
    model.add(Conv2D(filters=16,kernel_size=(3,3),padding='same'))
    model.add(MaxPooling2D(pool_size=(2,2),strides=2))
    model.add(LeakyReLU(alpha=0.1))
    
    model.add(Conv2D(filters=64,kernel_size=(3,3),padding='same'))
    model.add(LeakyReLU(alpha=0.1))
    model.add(MaxPooling2D(pool_size=(2,2),strides=2))
    
    model.add(Flatten())
    
    model.add(Dense(units=1024))
    model.add(Dropout(0.7))
    
    model.add(Dense(units=12,activation='softmax'))
    
    model.compile(optimizer=Adam(0.0001),loss='binary_crossentropy',metrics=['accuracy'])
    
    return model

然后我预处理我的测试图像。

def preprocess(IMG):
    IMG = cv2.imread(IMG)
    IMG = cv2.resize(IMG,(244,244))
    IMG = np.expand_dims(IMG,axis=0)/255
    return IMG

我将图像的大小调整为 (244,244,3) 的形状并扩展尺寸以匹配我的模型中给定的输入。

谁能解释我哪里出错了,我该如何解决?

有人还可以解释我如何将相同的预处理功能应用于我的测试图像吗?

提前致谢。

我的模型有点乱。除了使用 LeakyReLU 代替 ReLU 和使用 softmax 函数代替 sigmoid 函数之外,我没有做任何重要的事情,因为我有超过 2 类。 我的模型摘要是

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 224, 244, 32)      896       
_________________________________________________________________
leaky_re_lu (LeakyReLU)      (None, 224, 244, 32)      0         
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 112, 122, 32)      0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 112, 122, 16)      4624      
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 56, 61, 16)        0         
_________________________________________________________________
leaky_re_lu_1 (LeakyReLU)    (None, 56, 61, 16)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 56, 61, 64)        9280      
_________________________________________________________________
leaky_re_lu_2 (LeakyReLU)    (None, 56, 61, 64)        0         
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 28, 30, 64)        0         
_________________________________________________________________
flatten (Flatten)            (None, 53760)             0         
_________________________________________________________________
dense (Dense)                (None, 1024)              55051264  
_________________________________________________________________
dropout (Dropout)            (None, 1024)              0         
_________________________________________________________________
dense_1 (Dense)              (None, 12)                12300     
=================================================================
Total params: 55,078,364
Trainable params: 55,078,364
Non-trainable params: 0
_________________________________________________________________
model.add(Conv2D(filters=32,kernel_size=(3,3),activation='relu',padding='same',input_shape=(224,244,3)))

错误在这里。你有 (224, 244) 而不是 (244, 244)。

IMG = cv2.resize(IMG,(224,244))

您的网络输入是 (224,244,32),但图像大小是 (244,244),这是不合适的。将调整大小参数 x 从 244 更改为 224。此外,您的网络输入包含 32 个通道输入。如果是3通道图像数据改变网络输入为(224,244,3).

网络的输入和数据的大小必须是相同的维度。