Created
April 1, 2018 09:15
-
-
Save piyush2896/d66ccb5b3a5addf461ca376da9f2fe6e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Image Recognition\n", | |
"This notebook lets you make an image recognition neural network using keras - backend TensorFlow. Make sure to only alter the architecture of Neural Network." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"Using TensorFlow backend.\n" | |
] | |
} | |
], | |
"source": [ | |
"import keras\n", | |
"from keras.layers import Conv2D, MaxPooling2D, Dense, BatchNormalization, Dropout, Flatten\n", | |
"from keras.models import Model, Sequential\n", | |
"from keras.datasets import mnist, cifar10\n", | |
"from keras.utils.np_utils import to_categorical" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"MNIST_DATA = 0\n", | |
"CIFAR10_DATA = 1" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Helper Functions\n", | |
"\n", | |
"- **one_hot** - Takes y_train and y_test and returns them in one-hot form\n", | |
"- **load_data** - Take data_type (MNIST_DATA or CIFAR10_DATA) and loads data with y_train and y_test as one hot and x_train and x_test as a 4D array" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def one_hot(y_train, y_test):\n", | |
" return to_categorical(y_train), to_categorical(y_test)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def load_data(data_type=MNIST_DATA):\n", | |
" if data_type == MNIST_DATA:\n", | |
" (x_train, y_train), (x_test, y_test) = mnist.load_data()\n", | |
" y_train, y_test = one_hot(y_train, y_test)\n", | |
" x_train = np.expand_dims(x_train, axis=3)\n", | |
" x_test = np.expand_dims(x_test, axis=3)\n", | |
" return (x_train, y_train), (x_test, y_test)\n", | |
" elif data_type == CIFAR10_DATA:\n", | |
" (x_train, y_train), (x_test, y_test) = cifar10.load_data()\n", | |
" y_train, y_test = one_hot(y_train, y_test)\n", | |
" return (x_train, y_train), (x_test, y_test)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Some Params of Model\n", | |
"Update these params according to needs" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"INPUT_SHAPE = [28, 28, 1] # if data is cifar10 then [32, 32, 3]\n", | |
"\n", | |
"LEARNING_RATE = 1e-2\n", | |
"\n", | |
"DATA_TYPE = MNIST_DATA\n", | |
"\n", | |
"BATCH_SIZE = 256\n", | |
"\n", | |
"DROPOUT = 0.5\n", | |
"\n", | |
"EPOCHS = 5" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Do Not Change These Params" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"save_path_json = '../model/model.json'\n", | |
"save_path_weights = '../model/model.h5'" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Start Defining Model Architecture" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"_________________________________________________________________\n", | |
"Layer (type) Output Shape Param # \n", | |
"=================================================================\n", | |
"conv2d_1 (Conv2D) (None, 28, 28, 64) 640 \n", | |
"_________________________________________________________________\n", | |
"conv2d_2 (Conv2D) (None, 28, 28, 64) 36928 \n", | |
"_________________________________________________________________\n", | |
"max_pooling2d_1 (MaxPooling2 (None, 14, 14, 64) 0 \n", | |
"_________________________________________________________________\n", | |
"conv2d_3 (Conv2D) (None, 14, 14, 128) 73856 \n", | |
"_________________________________________________________________\n", | |
"conv2d_4 (Conv2D) (None, 14, 14, 128) 147584 \n", | |
"_________________________________________________________________\n", | |
"max_pooling2d_2 (MaxPooling2 (None, 7, 7, 128) 0 \n", | |
"_________________________________________________________________\n", | |
"conv2d_5 (Conv2D) (None, 7, 7, 256) 295168 \n", | |
"_________________________________________________________________\n", | |
"conv2d_6 (Conv2D) (None, 7, 7, 256) 590080 \n", | |
"_________________________________________________________________\n", | |
"max_pooling2d_3 (MaxPooling2 (None, 3, 3, 256) 0 \n", | |
"_________________________________________________________________\n", | |
"flatten_1 (Flatten) (None, 2304) 0 \n", | |
"_________________________________________________________________\n", | |
"dense_1 (Dense) (None, 10) 23050 \n", | |
"=================================================================\n", | |
"Total params: 1,167,306\n", | |
"Trainable params: 1,167,306\n", | |
"Non-trainable params: 0\n", | |
"_________________________________________________________________\n" | |
] | |
} | |
], | |
"source": [ | |
"model = Sequential()\n", | |
"\n", | |
"model.add(Conv2D(64, kernel_size=3, strides=1,\n", | |
" activation='relu', padding='same',\n", | |
" input_shape=INPUT_SHAPE))\n", | |
"model.add(Conv2D(64, kernel_size=3, strides=1,\n", | |
" activation='relu', padding='same'))\n", | |
"model.add(MaxPooling2D(2, strides=2))\n", | |
"\n", | |
"model.add(Conv2D(128, kernel_size=3, strides=1,\n", | |
" activation='relu', padding='same'))\n", | |
"model.add(Conv2D(128, kernel_size=3, strides=1,\n", | |
" activation='relu', padding='same'))\n", | |
"model.add(MaxPooling2D(2, strides=2))\n", | |
"\n", | |
"model.add(Conv2D(256, kernel_size=3, strides=1,\n", | |
" activation='relu', padding='same'))\n", | |
"model.add(Conv2D(256, kernel_size=3, strides=1,\n", | |
" activation='relu', padding='same'))\n", | |
"model.add(MaxPooling2D(2, strides=2))\n", | |
"\n", | |
"model.add(Flatten())\n", | |
"\n", | |
"model.add(Dense(10, activation='softmax'))\n", | |
"\n", | |
"model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam')\n", | |
"\n", | |
"model.summary()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"(x_train, y_train), (x_test, y_test) = load_data(DATA_TYPE)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Train on 54000 samples, validate on 6000 samples\n", | |
"Epoch 1/5\n", | |
"54000/54000 [==============================] - 164s 3ms/step - loss: 1.1945 - acc: 0.7786 - val_loss: 0.0596 - val_acc: 0.9825\n", | |
"Epoch 2/5\n", | |
"54000/54000 [==============================] - 136s 3ms/step - loss: 0.0517 - acc: 0.9841 - val_loss: 0.0327 - val_acc: 0.9907\n", | |
"Epoch 3/5\n", | |
"54000/54000 [==============================] - 137s 3ms/step - loss: 0.0351 - acc: 0.9891 - val_loss: 0.0375 - val_acc: 0.9905\n", | |
"Epoch 4/5\n", | |
"54000/54000 [==============================] - 137s 3ms/step - loss: 0.0254 - acc: 0.9921 - val_loss: 0.0382 - val_acc: 0.9907\n", | |
"Epoch 5/5\n", | |
"54000/54000 [==============================] - 139s 3ms/step - loss: 0.0202 - acc: 0.9934 - val_loss: 0.0326 - val_acc: 0.9913\n" | |
] | |
} | |
], | |
"source": [ | |
"hist = model.fit(x_train, y_train,\n", | |
" validation_split=0.1,\n", | |
" batch_size=BATCH_SIZE,\n", | |
" shuffle=True,\n", | |
" epochs=EPOCHS)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[0.026538163273234387, 0.99139999999999995]" | |
] | |
}, | |
"execution_count": 11, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"model.evaluate(x_test, y_test,\n", | |
" batch_size=BATCH_SIZE,\n", | |
" verbose=2)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Saving the Model" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# save model to json\n", | |
"model_json = model.to_json()\n", | |
"with open(save_path_json, \"w+\") as json_file:\n", | |
" json_file.write(model_json)\n", | |
"\n", | |
"model.save_weights(save_path_weights)" | |
] | |
} | |
], | |
"metadata": { | |
"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.5.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment