Path: blob/master/site/en-snapshot/tutorials/generative/autoencoder.ipynb
25118 views
Copyright 2020 The TensorFlow Authors.
Intro to Autoencoders
This tutorial introduces autoencoders with three examples: the basics, image denoising, and anomaly detection.
An autoencoder is a special type of neural network that is trained to copy its input to its output. For example, given an image of a handwritten digit, an autoencoder first encodes the image into a lower dimensional latent representation, then decodes the latent representation back to an image. An autoencoder learns to compress the data while minimizing the reconstruction error.
To learn more about autoencoders, please consider reading chapter 14 from Deep Learning by Ian Goodfellow, Yoshua Bengio, and Aaron Courville.
Import TensorFlow and other libraries
Load the dataset
To start, you will train the basic autoencoder using the Fashion MNIST dataset. Each image in this dataset is 28x28 pixels.
First example: Basic autoencoder
Define an autoencoder with two Dense layers: an encoder
, which compresses the images into a 64 dimensional latent vector, and a decoder
, that reconstructs the original image from the latent space.
To define your model, use the Keras Model Subclassing API.
Train the model using x_train
as both the input and the target. The encoder
will learn to compress the dataset from 784 dimensions to the latent space, and the decoder
will learn to reconstruct the original images. .
Now that the model is trained, let's test it by encoding and decoding images from the test set.
Second example: Image denoising
An autoencoder can also be trained to remove noise from images. In the following section, you will create a noisy version of the Fashion MNIST dataset by applying random noise to each image. You will then train an autoencoder using the noisy image as input, and the original image as the target.
Let's reimport the dataset to omit the modifications made earlier.
Adding random noise to the images
Plot the noisy images.
Define a convolutional autoencoder
In this example, you will train a convolutional autoencoder using Conv2D layers in the encoder
, and Conv2DTranspose layers in the decoder
.
Let's take a look at a summary of the encoder. Notice how the images are downsampled from 28x28 to 7x7.
The decoder upsamples the images back from 7x7 to 28x28.
Plotting both the noisy images and the denoised images produced by the autoencoder.
Third example: Anomaly detection
Overview
In this example, you will train an autoencoder to detect anomalies on the ECG5000 dataset. This dataset contains 5,000 Electrocardiograms, each with 140 data points. You will use a simplified version of the dataset, where each example has been labeled either 0
(corresponding to an abnormal rhythm), or 1
(corresponding to a normal rhythm). You are interested in identifying the abnormal rhythms.
Note: This is a labeled dataset, so you could phrase this as a supervised learning problem. The goal of this example is to illustrate anomaly detection concepts you can apply to larger datasets, where you do not have labels available (for example, if you had many thousands of normal rhythms, and only a small number of abnormal rhythms).
How will you detect anomalies using an autoencoder? Recall that an autoencoder is trained to minimize reconstruction error. You will train an autoencoder on the normal rhythms only, then use it to reconstruct all the data. Our hypothesis is that the abnormal rhythms will have higher reconstruction error. You will then classify a rhythm as an anomaly if the reconstruction error surpasses a fixed threshold.
Load ECG data
The dataset you will use is based on one from timeseriesclassification.com.
Normalize the data to [0,1]
.
You will train the autoencoder using only the normal rhythms, which are labeled in this dataset as 1
. Separate the normal rhythms from the abnormal rhythms.
Plot a normal ECG.
Plot an anomalous ECG.
Build the model
Notice that the autoencoder is trained using only the normal ECGs, but is evaluated using the full test set.
You will soon classify an ECG as anomalous if the reconstruction error is greater than one standard deviation from the normal training examples. First, let's plot a normal ECG from the training set, the reconstruction after it's encoded and decoded by the autoencoder, and the reconstruction error.
Create a similar plot, this time for an anomalous test example.
Detect anomalies
Detect anomalies by calculating whether the reconstruction loss is greater than a fixed threshold. In this tutorial, you will calculate the mean average error for normal examples from the training set, then classify future examples as anomalous if the reconstruction error is higher than one standard deviation from the training set.
Plot the reconstruction error on normal ECGs from the training set
Choose a threshold value that is one standard deviations above the mean.
Note: There are other strategies you could use to select a threshold value above which test examples should be classified as anomalous, the correct approach will depend on your dataset. You can learn more with the links at the end of this tutorial.
Classify an ECG as an anomaly if the reconstruction error is greater than the threshold.
Next steps
To learn more about anomaly detection with autoencoders, check out this excellent interactive example built with TensorFlow.js by Victor Dibia. For a real-world use case, you can learn how Airbus Detects Anomalies in ISS Telemetry Data using TensorFlow. To learn more about the basics, consider reading this blog post by François Chollet. For more details, check out chapter 14 from Deep Learning by Ian Goodfellow, Yoshua Bengio, and Aaron Courville.