Generating Images with Autoencoders

And an important question, how computationally intensive would it be to implement?This is an ideal situation to use a variational autoencoder.

We can train a neural network to learn latent features about the plant, and then every time one pops up in our world, we can take a random sample from our “learned” features and generate a unique plant.

This is, in fact, how many open world games have started to generate aspects of the scenery within their worlds.

Let’s go for a more graphical example.

Imagine we are an architect and want to generate floor plans for a building of arbitrary shape.

We can an autoencoder network to learn a data generating distribution given an arbitrary build shape, and it will take a sample from our data generating distribution and produce a floor plan.

This idea is shown in the animation below.

The potential of these for designers is arguably the most prominent.

Imagine instead we work for a fashion company and are tasked with creating new styles of clothing, we could, in fact, just train an autoencoder on “fashionable” items and allow the network to learn a data generating distribution for fashionable clothing.

Subsequently, we can take samples from this low-dimensional latent distribution and use this to create new ideas.

This final example is the one that we will work with during the final section of this tutorial, where will study the fashion MNIST dataset.

AutoencodersTraditional AutoencodersAutoencoders are surprisingly simple neural architectures.

They are basically a form of compression, similar to the way an audio file is compressed using MP3, or an image file is compressed using JPEG.

Autoencoders are closely related to principal component analysis (PCA).

In fact, if the activation function used within the autoencoder is linear within each layer, the latent variables present at the bottleneck (the smallest layer in the network, aka.

code) directly correspond to the principal components from PCA.

Generally, the activation function used in autoencoders is non-linear, typical activation functions are ReLU (Rectified Linear Unit) and sigmoid.

The math behind the networks is fairly easy to understand, so I will go through it briefly.

Essentially, we split the network into two segments, the encoder, and the decoder.

The encoder function, denoted by ϕ, maps the original data X, to a latent space F, which is present at the bottleneck.

The decoder function, denoted by ψ, maps the latent space F at the bottleneck to the output.

The output, in this case, is the same as the input function.

Thus, we are basically trying to recreate the original image after some generalized non-linear compression.

The encoding network can be represented by the standard neural network function passed through an activation function, where z is the latent dimension.

Similarly, the decoding network can be represented in the same fashion, but with different weight, bias, and potentially activation functions being used.

The loss function can then be written in terms of these network functions, and it is this loss function that we will use to train the neural network through the standard backpropagation procedure.

Since the input and output are the same images, this is not really supervised or unsupervised learning, so we typically call this self-supervised learning.

The aim of the autoencoder is to select our encoder and decoder functions in such a way that we require the minimal information to encode the image such that it be can regenerated on the other side.

If we use too few nodes in the bottleneck layer, our capacity to recreate the image will be limited and we will regenerate images that are blurry or unrecognizable from the original.

If we use too many nodes, then there is little point in using compression at all.

The case for compression is pretty simple, whenever you download something on Netflix, for example, the data that is sent to you is compressed.

Once it arrives at your computer, it is passed through a decompression algorithm and then displayed on your computer.

This is analogous to how zip files work, except it is done behind the scenes via a streaming algorithm.

Denoising AutoencodersThere are several other types of autoencoders.

One of the most commonly used is a denoising autoencoder, which will analyze with Keras later in this tutorial.

These autoencoders add some white noise to the data prior to training but compare the error to the original image when training.

This forces the network to not become overfit to arbitrary noise present in images.

We will use this later to remove creases and darkened areas from scanned images of documents.

Sparse AutoencodersA sparse autoencoder, counterintuitively, has a larger latent dimension than the input or output dimensions.

However, each time the network is run, only a small fraction of the neurons fires, meaning that the network is inherently ‘sparse’.

This is similar to a denoising autoencoder in the sense that it is also a form of regularization to reduce the propensity for the network to overfit.

Contractive AutoencoderContractive encoders are much the same as the last two procedures, but in this case, we do not alter the architecture and simply add a regularizer to the loss function.

This can be thought of as a neural form of ridge regression.

So now that we understand how autoencoders are, we need to understand what they are not good at.

Some of the biggest challenges are:Gaps in the latent spaceSeparability in the latent spaceDiscrete latent spaceThese problems can all be illustrated in this diagram.

Latent space representation for MNIST dataset.

This diagram shows us the location of different labeled numbers within the latent space.

We can see that the latent space contains gaps, and we do not know what characters in these spaces may look like.

This is equivalent to having a lack of data in a supervised learning problem, as our network has not been trained for these circumstances of the latent space.

Another issue is the separability of the spaces, several of the numbers are well separated in the above figure, but there are also regions where the labeled is randomly interspersed, making it difficult to separate the unique features of characters (in this case the numbers 0–9).

Another issue here is the inability to study a continuous latent space, for example, we do not have a statistical model that has been trained for arbitrary input (and would not even if we closed all gaps in the latent space).

These issues with traditional autoencoders mean that we still have a way to go before we can learn the data generating distribution and produce new data and images.

Now that we understand how traditional autoencoders work, we will move on to variational autoencoders.

These are slightly more complex as they implement a form of variational inference taken from Bayesian statistics.

We will discuss this in more depth in the next sectionVariational AutoencodersVAEs inherit the architecture of traditional autoencoders and use this to learn a data generating distribution, which allows us to take random samples from the latent space.

These random samples can then be decoded using the decoder network to generate unique images that have similar characteristics to those that the network was trained on.

For those of you familiar with Bayesian statistics, the encoder is learning an approximation to the posterior distribution.

This distribution is typically intractable to do analytically since it does not have a closed form solution.

This means that we can either perform computationally expensive sampling procedures such as Markov Chain Monte Carlo (MCMC) methods, or we can use variational methods.

The variational autoencoder, as one might suspect, uses variational inference to generate its approximation to this posterior distribution.

We will discuss this procedure in a reasonable amount of detail, but for the in-depth analysis, I highly recommend checking out the blog post by Jaan Altosaar.

Variational inference is a topic for a graduate machine learning or statistics class, but you do not need a degree in statistics to understand the basic ideas.

Here is a link to Jaan’s article for those interested:Tutorial — What is a variational autoencoder? — Jaan AltosaarUnderstanding Variational Autoencoders (VAEs) from two perspectives: deep learning and graphical models.

jaan.

ioFor those of you not interested in the underlying mathematics, feel free to skip to the VAE coding tutorial.

The first thing we need to understand is the posterior distribution and why we cannot calculate it.

Take a look at the equation below, this is Bayes’ theorem.

The premise here is that we want to know how to learn how to generate data, x, from our latent variables, z.

This implies that we want to learn p(z|x).

Unfortunately, we do not know this distribution, but we do not need to since we can reformulate this probability with Bayes’ theorem.

This does not solve all of our problems, however, as the denominator, known as the evidence, is often intractable.

All is not lost though, as a cheeky solution exists that allows us to approximate this posterior distribution.

It turns out we can cast this inference problem into an optimization problem.

In order to approximate the posterior distribution, we need a way of assessing how good a proposal distribution is compared to the true posterior.

To do this, we use a Bayesian statistician’s best friend, the Kullback-Leibler divergence.

The KL divergence is a measure of how similar two probability distributions are; if they are the same, the divergence is zero, and if it is a positive number, the two distributions are different.

The KL divergence is strictly positive, although it is technically not a distance because the function is not symmetric.

We use the KL divergence in the following manner.

This equation may look intimidating, but the idea here is quite simple.

We propose a family of possible distributions that could possibly be how our data was generated, Q, and we want to find the optimal distribution, q*, which minimizes our distance between the proposed distribution and the actual distribution, which we are trying to approximate due to its intractability.

We still have one problem with this formula, namely, that we do not actually know p(z|x), so we cannot calculate the KL divergence.

How do we resolve this?This is where things get a little bit esoteric.

We can do some mathematical manipulation and rewrite the KL divergence in terms of something called the ELBO (Evidence Lower Bound) and another term involving p(x).

What is interesting here is that the ELBO is the only variable in this equation that depends on what distribution we select.

The other term is not influenced by our choice of distribution since it does not depend on q.

Thus, we can minimize the KL divergence by maximizing (since it is negative) the ELBO in the above equation.

The key point of this is that we can actually calculate the ELBO, meaning we can now perform an optimization procedure.

So all we need to do now is come up with a good choice for Q and then differentiate the ELBO, set it to zero and voila, we have our optimal distribution.

There are a few more snags before this is possible, first, we have to decide what is a good family of distributions to select.

Typically, mean field variational inference is done for simplicity when defining q.

This essentially says that each variational parameter is independent of each other.

We, therefore, have a single q for each data point, which we can multiply together to give a joint probability, giving us the ‘mean field’ q.

In reality, we could select as many fields, or clusters, as we would like.

In the case of MNIST, for example, we might select 10 clusters since we know that there are 10 possible numbers that could be present.

The second thing we need to do is something often known as the reparameterization trick, whereby we take the random variables outside of the derivative since taking the derivative of a random variable gives us much larger errors due to their inherent randomness.

The reparameterization trick is a little esoteric, but it basically says that I can write a normal distribution as a mean plus some standard deviation, multiplied by some error.

This means that when differentiating, we are not taking the derivative of the random function itself, merely its parameters.

If that did not make much sense, here is a good article that explains the trick and why it performs better than taking derivatives of the random variables themselves:Reparameterization TrickGoker Erdogan’s personal website.

gokererdogan.

github.

ioThis procedure does not have a general closed-form solution, so we are still somewhat constrained in our ability to approximate the posterior distribution.

However, the exponential family of distributions does, in fact, have a closed form solution.

This means that we can use standard distributions, such as the normal distribution, binomial, Poisson, beta, etc.

So, whilst we may not find the true posterior distribution, we can find an approximation which does the best job given the exponential family of distributions.

The art of variational inference is selecting our family of distributions, Q, to be large enough to get a good approximation of the posterior, but not too large that it takes an excessively long time to compute.

Now that we have a decent idea of how our network has been trained to learn the latent distribution of our data, we can look at how we generate data using this distribution.

The Data Generating ProcedureLooking at the below image, we can consider that our approximation to the data generating procedure decides that we want to generate the number ‘2’, so it generates the value 2 from the latent variable centroid.

However, we may not want to generate the same looking ‘2’ every time, as in our video game example with plants, so we add some random noise to this item in the latent space, which is based on a random number and the ‘learned’ spread of the distribution for the value ‘2’.

We pass this through our decoder network and we get a 2 which looks different to the original.

This was an oversimplified version which abstracted the architecture of the actual autoencoder network.

Below is a representation of the architecture of a real variational autoencoder using convolutional layers in the encoder and decoder networks.

We see that we are learning the centers and spreads of the data generating distributions within the latent space separately, and then ‘sampling’ from these distributions to generate essentially ‘fake’ data.

The inherent nature of the learning procedure means that parameters that look similar (stimulate the same network neurons to fire) are clustered together in the latent space, and are not spaced arbitrarily.

This is illustrated in the figure below.

We see that our values of 2’s begin to cluster together, whilst the value 3 gradually becomes pushed away.

This is useful as it means the network does not arbitrarily place characters in the latent space, making the transitions between values less spurious.

An overview of the entire network architecture is shown below.

Hopefully, at this point, the procedure makes sense.

We train the autoencoder using a set of images to learn our mean and standard deviations within the latent space, which forms our data generating distribution.

Next, when we want to generate a similar image, we sample from one of the centroids within the latent space, distort it slightly using our standard deviation and some random error, and then pass this through the decoder network.

It is clear from this example that the final output looks similar, but not the same, as the input image.

VAE Coding TutorialAll of the following code can be found within my personal GitHub repository on autoencoders, which can be found here:mrdragonbear/AutoencodersContribute to mrdragonbear/Autoencoders development by creating an account on GitHub.

github.

comIn this section, we will look at a simple denoising autoencoder for removing creases and marks on scanned images of documents, as well as removing noise within the fashion MNIST dataset.

We will then use VAEs to generate new items of clothing after training the network on the MNIST dataset.

Denoising AutoencodersFashion MNISTFor the first exercise, we will add some random noise (salt and pepper noise) to the fashion MNIST dataset, and we will attempt to remove this noise using a denoising autoencoder.

First, we perform our preprocessing: download the data, scale it, and then add our noise.

After this, we create the architecture for our autoencoder network.

This involves multiple layers of convolutional neural networks, max-pooling layers on the encoder network, and upscaling layers on the decoder network.

The model takes a while to run unless you have a GPU, it can take around 3–4 minutes per epoch.

Our input images, input images with noise, and our output images are shown below.

Input images from fashion MNIST.

Input images with salt and pepper noise.

Output from denoising networkAs you can see, we are able to remove the noise adequately from our noisy images, but we have lost a fair amount of resolution of the finer features of the clothing.

This is one of the prices we pay for a robust network.

The network can be tuned in order to make this final output more representative of the input images.

Text CleaningOur second example with denoising autoencoders involves cleaning scanned images of creases and dark areas.

Here are our input and output images that we would like to obtain.

Input images of ‘noisy’ text data.

Cleaned text images.

The data preprocessing for this is a bit more involved, and so I will not introduce that here, but it is available on my GitHub repository, along with the data itself.

The network architecture is as follows.

Variational AutoencodersFor our finale, we will try to generate new images of clothing items that are present in the fashion MNIST dataset.

The neural architecture for this is a little bit more complicated, and contains a sampling layer called a ‘Lambda’ layer.

This is the architecture, but we still need to insert the loss function and incorporate the KL divergence.

We can now view our reconstructed samples to see what our network was able to learn.

We can clearly see transitions between shoes, handbags, as well as clothing items.

Not all of the latent space is plotted here to help with image clarity.

We can also view the latent space and color code each of the 10 clothing items present in the fashion MNIST dataset.

We see that the items are separated into distinct clusters.

Final WordThis tutorial was a crash course in autoencoders, variational autoencoders, and variational inference.

I hope that the reader found this interesting, and now has a better understanding of what autoencoders are and how they can be used in real-world applications.

In my next tutorial, I will look at producing fake images of celebrities and anime characters using generative adversarial networks, which are a natural stepping stone from variational autoencoders.

Stay tuned!.