现在我正在和CNN学习AutoEncoder。为了研究,我为MNIST数据创建了一个模型。但我无法正确设置Conv2d
的输出调整。请看下面的模型图像。虽然我希望第一个Conv2d
输出应为(None, 16, 28, 28)
,但实际输出为(None, 1, 28, 16)
。关于文档,我的代码看起来并不坏。
https://keras.io/layers/convolutional/#conv2d
你能找到我的代码的任何错误吗?
我的环境
代码
from keras.layers import Input, Convolution2D, MaxPool2D, UpSampling2D, Conv2D
from keras.models import Model
input_img = Input(shape=(1, 28, 28))
x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPool2D((2,2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPool2D((2,2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPool2D((2,2), padding='same')(x)
x = Conv2D(8, (3,3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2,2))(x)
x = Conv2D(8, (3,3), activation='relu', padding='same')(x)
x = UpSampling2D((2,2))(x)
x = Conv2D(16, (3,3), activation='relu')(x)
x = UpSampling2D((2,2))(x)
decoded = Conv2D(1, (3,3), activation='sigmoid', padding='same')(x)
autoencoder= Model(input_img, decoded)
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')
from keras.utils import plot_model
plot_model(autoencoder, to_file="architecture.png", show_shapes=True)
更新
我添加了autoencoder.summary()
。所以我的问题是为什么CNN的第一个输出不是(None, 16, 28, 28)
? (None, 1, 28, 16)
不是我的期望。
Layer (type) Output Shape Param #
=================================================================
conv2d_181 (Conv2D) (None, 1, 28, 16) 4048
_________________________________________________________________
max_pooling2d_82 (MaxPooling (None, 1, 14, 16) 0
_________________________________________________________________
conv2d_182 (Conv2D) (None, 1, 14, 8) 1160
_________________________________________________________________
max_pooling2d_83 (MaxPooling (None, 1, 7, 8) 0
_________________________________________________________________
conv2d_183 (Conv2D) (None, 1, 7, 8) 584
_________________________________________________________________
max_pooling2d_84 (MaxPooling (None, 1, 4, 8) 0
_________________________________________________________________
conv2d_184 (Conv2D) (None, 1, 4, 8) 584
_________________________________________________________________
up_sampling2d_72 (UpSampling (None, 2, 8, 8) 0
_________________________________________________________________
conv2d_185 (Conv2D) (None, 2, 8, 8) 584
_________________________________________________________________
up_sampling2d_73 (UpSampling (None, 4, 16, 8) 0
_________________________________________________________________
conv2d_186 (Conv2D) (None, 4, 16, 16) 1168
_________________________________________________________________
up_sampling2d_74 (UpSampling (None, 8, 32, 16) 0
_________________________________________________________________
conv2d_187 (Conv2D) (None, 8, 32, 1) 145
=================================================================
Total params: 8,273.0
Trainable params: 8,273.0
Non-trainable params: 0.0
_________________________________________________________________
Updated2
我的input_img是为Theano设计的。所以我必须改变如下。否则我在image_dim_ordering
~/.keras/keras.json
# Theano style
input_img = Input(shape=(1, 28, 28))
# Tensorflow style
input_img = Input(shape=(28, 28, 1))
答案 0 :(得分:1)
这是图像排序的常见问题。 Theano将通道维度放在形状数组的第二个元素中,如(samples, channels, width, height)
,而TensorFlow将通道维度放在末尾,如(samples, width, height, channels)
。您正在使用Theano订购,但后端是Tensorflow。
只需更改形状以匹配正确的顺序即可。或者,您可以在~/.keras/keras.json
文件中将image_dim_ordering更改为“th”。