Python、keras、卷积自动编码器

标签 python keras conv-neural-network autoencoder

我正在尝试在 keras 中创建我的第一个卷积自动编码器,但我在层输出形状方面遇到问题。这是我的代码:

input_img = Input(shape=X_train.shape[1:])

x = Conv2D(32, (3, 3), activation='relu', padding='same', kernel_constraint=maxnorm(3))(input_img)
x = MaxPooling2D(pool_size=(2, 2), padding='same')(x)
x = Conv2D(16, (3, 3), activation='relu', padding='same', kernel_constraint=maxnorm(3))(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)

x = Conv2D(16, (3, 3), activation='relu', padding='same', kernel_constraint=maxnorm(3))(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same', kernel_constraint=maxnorm(3))(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)

autoencoder = Model(input_img, decoded)
print(autoencoder.summary())
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
autoencoder.fit(X_train, X_train, epochs=50, batch_size=32)

结果:

_________________________________________________________________
Layer (type)                 Output Shape              Param   
=================================================================
input_87 (InputLayer)        (None, 32, 32, 3)         0         
_________________________________________________________________
conv2d_327 (Conv2D)          (None, 32, 32, 3)         9248      
_________________________________________________________________
max_pooling2d_136 (MaxPoolin (None, 32, 16, 2)         0         
_________________________________________________________________
conv2d_328 (Conv2D)          (None, 16, 16, 2)         4624      
_________________________________________________________________
max_pooling2d_137 (MaxPoolin (None, 16, 8, 1)          0         
_________________________________________________________________
conv2d_329 (Conv2D)          (None, 16, 8, 1)          2320      
_________________________________________________________________
up_sampling2d_124 (UpSamplin (None, 16, 16, 2)         0         
_________________________________________________________________
conv2d_330 (Conv2D)          (None, 32, 16, 2)         4640      
_________________________________________________________________
up_sampling2d_125 (UpSamplin (None, 32, 32, 4)         0         
_________________________________________________________________
conv2d_331 (Conv2D)          (None, 1, 32, 4)          289       
=================================================================
Total params: 21,121
Trainable params: 21,121
Non-trainable params: 0
_________________________________________________________________
None

当然还有错误:

ValueError: Error when checking target: expected conv2d_331 to have shape (None, 1, 32, 4) but got array with shape (50000, 32, 32, 3)

你知道我做错了什么吗?为什么最后一个 UpSampling2D 返回该形状?

最佳答案

因此,您的 keras 似乎已将其图像尺寸设置为 channel_first (可能还有 Theano 作为后端),这意味着输入的最后两个维度被视为空间维度,而不是第二个和第三个维度。另一件事是您的输出应该有 3 过滤器而不是 1 因为这最终会导致目标维度不匹配。总结一下:

  1. 为了正确设置您的输入,您需要切换到 tensorflow 并将 channel 顺序更改为 channel_last 或通过以下方式转置您的输入:

    X = X.transpose([0, 3, 1, 2])
    
  2. 更改以下行:

    decoded = Conv2D(3, (3, 3), activation='sigmoid', padding='same')(x)
    

关于Python、keras、卷积自动编码器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44466157/

相关文章:

python - 从 Haxe 内部调用外部 Python 类函数

python-3.x - RuntimeError : You must compile your model before using it

machine-learning - 如何用更少的图像在 CNN 上获得更高的准确率

classification - 如何调整分类任务中标签的分级偏差?

python - 平均 Panda 数据帧中的连续 2 行

Python:最接近目标的二的幂

python - 神经网络的输入大小不匹配

tensorflow - tf.keras.layers.Concatenate() 使用列表但在张量元组上失败

python - 咖啡乐网 : Difference between `solver.step(1)` and `solver.net.forward()`

python - 一次生成一个数字的代码