Understanding Deepfakes with Keras
Task 1: Importing Libraries and Helper Functions
Please note: If you haven’t already, please install the required packages by executing the code cell below.
1 | # !pip3 install tensorflow==2.1.0 pillow matplotlib |
1 | %matplotlib notebook |
TensorFlow version: 2.1.0
Task 2: Importing and Plotting the Data
1 | (x_train, y_train), (x_test, y_test) = tfutils.datasets.mnist.load_data(one_hot=False) |
1 | x.shape |
(6903, 784)
1 | tfutils.datasets.mnist.plot_ten_random_examples(plt, x, np.zeros((x.shape[0], 1))).show() |
<IPython.core.display.Javascript object>
Task 3: Discriminator
1 | size = 28 |
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 13, 13, 64) 640
_________________________________________________________________
leaky_re_lu (LeakyReLU) (None, 13, 13, 64) 0
_________________________________________________________________
batch_normalization (BatchNo (None, 13, 13, 64) 256
_________________________________________________________________
conv2d_1 (Conv2D) (None, 5, 5, 128) 204928
_________________________________________________________________
leaky_re_lu_1 (LeakyReLU) (None, 5, 5, 128) 0
_________________________________________________________________
batch_normalization_1 (Batch (None, 5, 5, 128) 512
_________________________________________________________________
conv2d_2 (Conv2D) (None, 1, 1, 256) 819456
_________________________________________________________________
leaky_re_lu_2 (LeakyReLU) (None, 1, 1, 256) 0
_________________________________________________________________
batch_normalization_2 (Batch (None, 1, 1, 256) 1024
_________________________________________________________________
flatten (Flatten) (None, 256) 0
_________________________________________________________________
dense (Dense) (None, 1) 257
=================================================================
Total params: 1,027,073
Trainable params: 1,026,177
Non-trainable params: 896
_________________________________________________________________
Task 4: Generator
1 | generator = Sequential([ |
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_1 (Dense) (None, 256) 512
_________________________________________________________________
reshape (Reshape) (None, 1, 1, 256) 0
_________________________________________________________________
conv2d_transpose (Conv2DTran (None, 5, 5, 256) 1638656
_________________________________________________________________
batch_normalization_3 (Batch (None, 5, 5, 256) 1024
_________________________________________________________________
conv2d_transpose_1 (Conv2DTr (None, 9, 9, 128) 819328
_________________________________________________________________
batch_normalization_4 (Batch (None, 9, 9, 128) 512
_________________________________________________________________
conv2d_transpose_2 (Conv2DTr (None, 21, 21, 64) 204864
_________________________________________________________________
batch_normalization_5 (Batch (None, 21, 21, 64) 256
_________________________________________________________________
conv2d_transpose_3 (Conv2DTr (None, 25, 25, 32) 51232
_________________________________________________________________
batch_normalization_6 (Batch (None, 25, 25, 32) 128
_________________________________________________________________
conv2d_transpose_4 (Conv2DTr (None, 28, 28, 1) 513
=================================================================
Total params: 2,717,025
Trainable params: 2,716,065
Non-trainable params: 960
_________________________________________________________________
1 | noise = np.random.randn(1, noise_dim) |
<IPython.core.display.Javascript object>
Task 5: Generative Adversarial Network (GAN)
1 | input_layer = tf.keras.layers.Input(shape=(noise_dim,)) |
Model: "model"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, 1)] 0
_________________________________________________________________
sequential_1 (Sequential) (None, 28, 28, 1) 2717025
_________________________________________________________________
sequential (Sequential) (None, 1) 1027073
=================================================================
Total params: 3,744,098
Trainable params: 2,716,065
Non-trainable params: 1,028,033
_________________________________________________________________
Tasks 6 and 7: Training the GAN
1 | %%time |
Steps per epoch= 107
<IPython.core.display.Javascript object>
1 |