{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":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)\nimport matplotlib.pyplot as plt\n%matplotlib inline\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\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.utils.np_utils import to_categorical # convert to one-hot-encoding\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D, BatchNormalization\nfrom keras.optimizers import Adam\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import LearningRateScheduler","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"75eaa927e5311ffc5605c0eae424ce2faf0115e2"},"cell_type":"markdown","source":"**Load the data**"},{"metadata":{"trusted":true,"_uuid":"38cfabb8cd0e7a50338f5bafbd83e22d7be17ac3"},"cell_type":"code","source":"train_data = pd.read_csv(\"../input/train.csv\")\ntest_data = pd.read_csv(\"../input/test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6f44f90c1f2fea4bac1d0932837804616703a5c8"},"cell_type":"code","source":"print(train_data.shape)\ntrain_data.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6fc16f6da776f490fa231ff775593150d867025d"},"cell_type":"markdown","source":"Each size of the image is 28x28=784 , we can see that train data has 785 columns with first column is class label and rest of the 784 columns are pixel values."},{"metadata":{"trusted":true,"_uuid":"082f8558985aa77805a5a22d5690845612009d1f"},"cell_type":"code","source":"print(test_data.shape)\ntest_data.head()\n\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"491ebc1139e8497ce2db855954fafb63452cd85c"},"cell_type":"markdown","source":"Split the data into train and validation to avoid overfitting"},{"metadata":{"trusted":true,"_uuid":"ea6d243b5544a67ac54b112247f5101963ce16b1"},"cell_type":"code","source":"# all pixel values leaving 1st colum\nX_train = (train_data.iloc[:,1:].values).astype('float32') \n#  only labels i.e targets digits\nY_train = train_data.iloc[:,0].values.astype('int32') #\nX_test = test_data.values.astype('float32')\n\nx_train, x_val, y_train, y_val = train_test_split(\n    X_train, Y_train, train_size=0.2)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f999d6b1c0dcb79dccd019e559b395b690a1ac17"},"cell_type":"markdown","source":"**Visualize the data**"},{"metadata":{"trusted":true,"_uuid":"6c79a7bc5efb523a1ee9b61ca1ea99ae775e3503"},"cell_type":"code","source":"fig, ax = plt.subplots(1, 1, figsize=(12,6))\nax.imshow(x_train[0].reshape(28,28), cmap='gray')\nax.set_title(y_train[0])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7ee9f3e2788a0f3d76104ba2941db0c2533d82be"},"cell_type":"markdown","source":"**reshape the data**\nAs MNIST data in grey scale the channel info will be 1 "},{"metadata":{"trusted":true,"_uuid":"dc7d22377226c1255f65fd0ae0ab20224b6e9159"},"cell_type":"code","source":"x_train = x_train.reshape(-1, 28, 28, 1)\nx_val = x_val.reshape(-1, 28, 28, 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5b006a403d91b6c335d4190b1c2cfbb281ecedc3"},"cell_type":"code","source":"x_train.shape\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"20d776e38cc748937483e0028602dec086d9ab67"},"cell_type":"code","source":"x_val.shape","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1afc9a654a2143ef747d105798a28bbcded90b47"},"cell_type":"markdown","source":"**Preprocessing the images**"},{"metadata":{"_uuid":"6d93d3e5f5e99bceeb461308b924efb9ad8683f0"},"cell_type":"markdown","source":"** normalizing the data**\nNormalization means 2 things:\n\n1. Putting the data on the same scale (Scaling)\n    Scaling improves convergence speed and accuracy\n\n2. Balancing the data around a point (Centering)\n    Centering fights vanishing and exploding gradients, while probably also increasing convergence speed and accuracy\n"},{"metadata":{"_uuid":"d677635a18f88ccee2481445aecc749a792f0f3b","trusted":true},"cell_type":"code","source":"x_train = x_train.astype(\"float32\")/255.\nx_val = x_val.astype(\"float32\")/255.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2fe243dcdccd39f31e8620a198be1f18da50f1a1"},"cell_type":"markdown","source":"**One hot encoding**\n"},{"metadata":{"trusted":true,"_uuid":"7a5d7b4bf4176d41dee47c058653536df90f883e"},"cell_type":"code","source":"y_train","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a05e59cf41f0373ee9f6a888c83f274bb8b1bda5"},"cell_type":"code","source":"y_train = to_categorical(y_train)\ny_val = to_categorical(y_val)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"76aeeb3aa4da5cf2cd50121247fa723544c051ac"},"cell_type":"code","source":"y_train[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"88b6cb2d54182ad62bd25e5a25130346be11f218"},"cell_type":"code","source":"\ny_train.shape","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0e23f7400800c4383653890277ef2fcb6248a377"},"cell_type":"markdown","source":"**Train the model**"},{"metadata":{"trusted":true,"_uuid":"b0fdebac53ae4dc9b68f65229aaa8cd06565d786"},"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(filters=16, kernel_size=(3,3), activation='relu',\n                 input_shape = (28, 28, 1)))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(filters = 16, kernel_size = (3, 3), activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D(strides=(2,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(filters = 32, kernel_size = (3, 3), activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(filters = 32, kernel_size = (3, 3), activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D(strides=(2,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Flatten())\nmodel.add(Dense(512, activation='relu'))\nmodel.add(Dropout(0.25))\nmodel.add(Dense(1024, activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(10, activation='softmax'))\nmodel.compile(loss='categorical_crossentropy', \n              optimizer = Adam(lr=1e-4), metrics=[\"accuracy\"])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"23edfaab0c937ed476d0e74da473967b67cb82d0"},"cell_type":"markdown","source":"**Data Augumentation**\n\nIt is the technique used to increase the data size by\n1. Cropping\n2. Rotating\n3. Scaling\n4. Translating\n5. Flipping\nAdding Gaussian noise to input images etc."},{"metadata":{"trusted":true,"_uuid":"b8538772602f4b9778666a35ceacc0e8c4bf8f70"},"cell_type":"code","source":"datagen = 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":"eea5a4fef2e43da84ac22eb7e92c027aa35e9b4b"},"cell_type":"code","source":"batches = datagen.flow(x_train, y_train, batch_size=16)\nval_batches = datagen.flow(x_val, y_val, batch_size=16)\nannealer = LearningRateScheduler(lambda x: 1e-3 * 0.9 ** x)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9913be0b69020102680205173ac322740e853375"},"cell_type":"code","source":"hist = model.fit_generator(generator=batches,\n                           steps_per_epoch=batches.n,\n                           epochs=20, #Increase this when not on Kaggle kernel\n                           verbose=2,  #1 for ETA, 0 for silent\n                           validation_data=(x_val[:400,:], y_val[:400,:]), #For speed\n                           callbacks=[annealer])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9eaa79d1c91df44f08216f3f1b3f668896346493"},"cell_type":"code","source":"final_loss, final_acc = model.evaluate(x_val, y_val, verbose=0)\nprint(\"Final loss: {0:.4f}, final accuracy: {1:.4f}\".format(final_loss, final_acc))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3806c87540630ba93f4f78fe8133848d8630d103"},"cell_type":"code","source":"plt.plot(hist.history['loss'], color='b')\nplt.plot(hist.history['val_loss'], color='r')\nplt.show()\nplt.plot(hist.history['acc'], color='b')\nplt.plot(hist.history['val_acc'], color='r')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"97db6e1de8e28d8c60070ecb2b077aebb98a32f4"},"cell_type":"markdown","source":""}],"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}