{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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 in \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 \"../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# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"f = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')\nf.head(5)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"def getDataLabels(csv, label):\n    file = pd.read_csv(csv)\n    labels = file[label].to_numpy()\n    data = file.drop([label], axis=1).to_numpy(dtype=np.float32).reshape(file.shape[0],28,28)\n    data = np.expand_dims(data, axis=1)\n    return data, labels","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data, train_labels = getDataLabels('/kaggle/input/Kannada-MNIST/train.csv', 'label')\ntest_data, test_labels = getDataLabels('/kaggle/input/Kannada-MNIST/test.csv', 'id')\nother_data, other_labels = getDataLabels('/kaggle/input/Kannada-MNIST/Dig-MNIST.csv', 'label')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(f' Train:\\tshape of data:{train_data.shape}\\tshape of labels:{train_labels.shape}\\n \\\nTest:\\tshape of data:{test_data.shape} \\tshape of labels:{test_labels.shape}\\n \\\nOther:\\tshape of data:{other_data.shape}\\tshape of labels:{other_labels.shape}')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(train_data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.title(f'Train Label: {train_labels[5]}')\nplt.imshow(train_data[8,0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torch.optim as optim\n\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision import transforms","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class Kannada(Dataset):\n    \n    def __init__(self, data, labels, transform=None):\n        self.data =data\n        self.labels = labels\n        self.transform = transform\n        \n    def __len__(self):\n        return len(self.data)\n    \n    def __getitem__(self, i):\n        return self.data[i], self.labels[i]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"BATCH_SIZE = 256\nEPOCHS = 50\nDEVICE = torch.device('cuda' if torch.cuda.is_available else 'cpu')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"trans = transforms.Compose(transforms=[transforms.ToTensor()])\n\nKannadaTrainSet = Kannada(train_data, train_labels, trans)\ntrain_loader = DataLoader(dataset=KannadaTrainSet, batch_size=256, shuffle=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"KannadaTestSet = Kannada(test_data, test_labels, trans)\ntest_loader = DataLoader(dataset=KannadaTestSet, batch_size=100)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class _NonLocalBlockND(nn.Module):\n    def __init__(self, in_channels, inter_channels=None, dimension=3, sub_sample=True, bn_layer=True):\n        super(_NonLocalBlockND, self).__init__()\n\n        assert dimension in [1, 2, 3]\n\n        self.dimension = dimension\n        self.sub_sample = sub_sample\n\n        self.in_channels = in_channels\n        self.inter_channels = inter_channels\n\n        if self.inter_channels is None:\n            self.inter_channels = in_channels // 2\n            if self.inter_channels == 0:\n                self.inter_channels = 1\n\n        if dimension == 3:\n            conv_nd = nn.Conv3d\n            max_pool_layer = nn.MaxPool3d(kernel_size=(1, 2, 2))\n            bn = nn.BatchNorm3d\n        elif dimension == 2:\n            conv_nd = nn.Conv2d\n            max_pool_layer = nn.MaxPool2d(kernel_size=(2, 2))\n            bn = nn.BatchNorm2d\n        else:\n            conv_nd = nn.Conv1d\n            max_pool_layer = nn.MaxPool1d(kernel_size=(2))\n            bn = nn.BatchNorm1d\n\n        self.g = conv_nd(in_channels=self.in_channels, out_channels=self.inter_channels,\n                         kernel_size=1, stride=1, padding=0)\n\n        if bn_layer:\n            self.W = nn.Sequential(\n                conv_nd(in_channels=self.inter_channels, out_channels=self.in_channels,\n                        kernel_size=1, stride=1, padding=0),\n                bn(self.in_channels)\n            )\n            nn.init.constant_(self.W[1].weight, 0)\n            nn.init.constant_(self.W[1].bias, 0)\n        else:\n            self.W = conv_nd(in_channels=self.inter_channels, out_channels=self.in_channels,\n                             kernel_size=1, stride=1, padding=0)\n            nn.init.constant_(self.W.weight, 0)\n            nn.init.constant_(self.W.bias, 0)\n\n        self.theta = conv_nd(in_channels=self.in_channels, out_channels=self.inter_channels,\n                             kernel_size=1, stride=1, padding=0)\n        self.phi = conv_nd(in_channels=self.in_channels, out_channels=self.inter_channels,\n                           kernel_size=1, stride=1, padding=0)\n\n        if sub_sample:\n            self.g = nn.Sequential(self.g, max_pool_layer)\n            self.phi = nn.Sequential(self.phi, max_pool_layer)\n\n    def forward(self, x):\n        '''\n        :param x: (b, c, t, h, w)\n        :return:\n        '''\n\n        batch_size = x.size(0)\n\n        g_x = self.g(x).view(batch_size, self.inter_channels, -1)  # b,c,t,g,w-->b,c,tgw\n        g_x = g_x.permute(0, 2, 1)  # b,tgw,c\n\n        theta_x = self.theta(x).view(batch_size, self.inter_channels, -1)  # b,c,t,g,w-->b,c,tgw\n        theta_x = theta_x.permute(0, 2, 1)  # b,tgw,c\n        phi_x = self.phi(x).view(batch_size, self.inter_channels, -1)  # b,c,t,g,w-->b,c,tgw\n        f = torch.matmul(theta_x, phi_x)  # (tgw,c)x(c,tgw)-->(tgw,tgw)\n        f_div_C = F.softmax(f, dim=-1)  # 行softmax   (tgw,tgw)-->(tgw,tgw)\n\n        y = torch.matmul(f_div_C, g_x)\n        y = y.permute(0, 2, 1).contiguous()\n        y = y.view(batch_size, self.inter_channels, *x.size()[2:])\n        W_y = self.W(y)\n        z = W_y + x\n\n        return z\n\nclass NONLocalBlock2D(_NonLocalBlockND):\n    def __init__(self, in_channels, inter_channels=None, sub_sample=True, bn_layer=True):\n        super(NONLocalBlock2D, self).__init__(in_channels,\n                                              inter_channels=inter_channels,\n                                              dimension=2, sub_sample=sub_sample,\n                                              bn_layer=bn_layer)\n        \nclass Model(nn.Module):\n    def __init__(self):\n        super(Model, self).__init__()\n\n        self.convs = nn.Sequential(\n            nn.Conv2d(in_channels=1, out_channels=32, kernel_size=3, stride=1, padding=1),\n            nn.BatchNorm2d(32),\n            nn.ReLU(),\n            nn.MaxPool2d(2),\n\n            NONLocalBlock2D(in_channels=32),\n            nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, stride=1, padding=1),\n            nn.BatchNorm2d(64),\n            nn.ReLU(),\n            nn.MaxPool2d(2),\n\n            NONLocalBlock2D(in_channels=64),\n            nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, stride=1, padding=1),\n            nn.BatchNorm2d(128),\n            nn.ReLU(),\n            nn.MaxPool2d(2),\n        )\n\n        self.fc = nn.Sequential(\n            nn.Linear(in_features=128*3*3, out_features=256),\n            nn.ReLU(),\n            nn.Dropout(0.5),\n\n            nn.Linear(in_features=256, out_features=10)\n        )\n\n    def forward(self, x):\n        batch_size = x.size(0)\n        output = self.convs(x).view(batch_size, -1)\n        output = self.fc(output)\n        return output","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_loader_len = len(KannadaTrainSet)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"CNN = Model().to(DEVICE)\noptimizer = optim.Adam(CNN.parameters())\nloss_func = nn.CrossEntropyLoss()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def train(model, device, train_loader, optimizer, epoch):\n    model.train()\n    \n    correct = 0\n    for i, (data, target) in enumerate(train_loader):\n        data = data.to(device)\n        target = target.to(device)\n        \n        optimizer.zero_grad() # zero_grad\n        output = model(data) #forward\n        loss = loss_func(output, target) # loss\n        loss.backward() # backward   求导\n        optimizer.step() # 更新\n        \n        predict = output.max(dim=1, keepdim=True)[1]\n        correct += predict.eq(target.view_as(predict)).sum().item()\n    print('Training epoch:{}\\tTraining loss:{}\\tAccuracy:{:.0f}%\\n'.format(epoch, loss.item(), 100.*correct/train_loader_len))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for epoch in range(1, EPOCHS+1):\n    train(CNN, DEVICE, train_loader, optimizer, epoch)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"CNN.eval()\n\nallPredictList = []\nwith torch.no_grad():\n    for data, target in test_loader:\n        data = data.to(DEVICE)\n        target = target.to(DEVICE)\n        \n        output = CNN(data)\n        predict = output.max(dim=1)[1]\n        predictList = list(predict.to('cpu').numpy())\n        allPredictList += predictList","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = pd.DataFrame({'id':test_labels, 'label':np.array(allPredictList)})\nsubmission.to_csv(path_or_buf =\"submission.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":1}