{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":16017,"databundleVersionId":708189,"sourceType":"competition"}],"dockerImageVersionId":30587,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# 1. Import Libraries","metadata":{"execution":{"iopub.status.busy":"2023-12-07T12:17:53.186127Z","iopub.execute_input":"2023-12-07T12:17:53.186779Z","iopub.status.idle":"2023-12-07T12:17:57.933067Z","shell.execute_reply.started":"2023-12-07T12:17:53.186711Z","shell.execute_reply":"2023-12-07T12:17:57.931335Z"}}},{"cell_type":"code","source":"import torch\nfrom torch.utils.data import DataLoader, Dataset\nimport torchvision\nimport torchvision.transforms as transforms\nfrom torch.utils.data.sampler import SubsetRandomSampler\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd","metadata":{"execution":{"iopub.status.busy":"2023-12-07T14:45:43.168734Z","iopub.execute_input":"2023-12-07T14:45:43.169092Z","iopub.status.idle":"2023-12-07T14:45:43.175305Z","shell.execute_reply.started":"2023-12-07T14:45:43.169065Z","shell.execute_reply":"2023-12-07T14:45:43.174116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2. Define custom dataset class","metadata":{}},{"cell_type":"code","source":"class TrainMNIST(Dataset):\n    def __init__(self, file_path, transform = None):\n        self.data = pd.read_csv(file_path)\n        self.transform = transform\n    \n    def __len__(self):\n        return len(self.data)\n    \n    def __getitem__(self, index):\n        #load image as ndarray type (Height * Width * Channels)\n        image = self.data.iloc[index, 1:].values.astype(np.uint8).reshape((28,28,1))\n        label = self.data.iloc[index, 0]\n        \n        if self.transform is not None:\n            image = self.transform(image)\n    \n        return image, label","metadata":{"execution":{"iopub.status.busy":"2023-12-07T14:45:45.280627Z","iopub.execute_input":"2023-12-07T14:45:45.281011Z","iopub.status.idle":"2023-12-07T14:45:45.289195Z","shell.execute_reply.started":"2023-12-07T14:45:45.280982Z","shell.execute_reply":"2023-12-07T14:45:45.287644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class TestMNIST(Dataset):\n    def __init__(self, file_path, transform = None):\n        self.data = pd.read_csv(file_path)\n        self.transform = transform\n        \n    def __len__(self):\n        return len(self.data)\n    \n    def __getitem__(self, index):\n        #load image\n        image = self.data.iloc[index, 1:].values.astype(np.uint8).reshape((28,28,1))\n        \n        if self.transform is not None:\n            image = self.transform(image)\n    \n        return image","metadata":{"execution":{"iopub.status.busy":"2023-12-07T14:45:47.983559Z","iopub.execute_input":"2023-12-07T14:45:47.983972Z","iopub.status.idle":"2023-12-07T14:45:47.992071Z","shell.execute_reply.started":"2023-12-07T14:45:47.983942Z","shell.execute_reply":"2023-12-07T14:45:47.990868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 3. Load and Visualize the data","metadata":{}},{"cell_type":"code","source":"train_data = TrainMNIST('/kaggle/input/Kannada-MNIST/train.csv', transform=transforms.ToTensor())\ntest_data = TestMNIST('/kaggle/input/Kannada-MNIST/test.csv', transform=transforms.ToTensor())","metadata":{"execution":{"iopub.status.busy":"2023-12-07T14:45:50.338606Z","iopub.execute_input":"2023-12-07T14:45:50.339799Z","iopub.status.idle":"2023-12-07T14:45:54.625092Z","shell.execute_reply.started":"2023-12-07T14:45:50.339749Z","shell.execute_reply":"2023-12-07T14:45:54.624008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_workers = 0\nbatch_size = 35\n\ntrain_loader = torch.utils.data.DataLoader(train_data, batch_size = batch_size, \n                                           num_workers = num_workers)\ntest_loader = torch.utils.data.DataLoader(test_data, batch_size = batch_size,\n                                         num_workers = num_workers)","metadata":{"execution":{"iopub.status.busy":"2023-12-07T14:45:54.628256Z","iopub.execute_input":"2023-12-07T14:45:54.628736Z","iopub.status.idle":"2023-12-07T14:45:55.366146Z","shell.execute_reply.started":"2023-12-07T14:45:54.628697Z","shell.execute_reply":"2023-12-07T14:45:55.364916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline\n\n#obtain one batch of training images\ndataiter = iter(train_loader)\nimages, labels = next(dataiter)\nimages = images.numpy()\n\n# plot the images in the batch, along with the corresponding labels\nfig = plt.figure(figsize = (25,4))\nfor idx in np.arange(20):\n    ax = fig.add_subplot(2, int(20/2), idx+1, xticks=[], yticks=[])\n    ax.imshow(np.squeeze(images[idx]), cmap = 'gray')\n    ax.set_title(str(labels[idx].item()))","metadata":{"execution":{"iopub.status.busy":"2023-12-07T14:46:03.729397Z","iopub.execute_input":"2023-12-07T14:46:03.729795Z","iopub.status.idle":"2023-12-07T14:46:04.989790Z","shell.execute_reply.started":"2023-12-07T14:46:03.729766Z","shell.execute_reply":"2023-12-07T14:46:04.988645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 4. Build a simple Neural Network","metadata":{}},{"cell_type":"code","source":"import torch.nn as nn\nimport torch.nn.functional as F\n\nclass NN(nn.Module):\n    def __init__(self):\n        super(NN, self).__init__()\n        hidden_layer1 = 512\n        hidden_layer2 = 512\n        self.fc1 = nn.Linear(28*28, hidden_layer1)\n        self.fc2 = nn.Linear(hidden_layer1, hidden_layer2)\n        self.fc3 = nn.Linear(hidden_layer2, 10)\n        \n    def forward(self, x):\n        #flatten image input\n        x = x.view(-1, 28*28)\n        # add hidden layer, with relu activation function\n        x = F.relu(self.fc1(x))\n        x = F.relu(self.fc2(x))\n        x = self.fc3(x)\n        return x\n    \nmodel = NN()\nprint(model)","metadata":{"execution":{"iopub.status.busy":"2023-12-07T14:25:24.308920Z","iopub.status.idle":"2023-12-07T14:25:24.309650Z","shell.execute_reply.started":"2023-12-07T14:25:24.309312Z","shell.execute_reply":"2023-12-07T14:25:24.309342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 5. Loss Function and Optimizer","metadata":{}},{"cell_type":"code","source":"criterion = nn.CrossEntropyLoss()\noptimizer = torch.optim.SGD(model.parameters(), lr=0.01)","metadata":{"execution":{"iopub.status.busy":"2023-12-07T14:25:24.311740Z","iopub.status.idle":"2023-12-07T14:25:24.312446Z","shell.execute_reply.started":"2023-12-07T14:25:24.312158Z","shell.execute_reply":"2023-12-07T14:25:24.312185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 6. Train the network","metadata":{}},{"cell_type":"code","source":"n_epochs = 40\ntrain_loss_min = np.Inf\n\nfor epoch in range(n_epochs):\n    train_loss = 0.0\n    model.train()\n    for data, target in train_loader:\n        # clear the gradients of all optimized variables\n        optimizer.zero_grad()\n        output = model(data)\n        loss = criterion(output, target)\n        loss.backward()\n        # perform a single optimization step (parameter update)\n        optimizer.step()\n        train_loss += loss.item()*data.size(0)\n        \n    train_loss = train_loss/len(train_loader.dataset)\n    print('Epoch: {} \\t Training loss {:.6f}'.format(epoch+1, train_loss))\n    \n    if train_loss <= train_loss_min:\n        print('Trianing loss decreased ({:.6f} --> {:.6f}). Saving model...'.format(train_loss_min,\n                                                                                    train_loss))\n        torch.save(model.state_dict(),'model.pt')\n        train_loss_min = train_loss","metadata":{"execution":{"iopub.status.busy":"2023-12-07T14:29:39.050617Z","iopub.execute_input":"2023-12-07T14:29:39.051041Z","iopub.status.idle":"2023-12-07T14:43:10.113319Z","shell.execute_reply.started":"2023-12-07T14:29:39.051011Z","shell.execute_reply":"2023-12-07T14:43:10.112338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Load the model with the lowest validation loss","metadata":{}},{"cell_type":"code","source":"model.load_state_dict(torch.load('model.pt'))","metadata":{"execution":{"iopub.status.busy":"2023-12-07T14:44:18.136527Z","iopub.execute_input":"2023-12-07T14:44:18.136948Z","iopub.status.idle":"2023-12-07T14:44:18.148528Z","shell.execute_reply.started":"2023-12-07T14:44:18.136918Z","shell.execute_reply":"2023-12-07T14:44:18.147612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n## Test the trained network","metadata":{}},{"cell_type":"code","source":"dataiter = iter(test_loader)\nimages = next(dataiter)\n\noutput = model(images)\n# convert output probabilities to predicted class\n_, preds = torch.max(output, 1)\nimages = images.numpy()\n\nfig = plt.figure(figsize = (25,4))\nfor idx in np.arange(20):\n    ax = fig.add_subplot(2, int(20/2), idx+1, xticks=[], yticks=[])\n    ax.imshow(np.squeeze(images[idx]), cmap = 'gray')\n    ax.set_title(\"{}\".format(str(preds[idx].item())))","metadata":{"execution":{"iopub.status.busy":"2023-12-07T14:46:11.262492Z","iopub.execute_input":"2023-12-07T14:46:11.262927Z","iopub.status.idle":"2023-12-07T14:46:13.627843Z","shell.execute_reply.started":"2023-12-07T14:46:11.262891Z","shell.execute_reply":"2023-12-07T14:46:13.626626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 7. Predict Kannada digit based on input","metadata":{}},{"cell_type":"code","source":"test_loader = torch.utils.data.DataLoader(test_data, batch_size=1, \n    num_workers=num_workers)","metadata":{"execution":{"iopub.status.busy":"2023-12-07T14:46:13.629592Z","iopub.execute_input":"2023-12-07T14:46:13.629921Z","iopub.status.idle":"2023-12-07T14:46:13.635876Z","shell.execute_reply.started":"2023-12-07T14:46:13.629893Z","shell.execute_reply":"2023-12-07T14:46:13.634414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict(model, dataloader):\n    prediction_list = []\n    for i, batch in enumerate(dataloader):\n        outputs = model(batch)\n        _, predicted = torch.max(outputs.data, 1) \n        prediction_list.append(predicted.cpu())\n    return prediction_list","metadata":{"execution":{"iopub.status.busy":"2023-12-07T14:46:13.637230Z","iopub.execute_input":"2023-12-07T14:46:13.637566Z","iopub.status.idle":"2023-12-07T14:46:13.649489Z","shell.execute_reply.started":"2023-12-07T14:46:13.637518Z","shell.execute_reply":"2023-12-07T14:46:13.647954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = predict(model,test_loader)\np = []\nfor a in predictions:\n    p.append(a.item())\np = np.array(p)","metadata":{"execution":{"iopub.status.busy":"2023-12-07T14:46:13.651798Z","iopub.execute_input":"2023-12-07T14:46:13.652153Z","iopub.status.idle":"2023-12-07T14:46:16.709334Z","shell.execute_reply.started":"2023-12-07T14:46:13.652125Z","shell.execute_reply":"2023-12-07T14:46:16.708089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 8. Create file submission","metadata":{}},{"cell_type":"code","source":"test_data = pd.read_csv(\"/kaggle/input/Kannada-MNIST/test.csv\")\n\n\nsubmission = pd.DataFrame(data={\n    \"id\": test_data.index,\n    \"label\": p\n})\nsubmission.to_csv(\"submission.csv\", index=None)","metadata":{"execution":{"iopub.status.busy":"2023-12-07T14:46:16.710753Z","iopub.execute_input":"2023-12-07T14:46:16.711085Z","iopub.status.idle":"2023-12-07T14:46:17.054067Z","shell.execute_reply.started":"2023-12-07T14:46:16.711057Z","shell.execute_reply":"2023-12-07T14:46:17.052911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2023-12-07T14:46:17.055412Z","iopub.execute_input":"2023-12-07T14:46:17.056073Z","iopub.status.idle":"2023-12-07T14:46:17.077903Z","shell.execute_reply.started":"2023-12-07T14:46:17.056041Z","shell.execute_reply":"2023-12-07T14:46:17.077010Z"},"trusted":true},"execution_count":null,"outputs":[]}]}