<jupyter_start><jupyter_text>Exercise 4 - MINST Convolutional Autoencoder (Instructor Version)In this exercise we will construct a convolutional autoencoder for the sample of the MNISTdataset.Import pickle, numpy, matplotlib as well as the *Model* class from **keras.models** and *Input* and *Conv2D*, *MaxPooling2D* and *UpSampling2D* from **keras.layers**.<jupyter_code>import pickle
import numpy as np
import matplotlib.pyplot as plt
from keras.models import Model
from keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D<jupyter_output>Using TensorFlow backend.<jupyter_text>Load the data<jupyter_code>with open('mnist.pkl', 'rb') as f:
    images = pickle.load(f)['images']<jupyter_output><empty_output><jupyter_text>Rescale the images to have values between 0 and 1.<jupyter_code>images = images / 255.<jupyter_output><empty_output><jupyter_text>We do need to reshape the images to add a single depth channel for use with convolutional stages.  Reshape the images to have a shape of 28 x 28 x 1.<jupyter_code>images = images.reshape((-1, 28, 28, 1))<jupyter_output><empty_output><jupyter_text>Define an input layer.  We will use the same shape input as an image.<jupyter_code>input_layer = Input(shape=(28, 28, 1,))<jupyter_output><empty_output><jupyter_text>Add a convolutional stage, with 16 layers or filters, a 3 x 3 weight matrix, a ReLU activation function and using **same** padding which means the output has the same length as the input image.<jupyter_code>hidden_encoding = Conv2D(
    16, # Number of layers or filters in the weight matrix
    (3, 3), # Shape of the weight matrix
    activation='relu',
    padding='same', # How to apply the weights to the images
)(input_layer)<jupyter_output>WARNING:tensorflow:From /home/ben/envs/packt/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.<jupyter_text>Add a max pooling layer to the encoder with a 2 x 2 kernel.<jupyter_code>encoded = MaxPooling2D((2, 2))(hidden_encoding)<jupyter_output><empty_output><jupyter_text>Add a decoding convolutional layer<jupyter_code>hidden_decoding = Conv2D(
    16, # Number of layers or filters in the weight matrix
    (3, 3), # Shape of the weight matrix
    activation='relu',
    padding='same', # How to apply the weights to the images
)(encoded)<jupyter_output><empty_output><jupyter_text>Add an upsampling layer.<jupyter_code>upsample_decoding = UpSampling2D((2, 2))(hidden_decoding)<jupyter_output><empty_output><jupyter_text>Add the final convolutional stage, using 1 layer as per the initial image depth<jupyter_code>decoded = Conv2D(
    1, # Number of layers or filters in the weight matrix
    (3, 3), # Shape of the weight matrix
    activation='sigmoid',
    padding='same', # How to apply the weights to the images
)(upsample_decoding)<jupyter_output><empty_output><jupyter_text>Construct the model by passing the first and last layers of the network to the Model class.<jupyter_code>autoencoder = Model(input_layer, decoded)<jupyter_output><empty_output><jupyter_text>Display the structure of the model<jupyter_code>autoencoder.summary()<jupyter_output>_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 28, 28, 1)         0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 28, 28, 16)        160       
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 14, 14, 16)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 14, 14, 16)        2320      
_________________________________________________________________
up_sampling2d_1 (UpSampling2 (None, 28, 28, 16)        0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 28, 28, 1)         145       
=================================================================
Total para[...]<jupyter_text>Compile the autoencoder using a binary cross entropy loss function and adadelta gradient descent.<jupyter_code>autoencoder.compile(loss='binary_crossentropy',
              optimizer='adadelta')<jupyter_output><empty_output><jupyter_text>Now let's fit the model, again we pass the images as the training data and as the desired output.  Train for 20 epochs as convolutional networks take a lot longer to compute.<jupyter_code>autoencoder.fit(images, images, epochs=20)<jupyter_output>WARNING:tensorflow:From /home/ben/envs/packt/lib/python3.7/site-packages/tensorflow/python/ops/math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.cast instead.
Epoch 1/20
10000/10000 [==============================] - 7s 746us/step - loss: 0.1165
Epoch 2/20
10000/10000 [==============================] - 7s 724us/step - loss: 0.0706
Epoch 3/20
10000/10000 [==============================] - 8s 786us/step - loss: 0.0685
Epoch 4/20
10000/10000 [==============================] - 8s 776us/step - loss: 0.0675
Epoch 5/20
10000/10000 [==============================] - 7s 728us/step - loss: 0.0669
Epoch 6/20
10000/10000 [==============================] - 8s 830us/step - loss: 0.0663
Epoch 7/20
10000/10000 [==============================] - 9s 880us/step - loss: 0.0659
Epoch 8/20
10000/10000 [==============================] - 9s 939us/step - loss: 0.0656
Epoch 9/20
10000/10000 [=================[...]<jupyter_text>Calculate and store the output of the encoding stage for the first 5 samples.<jupyter_code>encoder_output = Model(input_layer, encoded).predict(images[:5])<jupyter_output><empty_output><jupyter_text>Reshape the encoder output for visualisation.<jupyter_code>encoder_output = encoder_output.reshape((-1, 14 * 14, 16))<jupyter_output><empty_output><jupyter_text>Get the output of the decoder for the 5 images<jupyter_code>decoder_output = autoencoder.predict(images[:5])<jupyter_output><empty_output><jupyter_text>Reshape the decoder output to be 28 x 28 in size<jupyter_code>decoder_output = decoder_output.reshape((-1, 28, 28))<jupyter_output><empty_output><jupyter_text>Reshape the original images back to be 28 x 28 in size.<jupyter_code>images = images.reshape((-1, 28, 28))<jupyter_output><empty_output><jupyter_text>Plot the original image, the mean encoder output and the decoder.<jupyter_code>plt.figure(figsize=(10, 7))
for i in range(5):
    plt.subplot(3, 5, i + 1)
    plt.imshow(images[i], cmap='gray')
    plt.axis('off')
    
    plt.subplot(3, 5, i + 6)
    plt.imshow(encoder_output[i], cmap='gray')
    plt.axis('off')   
    
    plt.subplot(3, 5, i + 11)
    plt.imshow(decoder_output[i], cmap='gray')
    plt.axis('off')<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup