{"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":"code","source":"import cv2 as cv\nimport numpy as np\nimport pandas as pd\n\nimport tensorflow as tf\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-01-14T15:55:20.79911Z","iopub.execute_input":"2023-01-14T15:55:20.799595Z","iopub.status.idle":"2023-01-14T15:55:20.805378Z","shell.execute_reply.started":"2023-01-14T15:55:20.799558Z","shell.execute_reply":"2023-01-14T15:55:20.804373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest_set = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-01-14T16:04:26.450485Z","iopub.execute_input":"2023-01-14T16:04:26.450891Z","iopub.status.idle":"2023-01-14T16:04:30.323875Z","shell.execute_reply.started":"2023-01-14T16:04:26.450858Z","shell.execute_reply":"2023-01-14T16:04:30.322554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-14T15:55:26.642237Z","iopub.execute_input":"2023-01-14T15:55:26.642621Z","iopub.status.idle":"2023-01-14T15:55:26.65013Z","shell.execute_reply.started":"2023-01-14T15:55:26.642591Z","shell.execute_reply":"2023-01-14T15:55:26.648857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_set.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-14T15:55:28.221662Z","iopub.execute_input":"2023-01-14T15:55:28.222039Z","iopub.status.idle":"2023-01-14T15:55:28.229306Z","shell.execute_reply.started":"2023-01-14T15:55:28.222004Z","shell.execute_reply":"2023-01-14T15:55:28.227907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-14T15:55:29.455814Z","iopub.execute_input":"2023-01-14T15:55:29.456237Z","iopub.status.idle":"2023-01-14T15:55:29.47876Z","shell.execute_reply.started":"2023-01-14T15:55:29.456202Z","shell.execute_reply":"2023-01-14T15:55:29.477705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_set.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-14T15:56:51.170199Z","iopub.execute_input":"2023-01-14T15:56:51.170655Z","iopub.status.idle":"2023-01-14T15:56:51.190695Z","shell.execute_reply.started":"2023-01-14T15:56:51.170619Z","shell.execute_reply":"2023-01-14T15:56:51.189469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Selecting Labels, Features and Preprocessing the training Dataset","metadata":{}},{"cell_type":"code","source":"X_train = train_set.drop(labels = 'label',axis = 1) #features\ny_train = train_set['label'] #labels","metadata":{"execution":{"iopub.status.busy":"2023-01-14T16:04:39.076993Z","iopub.execute_input":"2023-01-14T16:04:39.07777Z","iopub.status.idle":"2023-01-14T16:04:39.203349Z","shell.execute_reply.started":"2023-01-14T16:04:39.077731Z","shell.execute_reply":"2023-01-14T16:04:39.202522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#converting the 1D array to 2D array because \n#image is going to be a 2D image \n#notice below that we have added one more dimesion to the shape after 28,28 it's becuase we need to process the image\n\nX_train = np.reshape(X_train.values, (-1,28,28,1))\n\n#downsampling the images\nX_train = X_train/255.","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2023-01-14T16:06:39.40971Z","iopub.execute_input":"2023-01-14T16:06:39.410072Z","iopub.status.idle":"2023-01-14T16:06:39.560727Z","shell.execute_reply.started":"2023-01-14T16:06:39.410043Z","shell.execute_reply":"2023-01-14T16:06:39.559574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-14T16:06:40.575958Z","iopub.execute_input":"2023-01-14T16:06:40.576427Z","iopub.status.idle":"2023-01-14T16:06:40.58333Z","shell.execute_reply.started":"2023-01-14T16:06:40.57639Z","shell.execute_reply":"2023-01-14T16:06:40.582318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Selecting Labels, Features and Preprocessing the test Dataset","metadata":{}},{"cell_type":"code","source":"X_test = np.reshape(test_set.values, (-1,28,28,1))\n\nX_test = X_test/255.","metadata":{"execution":{"iopub.status.busy":"2023-01-14T16:07:37.269149Z","iopub.execute_input":"2023-01-14T16:07:37.269569Z","iopub.status.idle":"2023-01-14T16:07:37.347457Z","shell.execute_reply.started":"2023-01-14T16:07:37.269537Z","shell.execute_reply":"2023-01-14T16:07:37.346271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# CNN Architecture Building","metadata":{}},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense, Conv2D, Dropout, Flatten, MaxPooling2D\n\n#Dense layer is used to create a 1D number of neurons in it\n\n#Conv2D is used to add up a convolution layer which will \n#reduce the size of the image as we go deeper into the convolution layer\n\n#Dropout layer is used to cutdown the number of connection for each iteration for making it most effective\n\n#Flatten layer is used to convert a 2D or a multdimensional image or a tenser to 1D tenser. \n#Tenser is called as the multidimensional numpy array\n\n#MaxPooling2D is used to pass a filter to a image to take \n#a max value out of it does reducing it's size","metadata":{"execution":{"iopub.status.busy":"2023-01-14T16:08:02.145608Z","iopub.execute_input":"2023-01-14T16:08:02.145966Z","iopub.status.idle":"2023-01-14T16:08:02.77161Z","shell.execute_reply.started":"2023-01-14T16:08:02.145924Z","shell.execute_reply":"2023-01-14T16:08:02.770403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(28, kernel_size = (3,3),input_shape = (28,28,1)))\nmodel.add(Conv2D(28, kernel_size = (3,3)))\nmodel.add(MaxPooling2D(pool_size = (2,2)))\n\nmodel.add(Flatten())\n\nmodel.add(Dense(128,activation = tf.nn.relu)) #activation function calling from tensorflow or simple activation = relu\nmodel.add(Dropout(0.3))\n\nmodel.add(Dense(10, activation = tf.nn.softmax))\n\nmodel.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy', metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-01-14T16:08:13.582107Z","iopub.execute_input":"2023-01-14T16:08:13.582534Z","iopub.status.idle":"2023-01-14T16:08:13.730817Z","shell.execute_reply.started":"2023-01-14T16:08:13.582497Z","shell.execute_reply":"2023-01-14T16:08:13.729233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(x = X_train, y = y_train, epochs = 10)","metadata":{"execution":{"iopub.status.busy":"2023-01-14T16:08:24.575775Z","iopub.execute_input":"2023-01-14T16:08:24.57701Z","iopub.status.idle":"2023-01-14T16:11:56.597925Z","shell.execute_reply.started":"2023-01-14T16:08:24.576964Z","shell.execute_reply":"2023-01-14T16:11:56.597148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(X_test,y_test)","metadata":{"execution":{"iopub.status.busy":"2023-01-14T17:01:16.416308Z","iopub.execute_input":"2023-01-14T17:01:16.416737Z","iopub.status.idle":"2023-01-14T17:01:20.999271Z","shell.execute_reply.started":"2023-01-14T17:01:16.416704Z","shell.execute_reply":"2023-01-14T17:01:20.998195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Taking images as the input to our model","metadata":{}},{"cell_type":"code","source":"list_images = ['eight','five','four','nine','one','seven','six','three','tow','zero']\nfor i in list_images:\n    img = cv.imread('/kaggle/input/test-images-for-mnist-digit-recognition-dataset/'+i+'.png')\n    gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)\n\n    gray = gray/255 #dividing all pixel values by 255\n    gray = 1. - gray #now subtacting each pixel value from 1 to get a image similar to our data base\n    gray = cv.resize(gray, (28,28)) #resizing the image similar to our dataset image (28,28)\n\n    plt.imshow(gray)\n    plt.show()\n\n    gray = np.reshape(gray, (1,28,28,1)) #Now reshaping the image for further procesing similar to our dataset (28,28,1)\n\n    print(\"This image is of a :\",np.argmax(model.predict(gray))) #returns the maximum values index\n    result = model.predict(gray)","metadata":{"execution":{"iopub.status.busy":"2023-01-14T16:18:49.983992Z","iopub.execute_input":"2023-01-14T16:18:49.984436Z","iopub.status.idle":"2023-01-14T16:18:52.792948Z","shell.execute_reply.started":"2023-01-14T16:18:49.984402Z","shell.execute_reply":"2023-01-14T16:18:52.791081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Saving the Model/Weights","metadata":{}},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-01-14T17:00:40.336257Z","iopub.execute_input":"2023-01-14T17:00:40.336639Z","iopub.status.idle":"2023-01-14T17:00:40.343913Z","shell.execute_reply.started":"2023-01-14T17:00:40.336609Z","shell.execute_reply":"2023-01-14T17:00:40.342536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{}},{"cell_type":"code","source":"predictions = model.predict(X_test)\nclasses = predictions.argmax(axis=-1)\n\ncsv = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\ncsv.Label = classes","metadata":{"execution":{"iopub.status.busy":"2023-01-14T17:00:52.243097Z","iopub.execute_input":"2023-01-14T17:00:52.243905Z","iopub.status.idle":"2023-01-14T17:00:57.313705Z","shell.execute_reply.started":"2023-01-14T17:00:52.243868Z","shell.execute_reply":"2023-01-14T17:00:57.312553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"csv.to_csv('submission.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2023-01-14T17:00:58.265368Z","iopub.execute_input":"2023-01-14T17:00:58.265794Z","iopub.status.idle":"2023-01-14T17:00:58.301943Z","shell.execute_reply.started":"2023-01-14T17:00:58.26576Z","shell.execute_reply":"2023-01-14T17:00:58.301084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"csv","metadata":{"execution":{"iopub.status.busy":"2023-01-14T17:01:08.125121Z","iopub.execute_input":"2023-01-14T17:01:08.125516Z","iopub.status.idle":"2023-01-14T17:01:08.138488Z","shell.execute_reply.started":"2023-01-14T17:01:08.125479Z","shell.execute_reply":"2023-01-14T17:01:08.137102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}