{"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":"gpu","dataSources":[{"sourceId":16017,"databundleVersionId":708189,"sourceType":"competition"}],"dockerImageVersionId":30919,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Libraries","metadata":{}},{"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 torch.nn as nn\nimport torch.nn.functional as F\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-12T01:11:30.184710Z","iopub.execute_input":"2025-03-12T01:11:30.184968Z","iopub.status.idle":"2025-03-12T01:11:36.187179Z","shell.execute_reply.started":"2025-03-12T01:11:30.184945Z","shell.execute_reply":"2025-03-12T01:11:36.186293Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Define DataClass, DataLoader","metadata":{}},{"cell_type":"code","source":"# DataClass\nclass TrainMNIST(Dataset):\n    def __init__(self, file_path, transform=None):\n        self.data = pd.read_csv(file_path)\n        self.transform = transform\n    def __len__(self):\n        return len(self.data)\n    def __getitem__(self, index):\n        image = self.data.iloc[index, 1:].values.astype(np.uint8).reshape((28,28,1))\n        label = self.data.iloc[index, 0]\n        if self.transform is not None:\n            image = self.transform(image)\n        return image, label","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-12T01:11:36.188118Z","iopub.execute_input":"2025-03-12T01:11:36.188584Z","iopub.status.idle":"2025-03-12T01:11:36.193898Z","shell.execute_reply.started":"2025-03-12T01:11:36.188552Z","shell.execute_reply":"2025-03-12T01:11:36.193016Z"}},"outputs":[],"execution_count":null},{"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    def __len__(self):\n        return len(self.data)\n    def __getitem__(self, index):\n        image = self.data.iloc[index, 1:].values.astype(np.uint8).reshape((28,28,1))\n        # label = self.data.iloc[index, 0]\n        if self.transform is not None:\n            image = self.transform(image)\n        return image","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-12T01:11:36.194692Z","iopub.execute_input":"2025-03-12T01:11:36.194933Z","iopub.status.idle":"2025-03-12T01:11:36.215272Z","shell.execute_reply.started":"2025-03-12T01:11:36.194913Z","shell.execute_reply":"2025-03-12T01:11:36.214620Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load the data\ntrain_data = TrainMNIST(\"/kaggle/input/Kannada-MNIST/train.csv\", transform=transforms.ToTensor())\ntest_data = TestMNIST(\"/kaggle/input/Kannada-MNIST/test.csv\", transform=transforms.ToTensor())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-12T01:11:36.217186Z","iopub.execute_input":"2025-03-12T01:11:36.217404Z","iopub.status.idle":"2025-03-12T01:11:41.238397Z","shell.execute_reply.started":"2025-03-12T01:11:36.217373Z","shell.execute_reply":"2025-03-12T01:11:41.237491Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# DataLoader\nnum_workers = 0\nbatch_size = 20\ntrain_loader = DataLoader(train_data, batch_size=batch_size, num_workers=num_workers)\ntest_loader = DataLoader(test_data, batch_size=batch_size, num_workers=num_workers)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-12T01:11:41.241883Z","iopub.execute_input":"2025-03-12T01:11:41.242097Z","iopub.status.idle":"2025-03-12T01:11:41.246177Z","shell.execute_reply.started":"2025-03-12T01:11:41.242077Z","shell.execute_reply":"2025-03-12T01:11:41.245353Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Visualize the data","metadata":{}},{"cell_type":"code","source":"# obtain 1 batch of training images\ndataiter = iter(train_loader)\nimages, labels = next(dataiter) # take 1 batch\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":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-12T01:11:41.246971Z","iopub.execute_input":"2025-03-12T01:11:41.247192Z","iopub.status.idle":"2025-03-12T01:11:42.278399Z","shell.execute_reply.started":"2025-03-12T01:11:41.247169Z","shell.execute_reply":"2025-03-12T01:11:42.277548Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Model Architecture","metadata":{}},{"cell_type":"code","source":"# Define the NN architecture\nclass Net(nn.Module):\n    def __init__(self):\n        super(Net, self).__init__()\n        # number of hidden nodes in each layer(512)\n        hidden_1 = 512\n        hidden_2 = 512\n        # linear layer (784-->hidden_1)\n        self.fc1 = nn.Linear(28*28, hidden_1)\n        # linear layer (hidden_1-->hidden_2)\n        self.fc2 = nn.Linear(hidden_1, hidden_2)\n        # linear layer (hidden_2-->10)\n        self.fc3 = nn.Linear(hidden_2, 10)\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\n# initialize the NN\ndevice = torch.device(\"cuda\")\nmodel = Net().to(device)\nprint(model)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-12T01:11:42.279193Z","iopub.execute_input":"2025-03-12T01:11:42.279511Z","iopub.status.idle":"2025-03-12T01:11:42.510046Z","shell.execute_reply.started":"2025-03-12T01:11:42.279481Z","shell.execute_reply":"2025-03-12T01:11:42.509175Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# specify loss function(categorical cross-entropy)\ncriterion = nn.CrossEntropyLoss()\n# specify optimizer (stochastic gradient descent) and learning rate=0.01\noptimizer = torch.optim.SGD(model.parameters(), lr=0.01)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-12T01:11:42.510862Z","iopub.execute_input":"2025-03-12T01:11:42.511173Z","iopub.status.idle":"2025-03-12T01:11:42.515312Z","shell.execute_reply.started":"2025-03-12T01:11:42.511143Z","shell.execute_reply":"2025-03-12T01:11:42.514484Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Train the Network","metadata":{}},{"cell_type":"code","source":"from tqdm import tqdm\n# number of epochs to train the model\nn_epochs = 20\n# initialize tracker for minimum validation loss\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 tqdm(train_loader):\n        optimizer.zero_grad()\n        # move to gpu\n        data = data.to(device)\n        target = target.to(device)\n        # forward pass: compute predicted outputs by passing inputs to the model\n        output = model(data)\n        # calculate the loss\n        loss = criterion(output, target)\n        # backward pass: compute gradient descent of the loss with respect to model parameter\n        loss.backward()\n        # perform a single optimization step\n        optimizer.step()\n        # update running training loss\n        train_loss += loss.item() * data.size(0)\n    train_loss = train_loss / len(train_loader.dataset)\n    print(f\"Epoch: {epoch+1} \\tTraining Loss:{train_loss:.6f}\")\n\n    # save model if validation loss has decreased\n    if train_loss <= train_loss_min:\n        print(f\"Training loss decreased ({train_loss_min}-->{train_loss}). Saving model...\")\n        torch.save(model.state_dict(), \"model.pt\")\n        train_loss_min = train_loss","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-12T01:11:42.516075Z","iopub.execute_input":"2025-03-12T01:11:42.516299Z","iopub.status.idle":"2025-03-12T01:16:00.265042Z","shell.execute_reply.started":"2025-03-12T01:11:42.516268Z","shell.execute_reply":"2025-03-12T01:16:00.264121Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.load_state_dict(torch.load(\"model.pt\", weights_only=True))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-12T01:16:00.265956Z","iopub.execute_input":"2025-03-12T01:16:00.266210Z","iopub.status.idle":"2025-03-12T01:16:00.278100Z","shell.execute_reply.started":"2025-03-12T01:16:00.266189Z","shell.execute_reply":"2025-03-12T01:16:00.277436Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Test the Trained Network","metadata":{}},{"cell_type":"code","source":"# Visualize test results\n# Obtain 1 batch of test images\ndataiter = iter(test_loader)\nimages = next(dataiter)\n\n# Move to GPU\nimages = images.to(device)\noutputs = model(images)\n_, preds = torch.max(outputs, 1)\n# Move back to CPU for visualization\nimages = images.cpu().numpy()\n\n# plot the images in the batch along with predicted and true 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(f\"{str(preds[idx].item())}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-12T01:20:55.570513Z","iopub.execute_input":"2025-03-12T01:20:55.570845Z","iopub.status.idle":"2025-03-12T01:20:56.377296Z","shell.execute_reply.started":"2025-03-12T01:20:55.570810Z","shell.execute_reply":"2025-03-12T01:20:56.376443Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def predict(model, dataloader):\n    prediction_list = []\n    model.eval()  # Set the model to evaluation mode\n    with torch.no_grad():  # Disable gradient calculation\n        for data in dataloader:\n            data = data.to(device)\n            outputs = model(data)\n            _, predicted = torch.max(outputs, 1)\n            prediction_list.extend(predicted.cpu().numpy())  # Append predictions as a list\n    return np.array(prediction_list)  # Convert the list to a 1D NumPy array","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-12T01:28:01.116316Z","iopub.execute_input":"2025-03-12T01:28:01.116632Z","iopub.status.idle":"2025-03-12T01:28:01.121063Z","shell.execute_reply.started":"2025-03-12T01:28:01.116609Z","shell.execute_reply":"2025-03-12T01:28:01.120344Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions = predict(model,test_loader)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-12T01:28:15.223185Z","iopub.execute_input":"2025-03-12T01:28:15.223518Z","iopub.status.idle":"2025-03-12T01:28:15.908987Z","shell.execute_reply.started":"2025-03-12T01:28:15.223489Z","shell.execute_reply":"2025-03-12T01:28:15.908333Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data = pd.read_csv(\"/kaggle/input/Kannada-MNIST/test.csv\")\nsubmission = pd.DataFrame(data={\n    \"id\": test_data.index + 1,\n    \"label\": predictions\n})\nsubmission.to_csv(\"submission.csv\", index=None)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-12T01:28:23.935129Z","iopub.execute_input":"2025-03-12T01:28:23.935457Z","iopub.status.idle":"2025-03-12T01:28:24.136320Z","shell.execute_reply.started":"2025-03-12T01:28:23.935429Z","shell.execute_reply":"2025-03-12T01:28:24.135611Z"}},"outputs":[],"execution_count":null}]}