{"nbformat_minor": 1, "nbformat": 4, "cells": [{"cell_type": "code", "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", "# 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", "\n", "# Any results you write to the current directory are saved as output."], "execution_count": null, "outputs": [], "metadata": {"_cell_guid": "c4c5341f-19b1-48bd-9600-a0b8cda90a37", "_uuid": "26b096baa4221f8d473633d59e590746a233506f"}}, {"cell_type": "code", "source": ["train = pd.read_csv(\"../input/train.csv\")\n", "test = pd.read_csv(\"../input/test.csv\")"], "execution_count": null, "outputs": [], "metadata": {"_cell_guid": "06c31093-20a5-476c-834a-e5c1acac50d5", "_uuid": "e487025a0682d946f9749315b9646f2171d3771e", "collapsed": true}}, {"cell_type": "code", "source": ["train.head()"], "execution_count": null, "outputs": [], "metadata": {"_cell_guid": "653c9067-65d8-44b2-a105-bb7dbbbb7425", "_uuid": "85211451470997e8dc58bb4be457521616f8f2d4"}}, {"cell_type": "code", "source": ["X_train = np.asarray(train.ix[:,1:].values,dtype=np.float32)\n", "Y_train = np.asarray(train.ix[:,0].values,dtype=np.int32)\n", "X_test = np.asarray(test.values,dtype=np.float32)"], "execution_count": null, "outputs": [], "metadata": {"_cell_guid": "8b60eb7f-0c27-46d0-a64e-3a42a770ddc0", "_uuid": "3ae94aae82f5122d9a30217207939a0cf6d0e96c"}}, {"cell_type": "code", "source": ["X_train"], "execution_count": null, "outputs": [], "metadata": {"_cell_guid": "714ac2a1-ae22-4b3b-9d17-d3b5b219c5bf", "scrolled": true, "_uuid": "7e2f1366f84f1e0185deccafcdb1fb5c34a4cc84"}}, {"cell_type": "code", "source": ["ins = np.shape(X_train)[0]\n", "print(np.shape(X_train))"], "execution_count": null, "outputs": [], "metadata": {"_cell_guid": "1b9c236c-d58a-4a02-9968-70c83040c74b", "_uuid": "bfc249b3e5d793764dcf01c55470e5cdee3ec2cd"}}, {"cell_type": "code", "source": ["Y_train"], "execution_count": null, "outputs": [], "metadata": {"_cell_guid": "fee21c48-04fc-48b5-9f29-477e7ca41ba9", "_uuid": "65a85d77dd7995ac05c40fd6829c002695cd91c1"}}, {"cell_type": "code", "source": ["import matplotlib.pyplot as plt\n", "%matplotlib inline"], "execution_count": null, "outputs": [], "metadata": {"_cell_guid": "c38d1476-d91e-43b6-9261-543b210e2d7e", "_uuid": "623e51b1d71d03de6db84a6e3d381dbcd0e53071", "collapsed": true}}, {"cell_type": "code", "source": ["X_train = np.reshape(X_train,(ins,28,28))\n", "\n", "for i in range(1,10):\n", "    plt.subplot(330+i)\n", "    plt.imshow(X_train[i], cmap=plt.get_cmap('gray'))\n", "    plt.title(Y_train[i])"], "execution_count": null, "outputs": [], "metadata": {"_cell_guid": "bcca1de5-5d00-4853-b218-4dd4fc556a9c", "_uuid": "7d69a03bc950f55c2af13749e6c8ecb19f55731c"}}, {"cell_type": "code", "source": ["X_train = np.reshape(X_train,(ins,28,28,1))"], "execution_count": null, "outputs": [], "metadata": {"_cell_guid": "a0ea7a24-a483-4e81-8c50-028748158fb2", "_uuid": "df0aa77dcb39be6f5aedfb8b635bf0327060e266", "collapsed": true}}, {"cell_type": "code", "source": ["X_test = np.reshape(X_test,(np.shape(X_test)[0],28,28,1))"], "execution_count": null, "outputs": [], "metadata": {"_cell_guid": "06f0610c-038c-477b-965a-a304727b1422", "_uuid": "05684dfbdd2737f59e66063b1d30075af8e73f79", "collapsed": true}}, {"cell_type": "code", "source": ["from keras.utils.np_utils import to_categorical,normalize"], "execution_count": null, "outputs": [], "metadata": {"_cell_guid": "0c5bd2d3-3d53-423f-9293-cd24c6ee2302", "_uuid": "5427d678da77087762ac13d794350b9da1389394"}}, {"cell_type": "code", "source": ["Y_train = to_categorical(Y_train)\n", "Y_train"], "execution_count": null, "outputs": [], "metadata": {"_cell_guid": "c42d4cd7-f309-4985-af07-3cb6c2fee9a6", "_uuid": "b54a65da66efab61f93112d2635df167cfaad0db"}}, {"cell_type": "code", "source": ["seed = 124\n", "np.random.seed = seed"], "execution_count": null, "outputs": [], "metadata": {"_cell_guid": "69975afa-73a4-4cd3-b6f6-8b25d94af55e", "_uuid": "8bf8c1d9d6685e4eb7f83a86cd6cee67c50be597", "collapsed": true}}, {"cell_type": "code", "source": ["from keras.models import Sequential\n", "from keras.layers.core import Dropout,Lambda,Flatten,Activation,Dense\n", "from keras.layers import Conv2D,MaxPool2D,AveragePooling2D,BatchNormalization\n", "from keras import optimizers"], "execution_count": null, "outputs": [], "metadata": {"_cell_guid": "243ceabd-6835-4c77-b5e3-b9cdc7e86f59", "_uuid": "8cc18325abe7c3eff6b6e04b9928f31f75b2ecd4", "collapsed": true}}, {"cell_type": "code", "source": ["model = Sequential()\n", "model.add(Conv2D(32,(5,5),strides=(2,2),padding=\"same\",input_shape=(28,28,1)))\n", "model.add(Activation('relu'))\n", "model.add(MaxPool2D(pool_size=(2,2),padding=\"same\"))\n", "model.add(Conv2D(64,(5,5),strides=(2,2),padding=\"same\"))\n", "model.add(Activation('relu'))\n", "model.add(MaxPool2D(pool_size=(2,2),padding=\"same\"))\n", "model.add(Conv2D(128,(5,5),strides=(2,2),padding=\"same\"))\n", "model.add(Activation('relu'))\n", "model.add(AveragePooling2D(pool_size=(2,2),padding=\"same\"))"], "execution_count": null, "outputs": [], "metadata": {"_cell_guid": "6df7d3cf-0a4f-4625-a5a4-8cc3b5ef2e15", "_uuid": "7875f6185f8818749add15c9255ed0f52c547cf1"}}, {"cell_type": "code", "source": ["model.add(Flatten())\n", "#model.add(Dense(1500,activation=\"relu\"))\n", "#model.add(BatchNormalization())\n", "#model.add(Dropout(0.5))\n", "model.add(Dense(256,activation=\"relu\"))\n", "model.add(BatchNormalization())\n", "model.add(Dropout(0.5))\n", "model.add(Dense(128,activation=\"relu\"))\n", "model.add(BatchNormalization())\n", "model.add(Dropout(0.5))\n", "model.add(Dense(10,activation=\"softmax\"))"], "execution_count": null, "outputs": [], "metadata": {"_cell_guid": "e6126d2e-2609-4274-aaf5-03afb874b080", "_uuid": "095661c499c24a8f594f887a8fc93677ecbf0a2f"}}, {"cell_type": "code", "source": ["print(model.input_shape)\n", "print(model.output_shape)"], "execution_count": null, "outputs": [], "metadata": {"_cell_guid": "8bf11687-f478-48f6-a880-3983922a21db", "_uuid": "eecb0584d2e9ff6835f59d674b7190a71a6bfc4e"}}, {"cell_type": "code", "source": ["model.compile(optimizer=optimizers.Nadam(),loss=\"categorical_crossentropy\",metrics=['accuracy'])"], "execution_count": null, "outputs": [], "metadata": {"_cell_guid": "07b3ec41-d0e8-4fa5-be0e-7d60e1c32379", "_uuid": "1c15abf72325cbba5e36af02130bcce8a73b39e9", "collapsed": true}}, {"cell_type": "code", "source": ["from keras.preprocessing import image\n", "gen = image.ImageDataGenerator()"], "execution_count": null, "outputs": [], "metadata": {"_cell_guid": "f6ea7d45-1a23-4106-9ef9-f7d6d1f17133", "_uuid": "5abcc04e875ee98d756d9da89107c85af70cd0a4", "collapsed": true}}, {"cell_type": "code", "source": ["from sklearn.model_selection import train_test_split\n", "X_train1, X_val, Y_train1, Y_val = train_test_split(X_train, Y_train, test_size=0.10, random_state=124)\n", "batches = gen.flow(X_train1, Y_train1, batch_size=50)\n", "val_batches=gen.flow(X_val, Y_val, batch_size=50)"], "execution_count": null, "outputs": [], "metadata": {"_cell_guid": "5e774638-7bd3-4576-a42a-824165987ce7", "_uuid": "4bdcbe97033be3d3f7cbdd6bb87a928d45ba8d14", "collapsed": true}}, {"cell_type": "code", "source": ["history=model.fit_generator(batches, batches.n, nb_epoch=1,validation_data=val_batches, nb_val_samples=val_batches.n)"], "execution_count": null, "outputs": [], "metadata": {"_cell_guid": "455e1e98-4b3c-481f-b70c-9cf210fdb245", "_uuid": "f805c9daf2e5e94ec9fd9e27d592f1dfba774f21"}}, {"cell_type": "code", "source": ["hist = history.history\n", "print(hist.keys)"], "execution_count": null, "outputs": [], "metadata": {"_cell_guid": "494cf18a-5b79-43e9-b579-d59ece680492", "_uuid": "e2485eaf4bd793fdb97ec11e73c4d6206c5697ca"}}, {"cell_type": "code", "source": ["print(hist['acc'])\n", "print(hist['val_acc'])"], "execution_count": null, "outputs": [], "metadata": {"_cell_guid": "6042463b-90d1-4ace-8455-0e993c6e1636", "_uuid": "2243e07e7fed7c897b81ab8e8470c1a2ced9527b"}}, {"cell_type": "code", "source": ["predictions = model.predict_classes(X_test, verbose=0)\n", "\n", "submissions=pd.DataFrame({\"ImageId\": list(range(1,len(predictions)+1)),\n", "                         \"Label\": predictions})\n", "submissions.to_csv(\"DR.csv\", index=False, header=True)"], "execution_count": null, "outputs": [], "metadata": {"_cell_guid": "c98b56ea-7a14-4210-8735-3122690c33df", "_kg_hide-input": false, "_uuid": "75d052e3be43213f553e87115503bc1f99dcec1f", "collapsed": true, "_kg_hide-output": false}}, {"cell_type": "code", "source": [], "execution_count": null, "outputs": [], "metadata": {"_cell_guid": "b1cf1280-ecdc-4bab-b177-102650ae2f7c", "_uuid": "1a9f3f0859135f4e94d86f062ea3e3332f3d6d2b", "collapsed": true}}], "metadata": {"language_info": {"nbconvert_exporter": "python", "file_extension": ".py", "pygments_lexer": "ipython3", "codemirror_mode": {"name": "ipython", "version": 3}, "name": "python", "version": "3.6.3", "mimetype": "text/x-python"}, "kernelspec": {"name": "python3", "display_name": "Python 3", "language": "python"}}}