{"cells":[{"metadata":{"trusted":true,"_uuid":"71cc518b516485fbd3ade06bc6998feb49bd4d8f"},"cell_type":"code","source":"!pip3 install --upgrade pip","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ebbd254e2a12662195e770b902ef792598b41e0e"},"cell_type":"code","source":"!pip install --upgrade cmake","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3f62a6361d26c60fa263ea25ae1c555c71235abc"},"cell_type":"code","source":"!pip install -U torch_nightly -f https://download.pytorch.org/whl/nightly/cu90/torch_nightly.html","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"003a3a4f1d663b8b881057897428ba6ad44c77da"},"cell_type":"code","source":"import torch\ntorch.__version__","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"27e464d4fdf9c01fb265e28c053c662d5e8415bd"},"cell_type":"code","source":"!pip3 install git+https://github.com/fastai/fastai.git","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e91c83db5b38b910d6c347a3c7ce0cede7abc05a"},"cell_type":"code","source":"import fastai\nfastai.__version__","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"77cb0010ed12d9f3888cba946379e97ca3aebe96"},"cell_type":"code","source":"#%reload_ext autoreload\n#%autoreload 2\n#%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"692851311916ee174deeb8dee6429d6438bd9635"},"cell_type":"code","source":"!git clone https://github.com/wdhorton/protein-atlas-fastai","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"838d353ce98fcefa6607f7e02105bdf518b5b17b"},"cell_type":"code","source":"!ls /kaggle/working/protein-atlas-fastai","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6c9562053f74415a3136ed7d6a11fe89c7a54edf"},"cell_type":"code","source":"import sys\n # Add directory holding utility functions to path to allow importing utility funcitons\n#sys.path.insert(0, '/kaggle/working/protein-atlas-fastai')\nsys.path.append('/kaggle/working/protein-atlas-fastai')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"241bdef73f1020560a8ddeec339ed48ff1db94e9"},"cell_type":"code","source":"from fastai.vision import * \nfrom fastai import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"172e793f00775fc37c02fc732f6a5b9f69d56fd2"},"cell_type":"code","source":"import cv2\nimport numpy as np\n\nfrom fastai.vision.image import *\n\n\n# adapted from https://www.kaggle.com/iafoss/pretrained-resnet34-with-rgby-0-460-public-lb\ndef open_4_channel(fname):\n    fname = str(fname)\n    # strip extension before adding color\n    if fname.endswith('.png'):\n        fname = fname[:-4]\n    colors = ['red','green','blue','yellow']\n    flags = cv2.IMREAD_GRAYSCALE\n    img = [cv2.imread(fname+'_'+color+'.png', flags).astype(np.float32)/255\n           for color in colors]\n    \n    x = np.stack(img, axis=-1)\n    return Image(pil2tensor(x, np.float32).float())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f9b4a43854678a9f2f39bcc31d743e0baf841ca7"},"cell_type":"code","source":"#import utils","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5cb902d7f5baa35c790460a1c882283408771492"},"cell_type":"code","source":"import os\nfrom pathlib import Path\nimport numpy as np\nimport torch.nn as nn\nimport torch.nn.functional as F\n\nfrom fastai import *\nfrom fastai.vision import *\n\n#from utils import open_4_channel\nfrom resnet import Resnet4Channel","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8e4e3f5791505864ac309078a1642c08a2855fe1"},"cell_type":"code","source":"bs = 16","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fd4d07489a11796a00c28b66745ac8487ba937c8"},"cell_type":"code","source":"path = Path('../input/')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5708a41b652fc2792c78681d854f3c4942c46b24"},"cell_type":"code","source":"df = pd.read_csv(path/'train.csv')\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"18c66831812664e3507faa45f1ddd6d908f05fb6"},"cell_type":"code","source":"np.random.seed(42)\nsrc = (ImageItemList.from_csv(path, 'train.csv', folder='train', suffix='.png',num_worker=0)\n       .random_split_by_pct(0.2)\n       .label_from_df(sep=' ',  classes=[str(i) for i in range(28)]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"28fcff07591c04605d83bd226d116a806e9dc688"},"cell_type":"code","source":"src.train.x.create_func = open_4_channel\nsrc.train.x.open = open_4_channel","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6aac0dab2a801bf9a5e108ab67d50e89ddaef387"},"cell_type":"code","source":"src.valid.x.create_func = open_4_channel\nsrc.valid.x.open = open_4_channel","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ced60c0de504ce9936f85ef096aac1622121cb85"},"cell_type":"code","source":"test_ids = list(sorted({fname.split('_')[0] for fname in os.listdir(path/'test')}))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"09b92c4582a091ee5d1d83f591cda5a94b71c8af"},"cell_type":"code","source":"test_fnames = [path/'test'/test_id for test_id in test_ids]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5d75083032ce75c538a10798414b74759ac50bd1"},"cell_type":"code","source":"test_fnames[:5]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"58f07b737695c7984785b0afec082cc466196214"},"cell_type":"code","source":"src.add_test(test_fnames, label='0');","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"db0dd084c8c74b1188f4a41a37c7ead95d4139a8"},"cell_type":"code","source":"src.test.x.create_func = open_4_channel\nsrc.test.x.open = open_4_channel","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"257179044305161c8a284153f4abee5afc3f06fb"},"cell_type":"code","source":"protein_stats = ([0.08069, 0.05258, 0.05487, 0.08282], [0.13704, 0.10145, 0.15313, 0.13814])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e2e5ce461df010e81b9cdabbc95916642edded31"},"cell_type":"code","source":"trn_tfms,_ = get_transforms(do_flip=True, flip_vert=True, max_rotate=30., max_zoom=1,\n                      max_lighting=0.05, max_warp=0.)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"608d680630d70f96d44c2c8d80df1c3465c3459d"},"cell_type":"code","source":"data = (src.transform((trn_tfms, _), size=224).databunch().normalize(protein_stats))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2e099fc7beac51c8765561a486e548a47846450d","scrolled":true},"cell_type":"code","source":"data.show_batch(rows=3, figsize=(12,9))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"86e154c45fdb771f414c95d6b47217e6d530c98c"},"cell_type":"code","source":"def resnet50(pretrained):\n    return Resnet4Channel(encoder_depth=50)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0435d8d113631be0398ed47dcc55fee980c4e83e"},"cell_type":"code","source":"# copied from https://github.com/fastai/fastai/blob/master/fastai/vision/learner.py\ndef _resnet_split(m): return (m[0][6],m[1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2b87fa9530667e406b185e55707e8dbc1e41a9d6"},"cell_type":"code","source":"f1_score = partial(fbeta, thresh=0.2, beta=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1ea83669ede84796ca74965cae4869bfd5191948"},"cell_type":"code","source":"TMP_PATH = \"/tmp/tmp\"\nMODEL_PATH = \"/tmp/model/\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4fbcdac07be83d43d1191c7aa9516cc0065ae486"},"cell_type":"code","source":"learn = create_cnn(\n    data,\n    resnet50,\n    cut=-2,\n    split_on=_resnet_split,\n    loss_func=F.binary_cross_entropy_with_logits,\n    path=path,    \n    metrics=[f1_score], \n    #temp_dir=TMP_PATH, \n    model_dir=MODEL_PATH\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2a577a78eb83a9c13b1c63b6300a4e2259334731"},"cell_type":"code","source":"learn.lr_find()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f3357c1ebe2ca7109c0a7f8bdf814632701be54e"},"cell_type":"code","source":"learn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"35f6afe1d281ccc93b6a03d2675f4aba66c4065e"},"cell_type":"code","source":"lr = 3e-2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c380500596843d03a74ad077f1b039b04edcbca6"},"cell_type":"code","source":"learn.fit_one_cycle(5, slice(lr))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"84d07ca2f3b2eeabad24020f57187bc1ca346a70"},"cell_type":"code","source":"learn.save('stage-1-rn50-datablocks')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8fa2703ec6b8155a6c95e4935597701ba342b43d"},"cell_type":"code","source":"learn.unfreeze()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7858fea704fd12f7d5938bbd8fb1f189bfc1f68e"},"cell_type":"code","source":"learn.lr_find()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6a2fe818837225784769db90c5a162a7c1571b77"},"cell_type":"code","source":"learn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"42073be5f9cb261a7cd2511aab9ee415396d78a1"},"cell_type":"code","source":"learn.fit_one_cycle(15, slice(3e-5, lr/5))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e3a745e9dd95b733cb7cdf5d4de0f9e2af90cb16"},"cell_type":"code","source":"learn.save('stage-2-rn50')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"164cb73f0a0b2623a5a89cb4355c7a0e0812e7bc"},"cell_type":"code","source":"preds,_ = learn.get_preds(DatasetType.Test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f6357e5b515b1b092c5e35a3d94e628cbb390269"},"cell_type":"code","source":"pred_labels = [' '.join(list([str(i) for i in np.nonzero(row>0.2)[0]])) for row in np.array(preds)]\ndf = pd.DataFrame({'Id':test_ids,'Predicted':pred_labels})\ndf.to_csv(path/'protein_predictions_datablocks.csv', header=True, index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1829a50aeda3c5667c6c9841bfd1127ef9a89db9"},"cell_type":"code","source":"!rm -rf /kaggle/working/protein-atlas-fastai","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a2b136e1827c198c056bb98b36b75fc41730935e"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b37db3b50d646dfa0e06f8896f0bf8e9e8a1a78d"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"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"},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"}},"nbformat":4,"nbformat_minor":1}