ziji
ziji

Reputation: 85

Flatten and back keras

I am trying to get values in the simple vector using autoencoder

here is my code

input_img = Input(shape=(28, 28, 1))

x = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)

Here I need a flatten layer

encoder = Model(input_img, encoded)

And then make it convolutional back

encoderOutputShape = encoded._keras_shape[1:]

# unflatten here
decoder_input= Input(encoderOutputShape)
decoder = Conv2D(32, (3, 3), activation='relu', padding='same')(decoder_input)
x = UpSampling2D((2, 2))(decoder)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)

decoder = Model(decoder_input, decoded)

auto_input = Input(shape=(28,28,1))

encoded = encoder(auto_input)
decoded = decoder(encoded)

auto_encoder = Model(auto_input, decoded)

How to do it in the right way?

In other words, I want to get the output of the encoder (or use random data), change it and put into the decoder and get the decoded result.

Upvotes: 1

Views: 1093

Answers (1)

willian ver valem
willian ver valem

Reputation: 395

There is a question here why do you flat the tensor if you don't use any Dense layers?

but you can make like this:

encoder_output = Flatten()(encoded)
decoder_input = Reshape((7, 7, 32))(encoder_output)

decoder = Conv2D(32, (3, 3), activation='relu', padding='same')(decoder_input)

that is because you need to reshape your tensor before.

Upvotes: 2

Related Questions