{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# CNN  digital recognition with tensorflow","metadata":{}},{"cell_type":"markdown","source":"###  **Please upvote if you like my approach or if you learned something from this notebook. Your support gives me motivation to create interesting stuff. Thank you.** ","metadata":{}},{"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# 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 20GB 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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-21T09:50:18.880376Z","iopub.execute_input":"2021-11-21T09:50:18.880950Z","iopub.status.idle":"2021-11-21T09:50:18.897130Z","shell.execute_reply.started":"2021-11-21T09:50:18.880827Z","shell.execute_reply":"2021-11-21T09:50:18.896089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# importing the libraries ","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom keras.models import Sequential\nfrom keras.callbacks import ReduceLROnPlateau\nfrom keras.utils.np_utils import to_categorical\n\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D, LeakyReLU\nfrom keras.optimizers import RMSprop","metadata":{"execution":{"iopub.status.busy":"2021-11-21T09:50:18.899048Z","iopub.execute_input":"2021-11-21T09:50:18.900005Z","iopub.status.idle":"2021-11-21T09:50:24.306002Z","shell.execute_reply.started":"2021-11-21T09:50:18.899962Z","shell.execute_reply":"2021-11-21T09:50:24.304329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 style=\"color:black\" align=\"center\">Handwritten digits classification using neural network</h1>\n\n\nIn this notebook we will classify handwritten digits using a simple neural network which has only input and output layers. We will than add a hidden layer and see how the performance of the model improves","metadata":{}},{"cell_type":"markdown","source":"after reading the data we are going to normalize it by divid each pixel with 255 ","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntrain.head()\ny = train.label\nx = train.drop(columns= 'label')\nx = x/255\nx.head()\n\ny = to_categorical(y, num_classes = 10)\nprint(y.shape)","metadata":{"execution":{"iopub.status.busy":"2021-11-21T09:50:24.310253Z","iopub.execute_input":"2021-11-21T09:50:24.310503Z","iopub.status.idle":"2021-11-21T09:50:27.601914Z","shell.execute_reply.started":"2021-11-21T09:50:24.310478Z","shell.execute_reply":"2021-11-21T09:50:27.600968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"here we are going to use sklearn models to fit the data into a NN model that have **64 nuroal** in the frist hidden layer, **32 nuroal **in the second hidden layer, and** 10 output** layers ","metadata":{}},{"cell_type":"markdown","source":" <img src= \"https://www.researchgate.net/profile/Woongje-Sung/publication/322313274/figure/fig1/AS:653009751134212@1532701065757/LeNet-59-CNN-for-handwritten-digit-recognition-task.png\" alt =\"nuroal network \"  align=\"center\" style='width: 500px;' >\n","metadata":{}},{"cell_type":"code","source":"X_train,X_val,y_train,y_val = train_test_split(x,y,test_size=0.2, random_state=100)\nprint(X_train.shape ,X_val.shape, y_train.shape, y_val.shape)\nX_train= X_train.values.reshape( -1 , 28 , 28 , 1)\nX_val= X_val.values.reshape( -1 , 28 , 28 , 1)\nprint(X_train.shape ,X_val.shape)\n","metadata":{"execution":{"iopub.status.busy":"2021-11-21T09:50:27.603538Z","iopub.execute_input":"2021-11-21T09:50:27.604117Z","iopub.status.idle":"2021-11-21T09:50:27.833247Z","shell.execute_reply.started":"2021-11-21T09:50:27.604074Z","shell.execute_reply":"2021-11-21T09:50:27.832342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# model \n>* first we are going to create CNN with 32 filters,  kernel 5x5 , and relu activation funciton  followed by Max pooling layer \n>* then create the second CNN layer with 64 filters,  kernel 3x3 , and relu activation funciton  followed by Max pooling layer \n>* then using flat the data into 1 dimension using flatten funciton, with 1024 NN,  relu activation function, and 50% dropout \n>* followed by second NN with 256 neuron with leaky Relu activation function, 50% dropout \n>* lastly the output NN with 10 neurons that calculated using softmax activation function. ","metadata":{}},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', \n                 activation ='tanh', input_shape = (28,28,1)))\n\nmodel.add(MaxPool2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', \n                 activation='relu'))\nmodel.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))\n\n\nmodel.add(Flatten())\nmodel.add(Dense(1024, activation=LeakyReLU(alpha=0.3)))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(256, activation=LeakyReLU(alpha=0.2)) )\nmodel.add(Dropout(0.5))\nmodel.add(Dense(10, activation = \"softmax\"))","metadata":{"execution":{"iopub.status.busy":"2021-11-21T09:50:27.834518Z","iopub.execute_input":"2021-11-21T09:50:27.834942Z","iopub.status.idle":"2021-11-21T09:50:29.750515Z","shell.execute_reply.started":"2021-11-21T09:50:27.834911Z","shell.execute_reply":"2021-11-21T09:50:29.749006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the optimizer\noptimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)\n# Compile the model\nmodel.compile(optimizer = optimizer , loss = \"binary_crossentropy\", metrics=[\"accuracy\"])\n# Set a learning rate annealer\nlearning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', \n                                            patience=3, \n                                            verbose=1, \n                                            factor=0.5, \n                                            min_lr=0.00001)\nepochs = 25 \nbatch_size = 80","metadata":{"execution":{"iopub.status.busy":"2021-11-21T09:50:29.752446Z","iopub.execute_input":"2021-11-21T09:50:29.753090Z","iopub.status.idle":"2021-11-21T09:50:29.770093Z","shell.execute_reply.started":"2021-11-21T09:50:29.753050Z","shell.execute_reply":"2021-11-21T09:50:29.769295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-21T09:50:29.771226Z","iopub.execute_input":"2021-11-21T09:50:29.771578Z","iopub.status.idle":"2021-11-21T09:50:29.787188Z","shell.execute_reply.started":"2021-11-21T09:50:29.771539Z","shell.execute_reply":"2021-11-21T09:50:29.786127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# model fitting","metadata":{}},{"cell_type":"code","source":"# Fit the model \nprint(X_train.shape ,X_val.shape, y_train.shape, y_val.shape)\n\n\nhistory = model.fit(x= X_train,y= y_train, batch_size=batch_size,\n                              epochs = epochs, validation_data = (X_val, y_val),\n                              verbose = 2, steps_per_epoch=(X_train.shape[0] // batch_size)\n                            , callbacks=[learning_rate_reduction])","metadata":{"execution":{"iopub.status.busy":"2021-11-21T09:50:29.788641Z","iopub.execute_input":"2021-11-21T09:50:29.789002Z","iopub.status.idle":"2021-11-21T09:51:10.788396Z","shell.execute_reply.started":"2021-11-21T09:50:29.788966Z","shell.execute_reply":"2021-11-21T09:51:10.787552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# model predicting","metadata":{}},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\nprint(test.shape)\n\nxtest = test/255\nxtest= xtest.values.reshape( -1 , 28 , 28 , 1)\nprint(xtest.shape)\nypre= model.predict(xtest)\nypre","metadata":{"execution":{"iopub.status.busy":"2021-11-21T09:51:10.791352Z","iopub.execute_input":"2021-11-21T09:51:10.791647Z","iopub.status.idle":"2021-11-21T09:51:13.973406Z","shell.execute_reply.started":"2021-11-21T09:51:10.791596Z","shell.execute_reply":"2021-11-21T09:51:13.972495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\nsample1 = sample.drop(columns= 'ImageId')\nsample1[:5]","metadata":{"execution":{"iopub.status.busy":"2021-11-21T09:51:13.975035Z","iopub.execute_input":"2021-11-21T09:51:13.975442Z","iopub.status.idle":"2021-11-21T09:51:14.002517Z","shell.execute_reply.started":"2021-11-21T09:51:13.975390Z","shell.execute_reply":"2021-11-21T09:51:14.001542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# display some answers ","metadata":{}},{"cell_type":"code","source":"y_predicted = [np.argmax(i) for i in ypre]\ny_predicted[:5]","metadata":{"execution":{"iopub.status.busy":"2021-11-21T09:51:14.004007Z","iopub.execute_input":"2021-11-21T09:51:14.004578Z","iopub.status.idle":"2021-11-21T09:51:14.096043Z","shell.execute_reply.started":"2021-11-21T09:51:14.004534Z","shell.execute_reply":"2021-11-21T09:51:14.095312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(sample1),'<-- sample prediction-->  ' ,  len(y_predicted))","metadata":{"execution":{"iopub.status.busy":"2021-11-21T09:51:14.097286Z","iopub.execute_input":"2021-11-21T09:51:14.097664Z","iopub.status.idle":"2021-11-21T09:51:14.103180Z","shell.execute_reply.started":"2021-11-21T09:51:14.097609Z","shell.execute_reply":"2021-11-21T09:51:14.102077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_predicted = pd.DataFrame(y_predicted)\nnewY = pd.concat([sample.drop(columns='Label'), y_predicted], axis = 1)\n","metadata":{"execution":{"iopub.status.busy":"2021-11-21T09:51:14.104638Z","iopub.execute_input":"2021-11-21T09:51:14.105082Z","iopub.status.idle":"2021-11-21T09:51:14.133391Z","shell.execute_reply.started":"2021-11-21T09:51:14.105045Z","shell.execute_reply":"2021-11-21T09:51:14.132652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"newY.columns=['ImageId', 'Label']\nnewY","metadata":{"execution":{"iopub.status.busy":"2021-11-21T09:51:14.134544Z","iopub.execute_input":"2021-11-21T09:51:14.135080Z","iopub.status.idle":"2021-11-21T09:51:14.149070Z","shell.execute_reply.started":"2021-11-21T09:51:14.135038Z","shell.execute_reply":"2021-11-21T09:51:14.148014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Evaluate the Modle ","metadata":{}},{"cell_type":"code","source":"model.evaluate(xtest,ypre)","metadata":{"execution":{"iopub.status.busy":"2021-11-21T09:51:14.150429Z","iopub.execute_input":"2021-11-21T09:51:14.150909Z","iopub.status.idle":"2021-11-21T09:51:16.161597Z","shell.execute_reply.started":"2021-11-21T09:51:14.150872Z","shell.execute_reply":"2021-11-21T09:51:16.160832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# saving the Model","metadata":{}},{"cell_type":"code","source":"newY.to_csv('my_submission.csv', index= False)\n","metadata":{"execution":{"iopub.status.busy":"2021-11-21T09:51:16.162831Z","iopub.execute_input":"2021-11-21T09:51:16.163156Z","iopub.status.idle":"2021-11-21T09:51:16.213757Z","shell.execute_reply.started":"2021-11-21T09:51:16.163120Z","shell.execute_reply":"2021-11-21T09:51:16.212999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}