{"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":"markdown","source":"Taken from : https://www.kaggle.com/leighplt/pytorch-starter-kit","metadata":{}},{"cell_type":"code","source":"import torch.nn as nn\nimport torch\nfrom torch.utils.data import Dataset, DataLoader, ConcatDataset\n\nimport torchvision\nfrom torchvision import transforms, utils\n\nimport glob\nimport os","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-10-10T05:22:50.9751Z","iopub.execute_input":"2021-10-10T05:22:50.975373Z","iopub.status.idle":"2021-10-10T05:22:50.980062Z","shell.execute_reply.started":"2021-10-10T05:22:50.975343Z","shell.execute_reply":"2021-10-10T05:22:50.979261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"en_dict = {}\npath = '../input/quickdraw-doodle-recognition/train_simplified'\n\nfilenames = glob.glob(os.path.join(path, '*.csv'))\n\nfilenames[:5]","metadata":{"execution":{"iopub.status.busy":"2021-10-10T05:22:50.9858Z","iopub.execute_input":"2021-10-10T05:22:50.986716Z","iopub.status.idle":"2021-10-10T05:22:51.000812Z","shell.execute_reply.started":"2021-10-10T05:22:50.986674Z","shell.execute_reply":"2021-10-10T05:22:51.000018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def encode_labels():\n    counter = 0\n    for fn in filenames:\n        en_dict[fn[:-4].split('/')[-1].replace(' ', '_')] = counter\n        counter += 1","metadata":{"execution":{"iopub.status.busy":"2021-10-10T05:22:51.002827Z","iopub.execute_input":"2021-10-10T05:22:51.003541Z","iopub.status.idle":"2021-10-10T05:22:51.008553Z","shell.execute_reply.started":"2021-10-10T05:22:51.003506Z","shell.execute_reply":"2021-10-10T05:22:51.007899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"encode_labels()","metadata":{"execution":{"iopub.status.busy":"2021-10-10T05:22:51.009755Z","iopub.execute_input":"2021-10-10T05:22:51.010358Z","iopub.status.idle":"2021-10-10T05:22:51.017138Z","shell.execute_reply.started":"2021-10-10T05:22:51.010318Z","shell.execute_reply":"2021-10-10T05:22:51.016395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dec_dict = {v: k for k , v in en_dict.items()}\n\ndef decode_labels(label):\n    return dec_dict[label]","metadata":{"execution":{"iopub.status.busy":"2021-10-10T05:22:51.01821Z","iopub.execute_input":"2021-10-10T05:22:51.018852Z","iopub.status.idle":"2021-10-10T05:22:51.025326Z","shell.execute_reply.started":"2021-10-10T05:22:51.018817Z","shell.execute_reply":"2021-10-10T05:22:51.024639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"decode_labels(181)","metadata":{"execution":{"iopub.status.busy":"2021-10-10T05:22:51.027606Z","iopub.execute_input":"2021-10-10T05:22:51.02828Z","iopub.status.idle":"2021-10-10T05:22:51.034532Z","shell.execute_reply.started":"2021-10-10T05:22:51.028239Z","shell.execute_reply":"2021-10-10T05:22:51.033811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_label(nfile):\n    #print(nfile[:-4].split('/')[-1].replace(' ', '_'))\n    return en_dict[nfile[:-4].split('/')[-1].replace(' ', '_')]","metadata":{"execution":{"iopub.status.busy":"2021-10-10T05:22:51.035679Z","iopub.execute_input":"2021-10-10T05:22:51.036353Z","iopub.status.idle":"2021-10-10T05:22:51.041928Z","shell.execute_reply.started":"2021-10-10T05:22:51.036316Z","shell.execute_reply":"2021-10-10T05:22:51.041244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_label(\"../input/quickdraw-doodle-recognition/train_simplified/The Eiffel Tower.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-10-10T05:22:51.042944Z","iopub.execute_input":"2021-10-10T05:22:51.04384Z","iopub.status.idle":"2021-10-10T05:22:51.051102Z","shell.execute_reply.started":"2021-10-10T05:22:51.043806Z","shell.execute_reply":"2021-10-10T05:22:51.05026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\na = pd.read_csv('../input/quickdraw-doodle-recognition/train_simplified/The Eiffel Tower.csv').head().iloc[0]['drawing']\na.split(']]')","metadata":{"execution":{"iopub.status.busy":"2021-10-10T05:22:51.052324Z","iopub.execute_input":"2021-10-10T05:22:51.053058Z","iopub.status.idle":"2021-10-10T05:22:51.632918Z","shell.execute_reply.started":"2021-10-10T05:22:51.053022Z","shell.execute_reply":"2021-10-10T05:22:51.63208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import ast\nimport numpy as np\nimport cv2\nimport matplotlib.pyplot as plt\n\nnew = ast.literal_eval(a)\nnew[0]\n\ntime_color = True\n\nBASE_SIZE=256\nlw = 6\n\nimg = np.zeros((BASE_SIZE, BASE_SIZE), np.uint8)\nfor t, stroke in enumerate(new):\n#     print(stroke)\n    for i in range(len(stroke[0]) - 1):\n        color = 255 - min(t,10) * 13 if time_color else 255\n        _ = cv2.line(img, (stroke[0][i], stroke[1][i]), (stroke[0][i+1], stroke[1][i+1]), color, lw)\n\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2021-10-10T05:22:51.63423Z","iopub.execute_input":"2021-10-10T05:22:51.634571Z","iopub.status.idle":"2021-10-10T05:22:51.842313Z","shell.execute_reply.started":"2021-10-10T05:22:51.634534Z","shell.execute_reply":"2021-10-10T05:22:51.841515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class DoodleDataset(Dataset):\n    def __init__(self, csv_file, root_dir, mode='train', nrows=1000, skiprows=None, size=256, transform=None):\n        self.root_dir = root_dir\n        file = os.path.join(root_dir, csv_file)\n        self.size=size\n        self.mode = mode\n        self.doodle = pd.read_csv(file, usecols=['drawing'], nrows=nrows, skiprows=skiprows)\n        self.transform = transform\n        if self.mode == 'train':\n            self.label = get_label(csv_file)\n    \n    @staticmethod\n    def _draw(raw_strokes, size=256, lw=6, time_color=True):\n        BASE_SIZE = 256\n        img = np.zeros((BASE_SIZE, BASE_SIZE),np.uint8 )\n        \n        for t, stroke in enumerate(raw_strokes):\n            for i in range(len(stroke[0]) - 1):\n                color = 255 - min(t,10) * 13 if time_color else 255\n                _ = cv2.line(img, (stroke[0][i], stroke[1][i]), (stroke[0][i+1], stroke[1][i+1]), color, lw)\n        \n        if size != BASE_SIZE:\n            return cv2.resize(img, (size, size))\n        \n        else:\n            return img\n    \n    def __len__(self):\n        return len(self.doodle)\n    \n    def __getitem__(self, index):\n        raw_strokes = ast.literal_eval(self.doodle.drawing[index])\n        sample = self._draw(raw_strokes, size=self.size, lw=2, time_color=True)\n        \n        if self.transform:\n            sample = self.transform(sample)\n        \n        if self.mode == 'train':\n            return (sample[None]/255).astype('float32'), self.label\n        else:\n            return (sample[None]/255).astype('float32')\n        ","metadata":{"execution":{"iopub.status.busy":"2021-10-10T05:22:51.844946Z","iopub.execute_input":"2021-10-10T05:22:51.84531Z","iopub.status.idle":"2021-10-10T05:22:51.857243Z","shell.execute_reply.started":"2021-10-10T05:22:51.845282Z","shell.execute_reply":"2021-10-10T05:22:51.856402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SIZE = 224\nselect_nrows= 10000\n\ndoodles = ConcatDataset([DoodleDataset(fn.split('/')[-1], path, mode='train', nrows=select_nrows, skiprows=None, size=SIZE, transform=None) for fn in filenames])\n","metadata":{"execution":{"iopub.status.busy":"2021-10-10T05:22:51.859168Z","iopub.execute_input":"2021-10-10T05:22:51.859753Z","iopub.status.idle":"2021-10-10T05:23:04.511687Z","shell.execute_reply.started":"2021-10-10T05:22:51.859715Z","shell.execute_reply":"2021-10-10T05:23:04.510788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(doodles))\n\ntrain_dataloader = DataLoader(doodles, batch_size=128, shuffle=True, num_workers=0)","metadata":{"execution":{"iopub.status.busy":"2021-10-10T05:23:04.513192Z","iopub.execute_input":"2021-10-10T05:23:04.513483Z","iopub.status.idle":"2021-10-10T05:23:04.777967Z","shell.execute_reply.started":"2021-10-10T05:23:04.513448Z","shell.execute_reply":"2021-10-10T05:23:04.777213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def imshow(img):\n    npimg = img.numpy()\n    plt.imshow(np.transpose(npimg, (1,2,0)))\n\nfor images, label in train_dataloader:\n    break\n    ","metadata":{"execution":{"iopub.status.busy":"2021-10-10T05:23:04.7814Z","iopub.execute_input":"2021-10-10T05:23:04.781637Z","iopub.status.idle":"2021-10-10T05:23:05.172332Z","shell.execute_reply.started":"2021-10-10T05:23:04.781611Z","shell.execute_reply":"2021-10-10T05:23:05.171603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(16,24))\nimshow(torchvision.utils.make_grid(images[:24]))","metadata":{"execution":{"iopub.status.busy":"2021-10-10T05:23:05.173849Z","iopub.execute_input":"2021-10-10T05:23:05.17411Z","iopub.status.idle":"2021-10-10T05:23:05.725008Z","shell.execute_reply.started":"2021-10-10T05:23:05.174073Z","shell.execute_reply":"2021-10-10T05:23:05.724338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def validation(get_loader,lossfn, scorefn):\n    model.eval()\n    loss, score = 0,0\n    vlen = len(get_loader)\n    \n    for X , y in get_loader:\n        X = X.to(device)\n        y = y.to(device)\n        output = model(X)\n        \n        loss += lossfn(output, y).item()\n        score += scorefn(output, y)[0].item()\n        \n    model.train()\n    return loss/vlen, score/vlen","metadata":{"execution":{"iopub.status.busy":"2021-10-10T05:23:05.726545Z","iopub.execute_input":"2021-10-10T05:23:05.726984Z","iopub.status.idle":"2021-10-10T05:23:05.733662Z","shell.execute_reply.started":"2021-10-10T05:23:05.726949Z","shell.execute_reply":"2021-10-10T05:23:05.73286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def accuracy(output, target, topk=(3,)):\n    with torch.no_grad():\n        maxk = max(topk)\n        batch_size = target.size(0)\n        \n        _, pred = output.topk(maxk, 1, True, True)\n        pred = pred.t()\n        correct = pred.eq(target.view(1,-1).expand_as(pred))\n        \n        res = []\n        for k in topk:\n            correct_k = correct[:k].view(-1).float().sum(0,keepdim=True)\n            res.append(correct_k.mul_(100.0/batch_size))\n        return res","metadata":{"execution":{"iopub.status.busy":"2021-10-10T05:23:05.735309Z","iopub.execute_input":"2021-10-10T05:23:05.735662Z","iopub.status.idle":"2021-10-10T05:23:05.744463Z","shell.execute_reply.started":"2021-10-10T05:23:05.735618Z","shell.execute_reply":"2021-10-10T05:23:05.743765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def mapk(output, target, k=3):\n    \"\"\"\n    Computes the mean average precision at k.\n    \n    Parameters\n    ----------\n    output (torch.Tensor): A Tensor of predicted elements.\n                           Shape: (N,C)  where C = number of classes, N = batch size\n    target (torch.int): A Tensor of elements that are to be predicted. \n                        Shape: (N) where each value is  0≤targets[i]≤C−1\n    k (int, optional): The maximum number of predicted elements\n    \n    Returns\n    -------\n    score (torch.float):  The mean average precision at k over the output\n    \"\"\"\n    with torch.no_grad():\n        batch_size = target.size(0)\n\n        _, pred = output.topk(k, 1, True, True)\n        pred = pred.t()\n        correct = pred.eq(target.reshape(1, -1).expand_as(pred))\n\n        for i in range(k):\n            correct[i] = correct[i]*(k-i)\n            \n        score = correct[:k].reshape(-1).float().sum(0, keepdim=True)\n        score.mul_(1.0 / (k * batch_size))\n        return score","metadata":{"execution":{"iopub.status.busy":"2021-10-10T05:23:05.746035Z","iopub.execute_input":"2021-10-10T05:23:05.746371Z","iopub.status.idle":"2021-10-10T05:23:05.755005Z","shell.execute_reply.started":"2021-10-10T05:23:05.746338Z","shell.execute_reply":"2021-10-10T05:23:05.753991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a = torch.randn(10,2,220,200)\n\nk=5\na[:k].view(-1)","metadata":{"execution":{"iopub.status.busy":"2021-10-10T05:23:05.756239Z","iopub.execute_input":"2021-10-10T05:23:05.756684Z","iopub.status.idle":"2021-10-10T05:23:05.775822Z","shell.execute_reply.started":"2021-10-10T05:23:05.756646Z","shell.execute_reply":"2021-10-10T05:23:05.77506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = torchvision.models.resnet18(pretrained=True)","metadata":{"execution":{"iopub.status.busy":"2021-10-10T05:23:05.776922Z","iopub.execute_input":"2021-10-10T05:23:05.777587Z","iopub.status.idle":"2021-10-10T05:23:06.060142Z","shell.execute_reply.started":"2021-10-10T05:23:05.777546Z","shell.execute_reply":"2021-10-10T05:23:06.059403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def squeeze_weights(m):\n    m.weight.data = m.weight.data.sum(dim=1)[:,None]\n    m.in_channels = 1\n\nmodel.conv1.apply(squeeze_weights)\n\nnum_classes = 340\n\nmodel.fc = nn.Linear(512, out_features=num_classes, bias=True)","metadata":{"execution":{"iopub.status.busy":"2021-10-10T05:23:06.061681Z","iopub.execute_input":"2021-10-10T05:23:06.062005Z","iopub.status.idle":"2021-10-10T05:23:06.07019Z","shell.execute_reply.started":"2021-10-10T05:23:06.06197Z","shell.execute_reply":"2021-10-10T05:23:06.069486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nmodel(torch.randn(12,1,224,224)).size()","metadata":{"execution":{"iopub.status.busy":"2021-10-10T05:23:06.073633Z","iopub.execute_input":"2021-10-10T05:23:06.07388Z","iopub.status.idle":"2021-10-10T05:23:06.80503Z","shell.execute_reply.started":"2021-10-10T05:23:06.07385Z","shell.execute_reply":"2021-10-10T05:23:06.804178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n\nmodel = model.to(device)","metadata":{"execution":{"iopub.status.busy":"2021-10-10T05:23:06.806528Z","iopub.execute_input":"2021-10-10T05:23:06.806792Z","iopub.status.idle":"2021-10-10T05:23:06.83522Z","shell.execute_reply.started":"2021-10-10T05:23:06.806758Z","shell.execute_reply":"2021-10-10T05:23:06.834582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"criterion = nn.CrossEntropyLoss()\noptimizer = torch.optim.Adam(model.parameters(), lr=0.002, amsgrad = True)\nscheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[5000, 12000, 18000], gamma=0.5)","metadata":{"execution":{"iopub.status.busy":"2021-10-10T05:23:06.836473Z","iopub.execute_input":"2021-10-10T05:23:06.836728Z","iopub.status.idle":"2021-10-10T05:23:06.843208Z","shell.execute_reply.started":"2021-10-10T05:23:06.836696Z","shell.execute_reply":"2021-10-10T05:23:06.842189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nepochs = 1\nlsize = len(train_dataloader)\nprint(f\"size of train : {lsize}\")\nitr =1\np_itr = 1000\nmodel.train()\ntloss, score =0,0\nfor epoch in range(epochs):\n    for X, y in train_dataloader:\n        X, y = X.to(device), y.to(device)\n        output = model(X)\n        loss = criterion(output, y)\n        tloss += loss.item()\n        optimizer.zero_grad()\n        loss.backward()\n        optimizer.step()\n        score += mapk(output, y)[0].item()\n        scheduler.step()\n        \n        if itr %p_itr ==0 :\n            print('Iteration {} -> Train Loss: {:.4f}, MAP@3: {:.3f}'.format(itr, tloss/p_itr, score/p_itr))\n            tloss, score = 0, 0\n        itr +=1\n        if itr >= 6000:\n            break \n            #you can continue this for better model\n        ","metadata":{"execution":{"iopub.status.busy":"2021-10-10T05:23:06.844834Z","iopub.execute_input":"2021-10-10T05:23:06.845085Z","iopub.status.idle":"2021-10-10T05:49:25.750413Z","shell.execute_reply.started":"2021-10-10T05:23:06.845048Z","shell.execute_reply":"2021-10-10T05:49:25.749472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filename_pth = 'checkpoint_resnet18.pth'\ntorch.save(model.state_dict(), filename_pth)","metadata":{"execution":{"iopub.status.busy":"2021-10-10T05:49:25.751992Z","iopub.execute_input":"2021-10-10T05:49:25.752464Z","iopub.status.idle":"2021-10-10T05:49:25.886814Z","shell.execute_reply.started":"2021-10-10T05:49:25.752427Z","shell.execute_reply":"2021-10-10T05:49:25.885986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testset = DoodleDataset('test_simplified.csv', '../input/quickdraw-doodle-recognition/', mode='test', nrows=None, size=SIZE)\ntestloader = DataLoader(testset, batch_size=128, shuffle=False, num_workers=0)","metadata":{"execution":{"iopub.status.busy":"2021-10-10T05:49:25.888261Z","iopub.execute_input":"2021-10-10T05:49:25.888663Z","iopub.status.idle":"2021-10-10T05:49:26.402726Z","shell.execute_reply.started":"2021-10-10T05:49:25.888626Z","shell.execute_reply":"2021-10-10T05:49:26.401989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tqdm\n\nmodel.eval()\nmodel = model.to(device)\nlabels = np.empty((0,3))\n#labels = labels.to(device)\nfor x in tqdm.tqdm(testloader):\n    x = x.to(device)\n    output = model(x)\n    _, pred = output.topk(3, 1, True, True)\n    labels = np.concatenate([labels, pred.cpu()], axis = 0)","metadata":{"execution":{"iopub.status.busy":"2021-10-10T05:51:55.133093Z","iopub.execute_input":"2021-10-10T05:51:55.133366Z","iopub.status.idle":"2021-10-10T05:53:58.900751Z","shell.execute_reply.started":"2021-10-10T05:51:55.133336Z","shell.execute_reply":"2021-10-10T05:53:58.899921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nsubmission = pd.read_csv('../input/quickdraw-doodle-recognition/test_simplified.csv', index_col='key_id')\nsubmission.drop(['countrycode', 'drawing'], axis=1, inplace=True)\nsubmission['word'] = ''\nfor i, label in enumerate(labels):\n    submission.word.iloc[i] = \" \".join([decode_labels(l) for l in label])","metadata":{"execution":{"iopub.status.busy":"2021-10-10T05:59:02.079935Z","iopub.execute_input":"2021-10-10T05:59:02.080469Z","iopub.status.idle":"2021-10-10T06:00:05.114107Z","shell.execute_reply.started":"2021-10-10T05:59:02.080429Z","shell.execute_reply":"2021-10-10T06:00:05.112725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-10-10T06:00:05.115582Z","iopub.execute_input":"2021-10-10T06:00:05.116214Z","iopub.status.idle":"2021-10-10T06:00:05.471438Z","shell.execute_reply.started":"2021-10-10T06:00:05.116175Z","shell.execute_reply":"2021-10-10T06:00:05.470668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"done\")","metadata":{"execution":{"iopub.status.busy":"2021-10-10T06:00:05.472501Z","iopub.execute_input":"2021-10-10T06:00:05.472854Z","iopub.status.idle":"2021-10-10T06:00:05.477544Z","shell.execute_reply.started":"2021-10-10T06:00:05.472813Z","shell.execute_reply":"2021-10-10T06:00:05.476843Z"},"trusted":true},"execution_count":null,"outputs":[]}]}