Professional Documents
Culture Documents
DP LARNING 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 Roserock on Feruar 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 ( ’ )
3 , ,
Anomal detection with Keras TensorFlow and Deep Learning tutorial two (
weeks from now )
,
A few weeks ago I pulished 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 PImageearch
log !
-
Tring 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 aout 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
, ’
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
aout deep learning applied to image datasets .
What are autoencoders ?
Autoencoders are a tpe of unsupervised neural network i e no class laels or ( . .,
)
laeled data that seek to :
1 Accept an input set of data i e the input( . ., ).
3 Reconstruct the input data from this latent representation i e the output ( . ., ).
,
Tpicall we think of an autoencoder having two components sunetworks / :
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 responsile 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 Pthon 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
’ , :
Huens article Deep inside Autoencoders )
Here ou can see that :
1 We input a digit to the autoencoder .
2 The encoder sunetwork creates a latent representation of the digit This .
latent representation is sustantiall smaller in terms of dimensionalit ( )
than the input .
3 The decoder sunetwork 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 .
: , ,
Figure 2 Autoencoders are useful for compression dimensionalit reduction denoising and ,
/ . , ’ /
anomal outlier detection In this tutorial we ll use Pthon 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 troule 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 aout 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 alwas) care more aout the encoder
and the latent-space.
, ’ .
Later in this tutorial we ll e training an autoencoder on the MNIT dataset The
MNIT dataset consists of digits that are 28×28 pixels with a single channel,
impling that each digit is represented 28 x 28 = 784 values. The
’
autoencoder we ll e training here will e ale 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 tpicall 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 Pthon and , ’
/
Keras TensorFlow to train a deep learning autoencoder image source .( )
Autoencoders are tpicall 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 mislaeled data points in a dataset
or detecting when an input data point falls well outside our tpical data
distriution ).
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 .
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 .
, -
Tpicall 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 passale 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 sstem up to speed :
.
How to install TensorFlow 2 0 on Uuntu Uuntu 18 04 O CPU and ( . ;
optional NVIDIA GPU )
.
How to install TensorFlow 2 0 on macO Catalina and Mojave Oes ( )
:
Please note PImageearch 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 laout/ :
→ Launch Jupter Noteook 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 Pthon scripts toda :
train_conv_autoencoder.py : Trains a digits autoencoder on the MNIT
dataset. Once the autoencoder is trained, we’ll loop over a numer of output
examples and write them to disk for later inspection.
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 contriution 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 Jupter Noteook 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
“Con몭guring our development environment” section aove.
width : Width of the input image in pixels.
height : Height of the input image in pixels.
depth : Numer 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.
’
We re now read to initialize our input and egin adding laers to our network :
→ Launch Jupter Noteook 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 numer of filters and add our
sets of CONV=>LeakyReLU=>BN (
laers 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 ).
latentDim=16 , we would have the following:
→ Launch Jupter Noteook 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. Nontrainable params: 192
26. _________________________________________________________________
Here we can oserve that :
Our encoder egins accepting a 28x28x1 input volume .
dimensions of our volumes .
,
After appling our 몭nal atch normalization we end up with a 7x7x64 volume ,
which is 몭attened into a 3136 dim vector - .
-
Our full connected laer i e the ( . ., Dense )
laer 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 Jupter Noteook 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 , :
start appling convolution Line 47 ( ).
,
Loop over the numer of 몭lters this time in reverse order while appling 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 Jupter Noteook 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 3tuple of the encoder, decoder, and autoencoder
71. return (encoder, decoder, autoencoder)
Wrapping up we , :
(
depth of the image 1 channel for single channel grascale 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 Jupter Noteook 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. Nontrainable params: 192
30. _________________________________________________________________
The decoder -
accepts our 16 dim latent representation from the encoder and
- - ,
then uilds a new full connected laer of 3136 dim which is the product of 7 x 7
=
x 64 3136 .
- ,
Using our new 3136 dim FC laer we reshape it into a 3D volume of 7 x 7 x 64 .
From there we can start appling 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 laer is applied to recover the original channel
. ,
depth of the image ince our images are grascale we learn a single 몭lter the ,
( . .,
output of which is a 28 x 28 x 1 volume i e the dimensions of the original MNIT
digit images ).
A print(autoencoder.summary()) operation shows the composed nature of
the encoder and decoder :
→ Launch Jupter Noteook 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. Nontrainable params: 384
14. _________________________________________________________________
.
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 .
→ Launch Jupter Noteook 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 .
implemented in the previous section .
. , ’
dataset For visualization we ll emplo OpenCV .
, ’ ,
Next we ll parse three command line arguments all of which are optional :
samples : The numer 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 hperparameters and preprocess our MNIT dataset :
→ Launch Jupter Noteook 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 numer of training epochs .
, ’ .
From there we ll work with our MNIT 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 Jupter Noteook 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=1e3)
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)
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.
-
ahead and compile it with mean squared error loss Lines 42 and 43 ( ).
’
Let s go ahead and plot our training histor :
→ Launch Jupter Noteook 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 Jupter Noteook 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 sidebyside
77. output = np.hstack([original, recon])
78.
79. # if the outputs array is empty, initialize it as the current
80. # sidebyside 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 numer 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 Jupter Noteook 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 ale
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 ,
indistinguishale 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 PImageearch so sta tuned !
' ?
What s next I recommend PImageearch
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 aout learning computer vision our next stop should ,
,
e PImageearch 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 .
✓ 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 Jupter Noteooks 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
PImageearch
✓ as one-click downloads for code, datasets, pre-trained models,
etc.
✓ Access on moile, 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 pulished here on PImageearch ), 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 liraries to help ou master CV
and DL !
Your email address D O W N L OA D T H C O D !
Aout the Author
, ’ , .
Hi there I m Adrian Roserock PhD All too often I see developers students, ,
, ,
and researchers wasting their time studing the wrong things and generall
struggling to get started with Computer Vision, Deep Learning, and OpenCV. I
created this wesite to show ou what I elieve is the est possile 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
hamala
, :
Feruar 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 Roserock
Adrian Roserock
,
Feruar 18 2020 at 1 57 pm :
What do ou mean medical text “ ”?
Gilles ONNT
,
Feruar 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 sstem ..
“
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 Roserock
Thanks Gilles !
Aki
,
Feruar 18 2020 at 3 59 pm :
Which is performing etter nowadas in Anomal Detection ?
Does machine learning approach outperform deep learning approach from
our experienc ?
Adrian Roserock
,
Feruar 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
, :
Feruar 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 Roserock
,
Feruar 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
, :
Feruar 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
,
Feruar 25 2020 at 4 16 pm :
I just thought aout it more :
Is this done ecause the MNIT Dataset comes in a single channel ?
Adrian Roserock
,
Feruar 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 MoileNet 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 :
Thanks
Luke eed
,
April 3 2020 at 12 47 pm :
Hi Adrian ,
I went through this tutorial and had no prolems 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 prolem is that when I go to see the output png 몭nal step all I am getting ( )
.
is lack output Do ou think the prolem 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 grescale MNIT)?
Thank ou.
haha
,
April 4 2020 at 10 12 am :
’
I m ver appreciate aout our wonderful post .
Just curious aout the sequence etween LeakReLU and N .
What is the di몭erence etween placing the N laer efore or after the
activation function ?
Adrian Roserock
,
April 9 2020 at 8 43 am :
. ’
It depends on our architecture I don t like placing the activation efore
Learning for Computer Vision with Pthon .
Muashir
,
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 aout 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 Roserock
,
April 15 2020 at 1 06 pm :
, ’
Thanks Muashir I m glad ou enjoed the log post however ; ,
’
PImageearch is a computer vision log and I don t tpicall 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 Roserock
,
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 Roserock here author and creator of PImageearch 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 .
PImageearch 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 Jupter Noteooks 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 PImageearch
✓ as one-click downloads for code, datasets, pre-trained models, etc.
✓ Access on moile, 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 FGM 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 RAPRRY PI T U TO R I A L
How to install OpenCV 3 on Raspian Jessie
Octoer 26 2015 ,
DP LARNING DL4CV
Con몭guring Uuntu for deep learning with Pthon
eptemer 25 2017 ,
OJCT DTCTION O P N C V T U TO R I A L T U TO R I A L
AprilTag with Pthon
Novemer 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 liraries 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 Lirar
Optical Character Recognition OCR( )
/
medded IoT and Computer Vision
Oject Detection
Oject Tracking
Face Applications
OpenCV Tutorials
Image Processing
Rasperr Pi
Interviews
Keras
Practical Pthon and OpenCV OpenCV Install Guides
Deep Learning for Computer Vision with Aout
Pthon
FAQ
PImageearch Gurus Course
log
Rasperr Pi for Computer Vision
Contact
Privac Polic
© 2021 PImageearch. All Rights Reserved.