{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"_kg_hide-output":true},"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\nimport numpy as np # linear algebra\nimport 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\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"from keras.layers import Conv2D, MaxPooling2D, Dense, Dropout, BatchNormalization, Input, Flatten\nfrom keras.models import Model\nfrom keras.optimizers import Adam\nfrom keras.preprocessing.image import ImageDataGenerator","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8dc5f2796a355840ce299821ff341946a11e4635"},"cell_type":"code","source":"train_file='../input/train.csv'\ntest_file='../input/test.csv'\ntrain = pd.read_csv(train_file)\ntest = pd.read_csv(test_file)\nprint(train.head())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a7c19417162e9b1e66cd56b42f984124af99b2fd"},"cell_type":"code","source":"inputs = Input(shape=(28,28, 1), name='inptuts')\nnet = Conv2D(16, kernel_size=(3,3), activation='relu', padding='same')(inputs)\nnet = Conv2D(16, kernel_size=(3,3), activation='relu', padding='same')(net)\nnet = MaxPooling2D((2,2))(net)\n# 64*14*14\nnet = Conv2D(84, kernel_size=(3,3), activation='relu', padding='same')(net)\nnet = Conv2D(84, kernel_size=(3,3), activation='relu', padding='same')(net)\nnet = MaxPooling2D((2,2))(net)\n# 128 * 7*7\nnet = Conv2D(256, kernel_size=(3,3), activation='relu', padding='same')(net)\nnet = Conv2D(256, kernel_size=(3,3), activation='relu', padding='same')(net)\nnet = Conv2D(256, kernel_size=(3,3), activation='relu', padding='same')(net)\nnet = MaxPooling2D((2,2))(net)\n# 256 * 4*4 \n# FC\nnet = Flatten()(net)\nnet = Dropout(rate=0.5)(net)\nnet = Dense(1024, activation='relu',)(net)\nnet = Dropout(rate=0.5)(net)\nnet = Dense(2048, activation='relu',)(net)\nnet = Dropout(rate=0.5)(net)\nnet = Dense(1000, activation='relu',)(net)\n\noutputs = Dense(10, activation='softmax')(net)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c0c22eac4c0c19f1da6efc025f765d4b32494685"},"cell_type":"code","source":"epochs = 50\nlearn_rate = 1e-4\nbatch_size = 128","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a0c07895b73ce722bbee2a6386615f35f6703032"},"cell_type":"code","source":"print(train.shape)\nonehot = train.join(pd.get_dummies(train['label'])) / 255\n\ndef get_samples():\n    while True:\n        sample = onehot.sample(n=16)\n        y_train = np.array(sample[[i for i in range(10)]])\n        x_train = np.array(sample.drop(['label',0, 1, 2, 3, 4, 5,6,7,8,9], axis=1)).reshape((-1,28,28,1))\n        yield (x_train, y_train)\n\ndef get_validations():\n    while True:\n        sample = onehot.sample(n=16)\n        y_test = np.array(sample[[i for i in range(10)]])\n        x_test = np.array(sample.drop(['label',0, 1, 2, 3, 4, 5,6,7,8,9], axis=1)).reshape((-1, 28,28,1))\n        yield (x_test, y_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4b619b5256d4751f55fe0367b473d9a177d823aa"},"cell_type":"code","source":"y_train = np.array(onehot[[i for i in range(10)]])\nx_train = np.array(onehot.drop(['label',0, 1, 2, 3, 4, 5,6,7,8,9], axis=1)).reshape((-1,28,28,1))\ndatagen = ImageDataGenerator(zoom_range = 0.1,\n                            height_shift_range = 0.1,\n                            width_shift_range = 0.1,\n                            rotation_range = 10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9eee14503c6b0b1529a6a2f1b961ef8d49b17341"},"cell_type":"code","source":"model = Model(inputs=inputs, outputs=outputs)\nmodel.compile(optimizer=Adam(lr=learn_rate),\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5bccc4dc9f1246ed63168887fd30899542bbaa32"},"cell_type":"code","source":"hist = model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size),\n                           steps_per_epoch=train.shape[0]/batch_size,\n                           nb_epoch=epochs,\n                           validation_data=get_validations(), \n                           nb_val_samples=500)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"39fe57663fdf1f4bc71a9077082279c78091613e"},"cell_type":"code","source":"final_loss, final_acc = model.evaluate(np.array(onehot.drop(['label',0, 1, 2, 3, 4, 5,6,7,8,9], axis=1)).reshape((-1,28,28,1)), \n                                       np.array(onehot[[i for i in range(10)]]) / 255, verbose=0)\nprint(\"Final loss: {0:.4f}, final accuracy: {1:.4f}\".format(final_loss, final_acc))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4b2a57ad5154d1bb2239bf1205747079204950cb"},"cell_type":"code","source":"x_test = np.array(test).reshape((-1,28,28,1))/255\ny_hat = model.predict(x_test, batch_size=batch_size)\ny_pred = np.argmax(y_hat, 1)\ny_pred = pd.Series(y_pred, name='Label', dtype=np.int32)\nimgid = pd.Series(range(1, 28001), name='ImageId')\nsubmission = pd.concat([imgid, y_pred], axis=1)\nprint(submission.head())\nsubmission.to_csv('submission.csv', index=False)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7701d53d5554f51ebbb3bd73bbaee8ae7c70bb1e"},"cell_type":"code","source":"import matplotlib.pyplot as plt\nhist.history.keys()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"12e017fe0cce443ef677ead2feee76d58d248a59"},"cell_type":"code","source":"plt.plot(hist.history['loss'], c='r')\nplt.plot(hist.history['val_loss'], c='b')\nplt.xlabel('epoch')\nplt.ylabel('loss')\nplt.legend(['loss', 'val_loss'])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"095838b035d9e59566cece275771c71c705d6e14"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"53058f714c81f9e17dcdb4feec4f7fe692a3e216"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}