{"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\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 read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 5GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","collapsed":true,"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":false},"cell_type":"markdown","source":"## 1. Load Necessary Libraries"},{"metadata":{"trusted":true},"cell_type":"code","source":"import keras\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Conv2D, MaxPooling2D, Flatten\nfrom keras.optimizers import SGD","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## 2. Load Data"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_path = \"/kaggle/input/digit-recognizer/train.csv\"\ntest_path = \"/kaggle/input/digit-recognizer/test.csv\"\nsample_path = \"/kaggle/input/digit-recognizer/sample_submission.csv\"\n\ntrain_df = pd.read_csv(train_path)\ntest_df = pd.read_csv(test_path) \nsample = pd.read_csv(sample_path) \n\n# view first 5 rows of the dataset\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# view first 5 rows of sample_submission\nsample.head() ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# view first 5 rows of test dataset\ntest_df.head() ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## 3. Prepare Data"},{"metadata":{"trusted":true},"cell_type":"code","source":"n_height = 28 # height of the image\nn_width = 28  # width of the image\nn_depth = 1   # depth of the image","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# define the input variable X for training set\nX = train_df.drop([\"label\"], axis=1)\n\n# reshape the input variable X to (m, 28,28,1)\nX = np.array(X).reshape(-1, n_height,n_width,n_depth)\n\n# define the target variable Y for training set\nY = train_df['label']\n\n# convert the output variable Y to one hot vector\nn_classes = np.max(Y) + 1\n\nY = np.eye(n_classes)[Y]\n\n# check shape of X and Y\nprint(\"X.shape: \",X.shape)\nprint(\"Y.shape: \",Y.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# define X_test\nX_test = test_df\n\n# reshape the X_test\nX_test = np.array(X_test).reshape(-1, n_height, n_width, n_depth) \nX_test.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# define the training and validation set\n\n# training set\nX_train = X[0:37000]\nY_train = Y[0:37000]\n\n# validation set\nX_val = X[37000:]\nY_val = Y[37000:]\n\n# print shape of training and valudation set\nprint(\"X_train.shape: {0}    Y_train.shape: {1}\".format(X_train.shape, Y_train.shape))\nprint(\"X_val.shape:   {0}    Y_val.shape:   {1}\".format(X_val.shape, Y_val.shape))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## 4. Defining the architecture of the CNN model"},{"metadata":{"trusted":true},"cell_type":"code","source":"# define number of filters \nn_filters = [32, 64]\n\n# define the hyper-parameters\nlearning_rate = 0.001\nn_epochs = 5\nbatch_size = 64","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = Sequential()\n\n# first convolutional layer with 32 filters of size (3,3)\nmodel.add(Conv2D(filters = n_filters[0], input_shape = (n_width, n_height, n_depth), kernel_size=3,\n                padding= 'SAME', activation='relu'))\n\nassert(model.output_shape == (None, 28,28,32))\n\n# first pooling layer with region of size 2x2 and stride 2\nmodel.add(MaxPooling2D(pool_size=(2,2), strides = (2,2)))\n\nassert(model.output_shape == (None, 14,14,32))\n\n# second convolutional layer with 64 filters of size (3,3)\nmodel.add(Conv2D(filters = n_filters[1], input_shape = (n_width, n_height, n_depth), kernel_size=3,\n                padding= 'SAME', activation='relu'))\n\nassert(model.output_shape == (None, 14,14,64))\n\n# second pooling layer with region of size 2x2 and stride 2\nmodel.add(MaxPooling2D(pool_size=(2,2), strides = (2,2)))\n\nassert(model.output_shape == (None, 7,7,64))\n\n# flatten the layer \nmodel.add(Flatten())\n\nassert(model.output_shape == (None, 7*7*64))\n\n# fully-connected layer with 1024 neurons\nmodel.add(Dense(units = 1024, activation='relu'))\n\nassert(model.output_shape == (None, 1024))\n\n# another fully-connected layer with 512 neurons\nmodel.add(Dense(units = 512, activation='relu'))\n\nassert(model.output_shape == (None, 512))\n\n# finally the output layer with softmax activation\nmodel.add(Dense(units = 10, activation='softmax'))\n\n# print model summary\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## 5. Compile, Train and Evaluate the model"},{"metadata":{"trusted":true},"cell_type":"code","source":"# compile the model\nmodel.compile(loss='categorical_crossentropy', \n             optimizer = SGD(lr = learning_rate),\n             metrics = ['accuracy'])\n\n# train the model \nmodel.fit(X_train, Y_train, batch_size = 1,\n         epochs = 5)\n\n# evaluate the model\nscore = model.evaluate(X_val, Y_val)\n\nprint(\"validation loss: \",score[0])\nprint(\"validation accuracy: \",score[1])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## 6. Save the model"},{"metadata":{"trusted":true},"cell_type":"code","source":"# save the model\nmodel.save(\"/kaggle/working/digit_recognizer_model\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# load the model\nmodel = keras.models.load_model(\"/kaggle/working/digit_recognizer_model\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"markdown","source":"## 7. Make Predictions"},{"metadata":{"trusted":true},"cell_type":"code","source":"# make predictions\ny_pred = model.predict(X_test) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"preds = keras.backend.argmax(y_pred)\npreds = np.array(preds)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"imageid = np.arange(1,28001) \nprint(\"preds.shape: \", preds.shape) \nprint(\"imageid.shape: \", imageid.shape) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"arr = np.vstack((imageid, preds)). T \narr.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# create submission dataframe\nsubmission = pd.DataFrame(data=arr, columns=[\"ImageId\", \"Label\"]) \nsubmission.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission.tail() ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission.to_csv(\"/kaggle/working/digit_recognizer_submission.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from IPython.display import FileLink\nFileLink(\"/kaggle/working/digit_recognizer_submission.csv\") ","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}