{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30673,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport random\nimport matplotlib.pyplot as plt\n\nfrom sklearn.model_selection import train_test_split\n\n\nimport tensorflow as tf\nimport keras\nfrom keras.utils import to_categorical\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPooling2D, Flatten, Dense\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-03T17:57:50.015381Z","iopub.execute_input":"2024-05-03T17:57:50.015779Z","iopub.status.idle":"2024-05-03T17:58:06.620523Z","shell.execute_reply.started":"2024-05-03T17:57:50.015746Z","shell.execute_reply":"2024-05-03T17:58:06.619099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Import and Preprocessing** ","metadata":{}},{"cell_type":"markdown","source":"# 1. Import Data","metadata":{}},{"cell_type":"code","source":"train_path = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\n\ntest_path = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-05-03T17:58:06.623381Z","iopub.execute_input":"2024-05-03T17:58:06.624283Z","iopub.status.idle":"2024-05-03T17:58:13.240717Z","shell.execute_reply.started":"2024-05-03T17:58:06.624232Z","shell.execute_reply":"2024-05-03T17:58:13.239546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2. Extracting pixel values and labels","metadata":{}},{"cell_type":"code","source":"X_train = train_path.drop(\"label\", axis = 1).values.astype(\"float32\")\nX_test = test_path.values.astype(\"float32\")\n\ny_train = train_path[\"label\"].values","metadata":{"execution":{"iopub.status.busy":"2024-05-03T17:58:13.245820Z","iopub.execute_input":"2024-05-03T17:58:13.246252Z","iopub.status.idle":"2024-05-03T17:58:13.510348Z","shell.execute_reply.started":"2024-05-03T17:58:13.246214Z","shell.execute_reply":"2024-05-03T17:58:13.509305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 3. Normalizng pixel values","metadata":{}},{"cell_type":"code","source":"X_train = X_train / 255.0\nX_test = X_test / 255.0","metadata":{"execution":{"iopub.status.busy":"2024-05-03T17:58:13.513990Z","iopub.execute_input":"2024-05-03T17:58:13.514432Z","iopub.status.idle":"2024-05-03T17:58:13.590909Z","shell.execute_reply.started":"2024-05-03T17:58:13.514396Z","shell.execute_reply":"2024-05-03T17:58:13.589912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 4. Reshape the data into correct format","metadata":{}},{"cell_type":"code","source":"X_train = X_train.reshape(-1, 28, 28, 1)\nX_test = X_test.reshape(-1, 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2024-05-03T17:58:13.592385Z","iopub.execute_input":"2024-05-03T17:58:13.592862Z","iopub.status.idle":"2024-05-03T17:58:13.598961Z","shell.execute_reply.started":"2024-05-03T17:58:13.592806Z","shell.execute_reply":"2024-05-03T17:58:13.597867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 5. Encoding the labels","metadata":{}},{"cell_type":"code","source":"y_train = to_categorical(y_train, num_classes=10)","metadata":{"execution":{"iopub.status.busy":"2024-05-03T17:58:13.600544Z","iopub.execute_input":"2024-05-03T17:58:13.601032Z","iopub.status.idle":"2024-05-03T17:58:13.611037Z","shell.execute_reply.started":"2024-05-03T17:58:13.600995Z","shell.execute_reply":"2024-05-03T17:58:13.609767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 6. Visualize random images","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10, 10))\nrandom_img = random.sample(range(len(X_train)), 25)\nfor i, idx in enumerate(random_img):\n    plt.subplot(5, 5, i + 1)\n    plt.imshow(X_train[i].reshape(28, 28), cmap='gray')\n    plt.title(f'Label: {np.argmax(y_train[i])}')\n    plt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-03T17:58:13.613021Z","iopub.execute_input":"2024-05-03T17:58:13.613453Z","iopub.status.idle":"2024-05-03T17:58:15.322776Z","shell.execute_reply.started":"2024-05-03T17:58:13.613422Z","shell.execute_reply":"2024-05-03T17:58:15.321674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 7. Splitting into Training and Test Set","metadata":{}},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-05-03T17:58:15.324096Z","iopub.execute_input":"2024-05-03T17:58:15.324417Z","iopub.status.idle":"2024-05-03T17:58:15.716675Z","shell.execute_reply.started":"2024-05-03T17:58:15.324390Z","shell.execute_reply":"2024-05-03T17:58:15.715481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Build the \"LeNet\" Model**","metadata":{}},{"cell_type":"code","source":"EPOCHS = 10\nBATCH_SIZE = 128\nOPTIMIZER = tf.keras.optimizers.Adam()\nVALIDATION_SPLIT = 0.9\nVERBOSE = 1","metadata":{"execution":{"iopub.status.busy":"2024-05-03T17:58:15.718037Z","iopub.execute_input":"2024-05-03T17:58:15.718392Z","iopub.status.idle":"2024-05-03T17:58:15.757886Z","shell.execute_reply.started":"2024-05-03T17:58:15.718363Z","shell.execute_reply":"2024-05-03T17:58:15.756381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(20, (5,5), activation=\"relu\", input_shape=(28, 28, 1)))\nmodel.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))\n\nmodel.add(Conv2D(50, (5,5), activation=\"relu\"))\nmodel.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))\n\nmodel.add(Flatten())\nmodel.add(Dense(500, activation=\"relu\"))\nmodel.add(Dense(10, activation=\"softmax\"))","metadata":{"execution":{"iopub.status.busy":"2024-05-03T17:58:15.759166Z","iopub.execute_input":"2024-05-03T17:58:15.759504Z","iopub.status.idle":"2024-05-03T17:58:15.869914Z","shell.execute_reply.started":"2024-05-03T17:58:15.759475Z","shell.execute_reply":"2024-05-03T17:58:15.868739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss=\"categorical_crossentropy\", optimizer=OPTIMIZER, metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2024-05-03T17:58:15.871265Z","iopub.execute_input":"2024-05-03T17:58:15.871652Z","iopub.status.idle":"2024-05-03T17:58:15.881870Z","shell.execute_reply.started":"2024-05-03T17:58:15.871583Z","shell.execute_reply":"2024-05-03T17:58:15.880683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-05-03T17:58:15.883317Z","iopub.execute_input":"2024-05-03T17:58:15.883687Z","iopub.status.idle":"2024-05-03T17:58:15.914972Z","shell.execute_reply.started":"2024-05-03T17:58:15.883657Z","shell.execute_reply":"2024-05-03T17:58:15.913816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(X_train, y_train, batch_size=BATCH_SIZE, epochs=EPOCHS, verbose=VERBOSE, validation_split=VALIDATION_SPLIT)","metadata":{"execution":{"iopub.status.busy":"2024-05-03T17:58:15.920608Z","iopub.execute_input":"2024-05-03T17:58:15.920980Z","iopub.status.idle":"2024-05-03T17:59:04.709295Z","shell.execute_reply.started":"2024-05-03T17:58:15.920951Z","shell.execute_reply":"2024-05-03T17:59:04.707770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score = model.evaluate(X_val, y_val, verbose=VERBOSE)\nprint(\"\\nTest score:\", score[0])\nprint(\"Test accuracy:\", score[1])","metadata":{"execution":{"iopub.status.busy":"2024-05-03T17:59:04.710928Z","iopub.execute_input":"2024-05-03T17:59:04.711282Z","iopub.status.idle":"2024-05-03T17:59:06.671939Z","shell.execute_reply.started":"2024-05-03T17:59:04.711253Z","shell.execute_reply":"2024-05-03T17:59:06.670577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Evaluate the model","metadata":{}},{"cell_type":"code","source":"# Plot loss per iteration \n\nplt.figure(figsize=(12,6))\nplt.subplot(1,2,1)\nplt.plot(history.history['loss'], label='loss', color=\"blue\")\nplt.plot(history.history['val_loss'], label='val_loss', color=\"orange\")\nplt.title('LeNet Model Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend()\n\n# Plot accuracy per iteration\n\nplt.subplot(1, 2, 2)\nplt.plot(history.history['accuracy'], label='acc', color=\"blue\")\nplt.plot(history.history['val_accuracy'], label='val_acc', color=\"orange\")\nplt.title('LeNet Model Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2024-05-03T17:59:06.673267Z","iopub.execute_input":"2024-05-03T17:59:06.673610Z","iopub.status.idle":"2024-05-03T17:59:07.264248Z","shell.execute_reply.started":"2024-05-03T17:59:06.673573Z","shell.execute_reply":"2024-05-03T17:59:07.262951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images = X_val\ntest_labels = y_val\npredictions = model.predict(test_images)\n\nfor i in range(10):\n    predicted_label_index = np.argmax(predictions[i])\n    predicted_label = predicted_label_index\n    true_label_index = np.argmax(test_labels[i])\n    true_label = true_label_index\n    \n    # Show image\n    plt.figure(figsize=(2,2))\n    plt.imshow(test_images[i])\n    plt.title(f\"Predicted Label: {predicted_label}\\nTrue Label: {true_label}\")\n    plt.axis('off')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-03T17:59:07.265559Z","iopub.execute_input":"2024-05-03T17:59:07.265926Z","iopub.status.idle":"2024-05-03T17:59:10.772116Z","shell.execute_reply.started":"2024-05-03T17:59:07.265889Z","shell.execute_reply":"2024-05-03T17:59:10.770066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Prediction Test Set**","metadata":{}},{"cell_type":"code","source":"y_pred = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-05-03T17:59:10.774664Z","iopub.execute_input":"2024-05-03T17:59:10.776020Z","iopub.status.idle":"2024-05-03T17:59:16.269736Z","shell.execute_reply.started":"2024-05-03T17:59:10.775957Z","shell.execute_reply":"2024-05-03T17:59:16.268603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred","metadata":{"execution":{"iopub.status.busy":"2024-05-03T17:59:16.271376Z","iopub.execute_input":"2024-05-03T17:59:16.271762Z","iopub.status.idle":"2024-05-03T17:59:16.280013Z","shell.execute_reply.started":"2024-05-03T17:59:16.271730Z","shell.execute_reply":"2024-05-03T17:59:16.278898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_img = 5\n\nfor i in range(num_img):\n    pred_label_idx = np.argmax(y_pred[i])\n    pred_label = pred_label_idx\n    \n    plt.figure(figsize=(2,2))\n    plt.imshow(X_test[i].reshape(28,28), cmap=\"gray\")\n    plt.title(f\"Predicted Label: {pred_label}\")\n    plt.axis(\"off\")\n    plt.show()\n    ","metadata":{"execution":{"iopub.status.busy":"2024-05-03T17:59:16.281452Z","iopub.execute_input":"2024-05-03T17:59:16.281828Z","iopub.status.idle":"2024-05-03T17:59:16.917297Z","shell.execute_reply.started":"2024-05-03T17:59:16.281798Z","shell.execute_reply":"2024-05-03T17:59:16.916020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Submission**","metadata":{}},{"cell_type":"code","source":"submission = pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")\n\nsubmission[\"Label\"] = np.argmax(y_pred, axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-05-03T17:59:16.919161Z","iopub.execute_input":"2024-05-03T17:59:16.919605Z","iopub.status.idle":"2024-05-03T17:59:16.940763Z","shell.execute_reply.started":"2024-05-03T17:59:16.919564Z","shell.execute_reply":"2024-05-03T17:59:16.939605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-03T17:59:16.942284Z","iopub.execute_input":"2024-05-03T17:59:16.942972Z","iopub.status.idle":"2024-05-03T17:59:16.959276Z","shell.execute_reply.started":"2024-05-03T17:59:16.942932Z","shell.execute_reply":"2024-05-03T17:59:16.958101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# os.remove(\"/kaggle/working/submission\")","metadata":{"execution":{"iopub.status.busy":"2024-05-03T17:59:16.961282Z","iopub.execute_input":"2024-05-03T17:59:16.962139Z","iopub.status.idle":"2024-05-03T17:59:16.967053Z","shell.execute_reply.started":"2024-05-03T17:59:16.962096Z","shell.execute_reply":"2024-05-03T17:59:16.965900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2024-05-03T17:59:16.968986Z","iopub.execute_input":"2024-05-03T17:59:16.969988Z","iopub.status.idle":"2024-05-03T17:59:17.018802Z","shell.execute_reply.started":"2024-05-03T17:59:16.969945Z","shell.execute_reply":"2024-05-03T17:59:17.017629Z"},"trusted":true},"execution_count":null,"outputs":[]}]}