{ "cells": [ { "cell_type": "markdown", "metadata": { "toc": true }, "source": [ "

Table of Contents

\n", "
" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# code for loading the format for the notebook\n", "import os\n", "\n", "# path : store the current path to convert back to it later\n", "path = os.getcwd()\n", "os.chdir(os.path.join('..', 'notebook_format'))\n", "\n", "from formats import load_style\n", "load_style(plot_style=False)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Using TensorFlow backend.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Ethen 2017-03-24 10:55:22 \n", "\n", "CPython 3.5.2\n", "IPython 5.3.0\n", "\n", "numpy 1.12.1\n", "pandas 0.19.2\n", "keras 2.0.2\n" ] } ], "source": [ "os.chdir(path)\n", "\n", "# 1. magic to print version\n", "# 2. magic so that the notebook will reload external python modules\n", "%load_ext watermark\n", "%load_ext autoreload \n", "%autoreload 2\n", "\n", "import numpy as np\n", "import pandas as pd\n", "import keras.backend as K\n", "from keras.datasets import mnist\n", "from keras.utils import np_utils\n", "from keras.models import Sequential\n", "from keras.layers import Conv2D, MaxPooling2D\n", "from keras.layers import Dense, Activation, Flatten\n", "\n", "%watermark -a 'Ethen' -d -t -v -p numpy,pandas,keras" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Convolutional Network" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "X_train shape: (60000, 28, 28)\n", "60000 train samples\n", "10000 test samples\n" ] } ], "source": [ "# loading the mnist dataset as an example\n", "(X_train, y_train), (X_test, y_test) = mnist.load_data()\n", "print('X_train shape:', X_train.shape)\n", "print(X_train.shape[0], 'train samples')\n", "print(X_test.shape[0] , 'test samples')" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "train shape: (60000, 28, 28, 1)\n" ] } ], "source": [ "# input image dimensions\n", "img_rows, img_cols = 28, 28\n", "\n", "# load training data and do basic data normalization\n", "(X_train, y_train), (X_test, y_test) = mnist.load_data()\n", "\n", "# the keras backend supports two different kind of image data format,\n", "# either channel first or channel last, we can detect it and transform\n", "# our raw data accordingly, if it's channel first, we add another dimension\n", "# to represent the depth (RGB color) at the very beginning (it is 1 here because\n", "# mnist is a grey scale image), if it's channel last, we add it at the end\n", "if K.image_data_format() == 'channels_first':\n", " X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols)\n", " X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)\n", " input_shape = (1, img_rows, img_cols)\n", "else:\n", " X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols, 1)\n", " X_test = X_test.reshape(X_test.shape[0], img_rows, img_cols, 1)\n", " input_shape = (img_rows, img_cols, 1)\n", "\n", "X_train = X_train.astype('float32')\n", "X_test = X_test.astype('float32')\n", "\n", "# images takes values between 0 - 255, we can normalize it\n", "# by dividing every number by 255\n", "X_train /= 255\n", "X_test /= 255\n", "print('train shape:', X_train.shape)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "y_train shape: (60000, 10)\n" ] } ], "source": [ "# one-hot encode the class (target) vectors\n", "n_class = 10\n", "y_train = np_utils.to_categorical(y_train, n_class)\n", "y_test = np_utils.to_categorical(y_test, n_class)\n", "print('y_train shape:', y_train.shape)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", "

\n", "The following code chunk takes A WHILE if you're running it on a laptop!!\n", "
" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Train on 60000 samples, validate on 10000 samples\n", "Epoch 1/12\n", "60000/60000 [==============================] - 67s - loss: 0.9482 - acc: 0.7719 - val_loss: 0.3632 - val_acc: 0.8982\n", "Epoch 2/12\n", "60000/60000 [==============================] - 65s - loss: 0.3059 - acc: 0.9115 - val_loss: 0.2427 - val_acc: 0.9294\n", "Epoch 3/12\n", "60000/60000 [==============================] - 71s - loss: 0.2126 - acc: 0.9388 - val_loss: 0.1648 - val_acc: 0.9513\n", "Epoch 4/12\n", "60000/60000 [==============================] - 67s - loss: 0.1419 - acc: 0.9589 - val_loss: 0.1065 - val_acc: 0.9704\n", "Epoch 5/12\n", "60000/60000 [==============================] - 68s - loss: 0.0985 - acc: 0.9719 - val_loss: 0.0786 - val_acc: 0.9766\n", "Epoch 6/12\n", "60000/60000 [==============================] - 65s - loss: 0.0752 - acc: 0.9784 - val_loss: 0.0665 - val_acc: 0.9802\n", "Epoch 7/12\n", "60000/60000 [==============================] - 73s - loss: 0.0636 - acc: 0.9816 - val_loss: 0.0605 - val_acc: 0.9813\n", "Epoch 8/12\n", "60000/60000 [==============================] - 69s - loss: 0.0560 - acc: 0.9838 - val_loss: 0.0578 - val_acc: 0.9815\n", "Epoch 9/12\n", "60000/60000 [==============================] - 64s - loss: 0.0519 - acc: 0.9848 - val_loss: 0.0517 - val_acc: 0.9831\n", "Epoch 10/12\n", "60000/60000 [==============================] - 66s - loss: 0.0463 - acc: 0.9864 - val_loss: 0.0511 - val_acc: 0.9834\n", "Epoch 11/12\n", "60000/60000 [==============================] - 63s - loss: 0.0429 - acc: 0.9872 - val_loss: 0.0512 - val_acc: 0.9834\n", "Epoch 12/12\n", "60000/60000 [==============================] - 68s - loss: 0.0402 - acc: 0.9884 - val_loss: 0.0490 - val_acc: 0.9844\n", "Test score: 0.0489532888404\n", "Test accuracy: 0.9844\n" ] } ], "source": [ "model = Sequential()\n", "\n", "# apply a 32 3x3 filters for the first convolutional layer\n", "# then we specify the `padding` to be 'same' so we get\n", "# the same width and height for the input (it will automatically do zero-padding),\n", "# the default stride is 1,\n", "# and since this is the first layer we need to specify the input shape of the image\n", "model.add(Conv2D(32, kernel_size = (3, 3), padding = 'same', input_shape = input_shape))\n", "\n", "# some activation function after conv layer\n", "model.add(Activation('relu'))\n", "model.add(Conv2D(64, kernel_size = (3, 3), padding = 'same'))\n", "model.add(Activation('relu'))\n", "\n", "# pooling layer, we specify the size of the filters for the pooling layer\n", "# the default `stride` is None, which will default to pool_size\n", "model.add(MaxPooling2D(pool_size = (2, 2)))\n", "\n", "# before calling the fully-connected layers, we'll have to flatten it\n", "model.add(Flatten())\n", "model.add(Dense(n_class))\n", "model.add(Activation('softmax'))\n", "model.compile(loss = 'categorical_crossentropy',\n", " optimizer = 'adam',\n", " metrics = ['accuracy'])\n", "\n", "n_epoch = 12\n", "batch_size = 2056\n", "model.fit(X_train, y_train, \n", " batch_size = batch_size, \n", " epochs = n_epoch,\n", " verbose = 1, \n", " validation_data = (X_test, y_test))\n", "\n", "# evaluating the score, categorical cross entropy error and accuracy\n", "score = model.evaluate(X_test, y_test, verbose = 0)\n", "print('Test score:', score[0])\n", "print('Test accuracy:', score[1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Reference" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- [Keras Example: mnist_cnn example](https://github.com/fchollet/keras/blob/master/examples/mnist_cnn.py)" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.4" }, "toc": { "nav_menu": { "height": "81px", "width": "252px" }, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": true, "toc_position": {}, "toc_section_display": "block", "toc_window_display": true }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 1 }