{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","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":"gpu","dataSources":[{"sourceId":16017,"databundleVersionId":708189,"sourceType":"competition"}],"dockerImageVersionId":30787,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"### 1. Importing library","metadata":{}},{"cell_type":"code","source":"# import libraries\nimport 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":{"_uuid":"b10febf7b1e1774132e7eb5d976678cdde13f98c","execution":{"iopub.status.busy":"2024-12-02T08:41:19.002709Z","iopub.execute_input":"2024-12-02T08:41:19.003029Z","iopub.status.idle":"2024-12-02T08:41:23.389054Z","shell.execute_reply.started":"2024-12-02T08:41:19.003001Z","shell.execute_reply":"2024-12-02T08:41:23.388366Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### 2. Define Custom dataset class","metadata":{}},{"cell_type":"markdown","source":"Nguyên tắc: 1 custom dataset class bao gồm cũng có 3 methods:\n\n1. __getitem__ : trả về data (và/hoặc label) khi biết số index\n2. __len__ : trả về số mẫu trong dataset\n3. __init__: thực hiện các thao tác trong khi khởi tạo 1 object dataset","metadata":{}},{"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((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":{"_uuid":"584462e29167042d6ab91465abf29682a19ca7b9","execution":{"iopub.status.busy":"2024-12-02T08:41:23.398559Z","iopub.execute_input":"2024-12-02T08:41:23.398930Z","iopub.status.idle":"2024-12-02T08:41:23.422529Z","shell.execute_reply.started":"2024-12-02T08:41:23.398884Z","shell.execute_reply":"2024-12-02T08:41:23.421707Z"},"trusted":true},"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((28, 28, 1))\n    \n        \n        if self.transform is not None:\n            image = self.transform(image)\n            \n        return image","metadata":{"_uuid":"cc2462c5b61c2ce5e36ab979fbd06a67b2e76906","execution":{"iopub.status.busy":"2024-12-02T08:41:24.168696Z","iopub.execute_input":"2024-12-02T08:41:24.169285Z","iopub.status.idle":"2024-12-02T08:41:24.174795Z","shell.execute_reply.started":"2024-12-02T08:41:24.169227Z","shell.execute_reply":"2024-12-02T08:41:24.173898Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"---\n### 3. Load and Visualize the [Data](http://pytorch.org/docs/stable/torchvision/datasets.html)\n\nThử dùng 2 class dataset cho 2 tập test và train ở trên để visualize data","metadata":{"_uuid":"7645b3c96629001448adfd2ed86b10c7c849bc79"}},{"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":{"_uuid":"9fcbee0f621939a82823b2842e1cf969c6ae35dd","execution":{"iopub.status.busy":"2024-12-02T08:41:25.907763Z","iopub.execute_input":"2024-12-02T08:41:25.908427Z","iopub.status.idle":"2024-12-02T08:41:29.813510Z","shell.execute_reply.started":"2024-12-02T08:41:25.908394Z","shell.execute_reply":"2024-12-02T08:41:29.812709Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# number of subprocesses to use for data loading\nnum_workers = 0\n# how many samples per batch to load\nbatch_size = 40\n\n# convert data to torch.FloatTensor\n#transform = transforms.ToTensor()\n\n\n# prepare data loaders\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":{"_uuid":"09a9b606cbcc82b3d2e42f95788354c147f060f0","execution":{"iopub.status.busy":"2024-12-02T08:41:29.838018Z","iopub.execute_input":"2024-12-02T08:41:29.838304Z","iopub.status.idle":"2024-12-02T08:41:29.846907Z","shell.execute_reply.started":"2024-12-02T08:41:29.838267Z","shell.execute_reply":"2024-12-02T08:41:29.846081Z"},"trusted":true},"outputs":[],"execution_count":null},{"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) # dataiter.next()\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, 20//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":{"_uuid":"4221b1b8f71ef7edb36c286c10c10150e8da866f","execution":{"iopub.status.busy":"2024-12-02T08:41:30.747867Z","iopub.execute_input":"2024-12-02T08:41:30.748636Z","iopub.status.idle":"2024-12-02T08:41:31.302587Z","shell.execute_reply.started":"2024-12-02T08:41:30.748603Z","shell.execute_reply":"2024-12-02T08:41:31.301670Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"---\n### 4. Thiết kế 1 mạng NN đơn giản [Architecture](http://pytorch.org/docs/stable/nn.html)\n\nThiết kế 1 mạng neural network đơn giản cho việc phân loại","metadata":{"_uuid":"5a64b7de7835b6171b01542ac5282aa1fb5bdc67"}},{"cell_type":"code","source":"import torch.nn as nn\nimport torch.nn.functional as F\n\n# 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        hidden_3 = 64\n        # linear layer (784 -> hidden_1)\n        self.fc1 = nn.Linear(28 * 28, hidden_1)\n        # linear layer (n_hidden -> hidden_2)\n        self.fc2 = nn.Linear(hidden_1, hidden_2)\n        # linear layer (hidden_2 -> hidden_3)\n        self.fc3 = nn.Linear(hidden_2, hidden_3)\n        # linear layer (n_hidden -> 10)\n        self.fc4 = nn.Linear(hidden_3, 10)\n\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        # add hidden layer, with relu activation function\n        x = F.relu(self.fc2(x))\n        # add hidden layer, with relu activation function\n        x = F.relu(self.fc3(x))\n        # add output layer\n        x = self.fc4(x)\n        return x\n\n# initialize the NN\nmodel = Net()\nprint(model)","metadata":{"_uuid":"7b62834c92649b8c4578b2a382245ae1d1ac39af","execution":{"iopub.status.busy":"2024-12-02T08:19:33.227109Z","iopub.execute_input":"2024-12-02T08:19:33.227482Z","iopub.status.idle":"2024-12-02T08:19:33.240287Z","shell.execute_reply.started":"2024-12-02T08:19:33.227448Z","shell.execute_reply":"2024-12-02T08:19:33.239404Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"###  5. Chọn [Loss Function](http://pytorch.org/docs/stable/nn.html#loss-functions) và [Optimizer](http://pytorch.org/docs/stable/optim.html)\n","metadata":{"_uuid":"869671841b5922527a521fb98c81cf567265b6b4"}},{"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":{"_uuid":"3fc4df3f886fa7677d9523ddd4f8202646eba2ee","execution":{"iopub.status.busy":"2024-12-02T08:19:39.229889Z","iopub.execute_input":"2024-12-02T08:19:39.230696Z","iopub.status.idle":"2024-12-02T08:19:39.235023Z","shell.execute_reply.started":"2024-12-02T08:19:39.230664Z","shell.execute_reply":"2024-12-02T08:19:39.234078Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"---\n### 6. Train the Network","metadata":{"_uuid":"acd559ded95cc6ac73d1256efb18640ab12a282d"}},{"cell_type":"code","source":"# number of epochs to train the model\nn_epochs = 60\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        # 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   \n    train_loss = train_loss/len(train_loader.dataset)\n    \n    print('Epoch: {} \\tTraining Loss: {:.6f}'.format(\n        epoch+1, \n        train_loss,\n        ))\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(\n        train_loss_min,\n        train_loss))\n        torch.save(model.state_dict(), 'model.pt')\n        train_loss_min = train_loss","metadata":{"_uuid":"108ed27f4199c4ecb086dd1b32d9c2e9f795eb98","execution":{"iopub.status.busy":"2024-12-02T08:35:06.876888Z","iopub.execute_input":"2024-12-02T08:35:06.877230Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"###  Load the Model with the Lowest Validation Loss","metadata":{"_uuid":"5739526c3eceba40018641fa9138832a87815133"}},{"cell_type":"code","source":"model.load_state_dict(torch.load('model.pt'))","metadata":{"_uuid":"5df15b169108c21d69fe157611ed770e369a60e4","execution":{"iopub.status.busy":"2024-12-02T08:23:50.330356Z","iopub.execute_input":"2024-12-02T08:23:50.330926Z","iopub.status.idle":"2024-12-02T08:23:50.339251Z","shell.execute_reply.started":"2024-12-02T08:23:50.330884Z","shell.execute_reply":"2024-12-02T08:23:50.338339Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"---\n## Test the Trained Network\n\nFinally, we test our best model on previously unseen **test data** and evaluate it's performance. Testing on unseen data is a good way to check that our model generalizes well. It may also be useful to be granular in this analysis and take a look at how this model performs on each class as well as looking at its overall loss and accuracy.","metadata":{"_uuid":"7df21ab196469532fc5c2b1a0103c41b669a79a6"}},{"cell_type":"markdown","source":"### Visualize Sample Test Results\n\nThis cell displays test images and their labels in this format: `predicted (ground-truth)`. The text will be green for accurately classified examples and red for incorrect predictions.","metadata":{"_uuid":"d3b265b4f80ff8b14341ac02cafbcc9d733952ba"}},{"cell_type":"code","source":"# obtain one batch of test images\ndataiter = iter(test_loader)\nimages = next(dataiter) # dataiter.next()\n\n# get sample outputs\noutput = model(images)\n# convert output probabilities to predicted class\n_, preds = torch.max(output, 1)\n# prep images for display\nimages = images.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, 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":{"_uuid":"051970cff13e735d6ba16b1a16b1cf6b986ae137","trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### 7. Predict digit dựa vào input >> tạo file submission trong kaggle","metadata":{}},{"cell_type":"code","source":"test_loader = torch.utils.data.DataLoader(test_data, batch_size=1, \n    num_workers=num_workers)","metadata":{"_uuid":"6fca0360024d210720b333484050f86cc68a7001","execution":{"iopub.status.busy":"2024-12-02T08:23:51.092569Z","iopub.execute_input":"2024-12-02T08:23:51.092821Z","iopub.status.idle":"2024-12-02T08:23:51.102534Z","shell.execute_reply.started":"2024-12-02T08:23:51.092796Z","shell.execute_reply":"2024-12-02T08:23:51.101672Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\ndef 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":{"_uuid":"f8fdf66ab03e98f9b1b6295d76e0cd29004bd5c9","execution":{"iopub.status.busy":"2024-12-02T08:23:51.103752Z","iopub.execute_input":"2024-12-02T08:23:51.104382Z","iopub.status.idle":"2024-12-02T08:23:51.112810Z","shell.execute_reply.started":"2024-12-02T08:23:51.104343Z","shell.execute_reply":"2024-12-02T08:23:51.112012Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions = predict(model,test_loader)\npredictions = np.array(predictions)\npredictions = predictions.flatten()","metadata":{"_uuid":"1ee21bfa8bfd26d300557560318e8c8b602d9612","execution":{"iopub.status.busy":"2024-12-02T08:23:53.907219Z","iopub.execute_input":"2024-12-02T08:23:53.907644Z","iopub.status.idle":"2024-12-02T08:23:56.739300Z","shell.execute_reply.started":"2024-12-02T08:23:53.907601Z","shell.execute_reply":"2024-12-02T08:23:56.738560Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(type(predictions))  # Kiểm tra kiểu dữ liệu\nprint(predictions.shape)  # Kiểm tra kích thước (nếu là numpy array hoặc tensor)\nprint(len(test_data))     # Số hàng trong test_data\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T08:23:56.748334Z","iopub.execute_input":"2024-12-02T08:23:56.748647Z","iopub.status.idle":"2024-12-02T08:23:56.757540Z","shell.execute_reply.started":"2024-12-02T08:23:56.748609Z","shell.execute_reply":"2024-12-02T08:23:56.756700Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(predictions)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-02T08:23:56.771293Z","iopub.execute_input":"2024-12-02T08:23:56.771620Z","iopub.status.idle":"2024-12-02T08:23:56.780906Z","shell.execute_reply.started":"2024-12-02T08:23:56.771588Z","shell.execute_reply":"2024-12-02T08:23:56.779904Z"}},"outputs":[],"execution_count":null},{"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\": predictions\n})\nsubmission.to_csv(\"submission.csv\", index=None)","metadata":{"_uuid":"72d92030cd2804203698185ef5cf6d5984349bdb","execution":{"iopub.status.busy":"2024-12-02T08:23:56.992868Z","iopub.execute_input":"2024-12-02T08:23:56.993122Z","iopub.status.idle":"2024-12-02T08:23:57.188560Z","shell.execute_reply.started":"2024-12-02T08:23:56.993096Z","shell.execute_reply":"2024-12-02T08:23:57.187858Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#### Kết quả thực tế trên Kaggle: Accuracy ~ 97%","metadata":{}},{"cell_type":"code","source":"print(submission)","metadata":{"_uuid":"9a159e75d27c3a2a3316fd41d606ed8840aa6fbf","trusted":true,"execution":{"iopub.status.busy":"2024-12-02T08:23:57.189715Z","iopub.execute_input":"2024-12-02T08:23:57.190065Z","iopub.status.idle":"2024-12-02T08:23:57.196739Z","shell.execute_reply.started":"2024-12-02T08:23:57.190029Z","shell.execute_reply":"2024-12-02T08:23:57.195880Z"}},"outputs":[],"execution_count":null}]}