{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.9","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30061,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import torch\nimport numpy as np\nimport pandas as pd\nimport torchvision.transforms as transforms\nimport torch.nn as nn\nimport torch\nimport matplotlib.pyplot as plt\nfrom torch.utils.data.sampler import SubsetRandomSampler\nfrom torch.autograd import Variable\n%matplotlib inline","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-10-26T01:48:38.295559Z","iopub.execute_input":"2024-10-26T01:48:38.296108Z","iopub.status.idle":"2024-10-26T01:48:38.308288Z","shell.execute_reply.started":"2024-10-26T01:48:38.296055Z","shell.execute_reply":"2024-10-26T01:48:38.306889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2024-10-26T01:48:38.311587Z","iopub.execute_input":"2024-10-26T01:48:38.312408Z","iopub.status.idle":"2024-10-26T01:48:38.321233Z","shell.execute_reply.started":"2024-10-26T01:48:38.312349Z","shell.execute_reply":"2024-10-26T01:48:38.320237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Exploring Dataset","metadata":{}},{"cell_type":"code","source":"# load dataset\ntrain_data = pd.read_csv('../input/digit-recognizer/train.csv')\ntest_data = pd.read_csv('../input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2024-10-26T01:48:38.322927Z","iopub.execute_input":"2024-10-26T01:48:38.323876Z","iopub.status.idle":"2024-10-26T01:48:43.312016Z","shell.execute_reply.started":"2024-10-26T01:48:38.323826Z","shell.execute_reply":"2024-10-26T01:48:43.311185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Total number of samples\",len(train_data),\"\\nTotal pixels: \", len(train_data.columns))","metadata":{"execution":{"iopub.status.busy":"2024-10-26T01:48:43.313453Z","iopub.execute_input":"2024-10-26T01:48:43.313767Z","iopub.status.idle":"2024-10-26T01:48:43.319036Z","shell.execute_reply.started":"2024-10-26T01:48:43.313737Z","shell.execute_reply":"2024-10-26T01:48:43.318029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()","metadata":{"execution":{"iopub.status.busy":"2024-10-26T01:48:43.324205Z","iopub.execute_input":"2024-10-26T01:48:43.324677Z","iopub.status.idle":"2024-10-26T01:48:43.347896Z","shell.execute_reply.started":"2024-10-26T01:48:43.324635Z","shell.execute_reply":"2024-10-26T01:48:43.346896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Get_Data(torch.utils.data.Dataset):\n    def __init__(self, data, transform=transforms.Compose([transforms.ToPILImage(),\n                                                                      transforms.RandomRotation(0, 0.5),\n                                                                      transforms.ToTensor(),\n                                                                      transforms.Normalize(mean=(0.5,), std=(0.5,))])):\n        \n        self.data = data\n        if len(data.columns) == len(train_data.columns):\n            self.X = data.iloc[:,1:].values.reshape((-1,28,28)).astype(np.uint8)[:,:,:,None]\n            self.y = torch.from_numpy(data.iloc[:,0].values)\n        else : \n            self.X = data.values.reshape((-1,28,28)).astype(np.uint8)[:,:,:,None]\n            self.y = None\n        self.transform = transform\n        \n    def __len__(self):\n        return len(self.data)\n    \n    def __getitem__(self, index):\n        if self.y is not None:\n            return self.transform(self.X[index]), self.y[index]\n        else:\n            return self.transform(self.X[index])\n","metadata":{"execution":{"iopub.status.busy":"2024-10-26T01:48:43.351316Z","iopub.execute_input":"2024-10-26T01:48:43.351618Z","iopub.status.idle":"2024-10-26T01:48:43.362383Z","shell.execute_reply.started":"2024-10-26T01:48:43.351590Z","shell.execute_reply":"2024-10-26T01:48:43.361345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = Get_Data(train_data)\ntest_df = Get_Data(test_data)\n","metadata":{"execution":{"iopub.status.busy":"2024-10-26T01:48:43.363781Z","iopub.execute_input":"2024-10-26T01:48:43.364191Z","iopub.status.idle":"2024-10-26T01:48:43.420038Z","shell.execute_reply.started":"2024-10-26T01:48:43.364146Z","shell.execute_reply":"2024-10-26T01:48:43.419285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 128\n\ntrain_loader = torch.utils.data.DataLoader(train_df, batch_size=batch_size)\ntest_loader = torch.utils.data.DataLoader(test_df, batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2024-10-26T01:48:43.421370Z","iopub.execute_input":"2024-10-26T01:48:43.421757Z","iopub.status.idle":"2024-10-26T01:48:43.428503Z","shell.execute_reply.started":"2024-10-26T01:48:43.421720Z","shell.execute_reply":"2024-10-26T01:48:43.427613Z"},"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)\nimages, labels = 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":{"execution":{"iopub.status.busy":"2024-10-26T01:48:43.429838Z","iopub.execute_input":"2024-10-26T01:48:43.430147Z","iopub.status.idle":"2024-10-26T01:48:44.604664Z","shell.execute_reply.started":"2024-10-26T01:48:43.430098Z","shell.execute_reply":"2024-10-26T01:48:44.603830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Viewing Image in More Detail**","metadata":{}},{"cell_type":"code","source":"img = np.squeeze(images[1])\n\nfig = plt.figure(figsize = (12,12)) \nax = fig.add_subplot(111)\nax.imshow(img, cmap='gray')\nwidth, height = img.shape\nthresh = img.max()/2.5\nfor x in range(width):\n    for y in range(height):\n        val = round(img[x][y],2) if img[x][y] >=0 else 0\n        ax.annotate(str(val), xy=(y,x),\n                    horizontalalignment='center',\n                    verticalalignment='center',\n                    color='white' if img[x][y]<thresh else 'black')","metadata":{"execution":{"iopub.status.busy":"2024-10-26T01:48:44.605867Z","iopub.execute_input":"2024-10-26T01:48:44.606171Z","iopub.status.idle":"2024-10-26T01:48:50.347673Z","shell.execute_reply.started":"2024-10-26T01:48:44.606132Z","shell.execute_reply":"2024-10-26T01:48:50.346737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nimport 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        self.features =nn.Sequential(\n            nn.Conv2d(1,32,kernel_size = 3, stride = 1 , padding = 1),\n            nn.BatchNorm2d(32),\n            nn.ReLU(True),\n            nn.Conv2d(32,64,kernel_size = 3, stride = 1 , padding = 1),\n            nn.BatchNorm2d(64),\n            nn.ReLU(True),\n            nn.Conv2d(64,64,kernel_size = 3, stride = 1 , padding = 1),\n            nn.BatchNorm2d(64),\n            nn.ReLU(True),\n            nn.Conv2d(64,128,kernel_size = 5, stride = 1 , padding = 2),\n            nn.BatchNorm2d(128),\n            nn.ReLU(True),\n            nn.MaxPool2d(2,2))\n        self.fc1 = nn.Linear(128*14*14,256)\n        self.fc2 = nn.Linear( 256,128)\n        self.fc3 = nn.Linear(128,64)\n        self.fc4 = nn.Linear(64,10)\n        self.dropout = nn.Dropout(0.35)\n        \n    def forward(self, x):\n        # flatten image input\n        x = self.features(x)\n        x = x.view(-1, 128*14*14)\n        # add hidden layer, with relu activation function\n        x = F.relu(self.fc1(x))\n        x = F.relu(self.fc2(x))\n        x = F.relu(self.fc3(x))\n        # add dropout layer\n        x = self.dropout(x)\n        # add hidden layer, with relu activation function\n        x = F.relu(self.fc4(x))\n        # add dropout layer\n        return x\n\n# initialize the NN\nmodel = Net()\nprint(model)\n\n","metadata":{"execution":{"iopub.status.busy":"2024-10-26T01:48:50.349023Z","iopub.execute_input":"2024-10-26T01:48:50.349370Z","iopub.status.idle":"2024-10-26T01:48:50.416670Z","shell.execute_reply.started":"2024-10-26T01:48:50.349338Z","shell.execute_reply":"2024-10-26T01:48:50.415533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.optim as optim\ntrain_on_gpu = torch.cuda.is_available()\nif train_on_gpu:\n        model.cuda()\n# specify loss function (categorical cross-entropy)\ncriterion = nn.CrossEntropyLoss()\n\n# specify optimizer\noptimizer = optim.SGD(model.parameters(), lr=0.1)\n\n","metadata":{"execution":{"iopub.status.busy":"2024-10-26T01:48:50.418714Z","iopub.execute_input":"2024-10-26T01:48:50.419228Z","iopub.status.idle":"2024-10-26T01:48:50.435561Z","shell.execute_reply.started":"2024-10-26T01:48:50.419180Z","shell.execute_reply":"2024-10-26T01:48:50.434472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_epochs = 30\n\nvalid_loss_min = np.Inf # track change in validation loss\n\nfor epoch in range(1, n_epochs+1):\n\n    # keep track of training and validation loss\n    train_loss = 0.0\n    valid_loss = 0.0\n    \n    ###################\n    # train the model #\n    ###################\n    model.train()\n    for data, target in train_loader:\n        # move tensors to GPU if CUDA is available\n        if train_on_gpu:\n            data, target = data.cuda(), target.cuda()\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 batch 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 training loss\n        train_loss += loss.item()*data.size(0)\n        \n    \n    # calculate average losses\n    train_loss = train_loss/len(train_loader.sampler)\n    #valid_loss = valid_loss/len(test_loader.sampler)\n        \n    # print training/validation statistics \n    print('Epoch: {} \\tTraining Loss: {:.6f} \\t'.format(\n        epoch, train_loss))\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2024-10-26T01:48:50.437398Z","iopub.execute_input":"2024-10-26T01:48:50.437769Z","iopub.status.idle":"2024-10-26T01:57:42.354748Z","shell.execute_reply.started":"2024-10-26T01:48:50.437733Z","shell.execute_reply":"2024-10-26T01:57:42.353741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.eval()\ntest_pred = torch.LongTensor()\n\nfor i, data in enumerate(test_loader):\n    data = Variable(data, volatile=True)\n    if torch.cuda.is_available():\n        data = data.cuda()\n\n    output = model(data)\n\n    pred = output.cpu().data.max(1, keepdim=True)[1]\n    test_pred = torch.cat((test_pred, pred), dim=0)\n\n","metadata":{"execution":{"iopub.status.busy":"2024-10-26T01:57:42.356052Z","iopub.execute_input":"2024-10-26T01:57:42.356413Z","iopub.status.idle":"2024-10-26T01:57:50.833517Z","shell.execute_reply.started":"2024-10-26T01:57:42.356381Z","shell.execute_reply":"2024-10-26T01:57:50.832543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out_df = pd.DataFrame(np.c_[np.arange(1, len(test_df)+1)[:,None], test_pred.numpy()], \n                      columns=['ImageId', 'Label'])","metadata":{"execution":{"iopub.status.busy":"2024-10-26T01:57:50.834728Z","iopub.execute_input":"2024-10-26T01:57:50.835034Z","iopub.status.idle":"2024-10-26T01:57:50.841229Z","shell.execute_reply.started":"2024-10-26T01:57:50.835005Z","shell.execute_reply":"2024-10-26T01:57:50.840222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-10-26T01:57:50.842827Z","iopub.execute_input":"2024-10-26T01:57:50.843245Z","iopub.status.idle":"2024-10-26T01:57:51.192478Z","shell.execute_reply.started":"2024-10-26T01:57:50.843200Z","shell.execute_reply":"2024-10-26T01:57:51.191682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}