{"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":30635,"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 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":{"execution":{"iopub.status.busy":"2024-01-12T09:22:18.464933Z","iopub.execute_input":"2024-01-12T09:22:18.465588Z","iopub.status.idle":"2024-01-12T09:22:20.786440Z","shell.execute_reply.started":"2024-01-12T09:22:18.465555Z","shell.execute_reply":"2024-01-12T09:22:20.785505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 2. Define Custom dataset class","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 # tức là ở đây mặc định transform = None nhưng transform dạng khác vẫn xử lí được\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)) # lấy ảnh\n        label = self.data.iloc[index, 0] # lấy nhãn\n        \n        if self.transform is not None:\n            image = self.transform(image) # có transform khác None mới transform\n            \n        return image, label","metadata":{"execution":{"iopub.status.busy":"2024-01-12T09:22:20.787887Z","iopub.execute_input":"2024-01-12T09:22:20.789371Z","iopub.status.idle":"2024-01-12T09:22:20.799297Z","shell.execute_reply.started":"2024-01-12T09:22:20.789324Z","shell.execute_reply":"2024-01-12T09:22:20.798218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2024-01-12T09:22:20.800936Z","iopub.execute_input":"2024-01-12T09:22:20.801895Z","iopub.status.idle":"2024-01-12T09:22:20.810907Z","shell.execute_reply.started":"2024-01-12T09:22:20.801854Z","shell.execute_reply":"2024-01-12T09:22:20.810086Z"},"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":"2024-01-12T09:22:20.814848Z","iopub.execute_input":"2024-01-12T09:22:20.815324Z","iopub.status.idle":"2024-01-12T09:22:26.075742Z","shell.execute_reply.started":"2024-01-12T09:22:20.815282Z","shell.execute_reply":"2024-01-12T09:22:26.074399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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 = 64\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":{"execution":{"iopub.status.busy":"2024-01-12T09:22:26.077439Z","iopub.execute_input":"2024-01-12T09:22:26.077812Z","iopub.status.idle":"2024-01-12T09:22:26.083583Z","shell.execute_reply.started":"2024-01-12T09:22:26.077782Z","shell.execute_reply":"2024-01-12T09:22:26.082534Z"},"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) # con trỏ (lúc đầu là lấy dòng space)\nimages, labels = next(dataiter) # vị trí tiếp theo của con trỏ\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, 10, idx+1, xticks=[], yticks=[])\n    ax.imshow(np.squeeze(images[idx]), cmap='gray') # show hình ảnh\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":{"execution":{"iopub.status.busy":"2024-01-12T09:22:26.085020Z","iopub.execute_input":"2024-01-12T09:22:26.085357Z","iopub.status.idle":"2024-01-12T09:22:27.526001Z","shell.execute_reply.started":"2024-01-12T09:22:26.085328Z","shell.execute_reply":"2024-01-12T09:22:27.525166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 4. Thiết kế 1 mạng NN đơn giản Architecture","metadata":{}},{"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        # linear layer (784 -> hidden_1)\n        \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) #nn.linear(a, b) (a: số lượng neuron lớp trước đó, b: lớp hiện tại)\n        # lớp thứ tự: input - hidden 1 - ... - hidden n - output (số neuron phải = input)\n\n    def forward(self, x):\n        # flatten image input\n        x = x.view(-1, 28 * 28)\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\n# device = torch.device(\"cuda\")\n\n#model = Net().to(device)\nmodel = Net()\nprint(model)","metadata":{"execution":{"iopub.status.busy":"2024-01-12T09:22:27.527240Z","iopub.execute_input":"2024-01-12T09:22:27.528164Z","iopub.status.idle":"2024-01-12T09:22:27.549101Z","shell.execute_reply.started":"2024-01-12T09:22:27.528116Z","shell.execute_reply":"2024-01-12T09:22:27.547956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model(torch.zeros((20,28,28,1)).to(device))\nmodel(torch.zeros((20,28,28,1)))","metadata":{"execution":{"iopub.status.busy":"2024-01-12T09:22:27.550853Z","iopub.execute_input":"2024-01-12T09:22:27.551328Z","iopub.status.idle":"2024-01-12T09:22:27.563558Z","shell.execute_reply.started":"2024-01-12T09:22:27.551295Z","shell.execute_reply":"2024-01-12T09:22:27.562519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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() # do số lượng neuron output >= 2\n\n# specify optimizer (stochastic gradient descent) and learning rate = 0.01\noptimizer = torch.optim.Adam(model.parameters(), lr=0.001)","metadata":{"execution":{"iopub.status.busy":"2024-01-12T09:22:27.565254Z","iopub.execute_input":"2024-01-12T09:22:27.565587Z","iopub.status.idle":"2024-01-12T09:22:27.571755Z","shell.execute_reply.started":"2024-01-12T09:22:27.565556Z","shell.execute_reply":"2024-01-12T09:22:27.570429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 6. Train the network","metadata":{}},{"cell_type":"code","source":"from tqdm import tqdm\n# number of epochs to train the model\nn_epochs = 16\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 tqdm(train_loader):\n        # clear the gradients of all optimized variables\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 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":{"execution":{"iopub.status.busy":"2024-01-12T09:22:27.573055Z","iopub.execute_input":"2024-01-12T09:22:27.573442Z","iopub.status.idle":"2024-01-12T09:28:35.569099Z","shell.execute_reply.started":"2024-01-12T09:22:27.573412Z","shell.execute_reply":"2024-01-12T09:28:35.567895Z"},"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":"2024-01-12T09:28:35.570709Z","iopub.execute_input":"2024-01-12T09:28:35.571386Z","iopub.status.idle":"2024-01-12T09:28:35.583790Z","shell.execute_reply.started":"2024-01-12T09:28:35.571332Z","shell.execute_reply":"2024-01-12T09:28:35.582625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n## Test the Trained Network","metadata":{}},{"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":{"execution":{"iopub.status.busy":"2024-01-12T09:28:35.585397Z","iopub.execute_input":"2024-01-12T09:28:35.585751Z","iopub.status.idle":"2024-01-12T09:28:35.592075Z","shell.execute_reply.started":"2024-01-12T09:28:35.585719Z","shell.execute_reply":"2024-01-12T09:28:35.590711Z"},"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        data = batch\n        outputs = model(data)\n        _, predicted = torch.max(outputs.data, 1) \n        prediction_list.append(predicted.cpu())\n    return prediction_list","metadata":{"execution":{"iopub.status.busy":"2024-01-12T09:28:35.597269Z","iopub.execute_input":"2024-01-12T09:28:35.597809Z","iopub.status.idle":"2024-01-12T09:28:35.603943Z","shell.execute_reply.started":"2024-01-12T09:28:35.597734Z","shell.execute_reply":"2024-01-12T09:28:35.603200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = predict(model,test_loader)\n#predictions = np.array(predictions)","metadata":{"execution":{"iopub.status.busy":"2024-01-12T09:33:44.737669Z","iopub.execute_input":"2024-01-12T09:33:44.738195Z","iopub.status.idle":"2024-01-12T09:33:47.921409Z","shell.execute_reply.started":"2024-01-12T09:33:44.738131Z","shell.execute_reply":"2024-01-12T09:33:47.920332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(predictions)","metadata":{"execution":{"iopub.status.busy":"2024-01-12T09:37:10.778674Z","iopub.execute_input":"2024-01-12T09:37:10.779134Z","iopub.status.idle":"2024-01-12T09:37:10.786939Z","shell.execute_reply.started":"2024-01-12T09:37:10.779096Z","shell.execute_reply":"2024-01-12T09:37:10.785828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = np.array(predictions, dtype = \"object\") # gặp lỗi inhomogenous after convert to 1d array --> chuyển sang object rồi tính gì thì tính =))","metadata":{"execution":{"iopub.status.busy":"2024-01-12T09:43:15.944963Z","iopub.execute_input":"2024-01-12T09:43:15.945903Z","iopub.status.idle":"2024-01-12T09:43:15.950758Z","shell.execute_reply.started":"2024-01-12T09:43:15.945861Z","shell.execute_reply":"2024-01-12T09:43:15.949820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = np.array(predictions, dtype = \"int\")","metadata":{"execution":{"iopub.status.busy":"2024-01-12T09:46:22.895274Z","iopub.execute_input":"2024-01-12T09:46:22.895674Z","iopub.status.idle":"2024-01-12T09:46:22.901461Z","shell.execute_reply.started":"2024-01-12T09:46:22.895642Z","shell.execute_reply":"2024-01-12T09:46:22.899777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2024-01-12T09:46:30.056267Z","iopub.execute_input":"2024-01-12T09:46:30.056993Z","iopub.status.idle":"2024-01-12T09:46:30.468750Z","shell.execute_reply.started":"2024-01-12T09:46:30.056946Z","shell.execute_reply":"2024-01-12T09:46:30.467873Z"},"trusted":true},"execution_count":null,"outputs":[]}]}