gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:17:05 27 4
gpt4 key购买 nike

我正在构建一个可以检测数字以及加法和减法符号的 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 函数之外,我没有做任何主要的事情,因为我有两个以上的类。我的模型摘要是

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
_________________________________________________________________

最佳答案

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

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

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

关于python - 为什么 Dense 层的形状为 (None, 50176)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67012816/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com