Deep Learning for Mechanical Engineering

Homework 08

Due Monday, 11/06/2021, 4:00 PM

Prof. Seungchul Lee
http://iailab.kaist.ac.kr/
Industrial AI Lab at KAIST
• For your handwritten solutions, please scan or take a picture of them. Alternatively, you can write them in markdown if you prefer.

• Ensure that your NAME and student ID are included in your .ipynb files. ex) IljeokKim_20202467_HW08.ipynb
• Compress all the files into a single .zip file.

• In the .zip file's name, include your NAME and student ID. ex) DogyeomPark_20202467_HW08.zip
• Submit this .zip file on KLMS
• Do not submit a printed version of your code, as it will not be graded.

# Problem 1: Convolutional Autoencoder (CAE)Â¶

In this problem, our objective is to develop a model capable of reconstructing dog images through a convolutional autoencoder architecture.

InÂ [Â ]:
import os
import cv2
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
InÂ [Â ]:
drive.mount('/content/drive')
Mounted at /content/drive
InÂ [Â ]:
#

dog_dataset =

(2) Design and train a convolutional autoencoder architecture.

InÂ [Â ]:
#
Epoch 1/50
313/313 [==============================] - 6s 12ms/step - loss: 0.0281
Epoch 2/50
313/313 [==============================] - 4s 14ms/step - loss: 0.0120
Epoch 3/50
313/313 [==============================] - 4s 12ms/step - loss: 0.0098
Epoch 4/50
313/313 [==============================] - 4s 12ms/step - loss: 0.0086
Epoch 5/50
313/313 [==============================] - 4s 12ms/step - loss: 0.0081
Epoch 6/50
313/313 [==============================] - 5s 15ms/step - loss: 0.0068
Epoch 7/50
313/313 [==============================] - 4s 11ms/step - loss: 0.0064
Epoch 8/50
313/313 [==============================] - 4s 11ms/step - loss: 0.0059
Epoch 9/50
313/313 [==============================] - 4s 13ms/step - loss: 0.0056
Epoch 10/50
313/313 [==============================] - 4s 13ms/step - loss: 0.0056
Epoch 11/50
313/313 [==============================] - 4s 12ms/step - loss: 0.0053
Epoch 12/50
313/313 [==============================] - 4s 12ms/step - loss: 0.0052
Epoch 13/50
313/313 [==============================] - 4s 13ms/step - loss: 0.0052
Epoch 14/50
313/313 [==============================] - 4s 13ms/step - loss: 0.0048
Epoch 15/50
313/313 [==============================] - 4s 12ms/step - loss: 0.0049
Epoch 16/50
313/313 [==============================] - 4s 12ms/step - loss: 0.0047
Epoch 17/50
313/313 [==============================] - 4s 14ms/step - loss: 0.0044
Epoch 18/50
313/313 [==============================] - 4s 12ms/step - loss: 0.0044
Epoch 19/50
313/313 [==============================] - 4s 12ms/step - loss: 0.0043
Epoch 20/50
313/313 [==============================] - 4s 12ms/step - loss: 0.0043
Epoch 21/50
313/313 [==============================] - 4s 14ms/step - loss: 0.0042
Epoch 22/50
313/313 [==============================] - 4s 12ms/step - loss: 0.0043
Epoch 23/50
313/313 [==============================] - 4s 12ms/step - loss: 0.0040
Epoch 24/50
313/313 [==============================] - 4s 12ms/step - loss: 0.0040
Epoch 25/50
313/313 [==============================] - 4s 14ms/step - loss: 0.0039
Epoch 26/50
313/313 [==============================] - 4s 12ms/step - loss: 0.0039
Epoch 27/50
313/313 [==============================] - 4s 12ms/step - loss: 0.0039
Epoch 28/50
313/313 [==============================] - 4s 13ms/step - loss: 0.0038
Epoch 29/50
313/313 [==============================] - 4s 13ms/step - loss: 0.0038
Epoch 30/50
313/313 [==============================] - 4s 11ms/step - loss: 0.0037
Epoch 31/50
313/313 [==============================] - 4s 11ms/step - loss: 0.0037
Epoch 32/50
313/313 [==============================] - 4s 14ms/step - loss: 0.0037
Epoch 33/50
313/313 [==============================] - 4s 12ms/step - loss: 0.0036
Epoch 34/50
313/313 [==============================] - 4s 12ms/step - loss: 0.0036
Epoch 35/50
313/313 [==============================] - 4s 12ms/step - loss: 0.0035
Epoch 36/50
313/313 [==============================] - 4s 14ms/step - loss: 0.0035
Epoch 37/50
313/313 [==============================] - 4s 12ms/step - loss: 0.0035
Epoch 38/50
313/313 [==============================] - 4s 12ms/step - loss: 0.0035
Epoch 39/50
313/313 [==============================] - 4s 12ms/step - loss: 0.0035
Epoch 40/50
313/313 [==============================] - 4s 14ms/step - loss: 0.0034
Epoch 41/50
313/313 [==============================] - 4s 12ms/step - loss: 0.0034
Epoch 42/50
313/313 [==============================] - 4s 12ms/step - loss: 0.0034
Epoch 43/50
313/313 [==============================] - 4s 13ms/step - loss: 0.0033
Epoch 44/50
313/313 [==============================] - 4s 13ms/step - loss: 0.0033
Epoch 45/50
313/313 [==============================] - 4s 12ms/step - loss: 0.0033
Epoch 46/50
313/313 [==============================] - 4s 12ms/step - loss: 0.0033
Epoch 47/50
313/313 [==============================] - 4s 14ms/step - loss: 0.0033
Epoch 48/50
313/313 [==============================] - 4s 12ms/step - loss: 0.0032
Epoch 49/50
313/313 [==============================] - 4s 12ms/step - loss: 0.0032
Epoch 50/50
313/313 [==============================] - 4s 12ms/step - loss: 0.0032
Out[Â ]:
<keras.src.callbacks.History at 0x7a69502e9e40>

(3) Select five random images and create plots to display both the original and reconstructed images.

InÂ [Â ]:
#
WARNING:matplotlib.image:Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
WARNING:matplotlib.image:Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).

(4) Load data of a white dog and a black dog.

• Resize each image to (64, 64, 3) and rescale the image pixels to a range of 0 to 1 by dividing them by 255.

InÂ [Â ]:
#

(5) Walk in the latent space

• Show the average pixel image of the black dog and white dog images in the original space.

• Show the decoded image after averaging the encoded representations of a white dog and a black dog in the latent space.

InÂ [Â ]:
#
1/1 [==============================] - 0s 73ms/step
1/1 [==============================] - 0s 21ms/step
WARNING:tensorflow:5 out of the last 10 calls to <function Model.make_predict_function.<locals>.predict_function at 0x7a6950258820> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has reduce_retracing=True option that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for  more details.
1/1 [==============================] - 0s 112ms/step

(6) Explain the reason for the difference between the two images in terms of the latent space.

InÂ [Â ]:
#

# Problem 2: SegmentationÂ¶

We studied the Fully Convolutional Network (FCN) model with the VGG16 network in class. In this problem, you will implement your FCN model using the VGG19 network as the encoder part of the model.

To achieve this, we will utilize a pre-trained VGG network in Problem 2 and then proceed with the FCN model in Problem 3.

InÂ [Â ]:
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import cv2

(1) Load the provided dataset and display its shape.

InÂ [Â ]:
#

train_imgs =
train_seg =
test_imgs =

n_train = train_imgs.shape[0]
n_test = test_imgs.shape[0]

print ("The number of training images : {}, shape : {}".format(n_train, train_imgs.shape))
print ("The number of testing images : {}, shape : {}".format(n_test, test_imgs.shape))
The number of training images : 289, shape : (289, 160, 576, 3)
The number of testing images : 290, shape : (290, 160, 576, 3)

(2) Visualize a randomly selected image from the training dataset.

InÂ [Â ]: