{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30664,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"#Import statements\nimport torch #Import the entire torch library\nfrom torch.utils.data import Dataset, DataLoader #Importing Dataset (Can create custom test and train dataset) and DataLoader (wrapper around Dataset to iterate)\nfrom torchvision.transforms import ToTensor #Transform function allows to convert PIL Images or numpy ndarray to tensors\nimport torch.nn as nn #Neural network from pytorch\nimport pandas as pd\nimport numpy as np\nfrom torchvision.transforms import ToTensor, RandomRotation, RandomAffine, GaussianBlur, Normalize\nfrom torchvision import transforms\n","metadata":{"_uuid":"e708f303-39fe-489f-add8-b37c61672b19","_cell_guid":"15159954-5d25-4268-879a-7d5dbf7ff771","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-20T09:31:07.174391Z","iopub.execute_input":"2024-03-20T09:31:07.175378Z","iopub.status.idle":"2024-03-20T09:31:07.180706Z","shell.execute_reply.started":"2024-03-20T09:31:07.175340Z","shell.execute_reply":"2024-03-20T09:31:07.179698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Standard code to check if GPU/cuda is available\nif torch.cuda.is_available():\n    device=torch.device(type=\"cuda\",index=0)\nelse:\n    device=torch.device(type=\"cpu\",index=0)\n\nprint(torch.cuda.is_available())\n\nprint(device)","metadata":{"_uuid":"54025bd9-2926-4af7-88fb-b9b9b0e4c834","_cell_guid":"c6327eda-a9b3-4d78-bd7b-d4eb44466db2","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-20T09:31:07.295536Z","iopub.execute_input":"2024-03-20T09:31:07.295830Z","iopub.status.idle":"2024-03-20T09:31:07.301839Z","shell.execute_reply.started":"2024-03-20T09:31:07.295805Z","shell.execute_reply":"2024-03-20T09:31:07.300864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CustomTrainDataset(Dataset):\n    def __init__(self, path, transform):\n        super().__init__()\n        self.original_data = pd.read_csv(path, header=\"infer\").values\n        self.synthetic_data = self.generate_synthetic_data()\n        self.combined_data = np.concatenate((self.original_data, self.synthetic_data), axis=0)\n        self.length = self.combined_data.shape[0]\n        self.transform = transform\n\n\n    def __len__(self):\n        return self.length\n\n    def __getitem__(self, idx):\n\n        image = self.combined_data[idx, 1:].astype(np.uint8)\n        image = np.reshape(image, (28, 28, 1))\n        image = ToTensor()(image)\n        if idx >= len(self.original_data):\n            image = self.transform(image)\n        label = self.combined_data[idx, 0]\n        return image, label\n\n    def generate_synthetic_data(self):\n        synthetic_data = np.copy(self.original_data[:,:])  # Copy original data without labels\n        return synthetic_data\n\nclass CustomTestDataset(Dataset):\n    def __init__(self, path, transform=None):\n        super().__init__()\n        self.data = pd.read_csv(path, header=\"infer\").values\n        self.length = self.data.shape[0]\n        self.transform = transform\n\n    def __len__(self):\n        return self.length\n\n    def __getitem__(self, idx):\n        flatimage = self.data[idx, :].astype(np.uint8)\n        image = np.reshape(flatimage, (28, 28, 1))\n        image = ToTensor()(image)\n        if self.transform:\n            image = self.transform(image)\n        return image\n\n# Data augmentation transforms for synthetic data\nsynthetic_transform = transforms.Compose([\n    RandomRotation(10),\n    RandomAffine(degrees=0, translate=(0.1, 0.1)),\n    GaussianBlur(3),\n    Normalize([.5],[.5])\n    \n])\n\ntrain_dataset = CustomTrainDataset('/kaggle/input/digit-recognizer/train.csv', transform=synthetic_transform)\ntest_dataset = CustomTestDataset('/kaggle/input/digit-recognizer/test.csv')\n\nbatch_size = 64\n\ntrain_dl = DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)\ntest_dl = DataLoader(dataset=test_dataset, batch_size=batch_size)\n\n# Summary of the model\n# summary(model, input_size=(1, 28, 28))  # Assuming model is defined elsewhere","metadata":{"execution":{"iopub.status.busy":"2024-03-20T09:38:12.661960Z","iopub.execute_input":"2024-03-20T09:38:12.662636Z","iopub.status.idle":"2024-03-20T09:38:17.014786Z","shell.execute_reply.started":"2024-03-20T09:38:12.662602Z","shell.execute_reply":"2024-03-20T09:38:17.013981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# class CustomTrainDataset(Dataset):\n#   def __init__(self, path, transform):\n#     super().__init__()\n#     self.data=pd.read_csv(path, header=\"infer\").values\n#     self.length = self.data.shape[0]\n#     self.transform = transform\n\n#   def __len__(self):\n#       return self.length\n\n#   def __getitem__(self, idx):\n#     flatimage = self.data[idx, 1:].astype(np.uint8)\n#     image = self.transform(np.reshape(flatimage, (28,28,1)))\n#     label = self.data[idx,0]\n#     return image, label\n\n# class CustomTestDataset(Dataset):\n#   def __init__(self, path, transform):\n#     super().__init__()\n#     self.data = pd.read_csv(path, header=\"infer\").values\n#     self.length = self.data.shape[0]\n#     self.transform = transform\n\n#   def __len__(self):\n#     return self.length\n\n#   def __getitem__(self, idx):\n#     flatimage = self.data[idx,:].astype(np.uint8)\n#     image = self.transform(np.reshape(flatimage, (28,28,1)))\n#     return image\n\n\n# train_dataset=CustomTrainDataset('/kaggle/input/digit-recognizer/train.csv', ToTensor())\n# test_dataset=CustomTestDataset('/kaggle/input/digit-recognizer/test.csv', ToTensor())\n\n# batch_size=64\n\n# train_dl=DataLoader(\n#     dataset=train_dataset,\n#     batch_size=batch_size,\n#     shuffle=True\n# )\n# test_dl=DataLoader(\n#     dataset=test_dataset,\n#     batch_size=batch_size,\n# )","metadata":{"_uuid":"671e9ed9-1805-4043-9202-faccb8bef581","_cell_guid":"e6ec699c-08ce-4a4d-a468-4712fcf68c6f","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-20T09:38:17.016557Z","iopub.execute_input":"2024-03-20T09:38:17.016858Z","iopub.status.idle":"2024-03-20T09:38:17.022081Z","shell.execute_reply.started":"2024-03-20T09:38:17.016829Z","shell.execute_reply":"2024-03-20T09:38:17.021147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nclass DigitRecognizer(nn.Module):\n  def __init__(self):\n    super().__init__()\n    self.relu = nn.ReLU()\n# 28\n    self.conv1 = nn.Conv2d(in_channels=1, out_channels=32, kernel_size=(3,3), stride=1, padding=0)\n    self.bn1 = nn.BatchNorm2d(32)\n    # 26\n    self.mp1 = nn.MaxPool2d(kernel_size=(2,2), stride=2, padding=0)\n# 13\n    self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=(3,3), stride=1, padding=0)\n    self.bn2 = nn.BatchNorm2d(64)\n# 11\n    self.conv3 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=(3,3), stride=1, padding=0)\n    self.bn3 = nn.BatchNorm2d(128)\n# 9\n    self.conv4 = nn.Conv2d(in_channels=128, out_channels=200, kernel_size=(3,3), stride=1, padding=0)\n    self.bn4 = nn.BatchNorm2d(200)\n# 7\n    self.flatten = nn.Flatten()\n    self.drop=nn.Dropout(0.2)\n\n    self.lin1 = nn.Linear(in_features=64*200, out_features=2048)\n    self.lin2 = nn.Linear(in_features=2048, out_features=500)\n    self.lin3 = nn.Linear(in_features=500, out_features=10)\n    self.bn5 = nn.BatchNorm1d(num_features=2048)\n    self.bn6 = nn.BatchNorm1d(num_features=500)\n    self.bn7 = nn.BatchNorm1d(num_features=10)\n\n  def forward(self, x):\n    x = self.conv1(x)\n    x = self.bn1(x)\n    x = self.relu(x)\n#     28,28,1 to 26,26,8\n    x=self.drop(x)\n    x = self.conv2(x)\n    x = self.bn2(x)\n    x = self.relu(x)\n    \n#     26,26,8 to 24,24,16\n    x=self.drop(x)\n    x = self.mp1(x)\n#     24,24,16 to 12,12,16\n    x = self.conv3(x)\n    x = self.bn3(x)\n    x = self.relu(x)\n#     12,12,16 to 10,10,32\n\n    x = self.conv4(x)\n    x = self.bn4(x)\n    x = self.relu(x)\n#     10,10,32 to 8,8,64\n    x=self.drop(x)\n\n    x = self.flatten(x)\n#     8,8,64 to 8*8*64\n    x = self.lin1(x)\n    x = self.bn5(x)\n    x=self.drop(x)\n\n    x = self.lin2(x)\n    x = self.bn6(x)\n    x=self.drop(x)    \n    x = self.lin3(x)\n    output = self.bn7(x)\n    return output","metadata":{"_uuid":"96b9c0bb-cad6-4be2-a791-7eb0f519e677","_cell_guid":"63aa302e-23c8-4aad-a95f-25eef18ee5fc","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-20T09:38:17.023209Z","iopub.execute_input":"2024-03-20T09:38:17.023450Z","iopub.status.idle":"2024-03-20T09:38:17.038752Z","shell.execute_reply.started":"2024-03-20T09:38:17.023429Z","shell.execute_reply":"2024-03-20T09:38:17.037893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class DigitRecognizer_1(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.relu = nn.ReLU()\n\n        self.conv1 = nn.Conv2d(in_channels=1, out_channels=64, kernel_size=(3, 3), stride=1, padding=0)\n        self.bn1 = nn.BatchNorm2d(64)\n        self.mp1 = nn.MaxPool2d(kernel_size=(2, 2), stride=2, padding=0)\n\n        self.conv2 = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=(3, 3), stride=1, padding=0)\n        self.bn2 = nn.BatchNorm2d(64)\n\n        self.conv3 = nn.Conv2d(in_channels=64, out_channels=32, kernel_size=(3, 3), stride=1, padding=0)\n        self.bn3 = nn.BatchNorm2d(32)\n\n        self.conv4 = nn.Conv2d(in_channels=32, out_channels=8, kernel_size=(3, 3), stride=1, padding=0)\n        self.bn4 = nn.BatchNorm2d(8)\n\n        self.flatten = nn.Flatten()\n\n        self.lin1 = nn.Linear(in_features=49*8, out_features=10)\n        self.bn5 = nn.BatchNorm1d(num_features=10)\n\n    def forward(self, x):\n        feature_maps = []\n\n        x = self.conv1(x)\n        x = self.bn1(x)\n        x = self.relu(x)\n        x = self.mp1(x)\n\n        x = self.conv2(x)\n        x = self.bn2(x)\n        x = self.relu(x)\n\n        x = self.conv3(x)\n        x = self.bn3(x)\n        x = self.relu(x)\n\n        x = self.conv4(x)\n        x = self.bn4(x)\n        x = self.relu(x)\n\n        x = self.flatten(x)\n\n        x = self.lin1(x)\n        output = self.bn5(x)\n\n        return output","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=DigitRecognizer_1().to(device)\nloss_fn=nn.CrossEntropyLoss()","metadata":{"_uuid":"d7dff07a-9ee5-47a0-b950-3f868d745fa5","_cell_guid":"dd3462ef-570c-48cf-8dd6-8e4484275a39","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-20T09:38:17.040383Z","iopub.execute_input":"2024-03-20T09:38:17.040822Z","iopub.status.idle":"2024-03-20T09:38:17.338659Z","shell.execute_reply.started":"2024-03-20T09:38:17.040797Z","shell.execute_reply":"2024-03-20T09:38:17.337752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef train(dataloader, model, loss_fn, optimizer):\n    size = len(dataloader.dataset)\n    correct_predictions=0\n    model.train()  # Set the model in training mode\n    for batch, (X, y) in enumerate(dataloader):\n        X, y = X.to(device), y.to(device)\n\n# X is image\n# y is label\n\n        # Compute prediction error\n        pred = model(X)\n        loss = loss_fn(pred, y)\n\n        # Backpropagation: \n        optimizer.zero_grad()\n        loss.backward()\n        optimizer.step()\n\n        # Calculate total correct predictions\n        predicted_class = torch.argmax(pred, dim=1)\n        acc = (predicted_class == y).sum().item()\n        correct_predictions+=acc\n        \n        \n        \n        if batch % 100 == 0:\n            loss, current = loss.item(), (batch + 1) * len(X)\n            print(f\"loss: {loss:>7f}  [{current:>5d}/{size:>5d}]\")\n    print(f\"correct predictions = {correct_predictions}\")\n    print(f\"Epoch Accuracy = {correct_predictions/(batch_size*len(dataloader))}\")","metadata":{"_uuid":"5bdf0612-7239-47ee-afef-077e4170ae19","_cell_guid":"4b591a60-314e-4c9b-8c9e-a468b2b0ea0e","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-20T09:38:17.339873Z","iopub.execute_input":"2024-03-20T09:38:17.340302Z","iopub.status.idle":"2024-03-20T09:38:17.349430Z","shell.execute_reply.started":"2024-03-20T09:38:17.340248Z","shell.execute_reply":"2024-03-20T09:38:17.348532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer = torch.optim.Adam(model.parameters(), lr=0.0009)\nepochs = 50\nfor epoch in range(epochs):\n    print(f\"Epoch {epoch+1}\\n-------------------------------\")\n    train(train_dl, model, loss_fn, optimizer)","metadata":{"_uuid":"a52320d5-0613-42fd-bbf1-619bf0e14047","_cell_guid":"bee05dda-783e-44ba-8f9f-f73c44e90c5b","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-20T09:38:17.350334Z","iopub.execute_input":"2024-03-20T09:38:17.350594Z","iopub.status.idle":"2024-03-20T09:38:23.476959Z","shell.execute_reply.started":"2024-03-20T09:38:17.350573Z","shell.execute_reply":"2024-03-20T09:38:23.475086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def test(dataloader, model, loss_fn,path):\n    data_submission=pd.read_csv(path)\n\n    size = len(dataloader.dataset)\n    num_batches = len(dataloader)\n    correct_predictions=0\n    model.eval()  # Set the model in evaluation mode\n    test_loss= 0\n    \n    \n\n\n    with torch.no_grad():\n        for i, X in enumerate(dataloader):\n            X= X.to(device)\n            pred = model(X)\n            prediction_labels=torch.argmax(pred,dim=1).cpu().numpy()\n            \n            start_index = i * dataloader.batch_size\n            end_index = min(start_index + dataloader.batch_size, len(data_submission))\n            data_submission.iloc[start_index:end_index, 1] = prediction_labels\n            \n#             test_loss += loss_fn(pred, y).item()\n#             correct_predictions += (pred.argmax(1) == y).sum().item()\n    data_submission.to_csv('submission.csv',index=False)","metadata":{"_uuid":"c81d6159-d0c6-42c0-8b99-3844d62d8304","_cell_guid":"d5cbb36a-4bd6-466d-88f8-b07ba13eea66","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-20T08:19:41.135544Z","iopub.status.idle":"2024-03-20T08:19:41.135898Z","shell.execute_reply.started":"2024-03-20T08:19:41.135727Z","shell.execute_reply":"2024-03-20T08:19:41.135741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test(test_dl, model,loss_fn,\"/kaggle/input/digit-recognizer/sample_submission.csv\")\nprint(\"Done\")","metadata":{"_uuid":"d00f27c2-a680-47f1-a0d5-34df6d6379f8","_cell_guid":"cd8feb3d-5030-4c14-959c-36c79d6c097f","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-03-20T08:19:41.139488Z","iopub.status.idle":"2024-03-20T08:19:41.140207Z","shell.execute_reply.started":"2024-03-20T08:19:41.140025Z","shell.execute_reply":"2024-03-20T08:19:41.140041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}