{"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 pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n%matplotlib inline\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":73,"outputs":[{"output_type":"stream","text":"['sample_submission.csv', 'test.csv', 'train.csv']\n","name":"stdout"}]},{"metadata":{"trusted":true,"_uuid":"ba5b81e9033c50d97a817026b13f3fcedc29f2bb"},"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D, BatchNormalization\nfrom keras.utils.np_utils import to_categorical\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ReduceLROnPlateau\nfrom keras.optimizers import Adam","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0d02f0612b69eb6fcc0e275655a927f16f6fe44d"},"cell_type":"markdown","source":"Loading Training data"},{"metadata":{"trusted":true,"_uuid":"89d79aaa4551192869d0bbece6b71d964bdb1641"},"cell_type":"code","source":"train_df = pd.read_csv('../input/train.csv')\n\ny_train = train_df[\"label\"]\nx_train = train_df.drop(labels = [\"label\"],axis = 1)\ndel train_df\n\nn_train = len(x_train)\nn_pixels = len(x_train.columns)\nn_class = len(set(y_train))\nprint('Number of training samples: {0}'.format(n_train))\nprint('Number of training pixels: {0}'.format(n_pixels))\nprint('Number of classes: {0}'.format(n_class))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0002d0a824ffec375e6d43fcd540e9fae60df982"},"cell_type":"markdown","source":"Loading Test data"},{"metadata":{"trusted":true,"_uuid":"8260d6581ab3442658c0ad4c2967e41bc8f374d8"},"cell_type":"code","source":"x_test = pd.read_csv('../input/test.csv')\n\nn_test = len(x_test)\nn_pixels = len(x_test.columns)\n\nprint('Number of train samples: {0}'.format(n_test))\nprint('Number of test pixels: {0}'.format(n_pixels))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"29d7e357b24b4a39cc6935c4132a6b0a4330cd6f"},"cell_type":"markdown","source":"Normalizing"},{"metadata":{"trusted":true,"_uuid":"15793b92fa6fcbad8acfbf93ff5de67b3c0306bb"},"cell_type":"code","source":"x_train = x_train / 255.0\nx_test = x_test / 255.0","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b154c2398c0b51d4bb122c3dbad7153870b81f8f"},"cell_type":"markdown","source":"Reshaping to 28X28"},{"metadata":{"trusted":true,"_uuid":"a40f785c7987b5f0f44ed2acf00dfdaf4479f546"},"cell_type":"code","source":"x_train = x_train.values.reshape(-1,28,28,1)\nX = x_train\nx_test = x_test.values.reshape(-1,28,28,1)\n\n#Converting to one-hot vector\ny_train = to_categorical(y_train, num_classes = 10)\n#g = plt.imshow(x_train[0][:,:,0])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d5138d8fd43d8df221953954aae69660f5512166"},"cell_type":"markdown","source":"Model Definition"},{"metadata":{"trusted":true,"_uuid":"8ae1ab42566ad813a91fe0e9977f519704f3132d"},"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(filters = 32, kernel_size = 3,padding = 'Same', activation ='relu', input_shape = (28,28,1)))\n#model.add(Conv2D(32, kernel_size = 5, activation='relu', input_shape = (28, 28, 1)))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(32, kernel_size = 5, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(48, kernel_size = 5, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(BatchNormalization())\nmodel.add(Flatten())\n\n\nmodel.add(Dense(512, activation = \"relu\"))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.5))\n\nmodel.add(Dense(10, activation='softmax'))\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c8a5ff811420b1e455f1ed9a0d747082ca6f252e"},"cell_type":"markdown","source":"Optimizer"},{"metadata":{"trusted":true,"_uuid":"467f8bb4ab6a1f43b462d393abd94d2c4cfb36d3"},"cell_type":"code","source":"optimizer = Adam(lr=0.001)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"254f851be0c30ad4b53a867ba8575b8ce4aa6474"},"cell_type":"code","source":"model.compile(optimizer = optimizer, loss = \"categorical_crossentropy\", metrics=[\"accuracy\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"27d2d92524beb76086769e996c2a9e7b303ad381"},"cell_type":"code","source":"# CREATE MORE IMAGES VIA DATA AUGMENTATION\ndatagen = ImageDataGenerator( rotation_range=10,  \n        zoom_range = 0.10,  \n        width_shift_range=0.1, \n        height_shift_range=0.1)\n\n#Reduce Learning rate by factor of 0.3\nlearning_rate_decay = ReduceLROnPlateau(monitor='acc', \n                                            patience=3, \n                                            verbose=1, \n                                            factor=0.3, \n                                            min_lr=1e-6)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c9bcdd32a1ce9fdfd03ad32cfd9d329404ce7e75","scrolled":false},"cell_type":"code","source":"batch_size = 64\nepochs = 60\nhistory = model.fit_generator(datagen.flow(x_train,y_train, batch_size=batch_size), \n                    epochs = epochs, steps_per_epoch= x_train.shape[0] // batch_size, verbose = 2, \n                             callbacks=[learning_rate_decay])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3c57c315e1781defed5c2074f4feddbdd894a38a"},"cell_type":"code","source":"plt.plot(history.history['acc'],linestyle='-')\nplt.title('Model Accuracy')\nplt.ylabel('Accuracy')\nplt.xlabel('Epoch')\nplt.legend('Train', loc='upper left')\naxes = plt.gca()\naxes.set_ylim([0.98,1])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d921549ccbf6aa0bb04d2407e9214e8297963c14"},"cell_type":"code","source":"# predict results\nresults = model.predict(x_test)\n\n# select the indix with the maximum probability\nresults = np.argmax(results,axis = 1)\n\nresults = pd.Series(results,name=\"Label\")\n\nsubmission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),results],axis = 1)\n\nsubmission.to_csv(\"submission_cnn_mnist.csv\",index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fc8f4ea39b29379e7275d6afd0647e2c61e781f9"},"cell_type":"code","source":"results.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Explaining the Results"},{"metadata":{"trusted":true,"_uuid":"4673e494cf7e7462fc101df0ba89862fb34a2208"},"cell_type":"code","source":"import shap\n\n# select a set of background examples to take an expectation over\nbackground = x_train[np.random.choice(x_train.shape[0], 100, replace=False)]\n\n# explain predictions of the model on four images\ne = shap.DeepExplainer(model, background)\n# ...or pass tensors directly\n# e = shap.DeepExplainer((model.layers[0].input, model.layers[-1].output), background)\nshap_values = e.shap_values(x_test[1:5])\n\n# plot the feature attributions\nshap.image_plot(shap_values, -x_test[1:5])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"The plot above explains ten outputs (digits 0-9) for four different images. Red pixels increase the model's output while blue pixels decrease the output.\nRefer: https://github.com/slundberg/shap\n"},{"metadata":{"trusted":true},"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}