python - Keras 卷积自动编码器 : Layer Shapes

标签 python keras autoencoder

我有大约 70,000 个训练图像的列表,每个图像的形状(颜色 channel 数、高度宽度)= (3, 30, 30),以及大约 20,000 个测试图像。我的卷积自动编码器定义为:

 # Same as the code above, but with some params changed
# Now let's define the model. 

# Set input dimensions:
input_img = Input(shape=(3, 30, 30))

# Encoder: define a chain of Conv2D and MaxPooling2D layers
x = Convolution2D(128, 3, 3, 
                  activation='relu', 
                  border_mode='same')(input_img)
x = MaxPooling2D((2, 2), border_mode='same')(x)
x = Convolution2D(64, 3, 3, 
                  activation='relu', 
                  border_mode='same')(x)
x = MaxPooling2D((2, 2), border_mode='same')(x)
x = Convolution2D(64, 3, 3, 
                  activation='relu', 
                  border_mode='same')(x)
encoded = MaxPooling2D((2, 2), border_mode='same')(x)

# at this point, the representation is (8, 4, 4) i.e. 128-dimensional

# Decoder: a stack of Conv2D and UpSampling2D layers
x = Convolution2D(64, 3, 3, 
                  activation='relu', 
                  border_mode='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Convolution2D(64, 3, 3, 
                  activation='relu', 
                  border_mode='same')(x)
x = UpSampling2D((2, 2))(x)
x = Convolution2D(128, 3, 3, 
                  activation='relu')(x)
x = UpSampling2D((2, 2))(x)
decoded = Convolution2D(1, 3, 3, 
                        activation='sigmoid', 
                        border_mode='same')(x)

autoencoder2 = Model(input_img, decoded)
autoencoder2.compile(optimizer='adadelta', loss='mse')

这是 here 中的自动编码器。

它抛出一个错误:

Error when checking model target: expected convolution2d_14 to have shape (None, 1, 28, 28) but got array with shape (76960, 3, 30, 30)

这很奇怪,因为我已经明确地将指定的输入形状更改为 (3, 30, 30)。我是否缺少一些实现技术?

最佳答案

您忘记在解码器的最后一个卷积层中添加 border_mode='same' 。

关于python - Keras 卷积自动编码器 : Layer Shapes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39181976/

相关文章:

python - 如何在不硬编码维数的情况下枚举 ND 数组的所有元素?

nlp - 在 Keras 中实现 word2vec

c++ - 如何从 C++ 使用 Keras SavedModel

neural-network - 我可以在 Keras 中使用带有卷积神经网络的矩形图像吗?

tensorflow - 如何在 LSTM 变分自动编码器中将潜在向量传递给解码器

python - 找不到 Python 模块的正确目录

python - 如何根据 ID 和指向 df2 中列名称的值从另一个数据帧 (df2) 中的列获取值。 python / Pandas

python - 如何使用python发送rest API(Google Vision的API)请求?

tensorflow - 在编译模型之后或之前加载权重是否重要?

machine-learning - 如何使用掩蔽层来掩蔽 LSTM 自动编码器中的输入/输出?