简体   繁体   English

从Keras模型的中间进行预测

[英]Predicting from the middle of a Keras model

I am trying to develop an auto-encoder for compressing images using Keras. 我正在尝试开发一种自动编码器,用于使用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. 使用上面的代码,我可以训练autoencoder_model并使用encoder_model压缩图像,但是我不知道如何构造decoder_model ,主要是因为我不知道如何在模型中间插入新的输入。

Like this. 像这样。 Instead of the code_layer, need to define an input layer and build the decoder model with that input. 代替code_layer,需要定义一个输入层,并使用该输入构建解码器模型。

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: 您可以参考以下完整的VAE示例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM