{"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":"![](https://i.ytimg.com/vi/KMPHZrsAPfw/maxresdefault.jpg)","metadata":{}},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport tensorflow as tf \nimport matplotlib.pyplot as plt \nfrom tensorflow.keras import models","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-08-28T12:41:00.741800Z","iopub.execute_input":"2021-08-28T12:41:00.742172Z","iopub.status.idle":"2021-08-28T12:41:00.748011Z","shell.execute_reply.started":"2021-08-28T12:41:00.742142Z","shell.execute_reply":"2021-08-28T12:41:00.746077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Num GPUs Available: \", len(tf.config.list_physical_devices('GPU')))\n","metadata":{"execution":{"iopub.status.busy":"2021-08-28T12:41:00.799511Z","iopub.execute_input":"2021-08-28T12:41:00.799966Z","iopub.status.idle":"2021-08-28T12:41:00.807070Z","shell.execute_reply.started":"2021-08-28T12:41:00.799924Z","shell.execute_reply":"2021-08-28T12:41:00.805962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images_labels  = pd.read_csv(\"../input/digit-recognizer/train.csv\")\ntest_images_labels = pd.read_csv(\"../input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-08-28T12:41:00.813494Z","iopub.execute_input":"2021-08-28T12:41:00.814067Z","iopub.status.idle":"2021-08-28T12:41:05.181971Z","shell.execute_reply.started":"2021-08-28T12:41:00.814029Z","shell.execute_reply":"2021-08-28T12:41:05.181074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## We have 42 000 images to train and 28 000 to test our model","metadata":{}},{"cell_type":"code","source":"from termcolor import colored\nprint(\"train : \",colored(train_images_labels.shape,'blue'),\"test : \",colored(test_images_labels.shape,'blue'))","metadata":{"execution":{"iopub.status.busy":"2021-08-28T12:41:05.183469Z","iopub.execute_input":"2021-08-28T12:41:05.183784Z","iopub.status.idle":"2021-08-28T12:41:05.189333Z","shell.execute_reply.started":"2021-08-28T12:41:05.183751Z","shell.execute_reply":"2021-08-28T12:41:05.188517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Plot 9 random images","metadata":{}},{"cell_type":"code","source":"number_One=np.array(train_images_labels.iloc[10,1:]).reshape((28,28))\nf, axarr = plt.subplots(3,3)\naxarr[0,0].imshow(np.array(test_images_labels.iloc[0,0:]).reshape((28,28)))\naxarr[0,1].imshow(np.array(train_images_labels.iloc[1,1:]).reshape((28,28)))\naxarr[0,2].imshow(np.array(train_images_labels.iloc[9,1:]).reshape((28,28)))\naxarr[1,0].imshow(np.array(train_images_labels.iloc[3,1:]).reshape((28,28)))\naxarr[1,1].imshow(np.array(train_images_labels.iloc[46,1:]).reshape((28,28)))\naxarr[1,2].imshow(np.array(train_images_labels.iloc[52,1:]).reshape((28,28)))\naxarr[2,0].imshow(np.array(train_images_labels.iloc[6,1:]).reshape((28,28)))\naxarr[2,1].imshow(np.array(train_images_labels.iloc[7,1:]).reshape((28,28)))\naxarr[2,2].imshow(np.array(train_images_labels.iloc[8,1:]).reshape((28,28)))","metadata":{"execution":{"iopub.status.busy":"2021-08-28T12:41:05.191152Z","iopub.execute_input":"2021-08-28T12:41:05.191637Z","iopub.status.idle":"2021-08-28T12:41:05.893472Z","shell.execute_reply.started":"2021-08-28T12:41:05.191603Z","shell.execute_reply":"2021-08-28T12:41:05.892500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels , test_labels = train_images_labels.iloc[:,0],test_images_labels.iloc[:,0]","metadata":{"execution":{"iopub.status.busy":"2021-08-28T12:41:05.895171Z","iopub.execute_input":"2021-08-28T12:41:05.895529Z","iopub.status.idle":"2021-08-28T12:41:05.900872Z","shell.execute_reply.started":"2021-08-28T12:41:05.895491Z","shell.execute_reply":"2021-08-28T12:41:05.900066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Convolutional Neural Network","metadata":{}},{"cell_type":"markdown","source":"![](https://www.researchgate.net/profile/Amir_Hossein_Yazdani_Abyaneh/publication/329608053/figure/download/fig2/AS:703218313080832@1544671719021/Typical-CNN-architecture.jpg)","metadata":{}},{"cell_type":"markdown","source":"### If ou are interesting in understanding CNN from scratch i recommend this amazing playist from Andrew NG :\nhttps://www.youtube.com/watch?v=ArPaAX_PhIs&list=PLkDaE6sCZn6Gl29AoE31iwdVwSG-KnDzF&index=1","metadata":{}},{"cell_type":"code","source":"# callbacks help you to stop the training when you reach a% accuracy or b% loss it depends on you (:\nclass myCallback(tf.keras.callbacks.Callback):\n    def on_epoch_end(self, epoch, logs={}):\n        if(logs.get('accuracy')> 0.9999):\n            print(\"\\nReached 100% accuracy so cancelling training!\")\n            self.model.stop_training = True\n\ncallbacks = myCallback()\n\n\nmnist = tf.keras.datasets.mnist\n(training_images, training_labels), (test_images, test_labels) = mnist.load_data()\n\ntraining_images=training_images.reshape(training_images.shape[0], 28, 28, 1)\ntraining_images=training_images / 255.0\ntest_images = test_images.reshape(test_images.shape[0], 28, 28, 1)\ntest_images=test_images/255.0\n\n#define layers of our model\n#each element in parametre list is a layer of our CNN\nmodel = tf.keras.models.Sequential([\n        \n        tf.keras.layers.Conv2D(72, (3,3), activation='relu', input_shape=(28, 28, 1)),\n        tf.keras.layers.MaxPooling2D(2, 2),\n        tf.keras.layers.Conv2D(72, (3,3), activation='relu'),\n        tf.keras.layers.MaxPooling2D(2, 2),\n        tf.keras.layers.Flatten(),\n        tf.keras.layers.Dense(128, activation='relu'),\n        tf.keras.layers.Dense(10, activation='softmax')\n        \n])\n\nmodel.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n\n# this part is very interesting if you want to the input an output of each layer in you Neural Network\nmodel.summary()\n\n# model fitting\nhistory = model.fit(\n   \n    training_images, training_labels, epochs=100 ,  callbacks=[callbacks]\n    \n)\n# model fitting\n#history.epoch, history.history['acc'][-1]\n\n","metadata":{"execution":{"iopub.status.busy":"2021-08-28T12:41:05.902274Z","iopub.execute_input":"2021-08-28T12:41:05.902842Z","iopub.status.idle":"2021-08-28T12:44:57.848017Z","shell.execute_reply.started":"2021-08-28T12:41:05.902780Z","shell.execute_reply":"2021-08-28T12:44:57.847217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(np.array(test_images_labels.iloc[0:,0:]).reshape((28000,28,28,1)))","metadata":{"execution":{"iopub.status.busy":"2021-08-28T12:44:57.850723Z","iopub.execute_input":"2021-08-28T12:44:57.851056Z","iopub.status.idle":"2021-08-28T12:44:59.053685Z","shell.execute_reply.started":"2021-08-28T12:44:57.851027Z","shell.execute_reply":"2021-08-28T12:44:59.052879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction_s = predictions.argmax(axis=1) ","metadata":{"execution":{"iopub.status.busy":"2021-08-28T12:44:59.056841Z","iopub.execute_input":"2021-08-28T12:44:59.057094Z","iopub.status.idle":"2021-08-28T12:44:59.063341Z","shell.execute_reply.started":"2021-08-28T12:44:59.057069Z","shell.execute_reply":"2021-08-28T12:44:59.062561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#prediction_s","metadata":{"execution":{"iopub.status.busy":"2021-08-28T12:44:59.065957Z","iopub.execute_input":"2021-08-28T12:44:59.066314Z","iopub.status.idle":"2021-08-28T12:44:59.072002Z","shell.execute_reply.started":"2021-08-28T12:44:59.066284Z","shell.execute_reply":"2021-08-28T12:44:59.071051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit = pd.DataFrame({\"ImageId\":np.array(range(1,28001)),\"Label\":prediction_s})","metadata":{"execution":{"iopub.status.busy":"2021-08-28T12:44:59.073460Z","iopub.execute_input":"2021-08-28T12:44:59.073887Z","iopub.status.idle":"2021-08-28T12:44:59.089127Z","shell.execute_reply.started":"2021-08-28T12:44:59.073852Z","shell.execute_reply":"2021-08-28T12:44:59.088324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-08-28T12:44:59.090450Z","iopub.execute_input":"2021-08-28T12:44:59.090803Z","iopub.status.idle":"2021-08-28T12:44:59.146234Z","shell.execute_reply.started":"2021-08-28T12:44:59.090766Z","shell.execute_reply":"2021-08-28T12:44:59.145365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}