{"cells": [{"source": ["import numpy as np # linear algebra\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.metrics import confusion_matrix"], "cell_type": "code", "execution_count": null, "outputs": [], "metadata": {"collapsed": true, "_uuid": "6e2f29392ed361c5f651527fb3884b26aa59d069", "_cell_guid": "8eda0570-889e-4b9b-906f-02af5c85ee8d"}}, {"source": ["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"], "cell_type": "code", "execution_count": null, "outputs": [], "metadata": {"_uuid": "aa7fcb82ff462ad7810a0a85d6dbe416e63431a4", "_cell_guid": "29d508bc-bb43-4166-a290-3b0afa5f684e"}}, {"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)\n"], "cell_type": "code", "execution_count": null, "outputs": [], "metadata": {"collapsed": true, "_uuid": "f0effd5adc892eef144007b3104a6c7a5b926c78", "_cell_guid": "fe3e558d-c6fb-4712-9e5d-746f5aa30496"}}, {"source": ["plt.imshow(x_train[0].reshape(28,28), cmap='gray')"], "cell_type": "code", "execution_count": null, "outputs": [], "metadata": {"_uuid": "2051c6c0f82ccb19d0c324fe2c51ccfdd2aead34", "_cell_guid": "e923931a-388c-4c42-83ce-60cc28339f97"}}, {"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.\n"], "cell_type": "code", "execution_count": null, "outputs": [], "metadata": {"collapsed": true, "_uuid": "80ebc452e0167dca4d35b65cfde8c0140384b17b", "_cell_guid": "724a3ce6-1ce2-400a-ab31-2e621d88bd1f"}}, {"source": ["y_train = to_categorical(y_train)\n", "y_val = to_categorical(y_val)\n"], "cell_type": "code", "execution_count": null, "outputs": [], "metadata": {"collapsed": true, "_uuid": "6d59ad652cd590a16ab1f19562196c6b0ddc17b1", "_cell_guid": "33cc93b3-6a2c-4e93-91c5-7591ec6728af"}}, {"source": ["print(y_train[ 0 ])"], "cell_type": "code", "execution_count": null, "outputs": [], "metadata": {"_uuid": "2d91f58002effba172dc86dd4bdea30af4fb40cb", "_cell_guid": "29e528d6-5951-4738-b113-ee628196c73c"}}, {"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'))\n"], "cell_type": "code", "execution_count": null, "outputs": [], "metadata": {"collapsed": true, "_uuid": "9d23f641556f0d0904939ee732b0576282fff398", "_cell_guid": "4ebb15a6-62df-42c0-9101-46b183cfa219"}}, {"source": ["datagen = ImageDataGenerator(zoom_range = 0.1,\n", "                            height_shift_range = 0.1,\n", "                            width_shift_range = 0.1,\n", "                            rotation_range = 10)\n"], "cell_type": "code", "execution_count": null, "outputs": [], "metadata": {"collapsed": true, "_uuid": "a4429849d36ea6b46c34cb605f854c2ff8501d07", "_cell_guid": "378dd474-523d-459e-99c5-b88021a4e436"}}, {"source": ["model.compile(loss='categorical_crossentropy', optimizer = Adam(lr=1e-4), metrics=[\"accuracy\"])\n"], "cell_type": "code", "execution_count": null, "outputs": [], "metadata": {"collapsed": true, "_uuid": "ac52cb0dbdd71e4bc065150bfa49f12ef34d7c86", "_cell_guid": "682a20c1-910a-4be6-8761-3bac68f3452f"}}, {"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= 1,\n", "                           verbose=2,\n", "                           validation_data=(x_val[:400,:], y_val[:400,:]),\n", "                           callbacks=[annealer])\n"], "cell_type": "code", "execution_count": null, "outputs": [], "metadata": {"_uuid": "27636c32e5f9acfaa180aac7edc32303e647703f", "_cell_guid": "1acb5f18-e026-4340-ac30-1111d53f9f86"}}, {"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"], "cell_type": "code", "execution_count": null, "outputs": [], "metadata": {"collapsed": true, "_uuid": "6031de536bf6c9766dc64d8deed1835a1c510999", "_cell_guid": "316aecea-7ac6-4d4a-b0c3-3d3887abe900"}}, {"source": ["y_hat = model.predict(x_test, batch_size=64)\n", "y_pred = np.argmax(y_hat,axis=1)\n"], "cell_type": "code", "execution_count": null, "outputs": [], "metadata": {"collapsed": true, "_uuid": "de4a6180172207fe2a1673512ae28024c000a70a", "_cell_guid": "96938efb-1e7d-471d-8fe9-167e10a46d8f"}}, {"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"], "cell_type": "code", "execution_count": null, "outputs": [], "metadata": {"collapsed": true, "_uuid": "82e84f613fb2f2dd825adccd8c477a1948d012dc", "_cell_guid": "dab6c07f-2a94-4f41-a22d-cb61fd053948"}}], "metadata": {"kernelspec": {"language": "python", "name": "python3", "display_name": "Python 3"}, "language_info": {"nbconvert_exporter": "python", "name": "python", "pygments_lexer": "ipython3", "file_extension": ".py", "codemirror_mode": {"name": "ipython", "version": 3}, "mimetype": "text/x-python", "version": "3.6.3"}}, "nbformat_minor": 1, "nbformat": 4}