我正在尝试在Keras中构建深层卷积自动编码器,但它会不断输出错误的形状。
代码:
def build_network(input_shape):
input_input = Input(shape=input_shape)
#Encode
x = Conv2D(16, (3, 3), activation='relu', padding = 'same')(input_input)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
#Decode
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
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', padding='same')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
autoencoder = Model(input_input, decoded)
return autoencoder
if __name__ == "__main__":
print(build_network((1, 32, 32)).layers[-1].output)
我希望输出形状与输入形状相同,但对于(8, 32, 1)
,它应该是(1, 32, 32)
答案 0 :(得分:2)
尝试使用print(build_network((32,32,1)).layers[-1].output)
。或者,如果您要先使用频道,而不需要像这样更改模型,
def build_network(input_shape):
input_input = Input(shape=input_shape)
#Encode
x = Conv2D(16, (3, 3), activation='relu', padding = 'same')(input_input)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
#Decode
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D(size=(2, 2),data_format="channels_first")(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D(size=(2, 2),data_format="channels_first")(x)
x = Conv2D(16, (3, 3), activation='relu', padding='same')(x)
decoded = UpSampling2D(size=(2, 2),data_format="channels_first")(x)
# decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
autoencoder = Model(input_input, decoded)
return autoencoder
if __name__ == "__main__":
print(build_network((1, 32, 32)).layers[-1].output)
由于在UpSampling2D
中,默认值为“ channels_last”。