{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","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":30786,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"### 1. Importing library","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline\nimport numpy as np\nimport pandas as pd\n\nimport 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\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T03:21:39.041645Z","iopub.execute_input":"2024-11-30T03:21:39.042127Z","iopub.status.idle":"2024-11-30T03:21:44.250537Z","shell.execute_reply.started":"2024-11-30T03:21:39.042092Z","shell.execute_reply":"2024-11-30T03:21:44.249498Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### 2. Define Custom dataset class\r\n","metadata":{}},{"cell_type":"code","source":"# image size\nweight = 28\nheight = 28\n\n# number of subprocesses to use for data loading\nnum_workers = 0\n# how many samples per batch to load\nbatch_size = 20","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T03:21:44.252140Z","iopub.execute_input":"2024-11-30T03:21:44.252631Z","iopub.status.idle":"2024-11-30T03:21:44.257748Z","shell.execute_reply.started":"2024-11-30T03:21:44.252593Z","shell.execute_reply":"2024-11-30T03:21:44.256149Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class TrainMNIST(Dataset):\n    \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        # be carefull for converting dtype to np.uint8 [Unsigned integer (0 to 255)]\n        # in this example, we use ToTensor(), so we define the numpy array like (H, W, C)\n        image = self.data.iloc[index, 1:].values.astype(np.uint8).reshape((height, weight, 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":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T03:21:44.259058Z","iopub.execute_input":"2024-11-30T03:21:44.259362Z","iopub.status.idle":"2024-11-30T03:21:44.284269Z","shell.execute_reply.started":"2024-11-30T03:21:44.259332Z","shell.execute_reply":"2024-11-30T03:21:44.283035Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class TestMNIST(Dataset):\n    \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        # be carefull for converting dtype to np.uint8 [Unsigned integer (0 to 255)]\n        # in this example, we use ToTensor(), so we define the numpy array like (H, W, C)\n        image = self.data.iloc[index, 1:].values.astype(np.uint8).reshape((height, weight, 1))         \n        \n        if self.transform is not None:\n            image = self.transform(image)\n            \n        return image","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T03:21:44.287251Z","iopub.execute_input":"2024-11-30T03:21:44.287765Z","iopub.status.idle":"2024-11-30T03:21:44.298261Z","shell.execute_reply.started":"2024-11-30T03:21:44.287707Z","shell.execute_reply":"2024-11-30T03:21:44.297120Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### 3. Load and Visualize the Data","metadata":{"execution":{"iopub.status.busy":"2024-11-29T13:03:09.902915Z","iopub.execute_input":"2024-11-29T13:03:09.903301Z","iopub.status.idle":"2024-11-29T13:03:09.908846Z","shell.execute_reply.started":"2024-11-29T13:03:09.903268Z","shell.execute_reply":"2024-11-29T13:03:09.907515Z"}}},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T03:21:44.299623Z","iopub.execute_input":"2024-11-30T03:21:44.299984Z","iopub.status.idle":"2024-11-30T03:21:49.285669Z","shell.execute_reply.started":"2024-11-30T03:21:44.299944Z","shell.execute_reply":"2024-11-30T03:21:49.284578Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# prepare data loaders\ntrain_loader = torch.utils.data.DataLoader(train_data, batch_size=batch_size, num_workers=num_workers)\n\ntest_loader = torch.utils.data.DataLoader(test_data, batch_size=batch_size, num_workers=num_workers)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T03:21:49.287260Z","iopub.execute_input":"2024-11-30T03:21:49.287746Z","iopub.status.idle":"2024-11-30T03:21:49.294086Z","shell.execute_reply.started":"2024-11-30T03:21:49.287667Z","shell.execute_reply":"2024-11-30T03:21:49.292777Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 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(batch_size):\n    ax = fig.add_subplot(2, int(batch_size/2), idx+1, xticks=[], yticks=[])\n    ax.imshow(np.squeeze(images[idx]), cmap='gray')\n    # print out the correct label for each image\n    # .item() gets the value contained in a Tensor\n    ax.set_title(str(labels[idx].item()))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T03:21:49.295728Z","iopub.execute_input":"2024-11-30T03:21:49.296163Z","iopub.status.idle":"2024-11-30T03:21:50.375775Z","shell.execute_reply.started":"2024-11-30T03:21:49.296116Z","shell.execute_reply":"2024-11-30T03:21:50.374659Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### 4. Thiết kế 1 mạng NN đơn giản","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        # 28*28 tương ứng với số thuộc tính của 1 mẫu, do hình là 28*28 pixel\n        self.fc1 = nn.Linear(height * weight, hidden_1)\n        # linear layer (n_hidden -> hidden_2)\n        self.fc2 = nn.Linear(hidden_1, hidden_2)\n        # linear layer (n_hidden -> 10)\n        self.fc3 = nn.Linear(hidden_2, 10)\n\n\n    def forward(self, x):\n        # flatten image input\n        x = x.view(-1, height * weight)\n        # add hidden layer, with relu activation function\n        x = F.relu(self.fc1(x))\n        # add hidden layer, with relu activation function\n        x = F.relu(self.fc2(x))\n        # add output layer\n        x = self.fc3(x)\n        return x\n\n# initialize the NN\nmodel = Net()\nprint(model)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T03:21:50.377042Z","iopub.execute_input":"2024-11-30T03:21:50.377345Z","iopub.status.idle":"2024-11-30T03:21:50.394370Z","shell.execute_reply.started":"2024-11-30T03:21:50.377314Z","shell.execute_reply":"2024-11-30T03:21:50.393183Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"###  5. Chọn Loss Function và Optimizer","metadata":{}},{"cell_type":"code","source":"# specify loss function (categorical cross-entropy)\ncriterion = nn.CrossEntropyLoss()\n\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":"2024-11-30T03:21:50.395814Z","iopub.execute_input":"2024-11-30T03:21:50.396205Z","iopub.status.idle":"2024-11-30T03:21:50.404188Z","shell.execute_reply.started":"2024-11-30T03:21:50.396168Z","shell.execute_reply":"2024-11-30T03:21:50.403045Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### 6. Train the Network","metadata":{}},{"cell_type":"code","source":"# number of epochs to train the model\nn_epochs = 20\n\n# initialize tracker for minimum validation loss\ntrain_loss_min = np.Inf # set initial \"min\" to infinity\n\nfor epoch in range(n_epochs):\n    # monitor training loss\n    train_loss = 0.0\n    \n    ###################\n    # train the model #\n    ###################\n    model.train() # prep model for training\n    for data, target in train_loader:\n        # clear the gradients of all optimized variables        \n        optimizer.zero_grad()\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 of the loss with respect to model parameters                \n        loss.backward()\n        \n        # perform a single optimization step (parameter update)                \n        optimizer.step()\n        # update running training loss\n        train_loss += loss.item()*data.size(0)\n           \n    train_loss = train_loss/len(train_loader.dataset)    \n    #print('Epoch: {} \\tTraining Loss: {:.6f}'.format(epoch+1, train_loss, ))\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('Training loss decreased ({:.6f} --> {:.6f}).  Saving model ...'.format(train_loss_min, train_loss)) \n        print(f\"Training loss decreased ({train_loss_min:.6f} --> {train_loss:.6f}). Saving model ...\") \n        torch.save(model.state_dict(), \"model.pt\")\n        train_loss_min = train_loss","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T03:21:50.407736Z","iopub.execute_input":"2024-11-30T03:21:50.408210Z","iopub.status.idle":"2024-11-30T03:27:56.562114Z","shell.execute_reply.started":"2024-11-30T03:21:50.408173Z","shell.execute_reply":"2024-11-30T03:27:56.560928Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### 7. Load the Model with the Lowest Validation Loss","metadata":{}},{"cell_type":"code","source":"model.load_state_dict(torch.load(\"/kaggle/working/model.pt\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T03:27:56.563511Z","iopub.execute_input":"2024-11-30T03:27:56.563914Z","iopub.status.idle":"2024-11-30T03:27:56.575393Z","shell.execute_reply.started":"2024-11-30T03:27:56.563873Z","shell.execute_reply":"2024-11-30T03:27:56.574196Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def predict(model, dataloader):\n    prediction_list = []\n    \n    for img_batch in dataloader:\n        # img_batch 20 items\n        output = model(img_batch)\n        _, predicted = torch.max(output.data, 1)\n        prediction_list.append(predicted.cpu().tolist())\n    \n    return prediction_list","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T03:29:28.739544Z","iopub.execute_input":"2024-11-30T03:29:28.740012Z","iopub.status.idle":"2024-11-30T03:29:28.746287Z","shell.execute_reply.started":"2024-11-30T03:29:28.739973Z","shell.execute_reply":"2024-11-30T03:29:28.744963Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions = predict(model, test_loader)\npredictions = np.concatenate(predictions, axis=0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T03:29:30.266456Z","iopub.execute_input":"2024-11-30T03:29:30.266872Z","iopub.status.idle":"2024-11-30T03:29:31.235305Z","shell.execute_reply.started":"2024-11-30T03:29:30.266837Z","shell.execute_reply":"2024-11-30T03:29:31.234096Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data = pd.read_csv(\"/kaggle/input/Kannada-MNIST/test.csv\")\n\nsubmission = pd.DataFrame(data={\n    \"id\": test_data.index,\n    \"label\": predictions\n})\nsubmission.to_csv(\"submission.csv\", index=None)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T03:29:32.860527Z","iopub.execute_input":"2024-11-30T03:29:32.860932Z","iopub.status.idle":"2024-11-30T03:29:32.874873Z","shell.execute_reply.started":"2024-11-30T03:29:32.860896Z","shell.execute_reply":"2024-11-30T03:29:32.873564Z"}},"outputs":[],"execution_count":null}]}