How to build and train autoencoders in keras

share link

by vsasikalabe dot icon Updated: Jul 6, 2023

technology logo
technology logo

Solution Kit Solution Kit  

We can use the Keras Model Subclassing API to define our model. We can train our model using x_train as both the input and the target. The encoder compresses the data from certain dimensions to the unused space. The decoder reconstructs the original data. We have made a deep convolutional autoencoder. Before we can train an autoencoder, we should implement the autoencoder architecture itself.  

 

Chitta's approach to training uses only a data subset for good profiles. We can also create an Auto-Encoder using Keras functional API. Autoencoding is a data compression algorithm. It is where the compression and decompression functions are:  

  • data-specific,  
  • lossy, and  
  • learned from examples rather than being engineered by a human.  


A sparse autoencoder is an ordinary autoencoder but enforces exiguity through an activity. We need to build the encoder model and decoder model. It will help differentiate between the input and output. We can extract the encoder, which takes the input image as the input. The output of the encoder is the encoded image of dimension 32. We can visualize the reconstructed inputs and the encoded representations. The generative model displays the distribution that generates the features themselves.  

 

Autoencoders are not suited for the image generation method when compared to GANs. We can also generate stunning new images based on the input images provided to the GANs. To build an autoencoder, you need three things:  

  • an encoding function,  
  • a decoding function, and  
  • a distance function. 

 

It is between the information loss between the compressed representation and the decompressed. Keras is a high-level API. Lossy operations mean the reconstructed image needs to be at a higher resolution in quality. It varies from the original, and the difference is greater for reconstructions. It is known as a lossy operation.  

 

The generator network aims to generate such images. It can fool the discriminator and make it believe the generated images are real. These networks were used for dimensionality reduction. The true value of the autoencoder lives inside that latent-space representation.  

 

It represents a pure six and a pure zero between the latent space points. We are using the TensorFlow backend and the TensorBoard callback. Finally, a decoder network maps these latent space points back to the original input data. We can visualize the reconstructed inputs and the encoded representations. There are several convolutional, reshaping, dense layers, and skip connections.  

 

In the generator model, we are using two types of layers:  

 

Upsample layer (UpSampling2D)- It doubles the dimensions of the input. It is a simple scaling up of the image by using the nearest value.  

 

The advantage - it is cheap.  

 

The transpose convolutional layer (Conv2DTranspose) performs an inverse convolution operation. It is like a normal conv2d operation while training your model.  

 

In Python, Keras is a neural network Application (API) accessed with TensorFlow. These models offer a simple, user-friendly way to define a network. When the input size and kernel size do not fit, padding is added as required to make up for overlaps.  

 

Autoencoders (neural networks) compress and reconstruct data. The encoder compresses input, and the decoder recreates the information from compressed data. Optimizer has implemented the Adadelta algorithm. Adadelta optimization is based on adaptive learning rate per dimension. It indicates two drawbacks:  

  • The frequent decay of learning rates throughout training.  
  • The need is to select global learning manually.  


The MNIST digits are reconstructed like this by a variational autoencoder. We must apply a final layer used to recover the original channel. You can use the Model class to create the model itself. In Keras, we make noise vectors. During the training process, our goal is to train a network that can learn how to reconstruct our input data. Our fully-connected layer (i.e., the layer) serves as our latent-space representation. You can append the encoder, without trainable parameters, to your transformer model.  

 

Here is an example of how to build and train autoencoders in keras:  

Fig : Preview of the output that you will get on running this code from your IDE.

Code

In this solution, we used keras library of Python.

from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D
from keras.models import Model
from keras import backend as K

input_img = Input(shape=(28, 28, 1))  # adapt this if using `channels_first` image data format

x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
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)
encoded = MaxPooling2D((2, 2), padding='same')(x)

# at this point the representation is (4, 4, 8) i.e. 128-dimensional

x = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
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')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)

autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 28, 28, 1)         0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 28, 28, 16)        160       
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 14, 14, 16)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 14, 14, 8)         1160      
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 7, 7, 8)           0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 7, 7, 8)           584       
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 4, 4, 8)           0         
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 4, 4, 8)           584       
_________________________________________________________________
up_sampling2d_1 (UpSampling2 (None, 8, 8, 8)           0         
_________________________________________________________________
conv2d_5 (Conv2D)            (None, 8, 8, 8)           584       
_________________________________________________________________
up_sampling2d_2 (UpSampling2 (None, 16, 16, 8)         0         
_________________________________________________________________
conv2d_6 (Conv2D)            (None, 14, 14, 16)        1168      
_________________________________________________________________
up_sampling2d_3 (UpSampling2 (None, 28, 28, 16)        0         
_________________________________________________________________
conv2d_7 (Conv2D)            (None, 28, 28, 1)         145       
=================================================================
Total params: 4,385
Trainable params: 4,385
Non-trainable params: 0

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

x = Conv1D(32, (9), activation='relu', padding='same')(input_img)
x = MaxPooling1D((4), padding='same')(x)
x = Conv1D(16, (9), activation='relu', padding='same')(x)
x = MaxPooling1D((4), padding='same')(x)
x = Conv1D(8, (9), activation='relu', padding='same')(x)
encoded = MaxPooling1D(4, padding='same')(x)

x = Conv1D(8, (9), activation='relu', padding='same')(encoded)
x = UpSampling1D((4))(x)
x = Conv1D(16, (9), activation='relu', padding='same')(x)
x = UpSampling1D((4))(x) 
x = Conv1D(32, (9), activation='relu')(x)
x = UpSampling1D((8))(x)              ##   <-- change here (was 4)
decoded = Conv1D(1, (9), activation='sigmoid', padding='same')(x)  

autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 64, 1)             0         
_________________________________________________________________
conv1d_1 (Conv1D)            (None, 64, 32)            320       
_________________________________________________________________
max_pooling1d_1 (MaxPooling1 (None, 16, 32)            0         
_________________________________________________________________
conv1d_2 (Conv1D)            (None, 16, 16)            4624      
_________________________________________________________________
max_pooling1d_2 (MaxPooling1 (None, 4, 16)             0         
_________________________________________________________________
conv1d_3 (Conv1D)            (None, 4, 8)              1160      
_________________________________________________________________
max_pooling1d_3 (MaxPooling1 (None, 1, 8)              0         
_________________________________________________________________
conv1d_4 (Conv1D)            (None, 1, 8)              584       
_________________________________________________________________
up_sampling1d_1 (UpSampling1 (None, 4, 8)              0         
_________________________________________________________________
conv1d_5 (Conv1D)            (None, 4, 16)             1168      
_________________________________________________________________
up_sampling1d_2 (UpSampling1 (None, 16, 16)            0         
_________________________________________________________________
conv1d_6 (Conv1D)            (None, 8, 32)             4640      
_________________________________________________________________
up_sampling1d_3 (UpSampling1 (None, 64, 32)            0         
_________________________________________________________________
conv1d_7 (Conv1D)            (None, 64, 1)             289       
=================================================================
Total params: 12,785
Trainable params: 12,785
Non-trainable params: 0

Instructions

Follow the steps carefully to get the output easily.

  1. Download and Install the PyCharm Community Edition on your computer.
  2. Open the terminal and install the required libraries with the following commands.
  3. Install Keras - pip install keras
  4. Create a new Python file on your IDE.
  5. Copy the snippet using the 'copy' button and paste (till line no.25) into your Python file. ( Remove the remaining line of the codes )
  6. Add autoencoder.summary() to end of the line.(at line no.26)
  7. Run the current file to generate the output.


I hope you found this useful.


I found this code snippet by searching for ' keras autoencoder Error when checking target' in Kandi. You can try any such use case!

Environment Tested

I tested this solution in the following versions. Be mindful of changes when working with other versions.

  1. PyCharm Community Edition 2022.3.1
  2. The solution is created in Python 3.11.1 Version
  3. keras - 2.12.0 Version


Using this solution, we can able to build and train autoencoders in keras with simple steps. This process also facilities an easy way to use, hassle-free method to create a hands-on working version of code which would help us to build and train autoencoders in keras.

Dependent Libraries

kerasby keras-team

Python doticonstar image 58594 doticonVersion:v2.13.1-rc0doticon
License: Permissive (Apache-2.0)

Deep Learning for humans

Support
    Quality
      Security
        License
          Reuse

            kerasby keras-team

            Python doticon star image 58594 doticonVersion:v2.13.1-rc0doticon License: Permissive (Apache-2.0)

            Deep Learning for humans
            Support
              Quality
                Security
                  License
                    Reuse

                      If you do not have keras library that is required to run this code, you can install it by clicking on the above link.

                      You can search for any dependent library on kandi like keras.

                      FAQ:  

                      1. What is a deep convolutional autoencoder, and how does it work?  

                      A convolutional autoencoder is a neural network. It is used to regenerate its input image in the output layer. An image is passed through an encoder. It is a ConvNet that produces a low-dimensional representation of the image.  

                       

                      2. What are the advantages of Auto-Encoding Variational Bayes compared to other autoencoder architectures?  

                      The variational autoencoder is that we can understand smooth latent state representations. For standard autoencoders, we need to get details on an encoding. It allows us to reproduce the input.  

                       

                      3. How can an encoded image be used in a Generative Adversarial Network?  

                      We can do image-to-image translation using deep learning generative adversarial networks. A GAN has a generator network and one or more discriminator networks. They are trained to maximize performance.  

                       

                      4. Can autoencoders be used for image compression?  

                      The convolution autoencoder helps compress the image, not compromising the data quality. The loss function value determines the difference between the output and the input. The smaller the value, the better the performance.  

                       

                      5. What is the difference between an encoded and decoded image?  

                      Encoding is writing characters into a particular format for efficient transmission or storage. Decoding is the opposite process. The conversion of an encoded format is back into the original sequence of characters. 

                      Support

                      1. For any support on kandi solution kits, please use the chat
                      2. For further learning resources, visit the Open Weaver Community learning page

                      See similar Kits and Libraries