{ "cells": [ { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "x4HI2mpwlrcn" }, "source": [ "##### Copyright 2019 The TensorFlow Authors." ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "cellView": "form", "colab": {}, "colab_type": "code", "id": "679Lmwt3l1Bk" }, "outputs": [], "source": [ "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", "# You may obtain a copy of the License at\n", "#\n", "# https://www.apache.org/licenses/LICENSE-2.0\n", "#\n", "# Unless required by applicable law or agreed to in writing, software\n", "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", "# See the License for the specific language governing permissions and\n", "# limitations under the License." ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "DSPCom-KmApV" }, "source": [ "# Convolutional Neural Network (CNN)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "klAltGp8ycek" }, "source": [ "\u003ctable class=\"tfo-notebook-buttons\" align=\"left\"\u003e\n", " \u003ctd\u003e\n", " \u003ca target=\"_blank\" href=\"https://www.tensorflow.org/tutorials/images/cnn\"\u003e\n", " \u003cimg src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" /\u003e\n", " View on TensorFlow.org\u003c/a\u003e\n", " \u003c/td\u003e\n", " \u003ctd\u003e\n", " \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/tutorials/images/cnn.ipynb\"\u003e\n", " \u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003e\n", " Run in Google Colab\u003c/a\u003e\n", " \u003c/td\u003e\n", " \u003ctd\u003e\n", " \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/docs/blob/master/site/en/tutorials/images/cnn.ipynb\"\u003e\n", " \u003cimg src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003e\n", " View source on GitHub\u003c/a\u003e\n", " \u003c/td\u003e\n", " \u003ctd\u003e\n", " \u003ca href=\"https://storage.googleapis.com/tensorflow_docs/docs/site/en/tutorials/images/cnn.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/download_logo_32px.png\" /\u003eDownload notebook\u003c/a\u003e\n", " \u003c/td\u003e\n", "\u003c/table\u003e" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "qLGkt5qiyz4E" }, "source": [ "This tutorial demonstrates training a simple [Convolutional Neural Network](https://developers.google.com/machine-learning/glossary/#convolutional_neural_network) (CNN) to classify [CIFAR images](https://www.cs.toronto.edu/~kriz/cifar.html). Because this tutorial uses the [Keras Sequential API](https://www.tensorflow.org/guide/keras/overview), creating and training our model will take just a few lines of code.\n" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "m7KBpffWzlxH" }, "source": [ "### Import TensorFlow" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "iAve6DCL4JH4" }, "outputs": [], "source": [ "from __future__ import absolute_import, division, print_function, unicode_literals\n", "\n", "try:\n", " # %tensorflow_version only exists in Colab.\n", " %tensorflow_version 2.x\n", "except Exception:\n", " pass\n", "import tensorflow as tf\n", "\n", "from tensorflow.keras import datasets, layers, models\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "jRFxccghyMVo" }, "source": [ "### Download and prepare the CIFAR10 dataset\n", "\n", "\n", "The CIFAR10 dataset contains 60,000 color images in 10 classes, with 6,000 images in each class. The dataset is divided into 50,000 training images and 10,000 testing images. The classes are mutually exclusive and there is no overlap between them." ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "JWoEqyMuXFF4" }, "outputs": [], "source": [ "(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()\n", "\n", "# Normalize pixel values to be between 0 and 1\n", "train_images, test_images = train_images / 255.0, test_images / 255.0" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "7wArwCTJJlUa" }, "source": [ "### Verify the data\n", "\n", "To verify that the dataset looks correct, let's plot the first 25 images from the training set and display the class name below each image.\n" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "K3PAELE2eSU9" }, "outputs": [], "source": [ "class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',\n", " 'dog', 'frog', 'horse', 'ship', 'truck']\n", "\n", "plt.figure(figsize=(10,10))\n", "for i in range(25):\n", " plt.subplot(5,5,i+1)\n", " plt.xticks([])\n", " plt.yticks([])\n", " plt.grid(False)\n", " plt.imshow(train_images[i], cmap=plt.cm.binary)\n", " # The CIFAR labels happen to be arrays, \n", " # which is why you need the extra index\n", " plt.xlabel(class_names[train_labels[i][0]])\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "Oewp-wYg31t9" }, "source": [ "### Create the convolutional base" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "3hQvqXpNyN3x" }, "source": [ "The 6 lines of code below define the convolutional base using a common pattern: a stack of [Conv2D](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Conv2D) and [MaxPooling2D](https://www.tensorflow.org/api_docs/python/tf/keras/layers/MaxPool2D) layers.\n", "\n", "As input, a CNN takes tensors of shape (image_height, image_width, color_channels), ignoring the batch size. If you are new to these dimensions, color_channels refers to (R,G,B). In this example, you will configure our CNN to process inputs of shape (32, 32, 3), which is the format of CIFAR images. You can do this by passing the argument `input_shape` to our first layer.\n", "\n" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "L9YmGQBQPrdn" }, "outputs": [], "source": [ "model = models.Sequential()\n", "model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))\n", "model.add(layers.MaxPooling2D((2, 2)))\n", "model.add(layers.Conv2D(64, (3, 3), activation='relu'))\n", "model.add(layers.MaxPooling2D((2, 2)))\n", "model.add(layers.Conv2D(64, (3, 3), activation='relu'))" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "lvDVFkg-2DPm" }, "source": [ "Let's display the architecture of our model so far." ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "8-C4XBg4UTJy" }, "outputs": [], "source": [ "model.summary()" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "_j-AXYeZ2GO5" }, "source": [ "Above, you can see that the output of every Conv2D and MaxPooling2D layer is a 3D tensor of shape (height, width, channels). The width and height dimensions tend to shrink as you go deeper in the network. The number of output channels for each Conv2D layer is controlled by the first argument (e.g., 32 or 64). Typically, as the width and height shrink, you can afford (computationally) to add more output channels in each Conv2D layer." ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "_v8sVOtG37bT" }, "source": [ "### Add Dense layers on top\n", "To complete our model, you will feed the last output tensor from the convolutional base (of shape (3, 3, 64)) into one or more Dense layers to perform classification. Dense layers take vectors as input (which are 1D), while the current output is a 3D tensor. First, you will flatten (or unroll) the 3D output to 1D, then add one or more Dense layers on top. CIFAR has 10 output classes, so you use a final Dense layer with 10 outputs and a softmax activation." ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "mRs95d6LUVEi" }, "outputs": [], "source": [ "model.add(layers.Flatten())\n", "model.add(layers.Dense(64, activation='relu'))\n", "model.add(layers.Dense(10, activation='softmax'))" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "ipGiQMcR4Gtq" }, "source": [ "Here's the complete architecture of our model." ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "8Yu_m-TZUWGX" }, "outputs": [], "source": [ "model.summary()" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "xNKXi-Gy3RO-" }, "source": [ "As you can see, our (3, 3, 64) outputs were flattened into vectors of shape (576) before going through two Dense layers." ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "P3odqfHP4M67" }, "source": [ "### Compile and train the model" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "MdDzI75PUXrG" }, "outputs": [], "source": [ "model.compile(optimizer='adam',\n", " loss='sparse_categorical_crossentropy',\n", " metrics=['accuracy'])\n", "\n", "history = model.fit(train_images, train_labels, epochs=10, \n", " validation_data=(test_images, test_labels))" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "jKgyC5K_4O0d" }, "source": [ "### Evaluate the model" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "gtyDF0MKUcM7" }, "outputs": [], "source": [ "plt.plot(history.history['accuracy'], label='accuracy')\n", "plt.plot(history.history['val_accuracy'], label = 'val_accuracy')\n", "plt.xlabel('Epoch')\n", "plt.ylabel('Accuracy')\n", "plt.ylim([0.5, 1])\n", "plt.legend(loc='lower right')\n", "\n", "test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "0LvwaKhtUdOo" }, "outputs": [], "source": [ "print(test_acc)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "8cfJ8AR03gT5" }, "source": [ "Our simple CNN has achieved a test accuracy of over 70%. Not bad for a few lines of code! For another CNN style, see an example using the Keras subclassing API and a `tf.GradientTape` [here](https://www.tensorflow.org/tutorials/quickstart/advanced)." ] } ], "metadata": { "accelerator": "GPU", "colab": { "collapsed_sections": [], "name": "cnn.ipynb", "private_outputs": true, "provenance": [], "toc_visible": true, "version": "0.3.2" }, "kernelspec": { "display_name": "Python 3", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 0 }