Deep Learning for Mechanical Engineering

Homework 09

Due Monday, 11/13/2023, 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.

  • Only .ipynb files will be graded for your code.

    • 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

The Turing test, developed by Alan Turing in 1950, is a test of a machine's ability to exhibit intelligent behaviour equivalent to, or indistinguishable from, that of a human. Turing proposed that a human evaluator would judge natural language conversations between a human and a machine designed to generate human-like responses. The evaluator would be aware that one of the two partners in conversation is a machine, and all participants would be separated from one another. The conversation would be limited to a text-only channel such as a computer keyboard and screen so the result would not depend on the machine's ability to render words as speech. If the evaluator cannot reliably tell the machine from the human, the machine is said to have passed the test. The test results do not depend on the machine's ability to give correct answers to questions, only how closely its answers resemble those a human would give.

(1) Explain the GAN algorithm using the figure provided below.

(2) You are also asked to write loss functions and outline optimizaton procedure as well using the figure provided below..


(3) While training a GAN to generate high-quality human face images, you notice that the generator's cost, $J^{(G)}$, is extremely low. However, the generated images lack meaningful output images. Can you explain this phenomenon in the context of the GAN's loss functions?

  • hint: Non-Saturating Game

Problem 2

In the GAN example, we employed the following fully connected neural networks. However, we discovered that DCGAN is a direct extension of GAN, differing primarily in its utilization of convolutional and convolutional-transpose layers in the discriminator and generator. Now, let's construct a custom generative model for generating MNIST digit 2 using these convolutional and convolutional-transpose layers.

In [ ]:
## your code here
#

Problem 3

By the CycleGAN algorithm, we can even change the style of images. In this problem, we will change the white-colored digit in a black background image to the black-colored digit in a white background image.We will use the following CycleGAN structure.





Description

  • Domain A: white-colored number in a black background
  • Domain B: black-colored number in a white background
  • Generate Xab image by generator Gab

    • the structure of Gab is like an autoencoder
  • Generate Xaba image by generator Gba

    • the structure of Gba is like an autoencoder
  • Train the style to Gab generate styled image Xab
    • give reconstructed Xab image as a fake image and style image as a real image to the discriminator.

Note: I strongly recommend you to use either GPU or Colab. Don't use CPU to train your model! It will take a long time to complete training.

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

(1) Load images corresponding to the number 1 from the MNIST dataset. Then use half as they are and for the other half, invert the colors so that the background is white and the digits are black. Notice that they are not paired datasets.

In [ ]:
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()

train_x = train_images[np.where(train_labels == 1)]/255.0
half_index = train_x.shape[0] // 2

# unpaired dataset
wnum = train_x[:half_index].reshape(-1,784)

bnum = train_x[half_index:].reshape(-1,784)
bnum = np.abs(1 - bnum)
In [ ]:
# plot a randomly selected image from wnum

## your code here
#
In [ ]:
# plot a randomly selected image from bnum

## your code here
#

(2) Define your model.

  • Generator AB
    • n_G_input: 28 $\times$ 28
    • n_G_encoder1: 256
    • n_G_latent1: 50
    • n_G_decoder1: 256
  • Generator BA
    • n_G_encoder2: 256
    • n_G_latent2: 50
    • n_G_decoder2: 256
  • Discriminator
    • n_D_input: 28 $\times$ 28
    • n_D_hidden: 256
    • n_D_output: 2
In [ ]:
Gab = # your code here
In [ ]:
Gba = # your code here
In [ ]:
discriminator = # your code here
In [ ]:
# Discriminator compile

## your code here
#

(3) Refer to the loss function below and train the model. And plot Xab image and Xaba image for each print. Note that we are not asking you to make a perfect inverted image.


1: Fix $G$ and perform a gradient step to


$$\min_{D} E_{x \sim p_{\text{data}}(x)}\left[-\log D(x)\right] + E_{x \sim p_{z}(z)}\left[-\log (1-D(G(z)))\right]$$


2: Fix $D$ and perform a gradient step to


$$\min_{G} E_{x \sim p_{z}(z)}\left[-\log D(G(z))\right]$$


3: Minimize reconstruct error


$$\min \, (X_a-X_{aba})^2$$
In [ ]:
# Genreator AB

## your code here
#
In [ ]:
# Generator BA

## your code here
#
In [ ]:
def plot_generated_images(imgs, Gab, Gba):

    idx = np.random.randint(0, imgs.shape[0], 1 )

    Xab = Gab.predict(imgs[idx].reshape(-1, 28*28), verbose = 0)
    Xaba = Gba.predict(Xab.reshape(-1, 28*28), verbose = 0)

    plt.figure(figsize = (8, 3))
    plt.subplot(1, 3, 1)
    plt.imshow(imgs[idx].reshape(28, 28), 'gray', interpolation = 'nearest')
    plt.title('Xa')
    plt.axis('off')
    plt.tight_layout()

    plt.subplot(1, 3, 2)
    plt.imshow(Xab.reshape(28, 28), 'gray', interpolation = 'nearest')
    plt.title('Xab')
    plt.axis('off')
    plt.tight_layout()

    plt.subplot(1, 3, 3)
    plt.imshow(Xaba.reshape(28, 28), 'gray', interpolation = 'nearest')
    plt.title('Xaba')
    plt.axis('off')
    plt.tight_layout()
    plt.show()
In [ ]:
## your code here
#
Epoch :  0
D_loss: 2.678629
G_loss: 1.177035
Epoch :  5000
D_loss: 0.025170
G_loss: 4.507219
Epoch :  10000
D_loss: 0.029599
G_loss: 4.747540
Epoch :  15000
D_loss: 0.001686
G_loss: 6.903833
Epoch :  20000
D_loss: 0.113272
G_loss: 3.335354
Epoch :  25000
D_loss: 0.796075
G_loss: 1.335942