|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "#### Convolutional Neural Networks Using - TensorFlow, Theano, Keras" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "code", |
| 12 | + "execution_count": 1, |
| 13 | + "metadata": { |
| 14 | + "collapsed": true |
| 15 | + }, |
| 16 | + "outputs": [], |
| 17 | + "source": [ |
| 18 | + "# Convolutional Neural Network\n", |
| 19 | + "\n", |
| 20 | + "# Installing Theano\n", |
| 21 | + "# pip install --upgrade --no-deps git+git://github.com/Theano/Theano.git\n", |
| 22 | + "\n", |
| 23 | + "# Installing Tensorflow\n", |
| 24 | + "# Install Tensorflow from the website: https://www.tensorflow.org/versions/r0.12/get_started/os_setup.html\n", |
| 25 | + "\n", |
| 26 | + "# Installing Keras\n", |
| 27 | + "# pip install --upgrade keras" |
| 28 | + ] |
| 29 | + }, |
| 30 | + { |
| 31 | + "cell_type": "markdown", |
| 32 | + "metadata": {}, |
| 33 | + "source": [ |
| 34 | + "#### Dataset Problem Statement - It is an image classification problem where the dataset consists of images for dogs and cats. We will train a convolutional neural network to predict if an image is a photo of a dog or a cat. Even though classifying cats and dogs isn't very important, the primary goal is to build a model, which can later be used for any important image classification problem (ex. medical imaging, tumor detection)" |
| 35 | + ] |
| 36 | + }, |
| 37 | + { |
| 38 | + "cell_type": "code", |
| 39 | + "execution_count": 2, |
| 40 | + "metadata": {}, |
| 41 | + "outputs": [ |
| 42 | + { |
| 43 | + "name": "stderr", |
| 44 | + "output_type": "stream", |
| 45 | + "text": [ |
| 46 | + "Using TensorFlow backend.\n" |
| 47 | + ] |
| 48 | + } |
| 49 | + ], |
| 50 | + "source": [ |
| 51 | + "# Part 1 - Building the CNN\n", |
| 52 | + "\n", |
| 53 | + "# Importing the Keras libraries and packages\n", |
| 54 | + "from keras.models import Sequential\n", |
| 55 | + "from keras.layers import Convolution2D\n", |
| 56 | + "from keras.layers import MaxPooling2D\n", |
| 57 | + "from keras.layers import Flatten\n", |
| 58 | + "from keras.layers import Dense" |
| 59 | + ] |
| 60 | + }, |
| 61 | + { |
| 62 | + "cell_type": "code", |
| 63 | + "execution_count": 3, |
| 64 | + "metadata": {}, |
| 65 | + "outputs": [ |
| 66 | + { |
| 67 | + "name": "stderr", |
| 68 | + "output_type": "stream", |
| 69 | + "text": [ |
| 70 | + "C:\\Users\\Jayant\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:5: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(32, (3, 3), input_shape=(64, 64, 3..., activation=\"relu\")`\n", |
| 71 | + " \"\"\"\n", |
| 72 | + "C:\\Users\\Jayant\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:11: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(32, (3, 3), activation=\"relu\")`\n", |
| 73 | + " # This is added back by InteractiveShellApp.init_path()\n", |
| 74 | + "C:\\Users\\Jayant\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:18: UserWarning: Update your `Dense` call to the Keras 2 API: `Dense(activation=\"relu\", units=128)`\n", |
| 75 | + "C:\\Users\\Jayant\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:19: UserWarning: Update your `Dense` call to the Keras 2 API: `Dense(activation=\"sigmoid\", units=1)`\n" |
| 76 | + ] |
| 77 | + } |
| 78 | + ], |
| 79 | + "source": [ |
| 80 | + "# Initialising the CNN\n", |
| 81 | + "classifier = Sequential()\n", |
| 82 | + "\n", |
| 83 | + "# Step 1 - Convolution\n", |
| 84 | + "classifier.add(Convolution2D(32, 3, 3, input_shape = (64, 64, 3), activation = 'relu'))\n", |
| 85 | + "\n", |
| 86 | + "# Step 2 - Pooling\n", |
| 87 | + "classifier.add(MaxPooling2D(pool_size = (2, 2)))\n", |
| 88 | + "\n", |
| 89 | + "# Adding a second convolutional layer\n", |
| 90 | + "classifier.add(Convolution2D(32, 3, 3, activation = 'relu'))\n", |
| 91 | + "classifier.add(MaxPooling2D(pool_size = (2, 2)))\n", |
| 92 | + "\n", |
| 93 | + "# Step 3 - Flattening\n", |
| 94 | + "classifier.add(Flatten())\n", |
| 95 | + "\n", |
| 96 | + "# Step 4 - Full connection\n", |
| 97 | + "classifier.add(Dense(output_dim = 128, activation = 'relu'))\n", |
| 98 | + "classifier.add(Dense(output_dim = 1, activation = 'sigmoid'))\n", |
| 99 | + "\n", |
| 100 | + "# Compiling the CNN\n", |
| 101 | + "classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])" |
| 102 | + ] |
| 103 | + }, |
| 104 | + { |
| 105 | + "cell_type": "code", |
| 106 | + "execution_count": null, |
| 107 | + "metadata": { |
| 108 | + "collapsed": true |
| 109 | + }, |
| 110 | + "outputs": [], |
| 111 | + "source": [ |
| 112 | + "# Part 2 - Fitting the CNN to the images\n", |
| 113 | + "\n", |
| 114 | + "from keras.preprocessing.image import ImageDataGenerator\n", |
| 115 | + "\n", |
| 116 | + "train_datagen = ImageDataGenerator(rescale = 1./255,\n", |
| 117 | + " shear_range = 0.2,\n", |
| 118 | + " zoom_range = 0.2,\n", |
| 119 | + " horizontal_flip = True)\n", |
| 120 | + "\n", |
| 121 | + "test_datagen = ImageDataGenerator(rescale = 1./255)" |
| 122 | + ] |
| 123 | + }, |
| 124 | + { |
| 125 | + "cell_type": "code", |
| 126 | + "execution_count": null, |
| 127 | + "metadata": { |
| 128 | + "collapsed": true |
| 129 | + }, |
| 130 | + "outputs": [], |
| 131 | + "source": [ |
| 132 | + "training_set = train_datagen.flow_from_directory('dataset/training_set',\n", |
| 133 | + " target_size = (64, 64),\n", |
| 134 | + " batch_size = 32,\n", |
| 135 | + " class_mode = 'binary')\n", |
| 136 | + "\n", |
| 137 | + "test_set = test_datagen.flow_from_directory('dataset/test_set',\n", |
| 138 | + " target_size = (64, 64),\n", |
| 139 | + " batch_size = 32,\n", |
| 140 | + " class_mode = 'binary')" |
| 141 | + ] |
| 142 | + } |
| 143 | + ], |
| 144 | + "metadata": { |
| 145 | + "kernelspec": { |
| 146 | + "display_name": "Python 3", |
| 147 | + "language": "python", |
| 148 | + "name": "python3" |
| 149 | + }, |
| 150 | + "language_info": { |
| 151 | + "codemirror_mode": { |
| 152 | + "name": "ipython", |
| 153 | + "version": 3 |
| 154 | + }, |
| 155 | + "file_extension": ".py", |
| 156 | + "mimetype": "text/x-python", |
| 157 | + "name": "python", |
| 158 | + "nbconvert_exporter": "python", |
| 159 | + "pygments_lexer": "ipython3", |
| 160 | + "version": "3.6.1" |
| 161 | + } |
| 162 | + }, |
| 163 | + "nbformat": 4, |
| 164 | + "nbformat_minor": 2 |
| 165 | +} |
0 commit comments