{"nbformat_minor": 1, "metadata": {"kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"}, "language_info": {"mimetype": "text/x-python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "name": "python", "file_extension": ".py", "version": "3.6.3", "codemirror_mode": {"name": "ipython", "version": 3}}}, "cells": [{"source": ["# This Python 3 environment comes with many helpful analytics libraries installed\n", "# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n", "# For example, here's several helpful packages to load in \n", "\n", "import numpy as np # linear algebra\n", "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n", "\n", "import matplotlib.pyplot as plt\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\n", "\n", "\n", "# Input data files are available in the \"../input/\" directory.\n", "# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n", "\n", "from subprocess import check_output\n", "print(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n", "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)\n", "x_train = x_train.reshape(-1, 28, 28, 1)\n", "x_val = x_val.reshape(-1, 28, 28, 1)\n", "plt.imshow(x_train[ 1 ].reshape(28,28), cmap='gray')\n", "x_train = x_train.astype(\"float32\")/255.\n", "x_val = x_val.astype(\"float32\")/255.\n", "y_train = to_categorical(y_train)\n", "y_val = to_categorical(y_val)\n", "print(y_train[ 1 ])\n", "\n", "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'))\n", "\n", "datagen = ImageDataGenerator(zoom_range = 0.1,\n", "                            height_shift_range = 0.1,\n", "                            width_shift_range = 0.1,\n", "                            rotation_range = 10)\n", "model.compile(loss='categorical_crossentropy', optimizer = Adam(lr=1e-4), metrics=[\"accuracy\"])\n", "\n", "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", "\n", "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", "y_hat = model.predict(x_test, batch_size=64)\n", "y_pred = np.argmax(y_hat,axis=1)\n", "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", "\n", "\n", "# Any results you write to the current directory are saved as output."], "outputs": [], "metadata": {"_cell_guid": "a728bec2-711d-4f12-80c4-65956c9de0f1", "_uuid": "9ce69877ec457a820d4f332be4ca3797ebe322bf"}, "execution_count": null, "cell_type": "code"}], "nbformat": 4}