You are on page 1of 36

MNU

DP LARNING   K  R A    A N D   T  N  O R F LO W   T U TO R I A L 

Autoencoders with Keras ,
TensorFlow  and Deep Learning ,
 Adrian Roserock on Feruar 17  2020 ,

Click here to download the source code to this post

,
In this tutorial  ou will learn how to implement and train autoencoders using

, ,
Keras  TensorFlow  and Deep Learning .
’ -
Toda s tutorial kicks o몭 a three part series on the applications of autoencoders :

1 , ,
Autoencoders with Keras  TensorFlow  and Deep Learning  toda s tutorial ( ’ )

2 Denoising autoenecoders with Keras and TensorFlow  next week s tutorial ( ’ )

3 , ,
Anomal detection with Keras  TensorFlow  and Deep Learning  tutorial two (
weeks from now )

,
A few weeks ago  I pulished an introductor guide to anomal outlier /
detection using standard machine learning algorithms .

M intention was to immediatel follow up that post with a a guide on deep

- ; ,
learning ased anomal detection  however  as I started writing the code for the

,
tutorial  I realized I had never covered autoencoders on the PImageearch

log !

-
Tring to discuss deep learning ased anomal detection without prior context

on what autoencoders are and how the work would e challenging to follow ,
comprehend  and digest , .

, ’
Therefore  we re going to spend the next couple of weeks looking at

,
autoencoder algorithms  including their practical  real world applications , - .

To learn aout the fundamentals of autoencoders using Keras and

,
TensorFlow  just keep reading !
Looking for the source code to this post ?
J U M P   R I G H T   TO   T H    D O W N LOA D      C T I O N  

Autoencoders with Keras  TensorFlow  and , ,


Deep Learning

, ’
In the 몭rst part of this tutorial  we ll discuss what autoencoders are  including ,
how convolutional autoencoders can e applied to image data  We ll also . ’
discuss the di몭erence etween autoencoders and other generative models ,
such as Generative Adversarial Networks  GANs ( ).

, ’
From there  I ll show ou how to implement and train a convolutional

autoencoder using Keras and TensorFlow .

’ ,
We ll then review the results of the training script  including visualizing how the

autoencoder did at reconstructing the input data.

, ’
Finall  I ll recommend next steps to ou if ou are interested in learning more

aout deep learning applied to image datasets .

What are autoencoders ?
Autoencoders are a tpe of unsupervised neural network  i e  no class laels or ( . .,
)
laeled data  that seek to :

1 Accept an input set of data  i e  the input( . ., ).

2 Internall compress the input data into a latent space representation  i e  a- ( . .,


single vector that compresses and quanti몭es the input ).

3 Reconstruct the input data from this latent representation  i e  the output ( . ., ).

,
Tpicall  we think of an autoencoder having two components sunetworks / :

1 :
ncoder  Accepts the input data and compresses it into the latent space  If - .
we denote our input data as   and the encoder as  , then the output latent-
space representation   , , would e  .

2 :
Decoder  The decoder is responsile for accepting the latent space -
representation   and then reconstructing the original input  If we denote the .
decoder function as   and the output of the detector as  , then we can
represent the decoder as  .

,
Using our mathematical notation  the entire training process of the autoencoder

can e written as :

Figure 1 elow demonstrates the asic architecture of an autoencoder :

: , , ,
Figure 1  Autoencoders with Keras  TensorFlow  Pthon  and Deep Learning don t have to e ’
. , ’
complex  reaking the concept down to its parts  ou ll have an input image that is passed

.(
through the autoencoder which results in a similar output image   몭gure inspired  Nathan

’ , :
Huens  article  Deep inside  Autoencoders )

Here ou can see that :

1 We input a digit to the autoencoder .

2 The encoder sunetwork creates a latent representation of the digit  This .
latent representation is sustantiall smaller  in terms of dimensionalit ( )
than the input .

3 The decoder sunetwork then reconstructs the original digit from the latent

representation .
You can thus think of an autoencoder as a network that reconstructs its input !

, ,
To train an autoencoder  we input our data  attempt to reconstruct it  and then ,
(
minimize the mean squared error  or similar loss function ).

,
Ideall  the output of the autoencoder will e near identical to the input .

An autoencoder reconstructs it s input  ’ — so what’s


the ig deal ?

: , ,
Figure 2  Autoencoders are useful for compression  dimensionalit reduction  denoising  and ,
/ . , ’ /
anomal outlier detection  In this tutorial  we ll use Pthon and Keras TensorFlow to train a deep

.(
learning autoencoder   image source )

,
At this point  some of ou might e thinking :

“ , ’
Adrian  what s the ig deal here ?

If the goal of an autoencoder is just to reconstruct the input  wh even use

the network in the 몭rst place ?
,

,
If I wanted a cop of m input data  I could literall just cop it with a single

function call .

Wh on earth would I appl deep learning and go through the troule of
training a network ?

, ,
This question  although a legitimate one  does indeed contain a large

misconception regarding autoencoders .

, ,
Yes  during the training process  our goal is to train a network that can learn how

to reconstruct our input data  — ut the true value of the autoencoder lives
-
inside that latent space representation .

Keep in mind that autoencoders compress our input data and  more to the point , ,
,
when we train autoencoders  what we reall care aout is the encoder   , , and
-
the latent space representation   , .

The decoder   , , is used to train the autoencoder end-to-end, ut in
practical applications, we often (ut not alwas) care more aout the encoder

and the latent-space.

, ’ .
Later in this tutorial  we ll e training an autoencoder on the MNIT dataset  The

MNIT dataset consists of digits that are 28×28 pixels with a single channel,

impling that each digit is represented  28 x 28 = 784 values. The


autoencoder we ll e training here will e ale to compress those digits into a

vector of onl 16 values  — that’s a reduction of nearl 98%!

o what can we do if an input data point is compressed into such a small vector ?


That s where things get reall interesting .

What are applications of autoencoders ?
:
Figure 3  Autoencoders are tpicall used for dimensionalit reduction  denoising  and , ,
/ . ,
anomal outlier detection  Outside of computer vision  the are extremel useful for Natural

( ) .
Language Processing  NLP  and text comprehension  In this tutorial  we ll use Pthon and , ’
/
Keras TensorFlow to train a deep learning autoencoder   image source .( )

Autoencoders are tpicall used for :

( . .,
Dimensionalit reduction  i e  think PCA ut more powerful intelligent / ).

( .,
Denoising  ex  removing noise and preprocessing images to improve OCR

accurac ).

/ ( .,
Anomal outlier detection  ex  detecting mislaeled data points in a dataset

or detecting when an input data point falls well outside our tpical data

distriution ).

Outside of the computer vision 몭eld  ou ll see autoencoders applied to Natural , ’


( )
Language Processing  NLP  and text comprehension prolems  including ,
understanding the semantic meaning of words  constructing word emeddings , ,
and even text summarization .

How are autoencoders di몭erent from GANs ?

If ou ve done an prior work with Generative Adversarial Networks  GANs  ou ( ),
might e wondering how autoencoders are di몭erent from GANs .

oth GANs and autoencoders are generative models  however  an autoencoder ; ,


is essentiall learning an identit function via compression .

The autoencoder will accept our input data  compress it down to the latent , -
,
space representation  and then attempt to reconstruct the input using just the

-
latent space vector .

, -
Tpicall  the latent space representation will have much fewer dimensions than

the original input data .

GANs on the other hand :

1 Accept a low dimensional input .

2 uild a high dimensional space from it .
3 ,
Generate the 몭nal output  which is not part of the original training data ut

ideall passes as such .

,
Furthermore  GANs have an evolving loss landscape  which autoencoders do ,
not .

,
As a GAN is trained  the generative model generates  fake  images that are “ ”
“ ”
— the discriminator model must then
then mixed with actual  real  images 

determine which images are “real” vs. “fake/generated”.

As the generative model ecomes etter and etter at generating fake images

,
that can fool the discriminator  the loss landscape evolves and changes  this is (
one of the reasons wh training GANs is so damn hard ).

While oth GANs and autoencoders are generative models  most of their ,
similarities end there .

Autoencoders cannot generate new  realistic data points that could e ,
“ ” .
considered  passale   humans  Instead  autoencoders are primaril used as a ,
method to compress input data points into a latent space representation  That - .
-
latent space representation can then e used for compression  denoising , ,
anomal detection  etc , .

For more details on the di몭erences etween GANs and autoencoders  I suggest ,
giving this thread on Quora a read .

Con몭guring our development environment


To follow along with toda s tutorial on autoencoders  ou should use ,
. .
TensorFlow 2 0  I have two installation tutorials for TF 2 0 and associated .
packages to ring our development sstem up to speed :

.
How to install TensorFlow 2 0 on Uuntu  Uuntu 18 04 O  CPU and ( . ;
optional NVIDIA GPU )

.
How to install TensorFlow 2 0 on macO  Catalina and Mojave Oes ( )

:
Please note  PImageearch does not support Windows  — refer to our FAQ.
Project structure

“ ”
e sure to gra the  Downloads  associated with the log post  From there . ,
.
extract the  zip and inspect the 몭le folder laout/ :

→ Launch Jupter Noteook on Google Cola

Autoencoders with Keras, TensorFlow, and Deep Learning
1.  $ tree ­­dirsfirst
2.  .
3.  ├── pyimagesearch
4.  │   ├── __init__.py
5.  │   └── convautoencoder.py
6.  ├── output.png
7.  ├── plot.png
8.  └── train_conv_autoencoder.py
9.  
10.  1 directory, 5 files

We will review two Pthon scripts toda :

convautoencoder.py : Contains the  ConvAutoencoder  class and  build


method required to assemle our neural network with  tf.keras .

train_conv_autoencoder.py : Trains a digits autoencoder on the MNIT
dataset. Once the autoencoder is trained, we’ll loop over a numer of output

examples and write them to disk for later inspection.

Our training script results in oth a  plot.png  몭gure and  output.png  image .


- -
The output image contains side  side samples of the original versus

reconstructed image .

,
In the next section  we will implement our autoencoder with the high level Keras -
API uilt into TensorFlow .

Implementing a convolutional autoencoder with

Keras and TensorFlow

,
efore we can train an autoencoder  we 몭rst need to implement the autoencoder

architecture itself .

, ’
To do so  we ll e using Keras and TensorFlow .


M implementation loosel follows Francois Chollet s own implementation of
.
autoencoders on the o몭cial Keras log  M primar contriution here is to go

into a it more detail regarding the implementation itself .

Open up the  convautoencoder.py ,
 몭le in our project structure  and insert the

following code :

→ Launch Jupter Noteook on Google Cola

Autoencoders with Keras, TensorFlow, and Deep Learning
1.  # import the necessary packages
2.  from tensorflow.keras.layers import BatchNormalization
3.  from tensorflow.keras.layers import Conv2D
4.  from tensorflow.keras.layers import Conv2DTranspose
5.  from tensorflow.keras.layers import LeakyReLU
6.  from tensorflow.keras.layers import Activation
7.  from tensorflow.keras.layers import Flatten
8.  from tensorflow.keras.layers import Dense
9.  from tensorflow.keras.layers import Reshape
10.  from tensorflow.keras.layers import Input
11.  from tensorflow.keras.models import Model
12.  from tensorflow.keras import backend as K
13.  import numpy as np
14.  
15.  class ConvAutoencoder:
16.    @staticmethod
17.    def build(width, height, depth, filters=(32, 64), latentDim=16):
18.      # initialize the input shape to be "channels last" along with
19.      # the channels dimension itself
20.      # channels dimension itself
21.      inputShape = (height, width, depth)
22.      chanDim = ­1

We egin with a selection of imports from  tf.keras  and one from NumP  If.


’ . ,
ou don t have TensorFlow 2 0 installed on our sstem  refer to the

“Con몭guring our development environment” section aove.

Our  ConvAutoencoder  class contains one static method   , build , which accepts


몭ve parameters :

width : Width of the input image in pixels.

height : Height of the input image in pixels.

depth : Numer of channels (i.e., depth) of the input volume.

filters : A tuple that contains the set of 몭lters for convolution operations. 
default, this parameter includes oth  32  and  64  몭lters.

latentDim : The numer of neurons in our full-connected ( Dense ) latent


vector.  default, if this parameter is not passed, the value is set to  16 .
,
From there  we initialize the  inputShape  and channel dimension  we assume (
“channels last” ordering).


We re now read to initialize our input and egin adding laers to our network :

→ Launch Jupter Noteook on Google Cola

Autoencoders with Keras, TensorFlow, and Deep Learning
24.      # define the input to the encoder
25.      inputs = Input(shape=inputShape)
26.      x = inputs
27.  
28.      # loop over the number of filters
29.      for f in filters:
30.        # apply a CONV => RELU => BN operation
31.        x = Conv2D(f, (3, 3), strides=2, padding="same")(x)
32.        x = LeakyReLU(alpha=0.2)(x)
33.        x = BatchNormalization(axis=chanDim)(x)
34.  
35.      # flatten the network and then construct our latent vector
36.      volumeSize = K.int_shape(x)
37.      x = Flatten()(x)
38.      latent = Dense(latentDim)(x)
39.  
40.      # build the encoder model
41.      encoder = Model(inputs, latent, name="encoder")

Lines 25 and 26 de몭ne the input to the encoder .

,
With our inputs read  we go loop over the numer of  filters  and add our

sets of  CONV=>LeakyReLU=>BN (
 laers  Lines 29 33 - ).

,
Next  we 몭atten the network and construct our latent vector  Lines 36 38   ( - ) — this
- ( . .,
is our actual latent space representation  i e  the  compressed  data “ ”
representation ).

We then uild our  encoder (
 model  Line 41 ).

If we were to do a  print(encoder.summary())  of the  encoder , assuming


× ( depth=1 ) and  filters=(32, 64)
28 28 single channel images   and 

latentDim=16 , we would have the following:

→ Launch Jupter Noteook on Google Cola

Autoencoders with Keras, TensorFlow, and Deep Learning
1.  Model: "encoder"
2.  _________________________________________________________________
3.  Layer (type)                 Output Shape              Param #
4.  =================================================================
5.  input_1 (InputLayer)         [(None, 28, 28, 1)]       0
6.  _________________________________________________________________
7.  conv2d (Conv2D)              (None, 14, 14, 32)        320
8.  _________________________________________________________________
9.  leaky_re_lu (LeakyReLU)      (None, 14, 14, 32)        0
10.  _________________________________________________________________
11.  batch_normalization (BatchNo (None, 14, 14, 32)        128
12.  _________________________________________________________________
13.  conv2d_1 (Conv2D)            (None, 7, 7, 64)          18496
14.  _________________________________________________________________
15.  leaky_re_lu_1 (LeakyReLU)    (None, 7, 7, 64)          0
16.  _________________________________________________________________
17.  batch_normalization_1 (Batch (None, 7, 7, 64)          256
18.  _________________________________________________________________
19.  flatten (Flatten)            (None, 3136)              0
20.  _________________________________________________________________
21.  dense (Dense)                (None, 16)                50192
22.  =================================================================
23.  Total params: 69,392
24.  Trainable params: 69,200
25.  Non­trainable params: 192
26.  _________________________________________________________________

Here we can oserve that :

Our encoder egins  accepting a 28x28x1 input volume .

We then appl two rounds of  CONV=>RELU=>BN , each with 3×3 strided


.
convolution  The strided convolution allows us to reduce the spatial

dimensions of our volumes .

,
After appling our 몭nal atch normalization  we end up with a 7x7x64 volume ,
which is 몭attened into a 3136 dim vector - .

-
Our full connected laer  i e  the  ( . ., Dense )
 laer  serves our as our latent-
space representation .

, ’
Next  let s learn how the decoder model can take this latent space -
representation and reconstruct the original input image :

→ Launch Jupter Noteook on Google Cola

Autoencoders with Keras, TensorFlow, and Deep Learning
43.      # start building the decoder model which will accept the
44.      # output of the encoder as its inputs
45.      latentInputs = Input(shape=(latentDim,))
46.      x = Dense(np.prod(volumeSize[1:]))(latentInputs)
47.      x = Reshape((volumeSize[1], volumeSize[2], volumeSize[3]))(x)
48.  
49.      # loop over our number of filters again, but this time in
50.      # reverse order
51.      for f in filters[::­1]:
52.        # apply a CONV_TRANSPOSE => RELU => BN operation
53.        x = Conv2DTranspose(f, (3, 3), strides=2,
54.          padding="same")(x)
55.        x = LeakyReLU(alpha=0.2)(x)
56.        x = BatchNormalization(axis=chanDim)(x)
To start uilding the decoder model  we , :

Construct the input to the decoder model ased on the  latentDim . (Lines


45 and 46 ).

Accept the 1D  latentDim  vector and turn it into a 2D volume so that we can

start appling convolution  Line 47 ( ).

,
Loop over the numer of 몭lters  this time in reverse order while appling a 

CONV_TRANSPOSE => RELU => BN (
 operation  Lines 51 56 - ).

Transposed convolution is used to increase the spatial dimensions  i e  width ( . .,
)
and height  of the volume .


Let s 몭nish creating our autoencoder :

→ Launch Jupter Noteook on Google Cola

Autoencoders with Keras, TensorFlow, and Deep Learning
58.      # apply a single CONV_TRANSPOSE layer used to recover the
59.      # original depth of the image
60.      x = Conv2DTranspose(depth, (3, 3), padding="same")(x)
61.      outputs = Activation("sigmoid")(x)
62.  
63.      # build the decoder model
64.      decoder = Model(latentInputs, outputs, name="decoder")
65.  
66.      # our autoencoder is the encoder + decoder
67.      autoencoder = Model(inputs, decoder(encoder(inputs)),
68.        name="autoencoder")
69.  
70.      # return a 3­tuple of the encoder, decoder, and autoencoder
71.      return (encoder, decoder, autoencoder)

Wrapping up  we , :

Appl a 몭nal  CONV_TRANSPOSE  laer used to recover the original channel

(
depth of the image  1 channel for single channel grascale images or 3 /
channels for RG images  on Line 60 ) .

Appl a sigmoid activation function  Line 61 ( ).

uild the  decoder ,
 model  and add it with the  encoder  to the  autoencoder
(Lines 64-68). The autoencoder ecomes the encoder + decoder.

- ,
Return a 3 tuple of the encoder  decoder  and autoencoder , .
If we were to complete a  print(decoder.summary()) ,
 operation here  we would

have the following :

→ Launch Jupter Noteook on Google Cola

Autoencoders with Keras, TensorFlow, and Deep Learning
1.  Model: "decoder"
2.  _________________________________________________________________
3.  Layer (type)                 Output Shape              Param #
4.  =================================================================
5.  input_2 (InputLayer)         [(None, 16)]              0
6.  _________________________________________________________________
7.  dense_1 (Dense)              (None, 3136)              53312
8.  _________________________________________________________________
9.  reshape (Reshape)            (None, 7, 7, 64)          0
10.  _________________________________________________________________
11.  conv2d_transpose (Conv2DTran (None, 14, 14, 64)        36928
12.  _________________________________________________________________
13.  leaky_re_lu_2 (LeakyReLU)    (None, 14, 14, 64)        0
14.  _________________________________________________________________
15.  batch_normalization_2 (Batch (None, 14, 14, 64)        256
16.  _________________________________________________________________
17.  conv2d_transpose_1 (Conv2DTr (None, 28, 28, 32)        18464
18.  _________________________________________________________________
19.  leaky_re_lu_3 (LeakyReLU)    (None, 28, 28, 32)        0
20.  _________________________________________________________________
21.  batch_normalization_3 (Batch (None, 28, 28, 32)        128
22.  _________________________________________________________________
23.  conv2d_transpose_2 (Conv2DTr (None, 28, 28, 1)         289
24.  _________________________________________________________________
25.  activation (Activation)      (None, 28, 28, 1)         0
26.  =================================================================
27.  Total params: 109,377
28.  Trainable params: 109,185
29.  Non­trainable params: 192
30.  _________________________________________________________________

The  decoder -
 accepts our 16 dim latent representation from the  encoder  and

- - ,
then uilds a new full connected laer of 3136 dim  which is the product of 7 x 7

=
x 64   3136 .

- ,
Using our new 3136 dim FC laer  we reshape it into a 3D volume of 7 x 7 x 64 .
From there we can start appling our  CONV_TRANSPOSE=>RELU=>BN  operation .
,
Unlike standard strided convolution  which is used to decrease volume size  our ,
transposed convolution is used to increase volume size .

,
Finall  a transposed convolution laer is applied to recover the original channel

. ,
depth of the image  ince our images are grascale  we learn a single 몭lter  the ,
( . .,
output of which is a 28 x 28 x 1 volume  i e  the dimensions of the original MNIT

digit images ).

A  print(autoencoder.summary())  operation shows the composed nature of
the encoder and decoder :

→ Launch Jupter Noteook on Google Cola

Autoencoders with Keras, TensorFlow, and Deep Learning
1.  Model: "autoencoder"
2.  _________________________________________________________________
3.  Layer (type)                 Output Shape              Param #
4.  =================================================================
5.  input_1 (InputLayer)         [(None, 28, 28, 1)]       0
6.  _________________________________________________________________
7.  encoder (Model)              (None, 16)                69392
8.  _________________________________________________________________
9.  decoder (Model)              (None, 28, 28, 1)         109377
10.  =================================================================
11.  Total params: 178,769
12.  Trainable params: 178,385
13.  Non­trainable params: 384
14.  _________________________________________________________________

The input to our  encoder  is the original 28 x 28 x 1 images from the MNIT

.
dataset  Our  encoder - -
 then learns a 16 dim latent space representation of the

,
data  after which the  decoder  reconstructs the original 28 x 28 x 1 images .

,
In the next section  we will develop our script to train our autoencoder .

Creating the convolutional autoencoder training

script

, ’
With our autoencoder architecture implemented  let s move on to the training

script .

Open up the  train_conv_autoencoder.py  in our project director structure ,


and insert the following code :

→ Launch Jupter Noteook on Google Cola

Autoencoders with Keras, TensorFlow, and Deep Learning
1.  # set the matplotlib backend so figures can be saved in the background
2.  import matplotlib
3.  matplotlib.use("Agg")
4.  
5.  # import the necessary packages
6.  from pyimagesearch.convautoencoder import ConvAutoencoder
7.  from tensorflow.keras.optimizers import Adam
8.  from tensorflow.keras.datasets import mnist
9.  import matplotlib.pyplot as plt
10.  import numpy as np
11.  import argparse
12.  import cv2
13.  
14.  # construct the argument parse and parse the arguments
15.  ap = argparse.ArgumentParser()
16.  ap.add_argument("­s", "­­samples", type=int, default=8,
17.    help="# number of samples to visualize when decoding")
18.  ap.add_argument("­o", "­­output", type=str, default="output.png",
19.    help="path to output visualization file")
20.  ap.add_argument("­p", "­­plot", type=str, default="plot.png",
21.    help="path to output plot file")
22.  args = vars(ap.parse_args())

- ,
On Lines 2 12  we handle our imports  We ll use the  . ’ "Agg"  ackend of 

matplotlib  so that we can export our training plot to disk .

We need our custom  ConvAutoencoder  architecture class which we

implemented in the previous section .

We will use the  Adam  optimizer as we train on the MNIT enchmarking

. , ’
dataset  For visualization  we ll emplo OpenCV .

, ’ ,
Next  we ll parse three command line arguments  all of which are optional :

­­samples : The numer of output samples for visualization.  default this
value is set to  8 .

­­output : The path the output visualization image. We’ll name our
visualization output.png  default

­­plot : The path to our matplotli output plot. A default of plot.png is
assigned if this argument is not provided in the terminal.


Now we ll set a couple hperparameters and preprocess our MNIT dataset :

→ Launch Jupter Noteook on Google Cola

Autoencoders with Keras, TensorFlow, and Deep Learning
24.  # initialize the number of epochs to train for and batch size
25.  EPOCHS = 25
26.  BS = 32
27.  
28.  # load the MNIST dataset
29.  print("[INFO] loading MNIST dataset...")
30.  ((trainX, _), (testX, _)) = mnist.load_data()
31.  
32.  # add a channel dimension to every image in the dataset, then scale
33.  # the pixel intensities to the range [0, 1]
34.  trainX = np.expand_dims(trainX, axis=­1)
35.  testX = np.expand_dims(testX, axis=­1)
36.  trainX = trainX.astype("float32") / 255.0
37.  testX = testX.astype("float32") / 255.0

Lines 25 and 26 initialize the atch size and numer of training epochs .
, ’ .
From there  we ll work with our MNIT dataset  TensorFlow Keras has a hand  /
load_data  method that we can call on  mnist (
 to gra the data  Line 30  From ).
, - ()
there  Lines 34 37  1  add a channel dimension to ever image in the dataset and

(2) scale the pixel intensities to the range [0, 1].


We re now read to uild and train our autoencoder :

→ Launch Jupter Noteook on Google Cola

Autoencoders with Keras, TensorFlow, and Deep Learning
39.  # construct our convolutional autoencoder
40.  print("[INFO] building autoencoder...")
41.  (encoder, decoder, autoencoder) = ConvAutoencoder.build(28, 28, 1)
42.  opt = Adam(lr=1e­3)
43.  autoencoder.compile(loss="mse", optimizer=opt)
44.  
45.  # train the convolutional autoencoder
46.  H = autoencoder.fit(
47.    trainX, trainX,
48.    validation_data=(testX, testX),
49.    epochs=EPOCHS,
50.    batch_size=BS)

To uild the convolutional autoencoder  we call the  , build  method on our 

ConvAutoencoder (
 class and pass the necessar arguments  Line 41  Recall that ).
this results in the  (encoder, decoder, autoencoder) — going forward in
 tuple 

,
this script  we onl need the  autoencoder  for training and predictions.

We initialize our  Adam  optimizer with an initial learning rate of  1e­3  and go

-
ahead and compile it with mean squared error loss  Lines 42 and 43 ( ).

From there  we  , fit ( )


  train  our  autoencoder  on the MNIT data  Lines 46 50 ( - ).


Let s go ahead and plot our training histor :

→ Launch Jupter Noteook on Google Cola

Autoencoders with Keras, TensorFlow, and Deep Learning
52.  # construct a plot that plots and saves the training history
53.  N = np.arange(0, EPOCHS)
54.  plt.style.use("ggplot")
55.  plt.figure()
56.  plt.plot(N, H.history["loss"], label="train_loss")
57.  plt.plot(N, H.history["val_loss"], label="val_loss")
58.  plt.title("Training Loss and Accuracy")
59.  plt.xlabel("Epoch #")
60.  plt.ylabel("Loss/Accuracy")
61.  plt.legend(loc="lower left")
62.  plt.savefig(args["plot"])

, ’
And from there  we ll make predictions on our testing set :
→ Launch Jupter Noteook on Google Cola

Autoencoders with Keras, TensorFlow, and Deep Learning
64.  # use the convolutional autoencoder to make predictions on the
65.  # testing images, then initialize our list of output images
66.  print("[INFO] making predictions...")
67.  decoded = autoencoder.predict(testX)
68.  outputs = None
69.  
70.  # loop over our number of output samples
71.  for i in range(0, args["samples"]):
72.    # grab the original image and reconstructed image
73.    original = (testX[i] * 255).astype("uint8")
74.    recon = (decoded[i] * 255).astype("uint8")
75.  
76.    # stack the original and reconstructed image side­by­side
77.    output = np.hstack([original, recon])
78.  
79.    # if the outputs array is empty, initialize it as the current
80.    # side­by­side image display
81.    if outputs is None:
82.      outputs = output
83.  
84.    # otherwise, vertically stack the outputs
85.    else:
86.      outputs = np.vstack([outputs, output])
87.  
88.  # save the outputs image to disk
89.  cv2.imwrite(args["output"], outputs)

.
Line 67 makes predictions on the test set  We then loop over the numer of 

­­samples (
 passed as a command line argument  Line 71  so that we can uild )
.
our visualization  Inside the loop  we , :

Gra oth the original and reconstructed images  Lines 73 and 74 ( ).

- -
tack the pair of images side  side  Line 77 ( ).

(
tack the pairs verticall  Lines 81 86 - ).

,
Finall  we output the visualization image to disk  Line 89 ( ).

, ’
In the next section  we ll see the results of our hard work .

Training the convolutional autoencoder with Keras

and TensorFlow

We are now read to see our autoencoder in action !

“ ”
Make sure ou use the  Downloads  section of this post to download the source
code  — from there ou can execute the following command:

→ Launch Jupter Noteook on Google Cola

Autoencoders with Keras, TensorFlow, and Deep Learning
1.  $ python train_conv_autoencoder.py
2.  [INFO] loading MNIST dataset...
3.  [INFO] building autoencoder...
4.  Train on 60000 samples, validate on 10000 samples
5.  Epoch 1/25
6.  60000/60000 [==============================] ­ 68s 1ms/sample ­ loss: 0.0188 ­ 
val_loss: 0.0108
7.  Epoch 2/25
8.  60000/60000 [==============================] ­ 68s 1ms/sample ­ loss: 0.0104 ­ 
val_loss: 0.0096
9.  Epoch 3/25
10.  60000/60000 [==============================] ­ 68s 1ms/sample ­ loss: 0.0094 ­ 
val_loss: 0.0086
11.  Epoch 4/25
12.  60000/60000 [==============================] ­ 68s 1ms/sample ­ loss: 0.0088 ­ 
val_loss: 0.0086
13.  Epoch 5/25
14.  60000/60000 [==============================] ­ 68s 1ms/sample ­ loss: 0.0084 ­ 
val_loss: 0.0080
15.  ...
16.  Epoch 20/25
17.  60000/60000 [==============================] ­ 83s 1ms/sample ­ loss: 0.0067 ­ 
val_loss: 0.0069
18.  Epoch 21/25
19.  60000/60000 [==============================] ­ 83s 1ms/sample ­ loss: 0.0066 ­ 
val_loss: 0.0069
20.  Epoch 22/25
21.  60000/60000 [==============================] ­ 83s 1ms/sample ­ loss: 0.0066 ­ 
val_loss: 0.0068
22.  Epoch 23/25
23.  60000/60000 [==============================] ­ 83s 1ms/sample ­ loss: 0.0066 ­ 
val_loss: 0.0068
24.  Epoch 24/25
25.  60000/60000 [==============================] ­ 83s 1ms/sample ­ loss: 0.0065 ­ 
val_loss: 0.0067
26.  Epoch 25/25
27.  60000/60000 [==============================] ­ 83s 1ms/sample ­ loss: 0.0065 ­ 
val_loss: 0.0068
28.  [INFO] making predictions...
: .
Figure 4  Our deep learning autoencoder training histor plot was generated with matplotli  Our

, ,
autoencoder was trained with Keras  TensorFlow  and Deep Learning .

,
As Figure 4 and the terminal output demonstrate  our training process was ale

to minimize the reconstruction loss of the autoencoder .

ut how well did the autoencoder do at reconstructing the training data ?

The answer is ver good :

: /
Figure 5  A sample of of Keras TensorFlow deep

learning autoencoder inputs (left) and outputs (right).

,
In Figure 5  on the left is our original image while the right is the reconstructed

.
digit predicted  the autoencoder  As ou can see  the digits are nearl ,
indistinguishale from each other !

,
At this point  ou ma e thinking :
“ Great … so I can train a network to reconstruct m original image.

representation .
-
ut ou said that what reall matters is the internal latent space

,
How can I access that representation  and how can I use it for denoising

/
and anomal outlier detection ?

Those are great questions  — I’ll e addressing oth in m next two tutorials
,
here on PImageearch  so sta tuned !

' ?
What s next  I recommend PImageearch

Universit .

Course information   :
• • : /
13 total classes   21h 2m video   Last updated  4 2021 

★★★★★ 4.84 (128 Ratings) • 3,690 tudents nrolled

I strongl elieve that if ou had the right teacher ou could master

computer vision and deep learning .
Do ou think learning computer vision and deep learning has to e

- , ,
time consuming  overwhelming  and complicated  Or has to involve ?
?
complex mathematics and equations  Or requires a degree in

computer science ?


That s not the case .

All ou need to master computer vision and deep learning is for

,
someone to explain things to ou in simple  intuitive terms  And that s . ’
.
exactl what I do  M mission is to change education and how complex

Arti몭cial Intelligence topics are taught .

'
If ou re serious aout learning computer vision  our next stop should ,
,
e PImageearch Universit  the most comprehensive computer

, ,
vision  deep learning  and OpenCV course online toda  Here ou ll . ’
learn how to successfull and con몭dentl appl computer vision to

, , .
our work  research  and projects  Join me in computer vision master .

Inside PImageearch Universit ou ll 몭nd ' :

✓ 13 courses on essential computer vision, deep learning, and
OpenCV topics

✓ 13 Certi몭cates of Completion

✓ 21h 2m on-demand video

✓ rand new courses released ever month, ensuring ou can keep
up with state-of-the-art techniques

✓ Pre-con몭gured Jupter Noteooks in Google Cola

✓ Run all code examples in our we rowser — works on Windows,
macO, and Linux (no dev environment con몭guration required!)

✓ Access to centralized code repos for all 400+ tutorials on
PImageearch
✓ as one-click downloads for code, datasets, pre-trained models,
etc.

✓ Access on moile, laptop, desktop, etc.

C L I C K   H  R    TO   J O I N   P Y I M AG    A R C H   U N I V  R  I T Y

ummar

,
In this tutorial  ou learned the fundamentals of autoencoders .

Autoencoders are generative models that consist of an encoder and a decoder

. ,
model  When trained  the encoder takes input data point and learns a latent -
.
space representation of the data  This latent space representation is a -
,
compressed representation of the data  allowing the model to represent it in far

fewer parameters than the original data.

-
The decoder model then takes the latent space representation and attempts to

reconstruct the original data point from it. When trained end-to-end, the encoder

and decoder function in a composed manner.

,
In practice  we use autoencoders for dimensionalit reduction  compression , ,
,
denoising  and anomal detection .

,
After we understood the fundamentals  we implemented a convolutional

autoencoder using Keras and TensorFlow .

’ , ’
In next week s tutorial  we ll learn how to use a convolutional autoencoder for

denoising .

(
To download the source code to this post  and e noti몭ed when future

tutorials are pulished here on PImageearch ), just enter our email address
in the form elow !
Download the ource Code and FR 17 page -
Resource Guide

.
nter our email address elow to get a  zip of the code and a FR 17 page -
, ,
Resource Guide on Computer Vision  OpenCV  and Deep Learning  Inside ou ll . '
- , , ,
몭nd m hand picked tutorials  ooks  courses  and liraries to help ou master CV

and DL !

Your email address D O W N L OA D   T H    C O D  !
Aout the Author

, ’ , .
Hi there  I m Adrian Roserock  PhD  All too often I see developers  students, ,
, ,
and researchers wasting their time  studing the wrong things  and generall

struggling to get started with Computer Vision, Deep Learning, and OpenCV. I

created this wesite to show ou what I elieve is the est possile wa to

get our start .

Previous Article :
‘ ’
OpenCV  dnn  with NVIDIA GPUs  1549 : % faster YOLO, D, and
Mask R-CNN

Next Article :
,
Denoising autoencoders with Keras  TensorFlow  and Deep Learning ,

:
20 responses to  Autoencoders with Keras ,
,
TensorFlow  and Deep Learning

hamala

, :
Feruar 17  2020 at 7 00 pm

.. ..
Hi sir  I am a research scholar  I need a guidance for doing text textt mining

on deep learning using medical text ..

Adrian Roserock
Adrian Roserock

,
Feruar 18  2020 at 1 57 pm :

What do ou mean   medical text “ ”?

Gilles ONNT

,
Feruar 24  2020 at 4 34 pm :

:
Catched our point   Medical doctors have awfull handwriting and onl few

can read them ut medical world  ure a deep learning ased sstem ..

would e helpfull to  decode  their writings ” … ut this is not the purpose of
this article  ..
:
TW   ver helpfull and well explained article  Congratulations Adrian ..

Adrian Roserock

Feruar 27  2020 at 9 03 am , :

Thanks Gilles !

Aki

,
Feruar 18  2020 at 3 59 pm :

Which is performing etter nowadas in Anomal Detection   ?
Does machine learning approach outperform deep learning approach from

our experienc ?

Adrian Roserock

,
Feruar 20  2020 at 9 14 am :


It s reall dependent on the project itself and how ou de몭ne the

“anomal”. I’ll e going into more detail in the anomal detection post so
sta tuned!
panneer selvam

, :
Feruar 25  2020 at 12 53 am

. . .
I have tensor몭ow 1 12 0 installed for m GPU  Please tell me whether our

. . . . .
code of encoders will work in 1 12 0 version  Also I am using keras 2 2 4

Adrian Roserock

,
Feruar 27  2020 at 9 03 am :

. .
This code was developed using TensorFlow 2 0  The code should still work

ut I have not tested with TensorFlow 1 12 . .

Alex Retana

, :
Feruar 25  2020 at 4 13 pm

- ,
On lines 34 35  ou extended the dimensions of the training and test data

nump arra .

? ’
What purpose does this serve  I don t quite see wh it was done or where it

.
ecomes important for the future  Do ou have an clari몭cations for this ?

Alex Retana

,
Feruar 25  2020 at 4 16 pm :

I just thought aout it more :

Is this done ecause the MNIT Dataset comes in a single channel ?
Adrian Roserock

,
Feruar 27  2020 at 9 02 am :

,
Correct  we need to explicitl add the channel dimension .

Han

,
March 18  2020 at 12 18 am :

.
Thanks  Great post and well explained   .
, (
I have a question  I am using MoileNet  pre trained from Keras  I want to - ),
appl autoencoders to it to enhance the result of the network  I want to uild (
a small visual search   ).
( )
ut  from m understanding  Conv autoencoders are CNN itself  so  how can , ,
this e done ?

,
Thanks again  and I appreciate our repl .

Mario

,
March 21  2020 at 9 08 am :

What is the di몭erence etween this model  encoder  decoder  autoencoder ( , , )


?
and the sequential model  Does it have an advantages ?

Thanks

Luke eed

,
April 3  2020 at 12 47 pm :

Hi Adrian ,
I went through this tutorial and had no prolems with our code uilding  as I (
), ,
would expect  training  and reproducing the correct output .

(
I adapted the code for m image set  which has non standard dimensions -
× ; )
74 385 and is color  so 74x385x3  which turned out to e quite the pain .
Apparentl the padding features of Conv2D don t work ver well and the 몭nal ’
’ .
output shapes don t match the input shapes  I eventuall resized to 72x384x3

which seemed to allow me to train .

.
M prolem is that when I go to see the output png  몭nal step  all I am getting ( )
.
is lack output  Do ou think the prolem is that m autoencoder couldn t ’
(
train from the dataset or is there a code issue  from switching the resolution

and to color rather than the grescale MNIT)? 

Thank ou.

haha

,
April 4  2020 at 10 12 am :


I m ver appreciate aout our wonderful post   .
Just curious aout the sequence etween LeakReLU and N   .
What is the di몭erence etween placing the N laer efore or after the

activation function ?

Adrian Roserock

,
April 9  2020 at 8 43 am :

. ’
It depends on our architecture  I don t like placing the activation efore

the N as that squashes an activations less than 0  That doesn t make . ’


.
sense prior to appling a N laer  I discuss that in more detail inside Deep

Learning for Computer Vision with Pthon .
Muashir

,
April 10  2020 at 7 14 am :

! .
Hi  Adrian thanks for our highl relevant tutorials  Would e grateful if ou

could kindl share a tutorial aout using autoencoders for processing

numeric data like those generated through atches from sensors  De몭nitel .
,
for such data  Convolutional Autoencoders might not e useful !

Adrian Roserock

,
April 15  2020 at 1 06 pm :

, ’
Thanks Muashir  I m glad ou enjoed the log post  however ; ,

PImageearch is a computer vision log and I don t tpicall cover non -
’ ’
CV tasks so it s prett unlikel I ll cover a numeric data example .

Walid

,
April 11  2020 at 6 19 pm :

Great article

: . ()
I think ou meant  encoder summar  and not print encoder summar ( . ())

Adrian Roserock

,
April 15  2020 at 1 08 pm :

“ ”
The  print  is required if ou are executing it via the command line which

this tutorial assumes ou are doing .
Comment section

, ,
He  Adrian Roserock here  author and creator of PImageearch  While I .
,
love hearing from readers  a couple ears ago I made the tough decision to

:
no longer o몭er 1 1 help over log post comments .

+
At the time I was receiving 200  emails per da and another 100  log +
.
post comments  I simpl did not have the time to moderate and respond to

,
them all  and the shear volume of requests was taking a toll on me .

,
Instead  m goal is to do the most good for the computer vision  deep ,
,
learning  and OpenCV communit at large  focusing m time on

- , ,
authoring high qualit log posts  tutorials  and ooks courses / .

If ou need help learning computer vision and deep learning  I suggest ,
ou refer to m full catalog of ooks and courses  — the have helped
, ,
tens of thousands of developers  students  and researchers just like

,
ourself learn Computer Vision  Deep Learning  and OpenCV , .

Click here to rowse m full catalog .

PImageearch Universit  — NOW NROLLING!

,
You can master Computer Vision  Deep Learning  and OpenCV ,

Course information   :
• • : /
13 total classes   21h 2m video   Last updated  4 2021 

★★★★★ 
4.84 (128 Ratings) • 3,690 tudents nrolled

✓ 13 courses on essential computer vision, deep learning, and OpenCV topics 

✓ 13 Certi몭cates of Completion 

✓ 21h 2m on-demand video 

✓ rand new courses released ever month, ensuring ou can keep up with state-of-

-
the art techniques 

✓ Pre-con몭gured Jupter Noteooks in Google Cola 

✓ Run all code examples in our we rowser — works on Windows, macO, and Linux

(no dev environment con몭guration required!) 

✓ Access to centralized code repos for all 400+ tutorials on PImageearch 

✓ as one-click downloads for code, datasets, pre-trained models, etc. 

✓ Access on moile, laptop, desktop, etc.

JOIN NOW

Picked For You

Mixing normal images and adversarial images when training CNNs
Defending against adversarial image attacks with Keras and TensorFlow

(
Adversarial attacks with FGM  Fast Gradient ign Method)

Contrastive Loss for iamese Networks with Keras and TensorFlow

, ,
Comparing images for similarit using siamese networks  Keras  and TensorFlow

imilar articles
O P  N C V   T U TO R I A L  RAPRRY PI T U TO R I A L 

How to install OpenCV 3 on Raspian Jessie

Octoer 26  2015 ,

DP LARNING DL4CV

Con몭guring Uuntu for deep learning with Pthon

eptemer 25  2017 ,

OJCT DTCTION O P  N C V   T U TO R I A L  T U TO R I A L 

AprilTag with Pthon

Novemer 2  2020 ,
You can learn Computer Vision  Deep Learning  and , ,
OpenCV .
, ,
Get our FR 17 page Computer Vision  OpenCV  and Deep Learning Resource

. ’ - , , ,
Guide PDF  Inside ou ll 몭nd m hand picked tutorials  ooks  courses  and liraries to

help ou master CV and DL .

Your email address D O W N L OA D   F O R   F R  

Topics Machine Learning and Computer Vision

Medical Computer Vision
Deep Learning

Dli Lirar
Optical Character Recognition  OCR( )

/
medded IoT and Computer Vision
Oject Detection

Oject Tracking
Face Applications

OpenCV Tutorials
Image Processing

Rasperr Pi
Interviews

Keras

ooks  & Courses PImageearch


, ,
FR CV  DL  and OpenCV Crash Course Get tarted

Practical Pthon and OpenCV OpenCV Install Guides

Deep Learning for Computer Vision with Aout

Pthon
FAQ

PImageearch Gurus Course
log

Rasperr Pi for Computer Vision
Contact

Privac Polic

© 2021 PImageearch. All Rights Reserved.

You might also like