{"cells":[{"metadata":{"_uuid":"3c6ccaa502f87e5d8da7135d2e01e5a64437e164"},"cell_type":"markdown","source":"This has been inspired from this wonderful paper! [https://arxiv.org/pdf/1610.02391.pdf ]"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nprint(os.listdir(\"../input\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9558135d0d9d59097903c80a5b1578aa6e4c6aaf"},"cell_type":"code","source":"from pathlib import Path\nfrom glob import glob\nfrom PIL import Image","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9761580c87520ba42a7678a95181b0a4214e37e7"},"cell_type":"code","source":"from fastai import *\nfrom fastai.vision import *\nfrom fastai.callbacks.hooks import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0dbb6011c0d7c18c231609981aab826e66460736"},"cell_type":"code","source":"PATH = Path('../input')\ntrn_df = pd.read_csv(PATH/f'train_labels.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"83285fa0205f1d39ead3720b2c02cda518a09a6c"},"cell_type":"code","source":"trn_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"70038d05546693ff48900d0053bea84100278320"},"cell_type":"code","source":"trn_df.columns = [0,1]\n\ntrn_df.to_csv('../working/trn.csv',index=False)\n\npath = Path('../input/train')\n\ntfms = []","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5436697d4dd3bc554db0a5ec01f42e67a276df0e"},"cell_type":"code","source":"src = ImageItemList.from_df(trn_df,'../input/train',suffix='.tif')\nsrc = src.random_split_by_pct()\nsrc = src.label_from_df()\nsrc = src.transform(tfms)\nsrc = src.databunch(path='../input/train').normalize(imagenet_stats)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cedc751da8c0646eaa5fc0513dd0f123fc5e65ba"},"cell_type":"code","source":"x,y = src.one_batch(DatasetType.Train,True,True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a7e0afc878759d2832586eedd5fa0d4535c02fe5"},"cell_type":"code","source":"grab_idx(x,63).size()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ac8d9de56dcb10237bd4b50cd86e6aec1471ac03"},"cell_type":"code","source":"src.show_batch(3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"15499341fc6c47d29a2d5dc280303548311ae434"},"cell_type":"code","source":"MODEL_PATH = \"/tmp/model/\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"db165a2d6f551642febb71a1237f5b5369078a06"},"cell_type":"code","source":"gc.collect()\nlearn = create_cnn(src, models.resnet34, metrics=error_rate, bn_final=True,model_dir=MODEL_PATH)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"670e4693dfee14ab056207f8bf048fe5053335b7"},"cell_type":"code","source":"learn.lr_find()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"713eba4b97b9d3fbbccd57efab1af3ab360892d2"},"cell_type":"code","source":"learn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9c410fcb972532e5f79535e548c493649b45b40d"},"cell_type":"code","source":"learn.fit_one_cycle(3, slice(1e-2), pct_start=0.8)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f545e48c625b33b49d78a052f7cb4a706f061b3f"},"cell_type":"code","source":"learn.unfreeze()\nlearn.fit_one_cycle(3, max_lr=slice(1e-6,1e-3), pct_start=0.8)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"032132bf55f562dcc088f4435854f2de4ee16adf"},"cell_type":"code","source":"m = learn.model.eval();","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"43b9ef89e22e18f420b92e8cd6e79b7f3107e300","_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"## was trying to make code modular : TODO\nclass Heatmap:\n    def __init__(self, data, model,xs, ys, idxs):\n        self.data, self.m = data, model\n        # self.ds = data.train_ds if mode == 'train' else data.valid_ds\n        self.xs, self.ys ,self.idxs = xs, ys, idxs\n        # self.xb,_ = self.data.one_item(self.xs)\n\n    def get_hm(self,idx): # this will be called by plot_* func\n        \n        xb_im = Image(self.data.denorm(self.xb)[0]) # this will the image to display\n        self.xb = self.xb.cuda() # pushed the xb to GPU\n        hook_a, hook_g = self.hooked_backward()\n        acts  = hook_a.stored[0].cpu()\n        avg_acts = acts.mean(0)\n        grad = hook_g.stored[0][0].cpu()\n        grad_chan = grad.mean(1).mean(1)\n        hm = (acts * grad_chan[...,None,None]).mean(0)\n        return xb_im, y, hm\n    @classmethod\n    def plot_rand(cls, data, model, mode='train'):\n        pass\n    @classmethod\n    def plot_pos(cls, data, model, count = 4, mode='train'):\n        dtype = DatasetType.Train if mode == 'train' else DatasetType.Valid\n        xs, ys = data.one_batch(dtype, True, True)\n        idxs = np.argwhere(y)[0][:count]\n        return cls(data, model, xs, ys, idxs)\n    @classmethod\n    def plot_neg(cls, data, model, mode='train'):\n        pass\n    \n    def hooked_backward(self):\n        with hook_output(self.m[0]) as hook_a: \n            with hook_output(self.m[0], grad=True) as hook_g:\n                preds = self.m(self.xb)\n                preds[0,int(self.y)].backward()\n        return hook_a,hook_g","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a6fed2c2a13cdd713cc79aab5c9c4bcfe560b6a1","_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"def show_heatmap():\n    _,ax = plt.subplots()\n    xb_im.show(ax)\n    ax.imshow(hm, alpha=0.6, extent=(0,96,96,0),\n              interpolation='bilinear', cmap='magma');","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"58341ae716fded6e45db5e59955b7cf90ea90744","_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"def get_xb_im(idx):\n    x,y = src.valid_ds[idx]\n    xb,_ = src.one_item(x)\n    xb_im = Image(src.denorm(xb)[0])\n    return xb_im,y","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3e8c69b2511717f6e9058edd9237fe7f4a6f095f","_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"def get_hm(idx):\n    x,y = src.train_ds[idx]\n    xb,_ = src.one_item(x)\n    xb = xb.cuda()\n    hook_a,hook_g = hooked_backward(y,m,xb)\n    acts  = hook_a.stored[0].cpu()\n    avg_acts = acts.mean(0)\n    grad = hook_g.stored[0][0].cpu()\n    grad_chan = grad.mean(1).mean(1)\n    hm = (acts*grad_chan[...,None,None]).mean(0)\n    return hm","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c7925e41eacd2f1c22c1e5fc035752f197b56491","_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"def hooked_backward(cat,m,xb):\n    with hook_output(m[0]) as hook_a: \n        with hook_output(m[0], grad=True) as hook_g:\n            preds = m(xb)\n            preds[0,int(cat)].backward()\n    return hook_a,hook_g","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"be3f6861d48154516c46fd16496cb0adbc3dbd54"},"cell_type":"code","source":"def plot(r,c,cmap='magma',figsize=(10,10)):\n    axes = plt.subplots(r, c, figsize=figsize)[1]\n    for i in range(r):\n        for j in range(c): \n            INDEX = np.random.randint(0,1000)\n            hm = get_hm(INDEX)\n            xb_im,y = get_xb_im(INDEX)\n            xb_im.show(axes[i][j])\n            axes[i][j].imshow(hm, alpha=0.6, extent=(0,96,96,0),\n                      interpolation='bilinear', cmap=cmap);\n            axes[i][j].set_title(y)\n            plt.axis('off')\n            plt.tight_layout()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6854b14e65c03c1bb564d5f1ffc942ddff9a27ef"},"cell_type":"code","source":"plot(4,4,figsize=(10,10))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7daac18b434d304ab32a1cfe30afd10cd18637f6"},"cell_type":"code","source":"plot(4,4,'PuOr',figsize=(10,10))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"53cead92ca1534d8e176b696b756f15bacf834aa"},"cell_type":"code","source":"plot(4,4,'plasma')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"925fa777f9fb31e1eedba69eca7e77c1f42868f6"},"cell_type":"code","source":"plot(4,4,'inferno')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e5bfd9b8e172527d82e5e35d96ad8e6013844d4a"},"cell_type":"code","source":"plot(4,4,'cividis')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"64584a6972542a978d993eb9e8f1c01940b888a8"},"cell_type":"code","source":"plot(4,4,'cool')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bba24595fb7e18f50189b0f23c78b7e635496486"},"cell_type":"code","source":"plot(4,4,'hsv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ffca2254a3610148892c097c2f1cd97906e43f4a"},"cell_type":"code","source":"plot(4,4,'hsv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"da7cee16a22ae8d109a2388880fcefc1038954a5"},"cell_type":"code","source":"plot(4,4,'tab20b')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4a0884fc2e98336de32c32a19ec53c834f24e2c4"},"cell_type":"code","source":"plot(4,4,'flag')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"58a5b669522c6b3760291718c3c1e7ecccb24f0e"},"cell_type":"code","source":"plot(4,4,'spring')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"66f493bf7490128baf83e10f0206d3f16aeaf560"},"cell_type":"markdown","source":"try out few more visualization  https://matplotlib.org/tutorials/colors/colormaps.html"},{"metadata":{"trusted":true,"_uuid":"ca545ea669aeb8f850375d74739f72dd3f5c703b"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}