{"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":"## Multi-label classification","metadata":{"_uuid":"cb801f365b015b725c3cb9ea682413604cf5710e"}},{"cell_type":"code","source":"%reload_ext autoreload\n%autoreload 2\n%matplotlib inline","metadata":{"_uuid":"bb4f2252d036204cf9cb82618a10d9137fdee51c","execution":{"iopub.status.busy":"2022-02-17T19:23:55.691483Z","iopub.execute_input":"2022-02-17T19:23:55.691867Z","iopub.status.idle":"2022-02-17T19:23:55.750672Z","shell.execute_reply.started":"2022-02-17T19:23:55.691782Z","shell.execute_reply":"2022-02-17T19:23:55.749933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.conv_learner import *","metadata":{"_uuid":"70150ab2e40e0b0092084d33e6898f2202b66c3b","execution":{"iopub.status.busy":"2022-02-17T19:23:58.481253Z","iopub.execute_input":"2022-02-17T19:23:58.481563Z","iopub.status.idle":"2022-02-17T19:24:00.189416Z","shell.execute_reply.started":"2022-02-17T19:23:58.481511Z","shell.execute_reply":"2022-02-17T19:24:00.18863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PATH = '../input/planet-understanding-the-amazon-from-space/'","metadata":{"_uuid":"2e83715fb1714e023e3602ac0b20db6493a836c0","execution":{"iopub.status.busy":"2022-02-17T19:24:02.213431Z","iopub.execute_input":"2022-02-17T19:24:02.213739Z","iopub.status.idle":"2022-02-17T19:24:02.244989Z","shell.execute_reply.started":"2022-02-17T19:24:02.213683Z","shell.execute_reply":"2022-02-17T19:24:02.2442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls {PATH}","metadata":{"_uuid":"b7f481a8de2b1d3b3a9b9abb91e19619d1df913e","execution":{"iopub.status.busy":"2022-02-17T19:24:04.053414Z","iopub.execute_input":"2022-02-17T19:24:04.053705Z","iopub.status.idle":"2022-02-17T19:24:04.753321Z","shell.execute_reply.started":"2022-02-17T19:24:04.053654Z","shell.execute_reply":"2022-02-17T19:24:04.752591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Multi-label versus single-label classification","metadata":{"heading_collapsed":true,"_uuid":"42186df5fcea2b68cf4f0b5f2dcf96ee8dbf057c"}},{"cell_type":"code","source":"from fastai.plots import *","metadata":{"hidden":true,"_uuid":"ee251ce78c0236d67ca000ab76af794c099dd24f","execution":{"iopub.status.busy":"2022-02-17T19:24:10.402Z","iopub.execute_input":"2022-02-17T19:24:10.402319Z","iopub.status.idle":"2022-02-17T19:24:10.435637Z","shell.execute_reply.started":"2022-02-17T19:24:10.402254Z","shell.execute_reply":"2022-02-17T19:24:10.434853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_1st(path, pattern): return glob(f'{path}/*{pattern}.*')[0]","metadata":{"hidden":true,"_uuid":"90fa5f8675358a36fc2fc0305463ef33a579bf1a","execution":{"iopub.status.busy":"2022-02-17T19:26:22.142039Z","iopub.execute_input":"2022-02-17T19:26:22.142343Z","iopub.status.idle":"2022-02-17T19:26:22.171632Z","shell.execute_reply.started":"2022-02-17T19:26:22.142285Z","shell.execute_reply":"2022-02-17T19:26:22.170822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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\")","metadata":{"hidden":true,"_uuid":"173e9342feb80cffb74e90b2f17e6f02d376e13b","execution":{"iopub.status.busy":"2022-02-17T19:26:25.485429Z","iopub.execute_input":"2022-02-17T19:26:25.485722Z","iopub.status.idle":"2022-02-17T19:26:25.603919Z","shell.execute_reply.started":"2022-02-17T19:26:25.485667Z","shell.execute_reply":"2022-02-17T19:26:25.602684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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,"_uuid":"c7730b949ef2e0957b183dc9302daa729c581158"}},{"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\")","metadata":{"hidden":true,"_uuid":"8476ba529928ff6ab6d8f76c2e0b05a6ba7d5dac","execution":{"iopub.status.busy":"2022-02-17T19:25:15.516396Z","iopub.execute_input":"2022-02-17T19:25:15.516693Z","iopub.status.idle":"2022-02-17T19:25:15.702031Z","shell.execute_reply.started":"2022-02-17T19:25:15.516635Z","shell.execute_reply":"2022-02-17T19:25:15.700655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"hidden":true,"_uuid":"c815574d1c2e444bab05c7c289b47a7bc4249f90"}},{"cell_type":"markdown","source":"## Multi-label models for Planet dataset","metadata":{"_uuid":"59441765329bb4fc465e685cc0ddb441269b493e"}},{"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)])","metadata":{"_uuid":"05bb1cba6c4b524036c95b8d90b914d5a66538ba","execution":{"iopub.status.busy":"2022-02-17T19:25:50.013786Z","iopub.execute_input":"2022-02-17T19:25:50.014078Z","iopub.status.idle":"2022-02-17T19:25:50.04986Z","shell.execute_reply.started":"2022-02-17T19:25:50.014027Z","shell.execute_reply":"2022-02-17T19:25:50.04909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metrics=[f2]\nf_model = resnet34","metadata":{"_uuid":"82fe8033a32d4a9da0628b5163b8b7ed89240129","execution":{"iopub.status.busy":"2022-02-17T19:27:14.520655Z","iopub.execute_input":"2022-02-17T19:27:14.520969Z","iopub.status.idle":"2022-02-17T19:27:14.550253Z","shell.execute_reply.started":"2022-02-17T19:27:14.520918Z","shell.execute_reply":"2022-02-17T19:27:14.549491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_csv = f'{PATH}train_v2.csv'\nn = len(list(open(label_csv)))-1\nval_idxs = get_cv_idxs(n)","metadata":{"_uuid":"37bf786c80690e57bfb84c6fe8e2c677b6f21934","execution":{"iopub.status.busy":"2022-02-17T19:27:17.385615Z","iopub.execute_input":"2022-02-17T19:27:17.385898Z","iopub.status.idle":"2022-02-17T19:27:17.430153Z","shell.execute_reply.started":"2022-02-17T19:27:17.38585Z","shell.execute_reply":"2022-02-17T19:27:17.429255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"_uuid":"2eeaa9efa705d4a75a6c85f2a0bfa10c47defad5"}},{"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')","metadata":{"_uuid":"8b2f4fd049b1fc9a1cc48e5aebf75b09dd815696","execution":{"iopub.status.busy":"2022-02-17T19:27:34.69802Z","iopub.execute_input":"2022-02-17T19:27:34.698298Z","iopub.status.idle":"2022-02-17T19:27:34.728298Z","shell.execute_reply.started":"2022-02-17T19:27:34.698249Z","shell.execute_reply":"2022-02-17T19:27:34.727572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = get_data(256)","metadata":{"_uuid":"6f4c614156cd1be629cb1a230c354c010f963f46","execution":{"iopub.status.busy":"2022-02-17T19:27:43.646949Z","iopub.execute_input":"2022-02-17T19:27:43.64724Z","iopub.status.idle":"2022-02-17T19:27:43.691355Z","shell.execute_reply.started":"2022-02-17T19:27:43.647188Z","shell.execute_reply":"2022-02-17T19:27:43.690046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x,y = next(iter(data.val_dl))","metadata":{"_uuid":"eda4fc22c342950dd2cfae6b53d61969ff569312","execution":{"iopub.status.busy":"2022-02-17T19:28:59.481941Z","iopub.execute_input":"2022-02-17T19:28:59.482247Z","iopub.status.idle":"2022-02-17T19:28:59.519467Z","shell.execute_reply.started":"2022-02-17T19:28:59.482193Z","shell.execute_reply":"2022-02-17T19:28:59.518392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y","metadata":{"_uuid":"27f041a0e1432ee9ad8cc73479d10ec40b3de093","execution":{"iopub.status.busy":"2022-02-17T19:28:57.253819Z","iopub.execute_input":"2022-02-17T19:28:57.254122Z","iopub.status.idle":"2022-02-17T19:28:57.291213Z","shell.execute_reply.started":"2022-02-17T19:28:57.254066Z","shell.execute_reply":"2022-02-17T19:28:57.290134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list(zip(data.classes, y[0]))","metadata":{"_uuid":"b4f339c905b63a40586ce6fae80bb9aa41de63f2","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(data.val_ds.denorm(to_np(x))[0]*1.4);","metadata":{"_uuid":"3d6d18c21aa6efa45830e5f24c239180a3aff2cd","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sz=64","metadata":{"_uuid":"4300352c83c5df0d9bd64932ca84e3d362ed78c4","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = get_data(sz)","metadata":{"_uuid":"6d2cdc35703739324ab66672e1a93fc7a20bb92c","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = data.resize(int(sz*1.3), '/tmp')","metadata":{"_uuid":"0ee8bd89b186d02fa2c5fe8ae7f5ff1d715c565f","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TMP_PATH = \"/tmp/tmp\"\nMODEL_PATH = \"/tmp/model/\"","metadata":{"_uuid":"717e710b865cf4e9a18e96c53a85e06d16f6918c","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = ConvLearner.pretrained(f_model, data, metrics=metrics, tmp_name=TMP_PATH, models_name=MODEL_PATH)","metadata":{"_uuid":"a5b4ff11cb0b49c126a85cc2aa1cbd283c86ab83","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lrf=learn.lr_find()\nlearn.sched.plot()","metadata":{"_uuid":"99cfa9fd6c841aaab8d662adfd22b55e4bb3ebe6","collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr = 0.2","metadata":{"_uuid":"e2650056d2536d546c325150ff821b6a9554ad00","collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit(lr, 3, cycle_len=1, cycle_mult=2)","metadata":{"_uuid":"780c3c98697f863e601f85b5b728dc9394ff71df","collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lrs = np.array([lr/9,lr/3,lr])","metadata":{"_uuid":"e0de7d3847aade10c5265dfbb09f2f5d5ada4893","collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.unfreeze()\nlearn.fit(lrs, 3, cycle_len=1, cycle_mult=2)","metadata":{"_uuid":"66a9551bb8a8726a8a7bf1fdad19ad8e146dc4f7","collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.save(f'{sz}')","metadata":{"_uuid":"a7b9694fbd0394c2f4a8291e58e14e4656db8e35","collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.sched.plot_loss()","metadata":{"_uuid":"36ed2f33f6ce0316060e88b6bbce8ebe28183b7c","collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sz=128","metadata":{"_uuid":"9f8e75b37e8cb8b5178a0e0ff6c9aac133ad6a40","collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.set_data(get_data(sz))\nlearn.freeze()\nlearn.fit(lr, 3, cycle_len=1, cycle_mult=2)","metadata":{"_uuid":"191c2644c634b04f4f5aa6460d3930574815c37d","collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.unfreeze()\nlearn.fit(lrs, 3, cycle_len=1, cycle_mult=2)\nlearn.save(f'{sz}')","metadata":{"_uuid":"ff6b763ad6b94d674fa615501e9ebfe2811e11e0","collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sz=256","metadata":{"_uuid":"44474d316906b787f56a5d78c3a2c4c48d2c207b","collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.set_data(get_data(sz))\nlearn.freeze()\nlearn.fit(lr, 3, cycle_len=1, cycle_mult=2)","metadata":{"_uuid":"2b44daa7eb36bf618de162a2dca4753525e17bc4","collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.unfreeze()\nlearn.fit(lrs, 3, cycle_len=1, cycle_mult=2)\nlearn.save(f'{sz}')","metadata":{"_uuid":"0b1085e6bc12f28ea156dab2cbd4fb6d8d4ab154","collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"multi_preds, y = learn.TTA()\npreds = np.mean(multi_preds, 0)","metadata":{"_uuid":"59b72838c54394c811f956461818ddf9a57cf000","collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f2(preds,y)","metadata":{"_uuid":"111c9d1c456c3e0cf8e106748abaf3111c2714c3","collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### End","metadata":{"_uuid":"2eb8468d7f88a973ff52f8d699f7608b52c90827"}},{"cell_type":"code","source":"","metadata":{"_uuid":"8a29561c143e7df0f3dd4ab606decc69453d7aca","collapsed":true,"jupyter":{"outputs_hidden":true}},"execution_count":null,"outputs":[]}]}