2

I'm trying to build a deep convolutional autoencoder in Keras, but it keeps outputting the wrong shape.

Code:

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)

I expect the output shape to be the same as the input shape, but it is instead (8, 32, 1) for (1, 32, 32)

1 Answer 1

2

Try to use print(build_network((32,32,1)).layers[-1].output). Or if you want to use channel first than you need to change model like this,

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)

Because in UpSampling2D, default is "channels_last".

Sign up to request clarification or add additional context in comments.

1 Comment

@ZeroMaxinumXZ glad to help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.