keras 具有非方形输入形状的CNN自动编码器

sy5wg1nm  于 2023-01-09  发布在  其他
关注(0)|答案(1)|浏览(168)

我已经实现了一个CNN自动编码器,它没有正方形形状的输入。我有点困惑。它是强制性的,有一个正方形形状的输入自动编码器?每个2D图像有一个形状的800x20。我已经根据形状馈送数据。但不知何故,形状不匹配时,模型是建立的。我已经分享了模型的代码和下面的错误消息。需要你的专业建议谢谢。

x = Input(shape=(800, 20,1)) 

# Encoder
conv1_1 = Conv2D(16, (3, 3), activation='relu', padding='same')(x)
pool1 = MaxPooling2D((2, 2), padding='same')(conv1_1)
conv1_2 = Conv2D(8, (3, 3), activation='relu', padding='same')(pool1)
pool2 = MaxPooling2D((2, 2), padding='same')(conv1_2)
conv1_3 = Conv2D(8, (3, 3), activation='relu', padding='same')(pool2)
h = MaxPooling2D((2, 2), padding='same')(conv1_3)

# Decoder
conv2_1 = Conv2D(8, (3, 3), activation='relu', padding='same')(h)
up1 = UpSampling2D((2, 2))(conv2_1)
conv2_2 = Conv2D(8, (3, 3), activation='relu', padding='same')(up1)
up2 = UpSampling2D((2, 2))(conv2_2)
conv2_3 = Conv2D(16, (3, 3), activation='relu', padding='same')(up2)
up3 = UpSampling2D((2, 2))(conv2_3)
r = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(up3)

model = Model(inputs=x, outputs=r)
model.compile(optimizer='adadelta', loss='binary_crossentropy', metrics=['accuracy'])

results = model.fit(x_train, x_train, epochs = 500, batch_size=16,validation_data= (x_test, x_test))

以下是错误:

ValueError: logits and labels must have the same shape ((16, 800, 24, 1) vs (16, 800, 20, 1))
qyswt5oh

qyswt5oh1#

如错误跟踪所示,您在尝试使用此自动编码器时遇到的问题是,模型期望输入形状为(?, 800, 20, 1),但输出形状为(?, 796, 20, 1)
(800, 20, 1). (检查模型摘要的输入和输出形状!)

我的建议-

1.我已经修正了padding = 'same',并重新调整了形状,使输入Tensor形状和输出相同。检查我修改过的内核大小,以获得所需的输出形状。
1.最重要的是,对于一个堆叠的conv编码器-解码器架构(如自动编码器),建议您将空间信息转换为后续层的特征Map/过滤器/通道。现在您从8个过滤器开始,然后在编码器中移动到4个。它应该更像4->8->16。检查此图以参考直觉。

根据上述建议,我在此进行了修改。

x = Input(shape=(800, 20,1)) 

# Encoder
conv1_1 = Conv2D(4, (3, 3), activation='relu', padding='same')(x)
pool1 = MaxPooling2D((2, 2), padding='same')(conv1_1)
conv1_2 = Conv2D(8, (3, 3), activation='relu', padding='same')(pool1)
pool2 = MaxPooling2D((2, 2), padding='same')(conv1_2)
conv1_3 = Conv2D(16, (3, 3), activation='relu', padding='same')(pool2)
h = MaxPooling2D((2, 1), padding='same')(conv1_3) #<------

# # Decoder

conv2_1 = Conv2D(16, (3, 3), activation='relu', padding='same')(h)
up1 = UpSampling2D((2, 2))(conv2_1)
conv2_2 = Conv2D(8, (3, 3), activation='relu', padding='same')(up1)
up2 = UpSampling2D((2, 2))(conv2_2)
conv2_3 = Conv2D(4, (3, 3), activation='sigmoid', padding='same')(up2) #<--- ADD PADDING HERE
up3 = UpSampling2D((2, 1))(conv2_3)
r = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(up3)

model = Model(inputs=x, outputs=r)
model.compile(optimizer='adadelta', loss='binary_crossentropy', metrics=['accuracy'])
model.summary()
Model: "model_13"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_16 (InputLayer)        [(None, 800, 20, 1)]      0         
_________________________________________________________________
conv2d_101 (Conv2D)          (None, 800, 20, 4)        40        
_________________________________________________________________
max_pooling2d_45 (MaxPooling (None, 400, 10, 4)        0         
_________________________________________________________________
conv2d_102 (Conv2D)          (None, 400, 10, 8)        296       
_________________________________________________________________
max_pooling2d_46 (MaxPooling (None, 200, 5, 8)         0         
_________________________________________________________________
conv2d_103 (Conv2D)          (None, 200, 5, 16)        1168      
_________________________________________________________________
max_pooling2d_47 (MaxPooling (None, 100, 5, 16)        0         
_________________________________________________________________
conv2d_104 (Conv2D)          (None, 100, 5, 16)        2320      
_________________________________________________________________
up_sampling2d_43 (UpSampling (None, 200, 10, 16)       0         
_________________________________________________________________
conv2d_105 (Conv2D)          (None, 200, 10, 8)        1160      
_________________________________________________________________
up_sampling2d_44 (UpSampling (None, 400, 20, 8)        0         
_________________________________________________________________
conv2d_106 (Conv2D)          (None, 400, 20, 4)        292       
_________________________________________________________________
up_sampling2d_45 (UpSampling (None, 800, 20, 4)        0         
_________________________________________________________________
conv2d_107 (Conv2D)          (None, 800, 20, 1)        37        
=================================================================
Total params: 5,313
Trainable params: 5,313
Non-trainable params: 0
_________________________________________________________________

为了显示模型现在可以运行了,让我创建一个随机数据集,它与您正在查看的数据集相同。
一个二个一个一个

相关问题