<jupyter_start><jupyter_text>Exercise 6 - Convolutional AutoencoderIn this exercise we will construct a convolutional autoencoder for the sample of the CIFAR-10 dataset.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('data_batch_1', 'rb') as f:
    dat = pickle.load(f, encoding='bytes')<jupyter_output><empty_output><jupyter_text>As this is an unsupervised learning method, we are only interested in the image data.  Load the image data as per the previous exercise.<jupyter_code>images = np.zeros((10000, 32, 32, 3), dtype='uint8')

for idx, img in enumerate(dat[b'data']):
    images[idx, :, :, 0] = img[:1024].reshape((32, 32)) # Red
    images[idx, :, :, 1] = img[1024:2048].reshape((32, 32)) # Green
    images[idx, :, :, 2] = img[2048:].reshape((32, 32)) # Blue<jupyter_output><empty_output><jupyter_text>As we are using a convolutional network we can use the images with only rescaling.<jupyter_code>images = images / 255.<jupyter_output><empty_output><jupyter_text>Define the convolutional autoencoder model.  We will use the same shape input as an image.<jupyter_code>input_layer = Input(shape=(32, 32, 3,))<jupyter_output><empty_output><jupyter_text>Add a convolutional stage, with 32 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(
    32, # 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.  Max Pooling looks at all the values in an image, scanning through with a 2 x 2 matrix.  The maximum value in each 2 x 2 area is returned, thus reducing the size of the encoded layer by a half.<jupyter_code>encoded = MaxPooling2D((2, 2))(hidden_encoding)<jupyter_output><empty_output><jupyter_text>Add a decoding convolutional layer<jupyter_code>hidden_decoding = Conv2D(
    32, # 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>Now we need to return the image to its original size, for this we will upsample by the same size as Max Pooling.<jupyter_code>upsample_decoding = UpSampling2D((2, 2))(hidden_decoding)<jupyter_output><empty_output><jupyter_text>Add the final convolutional stage, using 3 layers for the RGB channels of the images.<jupyter_code>decoded = Conv2D(
    3, # 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, 32, 32, 3)         0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 32, 32, 32)        896       
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 16, 16, 32)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 16, 16, 32)        9248      
_________________________________________________________________
up_sampling2d_1 (UpSampling2 (None, 32, 32, 32)        0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 32, 32, 3)         867       
=================================================================
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 [==============================] - 21s 2ms/step - loss: 0.5934
Epoch 2/20
10000/10000 [==============================] - 21s 2ms/step - loss: 0.5687
Epoch 3/20
10000/10000 [==============================] - 22s 2ms/step - loss: 0.5633
Epoch 4/20
10000/10000 [==============================] - 21s 2ms/step - loss: 0.5602
Epoch 5/20
10000/10000 [==============================] - 21s 2ms/step - loss: 0.5590: 0s - loss: 0
Epoch 6/20
10000/10000 [==============================] - 21s 2ms/step - loss: 0.5581
Epoch 7/20
10000/10000 [==============================] - 21s 2ms/step - loss: 0.5578
Epoch 8/20
10000/10000 [==============================] - 21s 2ms/step - loss: 0.5572
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>Each encoded image has a shape of 16 x 16 x 32 due to the number of filters selected for the convolutional stage.  As such we cannot visualise them without modification.  We will reshape them to be 256 x 32 in size for visualisation.<jupyter_code>encoder_output = encoder_output.reshape((-1, 256, 32))<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>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])
    plt.axis('off')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14245735607675905, 'fraction_numerical': 0.05183901918976546, 'mean_word_length': 5.151639344262295, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '18', 'dir': 'jupyter-structured-clean-dedup', 'id': '36048', 'license': 'MIT', 'n_tokens_mistral': 2575, 'n_tokens_neox': 2227, 'n_words': 700, 'path': 'Lesson05/Exercise23/Exercise23.ipynb', 'repo_name': 'TrainingByPackt/Unsupervised-Learning-with-Python', 'size': '7504'}