我正在尝试创建我的第一个卷积自动编码器在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)
结果:
^{pr2}$当然还有错误:
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
(可能还有{3
过滤器,而不是{为了正确设置您的输入,您需要切换到
tensorflow
并将通道顺序更改为channel_last
,或者通过以下方式将输入转置:更改以下行:
相关问题 更多 >
编程相关推荐