{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"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":"2023-09-30T07:28:28.351421Z","iopub.execute_input":"2023-09-30T07:28:28.351793Z","iopub.status.idle":"2023-09-30T07:28:28.657694Z","shell.execute_reply.started":"2023-09-30T07:28:28.351769Z","shell.execute_reply":"2023-09-30T07:28:28.656718Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"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\n#test","metadata":{"execution":{"iopub.status.busy":"2023-09-30T07:28:28.659330Z","iopub.execute_input":"2023-09-30T07:28:28.660299Z","iopub.status.idle":"2023-09-30T07:28:32.802504Z","shell.execute_reply.started":"2023-09-30T07:28:28.660265Z","shell.execute_reply":"2023-09-30T07:28:32.801551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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        \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":{"execution":{"iopub.status.busy":"2023-09-30T07:28:32.806744Z","iopub.execute_input":"2023-09-30T07:28:32.809171Z","iopub.status.idle":"2023-09-30T07:28:32.817748Z","shell.execute_reply.started":"2023-09-30T07:28:32.809131Z","shell.execute_reply":"2023-09-30T07:28:32.816983Z"},"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":"2023-09-30T07:28:32.821605Z","iopub.execute_input":"2023-09-30T07:28:32.822542Z","iopub.status.idle":"2023-09-30T07:28:32.832144Z","shell.execute_reply.started":"2023-09-30T07:28:32.822512Z","shell.execute_reply":"2023-09-30T07:28:32.831373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2023-09-30T07:28:32.833465Z","iopub.execute_input":"2023-09-30T07:28:32.834195Z","iopub.status.idle":"2023-09-30T07:28:37.084293Z","shell.execute_reply.started":"2023-09-30T07:28:32.834106Z","shell.execute_reply":"2023-09-30T07:28:37.083391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_workers = 0\nbatch_size = 20\n\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":{"execution":{"iopub.status.busy":"2023-09-30T07:28:37.085679Z","iopub.execute_input":"2023-09-30T07:28:37.086017Z","iopub.status.idle":"2023-09-30T07:28:37.091741Z","shell.execute_reply.started":"2023-09-30T07:28:37.085971Z","shell.execute_reply":"2023-09-30T07:28:37.090677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.nn as nn\nimport torch.nn.functional as F","metadata":{"execution":{"iopub.status.busy":"2023-09-30T07:28:37.093224Z","iopub.execute_input":"2023-09-30T07:28:37.093877Z","iopub.status.idle":"2023-09-30T07:28:37.103031Z","shell.execute_reply.started":"2023-09-30T07:28:37.093847Z","shell.execute_reply":"2023-09-30T07:28:37.102189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline\n\nimages, labels = next(iter(train_loader))\nimages = images.numpy()\n\nfig = plt.figure(figsize=(25, 4))\nfor idx in range(20):\n    ax = fig.add_subplot(2, 10, idx+1, xticks=[], yticks=[])\n    ax.imshow(np.squeeze(images[idx]), cmap='gray')\n    ax.set_title(str(labels[idx].item()))","metadata":{"execution":{"iopub.status.busy":"2023-09-30T07:28:37.104442Z","iopub.execute_input":"2023-09-30T07:28:37.104816Z","iopub.status.idle":"2023-09-30T07:28:38.149913Z","shell.execute_reply.started":"2023-09-30T07:28:37.104788Z","shell.execute_reply":"2023-09-30T07:28:38.149046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Net(nn.Module):\n    def __init__(self):\n        super(Net,self).__init__()\n        \n        hidden_1 = 256\n        hidden_2 = 512\n        hidden_3 = 256\n        \n        self.fc1 = nn.Linear(28*28,hidden_1)\n        self.fc2 = nn.Linear(hidden_1,hidden_2)\n        self.fc3 = nn.Linear(hidden_2,hidden_3)\n        self.fc4 = nn.Linear(hidden_3,10)\n        \n    def forward(self, x):\n        \n        x = x.view(-1,28*28)\n        x = F.relu(self.fc1(x))\n        x = F.relu(self.fc2(x))\n        x = F.relu(self.fc3(x))\n        x = self.fc4(x)\n        \n        return x\n    \nmodel = Net()\nprint(model)","metadata":{"execution":{"iopub.status.busy":"2023-09-30T07:28:38.151344Z","iopub.execute_input":"2023-09-30T07:28:38.151883Z","iopub.status.idle":"2023-09-30T07:28:38.166993Z","shell.execute_reply.started":"2023-09-30T07:28:38.151851Z","shell.execute_reply":"2023-09-30T07:28:38.165921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"criterion = nn.CrossEntropyLoss()\noptimizer = torch.optim.SGD(model.parameters(),lr=0.01)","metadata":{"execution":{"iopub.status.busy":"2023-09-30T07:28:38.171024Z","iopub.execute_input":"2023-09-30T07:28:38.171269Z","iopub.status.idle":"2023-09-30T07:28:38.175749Z","shell.execute_reply.started":"2023-09-30T07:28:38.171249Z","shell.execute_reply":"2023-09-30T07:28:38.174782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_epochs = 25\ntrain_loss_min = np.Inf\n\nfor epoch in range(n_epochs):\n    train_loss = 0.0\n    model.train()\n    \n    for data, target in train_loader:\n\n        optimizer.zero_grad()\n        \n        output = model(data)\n        \n        loss = criterion(output, target)\n        \n        loss.backward()\n        \n        optimizer.step()\n        \n        train_loss += loss.item()*data.size(0)\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\n    ","metadata":{"execution":{"iopub.status.busy":"2023-09-30T07:31:52.024253Z","iopub.execute_input":"2023-09-30T07:31:52.024585Z","iopub.status.idle":"2023-09-30T07:31:55.524457Z","shell.execute_reply.started":"2023-09-30T07:31:52.024561Z","shell.execute_reply":"2023-09-30T07:31:55.523072Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.load_state_dict(torch.load('model.pt'))","metadata":{"execution":{"iopub.status.busy":"2023-09-30T07:28:38.500115Z","iopub.status.idle":"2023-09-30T07:28:38.500534Z","shell.execute_reply.started":"2023-09-30T07:28:38.500321Z","shell.execute_reply":"2023-09-30T07:28:38.500343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# obtain one batch of test images\nimages = next(iter(test_loader))\n\n# get sample outputs\noutput = model(images)\n# convert output probabilities to predicted class\n_, preds = torch.max(output, 1)\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, 10, idx+1, xticks=[], yticks=[])\n    ax.imshow(np.squeeze(images[idx]), cmap='gray')\n    ax.set_title(\"{}\".format(str(preds[idx].item())))\n","metadata":{"execution":{"iopub.status.busy":"2023-09-30T07:28:38.501709Z","iopub.status.idle":"2023-09-30T07:28:38.502577Z","shell.execute_reply.started":"2023-09-30T07:28:38.502358Z","shell.execute_reply":"2023-09-30T07:28:38.502379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":"2023-09-30T07:28:38.503720Z","iopub.status.idle":"2023-09-30T07:28:38.504581Z","shell.execute_reply.started":"2023-09-30T07:28:38.504361Z","shell.execute_reply":"2023-09-30T07:28:38.504383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef predict(model, 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":{"execution":{"iopub.status.busy":"2023-09-30T07:28:38.505918Z","iopub.status.idle":"2023-09-30T07:28:38.506347Z","shell.execute_reply.started":"2023-09-30T07:28:38.506137Z","shell.execute_reply":"2023-09-30T07:28:38.506159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = predict(model,test_loader)\npredictions = np.array(predictions)","metadata":{"execution":{"iopub.status.busy":"2023-09-30T07:28:38.507564Z","iopub.status.idle":"2023-09-30T07:28:38.508771Z","shell.execute_reply.started":"2023-09-30T07:28:38.508554Z","shell.execute_reply":"2023-09-30T07:28:38.508576Z"},"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    \"ImageId\": test_data.index + 1,\n    \"Label\": predictions\n})\nsubmission.to_csv(\"submission.csv\", index=None)","metadata":{"execution":{"iopub.status.busy":"2023-09-30T07:28:38.509720Z","iopub.status.idle":"2023-09-30T07:28:38.510584Z","shell.execute_reply.started":"2023-09-30T07:28:38.510366Z","shell.execute_reply":"2023-09-30T07:28:38.510387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}