{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "Notebook_5_CNNs_from_scratch", "provenance": [], "collapsed_sections": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "accelerator": "GPU" }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "mY5WOZWrFI91", "colab_type": "text" }, "source": [ "# Neural Networks for Data Science Applications\n", "## Lab session 5: Building CNNs from scratch\n", "\n", "**Contents of the lab session:**\n", "+ Build flexible image pipelines in TF using tf.data.\n", "+ Defining a CNN architecture from scratch.\n", "+ Make code faster using `tf.function`." ] }, { "cell_type": "code", "metadata": { "id": "rnT0_RqFjcJr", "colab_type": "code", "outputId": "b5355bea-fe51-4eb5-865e-0cf9b740f993", "colab": { "base_uri": "https://localhost:8080/", "height": 156 } }, "source": [ "# Remember to enable a GPU on Colab by:\n", "# Runtime >> Change runtime type >> Hardware accelerator (before starting the VM).\n", "!pip install -q tensorflow-gpu==2.0.0" ], "execution_count": 1, "outputs": [ { "output_type": "stream", "text": [ "\u001b[K |████████████████████████████████| 380.8MB 44kB/s \n", "\u001b[K |████████████████████████████████| 3.8MB 44.3MB/s \n", "\u001b[K |████████████████████████████████| 450kB 45.4MB/s \n", "\u001b[K |████████████████████████████████| 81kB 8.8MB/s \n", "\u001b[31mERROR: tensorflow 1.15.0 has requirement tensorboard<1.16.0,>=1.15.0, but you'll have tensorboard 2.0.1 which is incompatible.\u001b[0m\n", "\u001b[31mERROR: tensorflow 1.15.0 has requirement tensorflow-estimator==1.15.1, but you'll have tensorflow-estimator 2.0.1 which is incompatible.\u001b[0m\n", "\u001b[31mERROR: tensorboard 2.0.1 has requirement grpcio>=1.24.3, but you'll have grpcio 1.15.0 which is incompatible.\u001b[0m\n", "\u001b[31mERROR: google-colab 1.0.0 has requirement google-auth~=1.4.0, but you'll have google-auth 1.7.1 which is incompatible.\u001b[0m\n", "\u001b[?25h" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "u3xJbtVsFsOj", "colab_type": "text" }, "source": [ "## About the task for this lab session" ] }, { "cell_type": "markdown", "metadata": { "id": "ANUF1kb7FjSZ", "colab_type": "text" }, "source": [ "The data can be downloaded as **Dataset 1** from here:\n", "https://github.com/SullyChen/driving-datasets\n", "\n", "Direct Drive link:\n", "https://drive.google.com/file/d/0B-KJCaaF7elleG1RbzVPZWV4Tlk/view?usp=sharing\n", "\n", "For each image (taken from a front-facing camera in a car), we want to predict the steering direction.)" ] }, { "cell_type": "markdown", "metadata": { "id": "nEuwwee9F5DR", "colab_type": "text" }, "source": [ "### Step 0 (ONLY COLAB): Load the Drive file\n", "This part is only needed in a Colab environment for downloading the Drive file.\n", "\n", "*Note*: the download code can be found in Colab from the left menu, under Code Snippets." ] }, { "cell_type": "code", "metadata": { "id": "Xilk9BqHl5KG", "colab_type": "code", "colab": {} }, "source": [ "# After this, restart the runtime to avoid authentication issues\n", "!pip install -q -U PyDrive" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "goMDxq-hkNpP", "colab_type": "code", "colab": {} }, "source": [ "# Import PyDrive and associated libraries.\n", "# This only needs to be done once per notebook.\n", "from pydrive.auth import GoogleAuth\n", "from pydrive.drive import GoogleDrive\n", "from google.colab import auth\n", "from oauth2client.client import GoogleCredentials\n", "\n", "# Authenticate and create the PyDrive client.\n", "# This only needs to be done once per notebook.\n", "auth.authenticate_user()\n", "gauth = GoogleAuth()\n", "gauth.credentials = GoogleCredentials.get_application_default()\n", "drive = GoogleDrive(gauth)" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "8O3QToxPkjbn", "colab_type": "code", "colab": {} }, "source": [ "# Download the file based on its file ID.\n", "# https://drive.google.com/file/d/0B-KJCaaF7elleG1RbzVPZWV4Tlk/view\n", "file_id = '0B-KJCaaF7elleG1RbzVPZWV4Tlk'\n", "downloaded = drive.CreateFile({'id': file_id})" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "LpjGMw7ioNfh", "colab_type": "code", "colab": {} }, "source": [ "downloaded.GetContentFile('driving_dataset.zip')" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "96X6DCWqpFFz", "colab_type": "code", "colab": {} }, "source": [ "!unzip -q driving_dataset.zip" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "5AS0D0WtpY4m", "colab_type": "code", "colab": {} }, "source": [ "# Explore the content! (Also from the left menu.)\n", "# You can see that image information is provided in a single .txt file,\n", "# making difficult the use of the ImageDataGenerator from tf.keras. \n", "# For this reason, we'll make a more flexible pipeline with tf.data.\n", "!ls driving_dataset" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "cy5FuAagGwpl", "colab_type": "text" }, "source": [ "### Step 1: Load the CSV file" ] }, { "cell_type": "code", "metadata": { "id": "XhRtM-rMp00M", "colab_type": "code", "colab": {} }, "source": [ "import pandas as pd\n", "files = pd.read_csv('driving_dataset/data.txt', \n", " header=None, sep=' ')" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "A19KBQmOqEGj", "colab_type": "code", "outputId": "f6bef716-0686-4600-9d98-85da3459acf2", "colab": { "base_uri": "https://localhost:8080/", "height": 202 } }, "source": [ "# First column is the file name, second column is the steering angle\n", "files.head()" ], "execution_count": 8, "outputs": [ { "output_type": "execute_result", "data": { "text/html": [ "
\n", " | 0 | \n", "1 | \n", "
---|---|---|
0 | \n", "0.jpg | \n", "0.0 | \n", "
1 | \n", "1.jpg | \n", "0.0 | \n", "
2 | \n", "2.jpg | \n", "0.0 | \n", "
3 | \n", "3.jpg | \n", "0.0 | \n", "
4 | \n", "4.jpg | \n", "0.0 | \n", "