{"cells":[{"metadata":{"_uuid":"cb801f365b015b725c3cb9ea682413604cf5710e"},"cell_type":"markdown","source":"## Multi-label classification"},{"metadata":{"collapsed":true,"_uuid":"bb4f2252d036204cf9cb82618a10d9137fdee51c","trusted":true},"cell_type":"code","source":"%reload_ext autoreload\n%autoreload 2\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"70150ab2e40e0b0092084d33e6898f2202b66c3b","trusted":true},"cell_type":"code","source":"from fastai.conv_learner import *","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"2e83715fb1714e023e3602ac0b20db6493a836c0","trusted":true},"cell_type":"code","source":"PATH = '../input/planet-understanding-the-amazon-from-space/'","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b7f481a8de2b1d3b3a9b9abb91e19619d1df913e","trusted":true,"collapsed":true},"cell_type":"code","source":"ls {PATH}","execution_count":null,"outputs":[]},{"metadata":{"heading_collapsed":true,"_uuid":"42186df5fcea2b68cf4f0b5f2dcf96ee8dbf057c"},"cell_type":"markdown","source":"## Multi-label versus single-label classification"},{"metadata":{"collapsed":true,"hidden":true,"_uuid":"ee251ce78c0236d67ca000ab76af794c099dd24f","trusted":true},"cell_type":"code","source":"from fastai.plots import *","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"hidden":true,"_uuid":"90fa5f8675358a36fc2fc0305463ef33a579bf1a","trusted":true},"cell_type":"code","source":"def get_1st(path, pattern): return glob(f'{path}/*{pattern}.*')[0]","execution_count":null,"outputs":[]},{"metadata":{"hidden":true,"_uuid":"173e9342feb80cffb74e90b2f17e6f02d376e13b","trusted":true,"collapsed":true},"cell_type":"code","source":"dc_path = \"../input/dogs-vs-cats-redux-kernels-edition/train\"\nlist_paths = [get_1st(f\"{dc_path}\", \"cat\"), get_1st(f\"{dc_path}\", \"dog\")]\nplots_from_files(list_paths, titles=[\"cat\", \"dog\"], maintitle=\"Single-label classification\")","execution_count":null,"outputs":[]},{"metadata":{"hidden":true,"_uuid":"c7730b949ef2e0957b183dc9302daa729c581158"},"cell_type":"markdown","source":"In single-label classification each sample belongs to one class. In the previous example, each image is either a *dog* or a *cat*."},{"metadata":{"hidden":true,"scrolled":false,"_uuid":"8476ba529928ff6ab6d8f76c2e0b05a6ba7d5dac","trusted":true,"collapsed":true},"cell_type":"code","source":"list_paths = [f\"{PATH}train-jpg/train_0.jpg\", f\"{PATH}train-jpg/train_1.jpg\"]\ntitles=[\"haze primary\", \"agriculture clear primary water\"]\nplots_from_files(list_paths, titles=titles, maintitle=\"Multi-label classification\")","execution_count":null,"outputs":[]},{"metadata":{"hidden":true,"_uuid":"c815574d1c2e444bab05c7c289b47a7bc4249f90"},"cell_type":"markdown","source":"In multi-label classification each sample can belong to one or more clases. In the previous example, the first images belongs to two clases: *haze* and *primary*. The second image belongs to four clases: *agriculture*, *clear*, *primary* and  *water*."},{"metadata":{"_uuid":"59441765329bb4fc465e685cc0ddb441269b493e"},"cell_type":"markdown","source":"## Multi-label models for Planet dataset"},{"metadata":{"trusted":true,"_uuid":"05bb1cba6c4b524036c95b8d90b914d5a66538ba","collapsed":true},"cell_type":"code","source":"# planet.py\n\nfrom fastai.imports import *\nfrom fastai.transforms import *\nfrom fastai.dataset import *\nfrom sklearn.metrics import fbeta_score\nimport warnings\n\ndef f2(preds, targs, start=0.17, end=0.24, step=0.01):\n    with warnings.catch_warnings():\n        warnings.simplefilter(\"ignore\")\n        return max([fbeta_score(targs, (preds>th), 2, average='samples')\n                    for th in np.arange(start,end,step)])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"82fe8033a32d4a9da0628b5163b8b7ed89240129","trusted":true,"collapsed":true},"cell_type":"code","source":"metrics=[f2]\nf_model = resnet34","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"37bf786c80690e57bfb84c6fe8e2c677b6f21934","trusted":true},"cell_type":"code","source":"label_csv = f'{PATH}train_v2.csv'\nn = len(list(open(label_csv)))-1\nval_idxs = get_cv_idxs(n)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2eeaa9efa705d4a75a6c85f2a0bfa10c47defad5"},"cell_type":"markdown","source":"We use a different set of data augmentations for this dataset - we also allow vertical flips, since we don't expect vertical orientation of satellite images to change our classifications."},{"metadata":{"collapsed":true,"_uuid":"8b2f4fd049b1fc9a1cc48e5aebf75b09dd815696","trusted":true},"cell_type":"code","source":"def get_data(sz):\n    tfms = tfms_from_model(f_model, sz, aug_tfms=transforms_top_down, max_zoom=1.05)\n    return ImageClassifierData.from_csv(PATH, 'train-jpg', label_csv, tfms=tfms,\n                    suffix='.jpg', val_idxs=val_idxs, test_name='test-jpg-v2')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6f4c614156cd1be629cb1a230c354c010f963f46","trusted":true,"collapsed":true},"cell_type":"code","source":"data = get_data(256)","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"eda4fc22c342950dd2cfae6b53d61969ff569312","trusted":true},"cell_type":"code","source":"x,y = next(iter(data.val_dl))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"27f041a0e1432ee9ad8cc73479d10ec40b3de093","trusted":true,"collapsed":true},"cell_type":"code","source":"y","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b4f339c905b63a40586ce6fae80bb9aa41de63f2","trusted":true,"collapsed":true},"cell_type":"code","source":"list(zip(data.classes, y[0]))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3d6d18c21aa6efa45830e5f24c239180a3aff2cd","trusted":true,"collapsed":true},"cell_type":"code","source":"plt.imshow(data.val_ds.denorm(to_np(x))[0]*1.4);","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"4300352c83c5df0d9bd64932ca84e3d362ed78c4","trusted":true},"cell_type":"code","source":"sz=64","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"6d2cdc35703739324ab66672e1a93fc7a20bb92c","trusted":true},"cell_type":"code","source":"data = get_data(sz)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0ee8bd89b186d02fa2c5fe8ae7f5ff1d715c565f","trusted":true,"collapsed":true},"cell_type":"code","source":"data = data.resize(int(sz*1.3), '/tmp')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"717e710b865cf4e9a18e96c53a85e06d16f6918c"},"cell_type":"code","source":"TMP_PATH = \"/tmp/tmp\"\nMODEL_PATH = \"/tmp/model/\"","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a5b4ff11cb0b49c126a85cc2aa1cbd283c86ab83","trusted":true,"collapsed":true},"cell_type":"code","source":"learn = ConvLearner.pretrained(f_model, data, metrics=metrics, tmp_name=TMP_PATH, models_name=MODEL_PATH)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"99cfa9fd6c841aaab8d662adfd22b55e4bb3ebe6","trusted":true,"collapsed":true},"cell_type":"code","source":"lrf=learn.lr_find()\nlearn.sched.plot()","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"e2650056d2536d546c325150ff821b6a9554ad00","trusted":true},"cell_type":"code","source":"lr = 0.2","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"780c3c98697f863e601f85b5b728dc9394ff71df","trusted":true,"collapsed":true},"cell_type":"code","source":"learn.fit(lr, 3, cycle_len=1, cycle_mult=2)","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"e0de7d3847aade10c5265dfbb09f2f5d5ada4893","trusted":true},"cell_type":"code","source":"lrs = np.array([lr/9,lr/3,lr])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"66a9551bb8a8726a8a7bf1fdad19ad8e146dc4f7","trusted":true,"collapsed":true},"cell_type":"code","source":"learn.unfreeze()\nlearn.fit(lrs, 3, cycle_len=1, cycle_mult=2)","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"a7b9694fbd0394c2f4a8291e58e14e4656db8e35","trusted":true},"cell_type":"code","source":"learn.save(f'{sz}')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"36ed2f33f6ce0316060e88b6bbce8ebe28183b7c","trusted":true,"collapsed":true},"cell_type":"code","source":"learn.sched.plot_loss()","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"9f8e75b37e8cb8b5178a0e0ff6c9aac133ad6a40","trusted":true},"cell_type":"code","source":"sz=128","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"191c2644c634b04f4f5aa6460d3930574815c37d","trusted":true,"collapsed":true},"cell_type":"code","source":"learn.set_data(get_data(sz))\nlearn.freeze()\nlearn.fit(lr, 3, cycle_len=1, cycle_mult=2)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ff6b763ad6b94d674fa615501e9ebfe2811e11e0","trusted":true,"collapsed":true},"cell_type":"code","source":"learn.unfreeze()\nlearn.fit(lrs, 3, cycle_len=1, cycle_mult=2)\nlearn.save(f'{sz}')","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"44474d316906b787f56a5d78c3a2c4c48d2c207b","trusted":true},"cell_type":"code","source":"sz=256","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2b44daa7eb36bf618de162a2dca4753525e17bc4","trusted":true,"collapsed":true},"cell_type":"code","source":"learn.set_data(get_data(sz))\nlearn.freeze()\nlearn.fit(lr, 3, cycle_len=1, cycle_mult=2)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0b1085e6bc12f28ea156dab2cbd4fb6d8d4ab154","trusted":true,"collapsed":true},"cell_type":"code","source":"learn.unfreeze()\nlearn.fit(lrs, 3, cycle_len=1, cycle_mult=2)\nlearn.save(f'{sz}')","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"59b72838c54394c811f956461818ddf9a57cf000","trusted":true},"cell_type":"code","source":"multi_preds, y = learn.TTA()\npreds = np.mean(multi_preds, 0)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"111c9d1c456c3e0cf8e106748abaf3111c2714c3","trusted":true,"collapsed":true},"cell_type":"code","source":"f2(preds,y)","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"2eb8468d7f88a973ff52f8d699f7608b52c90827"},"cell_type":"markdown","source":"### End"},{"metadata":{"collapsed":true,"_uuid":"8a29561c143e7df0f3dd4ab606decc69453d7aca","trusted":false},"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"},"toc":{"colors":{"hover_highlight":"#DAA520","navigate_num":"#000000","navigate_text":"#333333","running_highlight":"#FF0000","selected_highlight":"#FFD700","sidebar_border":"#EEEEEE","wrapper_background":"#FFFFFF"},"moveMenuLeft":true,"nav_menu":{"height":"99px","width":"252px"},"navigate_menu":true,"number_sections":true,"sideBar":true,"threshold":4,"toc_cell":false,"toc_section_display":"block","toc_window_display":false,"widenNotebook":false}},"nbformat":4,"nbformat_minor":1}