如何使解码器的输出形状等于输入图像的形状?

时间:2019-05-11 11:33:44

标签: keras conv-neural-network autoencoder

我正在尝试找出:decoder.output_shape[1:]==IMG_SHAPE

我具有已编程的以下功能。当我尝试运行decoder.output_shape[1:]==IMG_SHAPE时,我发现我的图像形状和解码器输出形状不相同。

def build_deep_autoencoder(img_shape, code_size):
    """PCA's deeper brother."""
    H,W,C = img_shape

    # encoder
    encoder = keras.models.Sequential()
    encoder.add(L.InputLayer(img_shape))

    # numbers of output channels: 32, 64, 128, 256.
    encoder.add(L.Conv2D(filters=32, kernel_size=(3, 3), padding='same', activation='elu'))
    encoder.add(L.MaxPooling2D())

    encoder.add(L.Conv2D(filters=64, kernel_size=(3, 3), padding='same', activation='elu'))
    encoder.add(L.MaxPooling2D())

    encoder.add(L.Conv2D(filters=128, kernel_size=(3, 3), padding='same', activation='elu'))
    encoder.add(L.MaxPooling2D())

    encoder.add(L.Conv2D(filters=256, kernel_size=(3, 3), padding='same', activation='elu'))
    encoder.add(L.MaxPooling2D())

    encoder.add(L.Flatten())                  #flatten image to vector
    encoder.add(L.Dense(code_size, activation = 'elu')) 


    # decoder
    decoder = keras.models.Sequential()
    decoder.add(L.InputLayer((code_size,)))

    # numbers of output channels: 128, 64, 32, 3.
    decoder.add(L.Dense(np.prod(img_shape), activation = 'elu'))  #actual decoder, height*width*3 units
    decoder.add(L.Reshape(img_shape))         #un-flatten

    decoder.add(L.Conv2DTranspose(filters=128, kernel_size=(3, 3), strides=2, activation='elu', padding='same'))

    decoder.add(Conv2DTranspose(filters=64, kernel_size=(3, 3), strides=2, activation='elu', padding='same'))

    decoder.add(L.Conv2DTranspose(filters=32, kernel_size=(3, 3), strides=2, activation='elu', padding='same'))

    decoder.add(L.Conv2DTranspose(filters=3, kernel_size=(3, 3), strides=2, activation=None, padding='same'))


    return encoder, decoder

请注意,code_size=32

我希望decoder.output_shape[1:]==IMG_SHAPE的输出为True,但实际输出为False

0 个答案:

没有答案