{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"markdown","source":"# Multilabel Classification"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true,"collapsed":true},"cell_type":"code","source":"\n%reload_ext autoreload\n%autoreload 2\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"44511790c42ea10851ce2ff4aef085931b817786","collapsed":true},"cell_type":"code","source":"from fastai.conv_learner import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e2f3202aee8eda8cb52ecefc34ec50503dcd1371","collapsed":true},"cell_type":"code","source":"PATH = '../input/planet-understanding-the-amazon-from-space/'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d710c98a2622406599fb493edfeff2258e9ff071"},"cell_type":"code","source":"ls {PATH}","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6a8745b33f43e3288bcd459f8ceac73460953078"},"cell_type":"markdown","source":"# Multi-label vs single-label classification"},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"e6a04a6428c00e8d0e5b83c8ee81ab0ab3d0c4dd"},"cell_type":"code","source":"from fastai.plots import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"43ad76570330e719d5885b05ac6a78ff715665a5"},"cell_type":"code","source":"def get_1st(path, pattern): return glob(f'{path}/*{pattern}.*')[2]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b7f0928fdc252946102ace69bf194b1da796eebd"},"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":{"trusted":true,"_uuid":"a0efa68554877f6fe52084bd1c6518d115d231b7"},"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":{"_uuid":"a6933299e53e50a01d78c4c8608cc997cc9d2462"},"cell_type":"markdown","source":"The first image belongs to two classes: [haze, primary] and while the second image has four: [agriculture clear primary water]"},{"metadata":{"_uuid":"03cf43bbce02b4b35bc9fe66cf57fcde23a681de"},"cell_type":"markdown","source":"# Multi-label models for Planet Dataset"},{"metadata":{"trusted":true,"_uuid":"f88172d43c8e416d1ee60aefab80de2cf676b556"},"cell_type":"code","source":"# the planet.py file\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":{"trusted":true,"collapsed":true,"_uuid":"aa809bb8eeb033a42e7390b11e09142cc92b34cb"},"cell_type":"code","source":"metrics=[f2]\nf_model = resnet34","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"8ded9befea1a2b4f9da7c34b5ce27c08815d8a3e"},"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":"73325356aba5f88382b94c245a6754df10d58a17"},"cell_type":"markdown","source":"Data augmentation step"},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"9dd022c748441a07a9cdae9bc57c3ddbc3e22691"},"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":{"trusted":true,"collapsed":true,"_uuid":"64bcd440cf57ee3d0374cca39c68419a90d09f4d"},"cell_type":"code","source":"data = get_data(256)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"266481158936caf1f4337fd16de53e59a096f65e"},"cell_type":"code","source":"x,y = next(iter(data.val_dl))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4e622961559883ee8847611030d7dcb8a5d3822a"},"cell_type":"code","source":"y","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"28dfd40514ee28edb7b054b86e56e44014a4c992"},"cell_type":"code","source":"list(zip(data.classes, y[0]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"65f95368eea5503e9f4d67930b6e942bfc0448a9"},"cell_type":"code","source":"plt.imshow(data.val_ds.denorm(to_np(x))[0]*1.4)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"1fd184b247ffac0ce5915773f7528e11868a449d"},"cell_type":"code","source":"sz=64","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"aba7939958ea9cda968c53fb1d8e1a3ecfc3b28f"},"cell_type":"code","source":"data = get_data(sz)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"373aaf8664b88c7a863b5b82fe58b1997326cb13"},"cell_type":"code","source":"#this will go through our images and resize them\ndata = data.resize(int(sz*1.3), '/tmp')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"4278cdb20702f06a40b39d79ed1df02d5b8a8e62"},"cell_type":"code","source":"TMP_PATH = \"/tmp/tmp\"\nMODEL_PATH =\"/tmp/model/\"","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"5241b2b94fd8adeb6f5e996bf06406dd8ae78370"},"cell_type":"markdown","source":"In this section we are trying to find the learning rate & setting up a pretrained model"},{"metadata":{"trusted":true,"_uuid":"6fc25f7ad9aa6d165d0acc384b77cc74f2c49a1b"},"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":{"trusted":true,"_uuid":"9d807c7f2330e93ad5f038a498309abb6ecc31a2"},"cell_type":"code","source":"lrf=learn.lr_find()\nlearn.sched.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"9dd2e736414131a2ed7c28dc7f399dfaae53fb8f"},"cell_type":"code","source":"lr = 0.2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6b62673ac56ecbd2a5d925093da9cb5d45e40d48"},"cell_type":"code","source":"learn.fit(lr, 3, cycle_len=1, cycle_mult=2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"6f429a5ca948ec6f3044f10ab6771ed1307a02b8"},"cell_type":"code","source":"# we are now using differential learning rates\nlrs = np.array([lr/9,lr/3,lr])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9a3af7dfdbb8c5c3b70d7d2b34a70a539914b9cf"},"cell_type":"code","source":"#unfreeze the previous layers so we can use our diff learning rates \nlearn.unfreeze()\nlearn.fit(lrs, 3, cycle_len=1, cycle_mult=2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"0641224936dc5f6ad3d3f6d0ba19a1543bcb3136"},"cell_type":"code","source":"learn.save(f'{sz}')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3264c279474d21b8cbcd537ab2b11e1ee23dfaa4"},"cell_type":"code","source":"learn.sched.plot_loss()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"48d249f67821f6f700fdc83c236f47f9af05bc8d"},"cell_type":"code","source":"sz=128","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"b5d73661607d62ed29f6ef30c39b1e5c00b8faba"},"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":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}