{"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":"from fastai.vision.all import *","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_full = pd.read_csv('../input/bms-molecular-translation/train_labels.csv')[:10_000]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_full","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! pip install chemparse","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import chemparse\n\ndf = df_full.join(\n    pd.json_normalize(df_full['InChI'].str.split('/',expand=True)[1].apply(chemparse.parse_formula)))\ndf.drop('InChI',axis=1,inplace=True)\ndf.head(10)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.fillna(0,inplace=True)\ndf","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[df.N>=1]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dblock = DataBlock(\n    blocks = (ImageBlock,RegressionBlock),\n    get_x = lambda x : f'../input/drawing-molecules-with-rdkit-inchi-to-png/data/{x[\"image_id\"]}.png',\n    get_y = lambda x : x[\"N\"]\n)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = dblock.dataloaders(df)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = cnn_learner(dls,resnet18)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(3,lr_max =3e-2)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Hook():\n    def __init__(self, m):\n        self.hook = m.register_forward_hook(self.hook_func)   \n    def hook_func(self, m, i, o): self.stored = o.detach().clone()\n    def __enter__(self, *args): return self\n    def __exit__(self, *args): self.hook.remove()\n\nclass HookBwd():\n    def __init__(self, m):\n        self.hook = m.register_backward_hook(self.hook_func)   \n    def hook_func(self, m, gi, go): self.stored = go[0].detach().clone()\n    def __enter__(self, *args): return self\n    def __exit__(self, *args): self.hook.remove()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def gradcam(path):\n    img = PILImage.create(path)\n    x, = first(dls.test_dl([img]))\n\n    with HookBwd(learn.model[0]) as hookg:\n        with Hook(learn.model[0]) as hook:\n            output = learn.model.eval()(x.cuda())\n            act = hook.stored\n        output.backward()\n        grad = hookg.stored\n\n    w = grad[0].mean(dim=[1,2], keepdim=True)\n    cam_map = (w * act[0]).sum(0)\n\n    x_dec = TensorImage(dls.train.decode((x,))[0][0])\n    _,ax = plt.subplots()\n    x_dec.show(ctx=ax)\n    ax.imshow(cam_map.detach().cpu(), alpha=0.6, extent=(0,512,512,0),\n                  interpolation='bilinear', cmap='magma');","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files = get_image_files('../input/drawing-molecules-with-rdkit-inchi-to-png')\ngradcam(files[222])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gradcam(files[3321])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}