{"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":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-05T16:08:23.311474Z","iopub.execute_input":"2024-05-05T16:08:23.312117Z","iopub.status.idle":"2024-05-05T16:08:23.325272Z","shell.execute_reply.started":"2024-05-05T16:08:23.312068Z","shell.execute_reply":"2024-05-05T16:08:23.322929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.optim as optim\nimport torchvision\nimport torchvision.transforms as transforms\nfrom torch.utils.data import DataLoader, Dataset\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\n","metadata":{"execution":{"iopub.status.busy":"2024-05-05T16:08:23.328195Z","iopub.execute_input":"2024-05-05T16:08:23.329494Z","iopub.status.idle":"2024-05-05T16:08:23.340527Z","shell.execute_reply.started":"2024-05-05T16:08:23.329459Z","shell.execute_reply":"2024-05-05T16:08:23.339464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nprint(device)","metadata":{"execution":{"iopub.status.busy":"2024-05-05T16:08:23.342103Z","iopub.execute_input":"2024-05-05T16:08:23.343443Z","iopub.status.idle":"2024-05-05T16:08:23.353174Z","shell.execute_reply.started":"2024-05-05T16:08:23.343382Z","shell.execute_reply":"2024-05-05T16:08:23.351372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CustomMNISTDataset(Dataset):\n    def __init__(self, csv_file, transform=None, is_test=False):\n        self.data_frame = pd.read_csv(csv_file)\n        self.transform = transform\n        self.is_test = is_test\n        \n    def __len__(self):\n        return len(self.data_frame)\n    \n    def __getitem__(self, index):\n        item = self.data_frame.iloc[index]\n        \n        if self.is_test:\n            image = item.values.reshape(28,28).astype(np.uint8)\n            label = None\n        else:\n            image = item[1:].values.reshape(28,28).astype(np.uint8)\n            label = item.iloc[0]\n            \n        image = transforms.ToPILImage()(image)\n        \n        if self.transform is not None:\n            image = self.transform(image)\n            \n        if self.is_test:\n            return image\n        else:\n            return image, label\n        ","metadata":{"execution":{"iopub.status.busy":"2024-05-05T16:08:23.356224Z","iopub.execute_input":"2024-05-05T16:08:23.356594Z","iopub.status.idle":"2024-05-05T16:08:23.367610Z","shell.execute_reply.started":"2024-05-05T16:08:23.356563Z","shell.execute_reply":"2024-05-05T16:08:23.366502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = transforms.Compose([\n    transforms.RandomRotation(15),\n    transforms.ToTensor(),\n    transforms.Normalize((0.5,),(0.5,))\n])","metadata":{"execution":{"iopub.status.busy":"2024-05-05T16:08:23.369291Z","iopub.execute_input":"2024-05-05T16:08:23.370119Z","iopub.status.idle":"2024-05-05T16:08:23.385245Z","shell.execute_reply.started":"2024-05-05T16:08:23.370079Z","shell.execute_reply":"2024-05-05T16:08:23.384050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = CustomMNISTDataset(csv_file = '/kaggle/input/digit-recognizer/train.csv', transform = transform, is_test=False)\ntest_dataset = CustomMNISTDataset(csv_file = '/kaggle/input/digit-recognizer/test.csv', transform=transform, is_test = True)","metadata":{"execution":{"iopub.status.busy":"2024-05-05T16:08:23.387158Z","iopub.execute_input":"2024-05-05T16:08:23.387487Z","iopub.status.idle":"2024-05-05T16:08:28.598167Z","shell.execute_reply.started":"2024-05-05T16:08:23.387459Z","shell.execute_reply":"2024-05-05T16:08:28.597145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Train Size: \" + str(len(train_dataset))+', Test Size: ' + str(len(test_dataset)))","metadata":{"execution":{"iopub.status.busy":"2024-05-05T16:08:28.599656Z","iopub.execute_input":"2024-05-05T16:08:28.600833Z","iopub.status.idle":"2024-05-05T16:08:28.605926Z","shell.execute_reply.started":"2024-05-05T16:08:28.600788Z","shell.execute_reply":"2024-05-05T16:08:28.605121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset[1]","metadata":{"execution":{"iopub.status.busy":"2024-05-05T16:08:28.607316Z","iopub.execute_input":"2024-05-05T16:08:28.607911Z","iopub.status.idle":"2024-05-05T16:08:28.764485Z","shell.execute_reply.started":"2024-05-05T16:08:28.607879Z","shell.execute_reply":"2024-05-05T16:08:28.763099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 64\ntrain_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True, num_workers=2)\ntest_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=True, num_workers=2)","metadata":{"execution":{"iopub.status.busy":"2024-05-05T16:13:55.525074Z","iopub.execute_input":"2024-05-05T16:13:55.525480Z","iopub.status.idle":"2024-05-05T16:13:55.533368Z","shell.execute_reply.started":"2024-05-05T16:13:55.525449Z","shell.execute_reply":"2024-05-05T16:13:55.531665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for example_data, example_labels in train_loader:\n    example_image = example_data[0]\n    print(\"Input Size:\",example_data.size())\n    \n    example_image_numpy = example_image.permute(1,2,0).numpy()\n    \n    plt.imshow(example_image_numpy)\n    plt.title(f\"Label: {example_labels[0]}\")\n    plt.show()\n    \n    break","metadata":{"execution":{"iopub.status.busy":"2024-05-05T16:14:57.614380Z","iopub.execute_input":"2024-05-05T16:14:57.614835Z","iopub.status.idle":"2024-05-05T16:14:58.103029Z","shell.execute_reply.started":"2024-05-05T16:14:57.614802Z","shell.execute_reply":"2024-05-05T16:14:58.101564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class SimpleCNN(nn.Module):\n    def __init__(self):\n        super(SimpleCNN, self).__init__()\n        self.conv1 = nn.Conv2d(in_channels = 1, out_channels=32, kernel_size=3, stride=1, padding=1)\n        self.relu = nn.ReLU()\n        self.pool = nn.MaxPool2d(kernel_size=2, stride=2)\n        self.conv2 = nn.Conv2d(in_channels=32, out_channels = 64, kernel_size=3, stride=1, padding=1)\n        self.conv3 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, stride=1, padding=1)\n        \n        self.fc1 = nn.Linear(128*7*7, 128)\n        self.dropout=nn.Dropout(0.5)\n        self.fc2 = nn.Linear(128,20)\n        self.fc3= nn.Linear(20,10)\n        \n    def forward(self, x):\n        \n        x= self.conv1(x)\n        x= self.relu(x)\n        x= self.pool(x)\n        x= self.conv2(x)\n        x= self.relu(x)\n        x= self.conv3(x)\n        x= self.pool(x)\n        \n        x_size = x.size(1) * x.size(2) * x.size(3)\n        \n        x = x.view(-1, x_size)\n        x = self.fc1(x)\n        x = self.relu(x)\n        x = self.fc2(x)\n        x = self.fc3(x)\n        return x","metadata":{"execution":{"iopub.status.busy":"2024-05-05T16:31:26.017478Z","iopub.execute_input":"2024-05-05T16:31:26.017966Z","iopub.status.idle":"2024-05-05T16:31:26.032645Z","shell.execute_reply.started":"2024-05-05T16:31:26.017930Z","shell.execute_reply":"2024-05-05T16:31:26.031305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = SimpleCNN().to(device)\ncriterion = nn.CrossEntropyLoss()\noptimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)\n","metadata":{"execution":{"iopub.status.busy":"2024-05-05T16:31:51.618778Z","iopub.execute_input":"2024-05-05T16:31:51.619248Z","iopub.status.idle":"2024-05-05T16:31:51.637807Z","shell.execute_reply.started":"2024-05-05T16:31:51.619215Z","shell.execute_reply":"2024-05-05T16:31:51.636921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_epochs = 100\nrunning_loss = 0.0\n\nfor epoch in range(num_epochs):\n    for i, data in enumerate(train_loader, 0):\n        inputs, labels = data\n        inputs, labels = inputs.to(device),labels.to(device)\n        \n        optimizer.zero_grad()\n        \n        outputs = model(inputs.float())\n        loss = criterion(outputs, labels)\n        loss.backward()\n        optimizer.step()\n        \n        running_loss += loss.item()\n        \n        \n        if i % 100 == 99:\n            print(f\"[{epoch+1}, {i+1}] Loss: {running_loss / 100:3f}\")\n            running_loss = 0.0\n            \n            \nprint(\"Training finished\")","metadata":{"execution":{"iopub.status.busy":"2024-05-05T16:44:10.530160Z","iopub.execute_input":"2024-05-05T16:44:10.530627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.eval()\npredictions = []\n\nwith torch.no_grad():\n    for data in test_loader:\n        data = data.to(device)\n        \n        outputs = model(data)\n        _, predicted = torch.max(outputs.data, 1)\n        predictions.extend(predicted.cpu().tolist())\n\nsubmission = pd.DataFrame({\n    'ImageId': range(1, len(predictions)+1),\n    \"Label\": predictions\n})\n        \n    \nsubmission.to_csv('predictions1231final.csv', index=False)","metadata":{},"execution_count":null,"outputs":[]}]}