{"nbformat_minor": 1, "cells": [{"source": ["import matplotlib.pyplot as plt\n", "import numpy as np # linear algebra\n", "%matplotlib inline\n", "\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.metrics import confusion_matrix\n", "\n", "from keras.utils.np_utils import to_categorical # convert to one-hot-encoding\n", "from keras.models import Sequential\n", "from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D, BatchNormalization\n", "from keras.optimizers import Adam\n", "from keras.preprocessing.image import ImageDataGenerator\n", "from keras.callbacks import LearningRateScheduler"], "execution_count": null, "metadata": {"scrolled": true, "_uuid": "1eb3456c8b612e9360c3f797f56e046636eccff5", "_cell_guid": "a1950283-52ac-45e8-9421-2661fcee24b5"}, "cell_type": "code", "outputs": []}, {"source": ["train_file = \"../input/train.csv\"\n", "test_file = \"../input/test.csv\"\n", "output_file = \"submission.csv\"\n", "\n", "raw_data = np.loadtxt(train_file, skiprows=1, dtype='int', delimiter=',')\n", "x_train, x_val, y_train, y_val = train_test_split(raw_data[:,1:], raw_data[:,0], test_size=0.1)"], "execution_count": null, "metadata": {"collapsed": true, "_uuid": "55f4d53bc0b618e5efc02c397e11921c9341dec0", "_cell_guid": "aaaa3871-57c9-4fcc-9420-cb5d2f8de562"}, "cell_type": "code", "outputs": []}, {"source": ["plt.imshow(x_train[ 5 ].reshape(28,28), cmap='gray')"], "execution_count": null, "metadata": {"_uuid": "1089a088d8633187f62286053a0dc528a860ceda", "_cell_guid": "fb84dc81-e3fc-4636-911f-4a691b391bdc"}, "cell_type": "code", "outputs": []}, {"source": ["x_train = x_train.reshape(-1, 28, 28, 1)\n", "x_val = x_val.reshape(-1, 28, 28, 1)\n", "\n", "x_train = x_train.astype(\"float32\")/255.\n", "x_val = x_val.astype(\"float32\")/255."], "execution_count": null, "metadata": {"collapsed": true, "_uuid": "7d80413b9f521e486401bbc7f386f9866b3be2db", "_cell_guid": "cd7feb4c-df02-4e54-b1fd-87377341b91d"}, "cell_type": "code", "outputs": []}, {"source": ["y_train = to_categorical(y_train)\n", "y_val = to_categorical(y_val)"], "execution_count": null, "metadata": {"collapsed": true, "_uuid": "0aeb35e263407bac21214eee2624f21a11cd5e16", "_cell_guid": "473d9c94-ff0d-4f14-9d9e-8877edb2089a"}, "cell_type": "code", "outputs": []}, {"source": ["print(y_train[ 5 ])"], "execution_count": null, "metadata": {"_uuid": "bc44e53e04d37e316f3e79f4ecbc7e3271f4564b", "_cell_guid": "d44d023d-2165-419a-80c5-e8052279bba7"}, "cell_type": "code", "outputs": []}, {"source": ["model = Sequential()\n", "\n", "model.add(Conv2D(filters = 16, kernel_size = (3, 3), activation='relu',\n", "                 input_shape = (28, 28, 1)))\n", "model.add(BatchNormalization())\n", "model.add(Conv2D(filters = 16, kernel_size = (3, 3), activation='relu'))\n", "model.add(BatchNormalization())\n", "model.add(MaxPool2D(strides=(2,2)))\n", "model.add(Dropout(0.25))\n", "\n", "model.add(Conv2D(filters = 32, kernel_size = (3, 3), activation='relu'))\n", "model.add(BatchNormalization())\n", "model.add(Conv2D(filters = 32, kernel_size = (3, 3), activation='relu'))\n", "model.add(BatchNormalization())\n", "model.add(MaxPool2D(strides=(2,2)))\n", "model.add(Dropout(0.25))\n", "\n", "model.add(Flatten())\n", "model.add(Dense(512, activation='relu'))\n", "model.add(Dropout(0.25))\n", "model.add(Dense(1024, activation='relu'))\n", "model.add(Dropout(0.5))\n", "model.add(Dense(10, activation='softmax'))"], "execution_count": null, "metadata": {"collapsed": true, "_uuid": "492bfa402f782df0169a686f138ccda3b39a003e", "_cell_guid": "2be2df7b-4436-467f-95ed-6035fc5530b9"}, "cell_type": "code", "outputs": []}, {"source": ["datagen = ImageDataGenerator(zoom_range = 0.1,\n", "                            height_shift_range = 0.1,\n", "                            width_shift_range = 0.1,\n", "                            rotation_range = 10)\n"], "execution_count": null, "metadata": {"collapsed": true, "_uuid": "1d4afa71d071ba1a567c8b13b404e26499d3b3bf", "_cell_guid": "18c1999e-308a-412b-8113-02d6c819aa6a"}, "cell_type": "code", "outputs": []}, {"source": ["model.compile(loss='categorical_crossentropy', optimizer = Adam(lr=1e-4), metrics=[\"accuracy\"])\n"], "execution_count": null, "metadata": {"collapsed": true, "_uuid": "a00505b316efd1c9a93af0019a208b2e7b84139c", "_cell_guid": "9fdb9bee-ef1a-441b-b86e-0370e52062fe"}, "cell_type": "code", "outputs": []}, {"source": ["annealer = LearningRateScheduler(lambda x: 1e-3 * 0.9 ** x)\n", "hist = model.fit_generator(datagen.flow(x_train, y_train, batch_size=16),\n", "                           steps_per_epoch=500,\n", "                           epochs= 20,\n", "                           verbose=2,\n", "                           validation_data=(x_val[:400,:], y_val[:400,:]),\n", "                           callbacks=[annealer])\n"], "execution_count": null, "metadata": {"_uuid": "19961506c42246b5500157dabca99d29c1c2cb81", "_cell_guid": "8f2539fc-2865-497c-8418-d7d1c6fc5a47"}, "cell_type": "code", "outputs": []}, {"source": ["mnist_testset = np.loadtxt(test_file, skiprows=1, dtype='int', delimiter=',')\n", "x_test = mnist_testset.astype(\"float32\")\n", "x_test = x_test.reshape(-1, 28, 28, 1)/255.\n"], "execution_count": null, "metadata": {"collapsed": true, "_uuid": "df94299e82f40a51632b622358ea7f4c876c3ec4", "_cell_guid": "3ee1f7c4-7a92-431e-8066-a310cafdaaf5"}, "cell_type": "code", "outputs": []}, {"source": ["y_hat = model.predict(x_test, batch_size=64)\n", "y_pred = np.argmax(y_hat,axis=1)\n"], "execution_count": null, "metadata": {"collapsed": true, "_uuid": "9317c7b0abc6dcdc59a66718a11d96edb54aa76d", "_cell_guid": "ed44af98-6ac6-4ac4-a011-467989b27315"}, "cell_type": "code", "outputs": []}, {"source": ["with open(output_file, 'w') as f :\n", "    f.write('ImageId,Label\\n')\n", "    for i in range(len(y_pred)) :\n", "        f.write(\"\".join([str(i+1),',',str(y_pred[i]),'\\n']))\n"], "execution_count": null, "metadata": {"collapsed": true, "_uuid": "039c5125522454df14be1272271a9a40d740a060", "_cell_guid": "7d2effff-9333-4155-a0a6-0a592b46965c"}, "cell_type": "code", "outputs": []}], "metadata": {"kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"}, "language_info": {"codemirror_mode": {"version": 3, "name": "ipython"}, "file_extension": ".py", "mimetype": "text/x-python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.3", "name": "python"}}, "nbformat": 4}