{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-09-14T04:44:14.145103Z","iopub.execute_input":"2023-09-14T04:44:14.145996Z","iopub.status.idle":"2023-09-14T04:44:14.463957Z","shell.execute_reply.started":"2023-09-14T04:44:14.145957Z","shell.execute_reply":"2023-09-14T04:44:14.463015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Importing Dependencies","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport torch\nimport torch.nn as nn\nimport torchvision\nfrom torchvision import transforms\nfrom torch.utils.data import DataLoader\nfrom torch.utils.data import TensorDataset\nfrom torch.utils.data import Dataset\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.model_selection import train_test_split\n\nfrom torch.autograd import Variable","metadata":{"execution":{"iopub.status.busy":"2023-09-14T06:19:57.691523Z","iopub.execute_input":"2023-09-14T06:19:57.691892Z","iopub.status.idle":"2023-09-14T06:20:02.772149Z","shell.execute_reply.started":"2023-09-14T06:19:57.691861Z","shell.execute_reply":"2023-09-14T06:20:02.771056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/Kannada-MNIST/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/Kannada-MNIST/Dig-MNIST.csv\")\nsubmission_set = pd.read_csv(\"/kaggle/input/Kannada-MNIST/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-09-14T06:20:02.774828Z","iopub.execute_input":"2023-09-14T06:20:02.775448Z","iopub.status.idle":"2023-09-14T06:20:08.849714Z","shell.execute_reply.started":"2023-09-14T06:20:02.775410Z","shell.execute_reply":"2023-09-14T06:20:08.848704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-14T06:20:08.851131Z","iopub.execute_input":"2023-09-14T06:20:08.851493Z","iopub.status.idle":"2023-09-14T06:20:08.880442Z","shell.execute_reply.started":"2023-09-14T06:20:08.851457Z","shell.execute_reply":"2023-09-14T06:20:08.879362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_img = train.drop(columns=[\"label\"])\ntrain_label = train[\"label\"]\n\ntest_img = test.drop(columns=[\"label\"])\ntest_label = test[\"label\"]\n\nprediction_image = submission_set.drop(columns=[\"id\"])","metadata":{"execution":{"iopub.status.busy":"2023-09-14T06:20:08.883373Z","iopub.execute_input":"2023-09-14T06:20:08.883746Z","iopub.status.idle":"2023-09-14T06:20:09.042879Z","shell.execute_reply.started":"2023-09-14T06:20:08.883710Z","shell.execute_reply":"2023-09-14T06:20:09.041727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_img, val_img, train_label, val_label = train_test_split(train_img, train_label, test_size=0.3)","metadata":{"execution":{"iopub.status.busy":"2023-09-14T06:20:09.044154Z","iopub.execute_input":"2023-09-14T06:20:09.044518Z","iopub.status.idle":"2023-09-14T06:20:09.430490Z","shell.execute_reply.started":"2023-09-14T06:20:09.044484Z","shell.execute_reply":"2023-09-14T06:20:09.429457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_img.reset_index(drop=True, inplace=True)\ntrain_label.reset_index(drop=True, inplace=True)\n\nval_img.reset_index(drop=True, inplace=True)\nval_label.reset_index(drop=True, inplace=True)\n\ntest_img.reset_index(drop=True, inplace=True)\ntest_label.reset_index(drop=True, inplace=True)\n\nprediction_image.reset_index(drop=True, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-09-14T06:20:09.431991Z","iopub.execute_input":"2023-09-14T06:20:09.432395Z","iopub.status.idle":"2023-09-14T06:20:09.439795Z","shell.execute_reply.started":"2023-09-14T06:20:09.432360Z","shell.execute_reply":"2023-09-14T06:20:09.438753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMAGE_SIZE = 28\n\nclass KDataset(Dataset):\n    def __init__(self, images, labels, transforms = None) -> None:\n        self.X = images\n        self.y = labels\n        self.transforms = transforms\n    \n    def __len__(self):\n        return (len(self.X))\n    \n    def __getitem__(self, i):\n        data = self.X.iloc[i,:]\n        data = np.array(data).astype(np.uint8).reshape(IMAGE_SIZE, IMAGE_SIZE)\n        if self.transforms:\n            data = self.transforms(data)  \n        if self.y is not None:\n            return (data, self.y[i])\n        else:\n            return data","metadata":{"execution":{"iopub.status.busy":"2023-09-14T06:20:09.441365Z","iopub.execute_input":"2023-09-14T06:20:09.442109Z","iopub.status.idle":"2023-09-14T06:20:09.451659Z","shell.execute_reply.started":"2023-09-14T06:20:09.442052Z","shell.execute_reply":"2023-09-14T06:20:09.450689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_transforms = transforms.Compose(([\n    transforms.ToPILImage(),\n    transforms.ToTensor(),\n]))\n\nval_transforms = transforms.Compose(([\n    transforms.ToPILImage(),\n    transforms.ToTensor(),\n]))","metadata":{"execution":{"iopub.status.busy":"2023-09-14T06:20:09.452939Z","iopub.execute_input":"2023-09-14T06:20:09.453344Z","iopub.status.idle":"2023-09-14T06:20:09.468313Z","shell.execute_reply.started":"2023-09-14T06:20:09.453311Z","shell.execute_reply":"2023-09-14T06:20:09.466947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_transforms = transforms.Compose(([\n    transforms.ToPILImage(),\n    transforms.ToTensor(),\n]))\n\nval_transforms = transforms.Compose(([\n    transforms.ToPILImage(),\n    transforms.ToTensor(),\n]))","metadata":{"execution":{"iopub.status.busy":"2023-09-14T06:20:09.473265Z","iopub.execute_input":"2023-09-14T06:20:09.473597Z","iopub.status.idle":"2023-09-14T06:20:09.481551Z","shell.execute_reply.started":"2023-09-14T06:20:09.473568Z","shell.execute_reply":"2023-09-14T06:20:09.480401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 45\n\ntrain_dataset = KDataset(train_img, train_label, train_transforms)\n\nval_dataset = KDataset(val_img, val_label, val_transforms)\n\ntest_dataset = KDataset(test_img, test_label, val_transforms)\n\nsubmission_dataset = KDataset(prediction_image, None, val_transforms)","metadata":{"execution":{"iopub.status.busy":"2023-09-14T06:20:11.016382Z","iopub.execute_input":"2023-09-14T06:20:11.016745Z","iopub.status.idle":"2023-09-14T06:20:11.023210Z","shell.execute_reply.started":"2023-09-14T06:20:11.016715Z","shell.execute_reply":"2023-09-14T06:20:11.021960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataloader = DataLoader(train_dataset,\n                              batch_size = BATCH_SIZE,\n                              shuffle = True)\nval_dataloader = DataLoader(val_dataset,\n                            batch_size = BATCH_SIZE,\n                            shuffle = False)\ntest_dataloader = DataLoader(test_dataset,\n                             batch_size = BATCH_SIZE,\n                             shuffle = False)\nprediction_dataloader = DataLoader(submission_dataset,\n                                   batch_size = BATCH_SIZE,\n                                   shuffle = False)","metadata":{"execution":{"iopub.status.busy":"2023-09-14T06:20:12.064181Z","iopub.execute_input":"2023-09-14T06:20:12.064553Z","iopub.status.idle":"2023-09-14T06:20:12.071379Z","shell.execute_reply.started":"2023-09-14T06:20:12.064522Z","shell.execute_reply":"2023-09-14T06:20:12.070053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images, labels = next(iter(train_dataloader))\nimages, labels = images[:2], labels[:2]\nplt.imshow(torchvision.utils.make_grid(images)[0,:,:], cmap=\"gray\")\nprint(labels)","metadata":{"execution":{"iopub.status.busy":"2023-09-14T06:20:13.481207Z","iopub.execute_input":"2023-09-14T06:20:13.481564Z","iopub.status.idle":"2023-09-14T06:20:13.887001Z","shell.execute_reply.started":"2023-09-14T06:20:13.481534Z","shell.execute_reply":"2023-09-14T06:20:13.886017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.nn as nn\nimport torch.nn.functional as F\nclass Net(nn.Module):\n    def __init__(self):\n        super(Net, self).__init__()\n        self.conv1 = nn.Conv2d(1, 32, 3)\n        self.conv2 = nn.Conv2d(32, 64, 3)\n        self.pool = nn.MaxPool2d(2, 2)\n        \n        # Adjusted fully connected layers\n        self.fc1 = nn.Linear(64 * 5 * 5, 128)  \n        self.fc2 = nn.Linear(128, 10)  \n\n    def forward(self, x):\n        x = self.pool(F.relu(self.conv1(x)))\n        x = self.pool(F.relu(self.conv2(x)))\n        \n        # Flatten the tensor\n        x = x.view(x.size(0), -1)  # Flatten all dimensions except batch\n        \n        x = F.relu(self.fc1(x))\n        x = self.fc2(x)\n        return x\n\nnet = Net()","metadata":{"execution":{"iopub.status.busy":"2023-09-14T06:21:05.128823Z","iopub.execute_input":"2023-09-14T06:21:05.129225Z","iopub.status.idle":"2023-09-14T06:21:05.142158Z","shell.execute_reply.started":"2023-09-14T06:21:05.129191Z","shell.execute_reply":"2023-09-14T06:21:05.141007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"criterion = nn.CrossEntropyLoss()\noptimizer = torch.optim.SGD(net.parameters(), lr=0.001,momentum=0.9)","metadata":{"execution":{"iopub.status.busy":"2023-09-14T06:21:05.941873Z","iopub.execute_input":"2023-09-14T06:21:05.942323Z","iopub.status.idle":"2023-09-14T06:21:05.948057Z","shell.execute_reply.started":"2023-09-14T06:21:05.942288Z","shell.execute_reply":"2023-09-14T06:21:05.946684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for epoch in range(25):  \n\n    running_loss = 0.0\n    for i, data in enumerate(train_dataloader, 0):\n        # get the inputs; data is a list of [inputs, labels]\n        inputs, labels = data\n\n        # zero the parameter gradients\n        optimizer.zero_grad()\n\n        # forward + backward + optimize\n        outputs = net(inputs)\n        loss = criterion(outputs, labels)\n        loss.backward()\n        optimizer.step()\n\n        # print statistics\n        running_loss += loss.item()\n        if i % 200 == 199:    \n            print(f'[{epoch + 1}, {i + 1:5d}] loss: {running_loss / 2000:.3f}')\n            running_loss = 0.0\n\nprint('Finished Training')","metadata":{"execution":{"iopub.status.busy":"2023-09-14T06:21:07.323349Z","iopub.execute_input":"2023-09-14T06:21:07.323714Z","iopub.status.idle":"2023-09-14T06:34:46.841873Z","shell.execute_reply.started":"2023-09-14T06:21:07.323685Z","shell.execute_reply":"2023-09-14T06:34:46.840709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"correct = 0\ntotal = 0\n# since we're not training, we don't need to calculate the gradients for our outputs\nwith torch.no_grad():\n    for data in test_dataloader:\n        images, labels = data\n        # calculate outputs by running images through the network\n        outputs = net(images)\n        # the class with the highest energy is what we choose as prediction\n        _, predicted = torch.max(outputs.data, 1)\n        total += labels.size(0)\n        correct += (predicted == labels).sum().item()\n\nprint(f'Accuracy of the network on the test images: {100 * correct // total} %')","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}