{"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":"### Import Libraries","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nfrom keras.preprocessing.image import ImageDataGenerator","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-07-15T12:06:28.198610Z","iopub.execute_input":"2023-07-15T12:06:28.199204Z","iopub.status.idle":"2023-07-15T12:06:37.233133Z","shell.execute_reply.started":"2023-07-15T12:06:28.199164Z","shell.execute_reply":"2023-07-15T12:06:37.232054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Data Handling","metadata":{}},{"cell_type":"code","source":"# Reading Data\ntrain_data = 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-07-15T12:06:37.235136Z","iopub.execute_input":"2023-07-15T12:06:37.235805Z","iopub.status.idle":"2023-07-15T12:06:42.851825Z","shell.execute_reply.started":"2023-07-15T12:06:37.235773Z","shell.execute_reply":"2023-07-15T12:06:42.850677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-07-15T12:06:42.853196Z","iopub.execute_input":"2023-07-15T12:06:42.853539Z","iopub.status.idle":"2023-07-15T12:06:42.894964Z","shell.execute_reply.started":"2023-07-15T12:06:42.853509Z","shell.execute_reply":"2023-07-15T12:06:42.893711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Label Counts\ntrain_data['label'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-07-15T12:06:42.897355Z","iopub.execute_input":"2023-07-15T12:06:42.897780Z","iopub.status.idle":"2023-07-15T12:06:42.913346Z","shell.execute_reply.started":"2023-07-15T12:06:42.897749Z","shell.execute_reply":"2023-07-15T12:06:42.912261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Seperating Pixels and Labels\nX = train_data.iloc[:, 1:]\ny = train_data['label']","metadata":{"execution":{"iopub.status.busy":"2023-07-15T12:06:42.914939Z","iopub.execute_input":"2023-07-15T12:06:42.915293Z","iopub.status.idle":"2023-07-15T12:06:42.923628Z","shell.execute_reply.started":"2023-07-15T12:06:42.915263Z","shell.execute_reply":"2023-07-15T12:06:42.922387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reshape data in 28x28 Black & White image format\nXr = X.values.reshape(-1, 28, 28, 1)\ntest_setr = test_set.values.reshape(-1,28,28, 1)","metadata":{"execution":{"iopub.status.busy":"2023-07-15T12:06:42.925099Z","iopub.execute_input":"2023-07-15T12:06:42.925430Z","iopub.status.idle":"2023-07-15T12:06:42.938536Z","shell.execute_reply.started":"2023-07-15T12:06:42.925400Z","shell.execute_reply":"2023-07-15T12:06:42.937123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Converts a class vector (integers) to binary class matrix.\nyr = tf.keras.utils.to_categorical(y, num_classes=10)","metadata":{"execution":{"iopub.status.busy":"2023-07-15T12:06:42.939838Z","iopub.execute_input":"2023-07-15T12:06:42.940292Z","iopub.status.idle":"2023-07-15T12:06:42.952747Z","shell.execute_reply.started":"2023-07-15T12:06:42.940244Z","shell.execute_reply":"2023-07-15T12:06:42.951628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Shape of Data\nXr.shape, yr.shape","metadata":{"execution":{"iopub.status.busy":"2023-07-15T12:06:42.954380Z","iopub.execute_input":"2023-07-15T12:06:42.954736Z","iopub.status.idle":"2023-07-15T12:06:42.969074Z","shell.execute_reply.started":"2023-07-15T12:06:42.954706Z","shell.execute_reply":"2023-07-15T12:06:42.967960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Image Augmentation \n\ntrain_datagen = ImageDataGenerator(rescale = 1./255,\n                                   shear_range=0.1,\n                                   zoom_range = 0.1,\n                                   width_shift_range=0.1,\n                                   height_shift_range=0.1,\n                                   rotation_range=10,)\n\nXr1 = train_datagen.flow(x=Xr, y=yr, batch_size=32)","metadata":{"execution":{"iopub.status.busy":"2023-07-15T12:06:42.970458Z","iopub.execute_input":"2023-07-15T12:06:42.971019Z","iopub.status.idle":"2023-07-15T12:06:43.063169Z","shell.execute_reply.started":"2023-07-15T12:06:42.970985Z","shell.execute_reply":"2023-07-15T12:06:43.062112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Image Augmentation : test data\n\ntest_datagen = ImageDataGenerator(rescale = 1./255)\n\ntest_setr1 = test_datagen.flow(x=test_setr, batch_size=32)","metadata":{"execution":{"iopub.status.busy":"2023-07-15T12:06:43.067126Z","iopub.execute_input":"2023-07-15T12:06:43.067477Z","iopub.status.idle":"2023-07-15T12:06:43.125836Z","shell.execute_reply.started":"2023-07-15T12:06:43.067447Z","shell.execute_reply":"2023-07-15T12:06:43.124590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Visualize some images\nf,ax = plt.subplots(3, 7, figsize=(21,9))\nfor i in range(7):\n    ax[0][i].imshow(Xr[i])\n    ax[0][i].set_xlabel(y[i])\n    ax[1][i].imshow(Xr[i+100])\n    ax[1][i].set_xlabel(y[i+100])\n    ax[2][i].imshow(Xr[i+500])\n    ax[2][i].set_xlabel(y[i+500])\n    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-15T12:06:43.128642Z","iopub.execute_input":"2023-07-15T12:06:43.129444Z","iopub.status.idle":"2023-07-15T12:06:46.022735Z","shell.execute_reply.started":"2023-07-15T12:06:43.129395Z","shell.execute_reply":"2023-07-15T12:06:46.021648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### CNN Model Training","metadata":{}},{"cell_type":"code","source":"# model \ncnn = tf.keras.models.Sequential()","metadata":{"execution":{"iopub.status.busy":"2023-07-15T12:06:46.024027Z","iopub.execute_input":"2023-07-15T12:06:46.024369Z","iopub.status.idle":"2023-07-15T12:06:46.134996Z","shell.execute_reply.started":"2023-07-15T12:06:46.024340Z","shell.execute_reply":"2023-07-15T12:06:46.133868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Adding Convolutional and Pooling Layers\n\ncnn.add(tf.keras.layers.Conv2D(filters=32, kernel_size=3, activation='relu', input_shape=[28,28,1]))\ncnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2))\n\n# Adding more layers\n\ncnn.add(tf.keras.layers.Conv2D(filters=64, kernel_size=3, activation='relu', padding='same'))\ncnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2, padding='same'))\n\ncnn.add(tf.keras.layers.Conv2D(filters=128, kernel_size=3, activation='relu', padding='same'))\ncnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2, padding='same'))","metadata":{"execution":{"iopub.status.busy":"2023-07-15T12:06:46.136551Z","iopub.execute_input":"2023-07-15T12:06:46.137651Z","iopub.status.idle":"2023-07-15T12:06:46.260035Z","shell.execute_reply.started":"2023-07-15T12:06:46.137604Z","shell.execute_reply":"2023-07-15T12:06:46.258813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Flattening layer\ncnn.add(tf.keras.layers.Flatten())","metadata":{"execution":{"iopub.status.busy":"2023-07-15T12:06:46.261529Z","iopub.execute_input":"2023-07-15T12:06:46.261873Z","iopub.status.idle":"2023-07-15T12:06:46.273091Z","shell.execute_reply.started":"2023-07-15T12:06:46.261843Z","shell.execute_reply":"2023-07-15T12:06:46.271979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Hidden Layer to train neural network\ncnn.add(tf.keras.layers.Dense(units=128, activation='relu'))","metadata":{"execution":{"iopub.status.busy":"2023-07-15T12:06:46.274287Z","iopub.execute_input":"2023-07-15T12:06:46.274630Z","iopub.status.idle":"2023-07-15T12:06:46.301992Z","shell.execute_reply.started":"2023-07-15T12:06:46.274602Z","shell.execute_reply":"2023-07-15T12:06:46.300966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Output Layer\ncnn.add(tf.keras.layers.Dense(units=10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2023-07-15T12:06:46.303277Z","iopub.execute_input":"2023-07-15T12:06:46.303601Z","iopub.status.idle":"2023-07-15T12:06:46.329364Z","shell.execute_reply.started":"2023-07-15T12:06:46.303573Z","shell.execute_reply":"2023-07-15T12:06:46.328205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compiling using Stochastic Gradient Descent and Loss function\ncnn.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-07-15T12:06:46.330686Z","iopub.execute_input":"2023-07-15T12:06:46.331384Z","iopub.status.idle":"2023-07-15T12:06:46.353251Z","shell.execute_reply.started":"2023-07-15T12:06:46.331336Z","shell.execute_reply":"2023-07-15T12:06:46.352232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train Neural Network\ncnn.fit(x=Xr1, epochs = 10)","metadata":{"execution":{"iopub.status.busy":"2023-07-15T12:06:46.354450Z","iopub.execute_input":"2023-07-15T12:06:46.355767Z","iopub.status.idle":"2023-07-15T12:17:52.739171Z","shell.execute_reply.started":"2023-07-15T12:06:46.355735Z","shell.execute_reply":"2023-07-15T12:17:52.737874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predict the Test data on neuron\nypred = cnn.predict(test_setr)","metadata":{"execution":{"iopub.status.busy":"2023-07-15T12:17:52.740793Z","iopub.execute_input":"2023-07-15T12:17:52.741268Z","iopub.status.idle":"2023-07-15T12:18:03.453086Z","shell.execute_reply.started":"2023-07-15T12:17:52.741227Z","shell.execute_reply":"2023-07-15T12:18:03.451939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Using argmax() to return maximum values along an axis.\ntest_preds = []\nfor i in range(len(ypred)):\n    test_preds.append(np.argmax(ypred[i]))","metadata":{"execution":{"iopub.status.busy":"2023-07-15T12:18:03.454903Z","iopub.execute_input":"2023-07-15T12:18:03.455726Z","iopub.status.idle":"2023-07-15T12:18:03.544966Z","shell.execute_reply.started":"2023-07-15T12:18:03.455682Z","shell.execute_reply":"2023-07-15T12:18:03.543957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Visualize some predicted images\nf,ax = plt.subplots(3, 7, figsize=(21,9))\nfor i in range(7):\n    ax[0][i].imshow(test_setr[i])\n    ax[0][i].set_xlabel(test_preds[i])\n    ax[1][i].imshow(test_setr[i+100])\n    ax[1][i].set_xlabel(test_preds[i+100])\n    ax[2][i].imshow(test_setr[i+500])\n    ax[2][i].set_xlabel(test_preds[i+500])\n    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-15T12:18:03.546456Z","iopub.execute_input":"2023-07-15T12:18:03.547392Z","iopub.status.idle":"2023-07-15T12:18:06.197359Z","shell.execute_reply.started":"2023-07-15T12:18:03.547317Z","shell.execute_reply":"2023-07-15T12:18:06.196207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Submission","metadata":{}},{"cell_type":"code","source":"sub = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-07-15T12:18:06.199031Z","iopub.execute_input":"2023-07-15T12:18:06.199441Z","iopub.status.idle":"2023-07-15T12:18:06.223968Z","shell.execute_reply.started":"2023-07-15T12:18:06.199409Z","shell.execute_reply":"2023-07-15T12:18:06.222935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.Label = test_preds","metadata":{"execution":{"iopub.status.busy":"2023-07-15T12:18:06.225103Z","iopub.execute_input":"2023-07-15T12:18:06.225416Z","iopub.status.idle":"2023-07-15T12:18:06.305204Z","shell.execute_reply.started":"2023-07-15T12:18:06.225389Z","shell.execute_reply":"2023-07-15T12:18:06.304081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-07-15T12:18:06.306564Z","iopub.execute_input":"2023-07-15T12:18:06.306924Z","iopub.status.idle":"2023-07-15T12:18:06.357009Z","shell.execute_reply.started":"2023-07-15T12:18:06.306867Z","shell.execute_reply":"2023-07-15T12:18:06.355938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}