{"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":{}},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T13:46:56.386239Z","iopub.execute_input":"2024-12-01T13:46:56.387403Z","iopub.status.idle":"2024-12-01T13:47:01.665639Z","shell.execute_reply.started":"2024-12-01T13:46:56.387348Z","shell.execute_reply":"2024-12-01T13:47:01.664763Z"}},"outputs":[],"execution_count":null},{"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        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":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T13:47:20.059710Z","iopub.execute_input":"2024-12-01T13:47:20.060792Z","iopub.status.idle":"2024-12-01T13:47:20.067273Z","shell.execute_reply.started":"2024-12-01T13:47:20.060746Z","shell.execute_reply":"2024-12-01T13:47:20.065998Z"}},"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\n    def __len__(self):\n        return len(self.data)\n\n    def __getitem__(self, index):\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":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T13:47:25.781348Z","iopub.execute_input":"2024-12-01T13:47:25.782352Z","iopub.status.idle":"2024-12-01T13:47:25.788263Z","shell.execute_reply.started":"2024-12-01T13:47:25.782312Z","shell.execute_reply":"2024-12-01T13:47:25.787199Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T13:48:45.434802Z","iopub.execute_input":"2024-12-01T13:48:45.435329Z","iopub.status.idle":"2024-12-01T13:48:50.372370Z","shell.execute_reply.started":"2024-12-01T13:48:45.435290Z","shell.execute_reply":"2024-12-01T13:48:50.371489Z"}},"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 = 20\n\n# prepare data loaders\ntrain_loader = torch.utils.data.DataLoader(train_data, batch_size=batch_size, num_workers=num_workers,)\ntest_loader = torch.utils.data.DataLoader(test_data, batch_size=batch_size, num_workers=num_workers)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T13:49:29.003527Z","iopub.execute_input":"2024-12-01T13:49:29.004281Z","iopub.status.idle":"2024-12-01T13:49:29.009569Z","shell.execute_reply.started":"2024-12-01T13:49:29.004243Z","shell.execute_reply":"2024-12-01T13:49:29.008524Z"}},"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)\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-12-01T13:50:35.396938Z","iopub.execute_input":"2024-12-01T13:50:35.398051Z","iopub.status.idle":"2024-12-01T13:50:36.454424Z","shell.execute_reply.started":"2024-12-01T13:50:35.397993Z","shell.execute_reply":"2024-12-01T13:50:36.453484Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"4. Thiết kế 1 mạng NN đơn giản","metadata":{}},{"cell_type":"code","source":"import torch.nn as nn \nimport torch.nn.functional as F","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T13:50:48.728543Z","iopub.execute_input":"2024-12-01T13:50:48.728954Z","iopub.status.idle":"2024-12-01T13:50:48.734171Z","shell.execute_reply.started":"2024-12-01T13:50:48.728918Z","shell.execute_reply":"2024-12-01T13:50:48.732988Z"}},"outputs":[],"execution_count":null},{"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 (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    def forward(self, x):\n        # flatten image input\n        x = x.view(-1, 28*28)\n        # add hidden layer 1, with relu activation function\n        x = F.relu(self.fc1(x))\n        # add hidden layer 2, with relu activation function\n        x = F.relu(self.fc2(x))\n        # add output layer \n        x = self.fc3(x)\n        return x ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T13:51:29.883251Z","iopub.execute_input":"2024-12-01T13:51:29.884170Z","iopub.status.idle":"2024-12-01T13:51:29.890534Z","shell.execute_reply.started":"2024-12-01T13:51:29.884128Z","shell.execute_reply":"2024-12-01T13:51:29.889488Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# initialize the NN \nmodel = Net()\nprint(model)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T13:51:32.189004Z","iopub.execute_input":"2024-12-01T13:51:32.189658Z","iopub.status.idle":"2024-12-01T13:51:32.203458Z","shell.execute_reply.started":"2024-12-01T13:51:32.189619Z","shell.execute_reply":"2024-12-01T13:51:32.202340Z"}},"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-12-01T13:51:44.451053Z","iopub.execute_input":"2024-12-01T13:51:44.451548Z","iopub.status.idle":"2024-12-01T13:51:44.457651Z","shell.execute_reply.started":"2024-12-01T13:51:44.451498Z","shell.execute_reply":"2024-12-01T13:51:44.456488Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"6. Train the network","metadata":{}},{"cell_type":"code","source":"# number of epochs to train model \nn_epochs = 20\n# initialize tracker for minimum validation loss\ntrain_loss_min = np.Inf","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T13:51:56.793379Z","iopub.execute_input":"2024-12-01T13:51:56.794124Z","iopub.status.idle":"2024-12-01T13:51:56.798686Z","shell.execute_reply.started":"2024-12-01T13:51:56.794083Z","shell.execute_reply":"2024-12-01T13:51:56.797493Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for epoch in range(n_epochs):\n    # monitor training loss\n    train_loss = 0.0\n\n    # train the model \n    model.train()\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    train_loss = train_loss/len(train_loader.dataset)\n\n    print(f'Epoch: {epoch+1} \\tTraining Loss: {train_loss:.6f}')\n\n    if train_loss <= train_loss_min:\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\n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T13:53:21.001353Z","iopub.execute_input":"2024-12-01T13:53:21.001780Z","iopub.status.idle":"2024-12-01T13:59:04.046502Z","shell.execute_reply.started":"2024-12-01T13:53:21.001745Z","shell.execute_reply":"2024-12-01T13:59:04.045528Z"}},"outputs":[],"execution_count":null},{"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', weights_only=True))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T13:59:36.135340Z","iopub.execute_input":"2024-12-01T13:59:36.135737Z","iopub.status.idle":"2024-12-01T13:59:36.146787Z","shell.execute_reply.started":"2024-12-01T13:59:36.135698Z","shell.execute_reply":"2024-12-01T13:59:36.145817Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# obtain one batch of test images\ndataiter = iter(test_loader)\nimages = next(dataiter)  # Trả về một batch ảnh\n\n# get sample outputs\noutput = model(images)\n# convert output probabilities to predicted class\n_, preds = torch.max(output, 1)\n\n# prep images for display\nimages = images.numpy()\n\n# plot the images in the batch, along with predicted labels\nfig = plt.figure(figsize=(25, 4))\nfor idx in np.arange(20):\n    ax = fig.add_subplot(2, int(batch_size/2), idx+1, xticks=[], yticks=[])\n    ax.imshow(np.squeeze(images[idx]), cmap='gray')\n    ax.set_title(\"{}\".format(str(preds[idx].item())))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T13:59:44.323029Z","iopub.execute_input":"2024-12-01T13:59:44.323969Z","iopub.status.idle":"2024-12-01T13:59:45.511541Z","shell.execute_reply.started":"2024-12-01T13:59:44.323926Z","shell.execute_reply":"2024-12-01T13:59:45.510323Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"7. Predict","metadata":{}},{"cell_type":"code","source":"test_loader = torch.utils.data.DataLoader(test_data, batch_size=1, num_workers=num_workers)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T14:00:03.652106Z","iopub.execute_input":"2024-12-01T14:00:03.653046Z","iopub.status.idle":"2024-12-01T14:00:03.657572Z","shell.execute_reply.started":"2024-12-01T14:00:03.653005Z","shell.execute_reply":"2024-12-01T14:00:03.656482Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def predict(mode, 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":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T14:00:05.887198Z","iopub.execute_input":"2024-12-01T14:00:05.887695Z","iopub.status.idle":"2024-12-01T14:00:05.893159Z","shell.execute_reply.started":"2024-12-01T14:00:05.887655Z","shell.execute_reply":"2024-12-01T14:00:05.892220Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions = predict(model, test_loader)\npredictions = np.array(predictions).flatten()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-01T14:00:21.942351Z","iopub.execute_input":"2024-12-01T14:00:21.943636Z","iopub.status.idle":"2024-12-01T14:00:24.610927Z","shell.execute_reply.started":"2024-12-01T14:00:21.943577Z","shell.execute_reply":"2024-12-01T14:00:24.609448Z"}},"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-12-01T14:00:27.341456Z","iopub.execute_input":"2024-12-01T14:00:27.342200Z","iopub.status.idle":"2024-12-01T14:00:27.602143Z","shell.execute_reply.started":"2024-12-01T14:00:27.342161Z","shell.execute_reply":"2024-12-01T14:00:27.601303Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}