Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2025 hot fixes #8

Merged
merged 3 commits into from
Feb 11, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
dev(C4W3 assignment): update to tf2.18
  • Loading branch information
cfav-dev committed Feb 7, 2025
commit fcc66ba94b469de8c35ef2e6c661301bd3431622
167 changes: 67 additions & 100 deletions Course 4 - Generative Deep Learning/W3/assignment/C4W3_Assignment.ipynb
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
"source": [
"# Week 3: Variational Autoencoders on Anime Faces\n",
"\n",
"For this exercise, you will train a Variational Autoencoder (VAE) using the [anime faces dataset by MckInsey666](https://github.com/bchao1/Anime-Face-Dataset). \n",
"\n",
"You will train the model using the techniques discussed in class. At the end, you should save your model and download it from Colab so that it can be submitted to the autograder for grading."
"In this lab, you will train a Variational Autoencoder (VAE) using the [anime faces dataset by MckInsey666](https://github.com/bchao1/Anime-Face-Dataset). You will develop the model using the techniques discussed in class. At the end, you will use this model to generate a gallery of anime faces. Remember to download this model from Colab so you can submit it to the autograder."
]
},
{
Expand All @@ -22,45 +20,6 @@
"***Important:*** *This colab notebook has read-only access so you won't be able to save your changes. If you want to save your work periodically, please click `File -> Save a Copy in Drive` to create a copy in your account, then work from there.* "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Use the Fallback Runtime**\n",
"\n",
"This notebook is optimized for a previous Colab runtime. While our team updates it, please use the fallback runtime to ensure you can use the GPU and run the cells accordingly. Otherwise, you might get slow training times or encounter errors. [Please follow the instructions here to use the fallback](https://community.deeplearning.ai/t/tf-at-using-the-fallback-runtime/530596). The test below will check if Colab is using the expected runtime."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"\n",
"EXPECTED = 10\n",
"CURRENT = sys.version_info[1]\n",
"\n",
"assert CURRENT == EXPECTED, \"Please use the Fallback runtime as mentioned above\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Install packages for compatibility with the Colab GPU and conversion for the autograder\n",
"\n",
"# NOTE: You can safely ignore errors about version incompatibility of\n",
"# Colab-bundled packages (e.g. xarray, pydantic, etc.)\n",
"\n",
"!pip install tf-keras==2.15 --quiet\n",
"!pip install tensorflow==2.15 --quiet\n",
"!pip install keras==2.15 --quiet"
]
},
{
"cell_type": "markdown",
"metadata": {
Expand Down Expand Up @@ -197,7 +156,7 @@
"\n",
" image = tf.cast(image, dtype=tf.float32)\n",
" image = tf.image.resize(image, (IMAGE_SIZE, IMAGE_SIZE))\n",
" image = image / 255.0 \n",
" image = image / 255.0\n",
" image = tf.reshape(image, shape=(IMAGE_SIZE, IMAGE_SIZE, 3,))\n",
"\n",
" return image"
Expand Down Expand Up @@ -297,7 +256,7 @@
" '''Displays input and predicted images.'''\n",
" plt.figure(figsize=(15, 5))\n",
" display_one_row(disp_input_images, 0, shape=(IMAGE_SIZE,IMAGE_SIZE,3))\n",
" display_one_row(disp_predicted, 20, shape=(IMAGE_SIZE,IMAGE_SIZE,3))\n"
" display_one_row(disp_predicted, 20, shape=(IMAGE_SIZE,IMAGE_SIZE,3))"
]
},
{
Expand Down Expand Up @@ -366,7 +325,7 @@
"class Sampling(tf.keras.layers.Layer):\n",
" def call(self, inputs):\n",
" \"\"\"Generates a random sample and combines with the encoder output\n",
" \n",
"\n",
" Args:\n",
" inputs -- output tensor from the encoder\n",
"\n",
Expand All @@ -380,7 +339,45 @@
" epsilon = None\n",
" z = None\n",
" ### END CODE HERE ###\n",
" return z"
" return z"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ps0yuE1d_cQc"
},
"source": [
"### Kullback–Leibler Divergence\n",
"\n",
"Next, you will define a layer to compute the [Kullback–Leibler Divergence](https://arxiv.org/abs/2002.07514) loss. This will be used to improve the generative capability of the model. This code is already given.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "tngFmDDwnDn-"
},
"outputs": [],
"source": [
"class KLDLoss(tf.keras.layers.Layer):\n",
" def call(self, inputs):\n",
" \"\"\"Computes the KLD loss and adds it to the model\n",
"\n",
" Args:\n",
" inputs -- tensor containing (mu, sigma)\n",
"\n",
" Returns:\n",
" kl_loss -- the computed Kullback–Leibler Divergence loss\n",
" \"\"\"\n",
"\n",
" mu, sigma = inputs\n",
" kl_loss = 1 + sigma - tf.square(mu) - tf.math.exp(sigma)\n",
" kl_loss = tf.reduce_mean(kl_loss) * -0.5\n",
" self.add_loss(kl_loss)\n",
"\n",
" return kl_loss"
]
},
{
Expand All @@ -391,9 +388,7 @@
"source": [
"### Encoder Layers\n",
"\n",
"Next, please use the Functional API to stack the encoder layers and output `mu`, `sigma` and the shape of the features before flattening. We expect you to use 3 convolutional layers (instead of 2 in the ungraded lab) but feel free to revise as you see fit. Another hint is to use `1024` units in the Dense layer before you get mu and sigma (we used `20` for it in the ungraded lab).\n",
"\n",
"*Note: If you did Week 4 before Week 3, please do not use LeakyReLU activations yet for this particular assignment. The grader for Week 3 does not support LeakyReLU yet. This will be updated but for now, you can use `relu` and `sigmoid` just like in the ungraded lab.*"
"Next, use the Functional API to stack the encoder layers and output `mu`, `sigma` and the shape of the features before flattening. We expect you to use 3 convolutional layers (instead of 2 in the ungraded lab) but feel free to revise as you see fit. Another hint is to use `1024` units in the Dense layer before you get mu and sigma (we used `20` for it in the ungraded lab)."
]
},
{
Expand Down Expand Up @@ -441,7 +436,7 @@
"source": [
"### Encoder Model\n",
"\n",
"You will feed the output from the above function to the `Sampling layer` you defined earlier. That will have the latent representations that can be fed to the decoder network later. Please complete the function below to build the encoder network with the `Sampling` layer."
"Now you will complete the function below to build the encoder network. You will feed the output from the function above to the `Sampling` layer you defined earlier. That will have the latent representations that can be fed to the decoder network later. The model should also use the `KLDLoss` layer to compute the Kullback–Leibler Divergence loss."
]
},
{
Expand All @@ -466,6 +461,7 @@
" inputs = None\n",
" mu, sigma, conv_shape = None\n",
" z = None\n",
" kl_loss = None\n",
" model = None\n",
" ### END CODE HERE ###\n",
" model.summary()\n",
Expand Down Expand Up @@ -494,7 +490,7 @@
"def decoder_layers(inputs, conv_shape):\n",
" \"\"\"Defines the decoder layers.\n",
" Args:\n",
" inputs -- output of the encoder \n",
" inputs -- output of the encoder\n",
" conv_shape -- shape of the features before flattening\n",
"\n",
" Returns:\n",
Expand Down Expand Up @@ -554,38 +550,6 @@
" return model"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ps0yuE1d_cQc"
},
"source": [
"### Kullback–Leibler Divergence\n",
"\n",
"Next, you will define the function to compute the [Kullback–Leibler Divergence](https://arxiv.org/abs/2002.07514) loss. This will be used to improve the generative capability of the model. This code is already given.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "tngFmDDwnDn-"
},
"outputs": [],
"source": [
"def kl_reconstruction_loss(mu, sigma):\n",
" \"\"\" Computes the Kullback-Leibler Divergence (KLD)\n",
" Args:\n",
" mu -- mean\n",
" sigma -- standard deviation\n",
"\n",
" Returns:\n",
" KLD loss\n",
" \"\"\"\n",
" kl_loss = 1 + sigma - tf.square(mu) - tf.math.exp(sigma)\n",
" return tf.reduce_mean(kl_loss) * -0.5"
]
},
{
"cell_type": "markdown",
"metadata": {
Expand All @@ -594,7 +558,7 @@
"source": [
"### Putting it all together\n",
"\n",
"Please define the whole VAE model. Remember to use `model.add_loss()` to add the KL reconstruction loss. This will be accessed and added to the loss later in the training loop."
"Now you will define the whole VAE model."
]
},
{
Expand All @@ -620,10 +584,6 @@
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" ### END CODE HERE ###\n",
" return model"
]
Expand Down Expand Up @@ -789,23 +749,23 @@
" # Iterate over the batches of the dataset.\n",
" for step, x_batch_train in enumerate(training_dataset):\n",
" with tf.GradientTape() as tape:\n",
" ### START CODE HERE ### \n",
" ### START CODE HERE ###\n",
" None\n",
" # Compute reconstruction loss\n",
" \n",
" \n",
" \n",
" \n",
"\n",
" \n",
"\n",
" grads = None\n",
" optimizer.apply_gradients(None)\n",
" ### END CODE HERE\n",
" \n",
"\n",
" loss_metric(loss)\n",
"\n",
" if step % 10 == 0:\n",
" display.clear_output(wait=False) \n",
" display.clear_output(wait=False)\n",
" generate_and_save_images(decoder, epoch, step, random_vector_for_generation)\n",
" print('Epoch: %s step: %s mean loss = %s' % (epoch, step, loss_metric.result().numpy()))"
]
Expand Down Expand Up @@ -885,7 +845,7 @@
" for col in range(cols):\n",
" grid[row*64:(row+1)*64, col*64:(col+1)*64, :] = images[row*cols + col]\n",
"\n",
" plt.figure(figsize=(12,12)) \n",
" plt.figure(figsize=(12,12))\n",
" plt.imshow(grid)\n",
" plt.title(title)\n",
" plt.show()\n",
Expand All @@ -908,29 +868,36 @@
"source": [
"## Save the Model\n",
"\n",
"Once you're satisfied with the results, you can save your model and upload it to the grader in Coursera."
"Once you're satisfied with the results, you can save your model and upload it to the grader in Coursera. Please run all succeeding cells to ensure that you will have a gradable submission. Otherwise, you might get this error message:\n",
"\n",
"`There was a problem grading your submission. Check stderr for more details.`\n",
"\n",
"First, save the model file in your Colab workspace."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"id": "ULCfGHEKkaO0"
},
"outputs": [],
"source": [
"vae.save(\"anime.h5\")"
"# Save the model you just trained\n",
"vae.save(\"anime.keras\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "NCd50-pubX_o"
"id": "QBRWKiGeedfg"
},
"outputs": [],
"source": [
"# You can use this cell as a shortcut for downloading your model\n",
"# You can also use this cell as a shortcut for downloading your model\n",
"from google.colab import files\n",
"files.download(\"anime.h5\")"
"files.download(\"anime.keras\")"
]
}
],
Expand Down Expand Up @@ -958,7 +925,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.9.6"
}
},
"nbformat": 4,
Expand Down