monte carlo
monte carlo

Reputation: 95

Predicting from the middle of a Keras model

I am trying to develop an auto-encoder for compressing images using Keras. I was able to train it and to compress images, but I am struggling with the decoder part of it. Specifically, given a compressed image, I don't know how to use the model to de-compress it.

This is what I have:

    input_layer = keras.layers.Input(shape=(64, 64, 3))
    code_layer = build_encoder(input_layer, size_of_code)  # add some convolution layers and max-pooling
    output_layer = build_decoder(code_layer)  # add some convolution layers and up-sampling

    autoencoder_model = keras.models.Model(input_layer, output_layer)
    encoder_model = keras.models.Model(input_layer, code_layer)
    decoder_model = ??
    autoencoder_model.compile(optimizer='adam', loss='binary_crossentropy')

using the code above I can train the autoencoder_model and compress the images using the encoder_model, but I don't know how to construct the decoder_model, mainly because I don't know how to insert a new input to the middle of the model.

Upvotes: 0

Views: 144

Answers (1)

Manoj Mohan
Manoj Mohan

Reputation: 6034

Like this. Instead of the code_layer, need to define an input layer and build the decoder model with that input.

latent_inputs = keras.layers.Input(shape=(size_of_code))
output_layer = build_decoder(latent_inputs)  # add some convolution layers and up-sampling
decoder_model = keras.models.Model(latent_inputs, output_layer)

You can refer this complete VAE example:

https://github.com/keras-team/keras/blob/master/examples/variational_autoencoder.py

Upvotes: 2

Related Questions