{"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":30646,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"\nimport numpy as np\nimport pandas as pd \nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-02-25T04:50:26.816232Z","iopub.execute_input":"2024-02-25T04:50:26.817154Z","iopub.status.idle":"2024-02-25T04:50:27.295504Z","shell.execute_reply.started":"2024-02-25T04:50:26.817099Z","shell.execute_reply":"2024-02-25T04:50:27.294228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport matplotlib.pyplot as plt \nfrom sklearn.model_selection  import train_test_split ","metadata":{"execution":{"iopub.status.busy":"2024-02-25T04:50:27.297708Z","iopub.execute_input":"2024-02-25T04:50:27.298256Z","iopub.status.idle":"2024-02-25T04:50:43.902705Z","shell.execute_reply.started":"2024-02-25T04:50:27.298218Z","shell.execute_reply":"2024-02-25T04:50:43.90137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train_path = \"/kaggle/input/digit-recognizer/train.csv\"\ndata_test_path = \"/kaggle/input/digit-recognizer/test.csv\"","metadata":{"execution":{"iopub.status.busy":"2024-02-25T04:50:43.90465Z","iopub.execute_input":"2024-02-25T04:50:43.905484Z","iopub.status.idle":"2024-02-25T04:50:43.910874Z","shell.execute_reply.started":"2024-02-25T04:50:43.905441Z","shell.execute_reply":"2024-02-25T04:50:43.909657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_data(csv_data):\n    data = pd.read_csv(csv_data)\n    pixel_data = data.values\n    images = []\n    labels = []\n    for pixels in pixel_data:\n        images.append(pixels[1:].reshape(28,28,1))\n        labels.append(pixels[0])\n    return images,labels","metadata":{"execution":{"iopub.status.busy":"2024-02-25T04:50:43.913356Z","iopub.execute_input":"2024-02-25T04:50:43.91434Z","iopub.status.idle":"2024-02-25T04:50:43.928943Z","shell.execute_reply.started":"2024-02-25T04:50:43.914304Z","shell.execute_reply":"2024-02-25T04:50:43.927923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_data_test(csv_data):\n    data = pd.read_csv(csv_data)\n    pixel_data = data.values\n    images = []\n    labels = []\n    for pixels in pixel_data:\n        images.append(pixels[1:].reshape(28,28,1))\n        labels.append(pixels[0])\n    return images,labels","metadata":{"execution":{"iopub.status.busy":"2024-02-25T04:50:43.930466Z","iopub.execute_input":"2024-02-25T04:50:43.931115Z","iopub.status.idle":"2024-02-25T04:50:43.944571Z","shell.execute_reply.started":"2024-02-25T04:50:43.931083Z","shell.execute_reply":"2024-02-25T04:50:43.943319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images,labels = load_data(data_train_path)","metadata":{"execution":{"iopub.status.busy":"2024-02-25T04:50:43.945894Z","iopub.execute_input":"2024-02-25T04:50:43.946205Z","iopub.status.idle":"2024-02-25T04:50:48.12092Z","shell.execute_reply.started":"2024-02-25T04:50:43.946177Z","shell.execute_reply":"2024-02-25T04:50:48.119795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"indices = np.random.randint(10000,size = 3)\nindices","metadata":{"execution":{"iopub.status.busy":"2024-02-25T04:50:48.124813Z","iopub.execute_input":"2024-02-25T04:50:48.125139Z","iopub.status.idle":"2024-02-25T04:50:48.135847Z","shell.execute_reply.started":"2024-02-25T04:50:48.125113Z","shell.execute_reply":"2024-02-25T04:50:48.134697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, idx in enumerate(indices,start=1):\n    plt.subplot(1,len(indices),i)\n    plt.imshow(images[idx])\n    plt.title(labels[idx])\n    plt.axis('off')\n    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-25T04:50:48.137552Z","iopub.execute_input":"2024-02-25T04:50:48.137952Z","iopub.status.idle":"2024-02-25T04:50:48.395572Z","shell.execute_reply.started":"2024-02-25T04:50:48.137922Z","shell.execute_reply":"2024-02-25T04:50:48.394164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(images),len(labels)","metadata":{"execution":{"iopub.status.busy":"2024-02-25T04:50:48.397014Z","iopub.execute_input":"2024-02-25T04:50:48.397867Z","iopub.status.idle":"2024-02-25T04:50:48.406558Z","shell.execute_reply.started":"2024-02-25T04:50:48.397827Z","shell.execute_reply":"2024-02-25T04:50:48.405102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_data_test(csv_data):\n    data = pd.read_csv(csv_data)\n    pixel_data = data.values\n    images = []\n    for pixels in pixel_data:\n        images.append(pixels[:].reshape(28,28,1))\n    return images\n\nimage_test = load_data_test(data_test_path)","metadata":{"execution":{"iopub.status.busy":"2024-02-25T04:50:48.411133Z","iopub.execute_input":"2024-02-25T04:50:48.412244Z","iopub.status.idle":"2024-02-25T04:50:50.962479Z","shell.execute_reply.started":"2024-02-25T04:50:48.412197Z","shell.execute_reply":"2024-02-25T04:50:50.961293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(images[0].shape)\nprint(image_test[0].shape)","metadata":{"execution":{"iopub.status.busy":"2024-02-25T04:50:50.964133Z","iopub.execute_input":"2024-02-25T04:50:50.964565Z","iopub.status.idle":"2024-02-25T04:50:50.970342Z","shell.execute_reply.started":"2024-02-25T04:50:50.964527Z","shell.execute_reply":"2024-02-25T04:50:50.969223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = np.array(images)\nimages_test = np.array(image_test)\nlabels = np.array(labels)\n\nimages = images / 255.0\nimages_test = images_test / 255.0\n\nprint(images.shape)\nprint(images_test.shape)\n\n\nX_train, X_val, y_train, y_val = train_test_split(images, labels, train_size=0.8, random_state=42)\n\nprint(f\"No of data train: {X_train.shape[0]}\")\nprint(f\"No of data val: {X_val.shape[0]}\")\nprint(f\"No of data test: {images_test.shape[0]}\")\n","metadata":{"execution":{"iopub.status.busy":"2024-02-25T04:51:42.265101Z","iopub.execute_input":"2024-02-25T04:51:42.265748Z","iopub.status.idle":"2024-02-25T04:51:42.782847Z","shell.execute_reply.started":"2024-02-25T04:51:42.265703Z","shell.execute_reply":"2024-02-25T04:51:42.780466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape)\nprint(X_val.shape)\nprint(y_train.shape)\nprint(y_val.shape)\n\nprint(type(X_train))\nprint(type(y_train))","metadata":{"execution":{"iopub.status.busy":"2024-02-25T04:51:45.525866Z","iopub.execute_input":"2024-02-25T04:51:45.526275Z","iopub.status.idle":"2024-02-25T04:51:45.533189Z","shell.execute_reply.started":"2024-02-25T04:51:45.526243Z","shell.execute_reply":"2024-02-25T04:51:45.531804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def CNN_Model(input_shape=(28, 28, 1)):\n    inputs = tf.keras.layers.Input(shape=input_shape)\n    \n    conv1 = tf.keras.layers.Conv2D(filters=32, kernel_size=(3, 3), padding=\"same\", activation=\"relu\")(inputs)\n    pool1 = tf.keras.layers.MaxPool2D(pool_size=(2, 2), strides=(2, 2))(conv1)\n    \n    conv2 = tf.keras.layers.Conv2D(filters=64, kernel_size=(3, 3), padding=\"same\", activation=\"relu\")(pool1)\n    pool2 = tf.keras.layers.MaxPool2D(pool_size=(2, 2), strides=(2, 2))(conv2)\n    \n    conv3 = tf.keras.layers.Conv2D(filters=128, kernel_size=(3, 3), padding=\"same\", activation=\"relu\")(pool2)\n    pool3 = tf.keras.layers.MaxPool2D(pool_size=(2, 2), strides=(2,2))(conv3)\n    \n    flatten = tf.keras.layers.Flatten()(pool3)\n    fc1 = tf.keras.layers.Dense(units=128, activation=\"relu\")(flatten)\n    fc2 = tf.keras.layers.Dense(units=256, activation=\"relu\")(fc1)\n    \n    outputs = tf.keras.layers.Dense(units=10, activation=\"softmax\")(fc2)\n    \n    model = tf.keras.models.Model(inputs, outputs)\n    model.summary()\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2024-02-25T04:51:48.353337Z","iopub.execute_input":"2024-02-25T04:51:48.353735Z","iopub.status.idle":"2024-02-25T04:51:48.367026Z","shell.execute_reply.started":"2024-02-25T04:51:48.353706Z","shell.execute_reply":"2024-02-25T04:51:48.365994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n\ninput_shape=(28, 28, 1): This specifies the shape of the input images. The default shape is 28x28 pixels with 1 color channel (grayscale). This input shape is typical for datasets like MNIST.\n\n\nFilters: A filter (or kernel) is a small matrix used to apply effects such as edge detection. The number of filters (32, 64, 128) dictates the number of output feature maps the layer will produce. Each filter extracts different features from the input image.\nKernel Size: The size of the filter matrix. Here, it's (3, 3) for all convolutional layers, meaning each filter covers a 3x3 area of the input image.\n\nPadding: \"same\" padding means the output feature map will have the same dimensions as the input. This is achieved by padding the input image with zeros around the border, allowing filters to be applied to the edge pixels.\n\nActivation: The activation function introduces non-linearity to the model, allowing it to learn more complex patterns. \"relu\" (Rectified Linear Unit) is a common choice, defined as  f(x)=max(0,x).\n\n\nPool Size: The size of the window to take the maximum over. (2, 2) means that it takes the maximum value over a 2x2 area of the input feature map, reducing its size by a factor of 4.\n\nStrides: The number of pixels by which the pool window is shifted when scanning the input. A stride of (2, 2) reduces the dimensionality of the input by half in both the height and width dimensions, further reducing the spatial size of the representation and the number of parameters.\n\nFlattening Layer (Flatten): Converts the 3D output of the last pooling layer into a 1D tensor, making it possible to connect it to a fully connected layer.\n\nUnits: The number of neurons in the layer. The model progressively increases the dimensionality from 128 to 256 in the dense layers, allowing the network to learn more complex representations.\n\nActivation: \"relu\" is used for intermediate layers to introduce non-linearity, and \"softmax\" for the output layer, which is suitable for multi-class classification problems. The softmax function converts the output to a probability distribution over the 10 classes.\n\nProduces a 10-element vector (due to units=10), where each element represents the model's confidence that the input image belongs to one of the 10 classes. The \"softmax\" activation ensures these confidence levels sum up to 1, making them interpretable as probabilities.","metadata":{}},{"cell_type":"code","source":"cnn_model = CNN_Model()\n\ncnn_model.compile(optimizer=tf.keras.optimizers.Adam(), loss=\"sparse_categorical_crossentropy\", metrics=[\"acc\"])\ncallbacks = tf.keras.callbacks.ReduceLROnPlateau(monitor=\"val_acc\", factor=0.5, patience=5, min_lr=1e-4)\n\nhistory = cnn_model.fit(X_train, y_train, validation_data = (X_val, y_val), epochs=10, callbacks=[callbacks])","metadata":{"execution":{"iopub.status.busy":"2024-02-25T04:51:54.155747Z","iopub.execute_input":"2024-02-25T04:51:54.156162Z","iopub.status.idle":"2024-02-25T04:57:18.0733Z","shell.execute_reply.started":"2024-02-25T04:51:54.156129Z","shell.execute_reply":"2024-02-25T04:57:18.071901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['acc'])\nplt.plot(history.history['val_acc'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'val'], loc='upper left')\nplt.show()\n\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('model loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'val'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-25T04:57:18.595251Z","iopub.execute_input":"2024-02-25T04:57:18.59562Z","iopub.status.idle":"2024-02-25T04:57:19.117917Z","shell.execute_reply.started":"2024-02-25T04:57:18.595589Z","shell.execute_reply":"2024-02-25T04:57:19.116462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = cnn_model.predict(images_test)\n\npreds = np.argmax(prediction, axis=1)\n\nprint(preds)","metadata":{"execution":{"iopub.status.busy":"2024-02-25T04:57:23.550376Z","iopub.execute_input":"2024-02-25T04:57:23.550806Z","iopub.status.idle":"2024-02-25T04:57:34.164059Z","shell.execute_reply.started":"2024-02-25T04:57:23.550773Z","shell.execute_reply":"2024-02-25T04:57:34.162701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = {\n    \"ImageId\": [i for i in range(images_test.shape[0])],\n    \"label\": preds\n}\n\ndf = pd.DataFrame(data)\n\nprint(df)\ndf.to_csv(\"submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-02-25T04:57:37.937762Z","iopub.execute_input":"2024-02-25T04:57:37.938141Z","iopub.status.idle":"2024-02-25T04:57:38.028416Z","shell.execute_reply.started":"2024-02-25T04:57:37.938113Z","shell.execute_reply":"2024-02-25T04:57:38.027127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"indices_test = np.random.randint(600,size = 4)\nindices_test","metadata":{"execution":{"iopub.status.busy":"2024-02-25T04:58:25.643411Z","iopub.execute_input":"2024-02-25T04:58:25.643831Z","iopub.status.idle":"2024-02-25T04:58:25.652236Z","shell.execute_reply.started":"2024-02-25T04:58:25.643801Z","shell.execute_reply":"2024-02-25T04:58:25.650932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, idx in enumerate(indices_test,start=1):\n    plt.subplot(1,len(indices),i)\n    plt.imshow(images_test[idx])\n    plt.title(preds[idx])\n    plt.axis('off')\n    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-25T04:59:10.698879Z","iopub.execute_input":"2024-02-25T04:59:10.699268Z","iopub.status.idle":"2024-02-25T04:59:11.321327Z","shell.execute_reply.started":"2024-02-25T04:59:10.699239Z","shell.execute_reply":"2024-02-25T04:59:11.319732Z"},"trusted":true},"execution_count":null,"outputs":[]}]}