{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"collapsed":true},"cell_type":"code","source":"%reload_ext autoreload\n%autoreload 2\n%matplotlib inline\nfrom fastai.conv_learner import *\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"789e4bb3c35d2be0e732ac8bb0599139a8873c16"},"cell_type":"code","source":"torch.cuda.is_available()","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"torch.backends.cudnn.enabled","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b286bc6d42a07e2638177f60ffb7f2d8673377c1","collapsed":true},"cell_type":"code","source":"INPUT_PATH = '../input/airbus-ship-detection/'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2c32bd751181e77fce1defd7cb0e8d30dd84a601","collapsed":true},"cell_type":"code","source":"TRAIN = os.path.join(INPUT_PATH, 'train')\nTEST = os.path.join(INPUT_PATH, 'test')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"37f5ec924e131058fd2df3be476862603aa833a7"},"cell_type":"code","source":"!mkdir tmp\n!mkdir model\nTMP = '/kaggle/working/tmp'\nMODEL = '/kaggle/working/model'\n!ls","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4c730cecbffc30e80fa77350beb9bdc9b9ba91b5"},"cell_type":"code","source":"masks = pd.read_csv(os.path.join(INPUT_PATH, 'train_ship_segmentations.csv'))\nmasks.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"8482970f96bac929149aa25041d209f503437627"},"cell_type":"code","source":"def is_boat(s):\n  s = str(s)\n  if len(s)>0 and ('nan' not in str.lower(s)):\n    return 1\n  else: return 0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"231b7109d13a41a1fe869f6bdcabe0327517e305"},"cell_type":"code","source":"masks['EncodedPixels']=masks['EncodedPixels'].apply(is_boat)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0f78e3e7cc4f82e1f9f43b4fa932619889524327"},"cell_type":"code","source":"masks.drop_duplicates(inplace=True)\nmasks.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b20730a7aef3547515ba8c04b292e9a6a19910d2"},"cell_type":"code","source":"masks.hist()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"15efbd159c7eb625df07d7c900ee4b1da6266aa5"},"cell_type":"code","source":"masks.to_csv('boat_count.csv',index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e2da27bc364962d0b439b33f28d2f3e3547639b0","collapsed":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(INPUT_PATH, 'train', label_csv, tfms=tfms,\n                    suffix='', val_idxs=val_idxs, test_name='test')\n      \nbs=64; \nf_model = resnet34","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7b09616d67a990651bbfa5aa70d367e73a9d7ce6"},"cell_type":"code","source":"n = len(list(open('boat_count.csv')))-1\nprint(n)\nlabel_csv = 'boat_count.csv'\nval_idxs = get_cv_idxs(n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"346582b48e462814b148dc964a2caca96817c05d"},"cell_type":"code","source":"sz = 64\ndata = get_data(sz)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":false,"_uuid":"7e0617fed959aaecd3982bc33eebbf381e11afd6"},"cell_type":"code","source":"from PIL import ImageFile\nImageFile.LOAD_TRUNCATED_IMAGES = True","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a7584c41c483f2d37f0abbcd49f4c540af123130"},"cell_type":"code","source":"data = data.resize(int(sz*1.3), '/tmp/')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f10851cd93a3f89a7c13966b6ed8c9254d309902"},"cell_type":"code","source":"x,y = next(iter(data.val_dl))\nplt.imshow(data.val_ds.denorm(to_np(x))[0]*1.4);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e414c0d79c16d78721b90cf0ffc0365e8dfc71eb"},"cell_type":"code","source":"!cp ../input/resnet34/resnet34.pth /tmp/.torch/models/resnet34-333f7ec4.pth","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c345bc7603441ed0a98641e39aa3ed101c88bc33","_kg_hide-output":false},"cell_type":"code","source":"learner = ConvLearner.pretrained(f_model, data, tmp_name=TMP,models_name=MODEL)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"eddb93c1da2ca2202004f0b65932883c7d7595f5"},"cell_type":"code","source":"lrf=learner.lr_find()\nlearner.sched.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"d469f25f43077b8707f8f97a29d687426246696c"},"cell_type":"code","source":"lr = (1E-2)/2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"915d38b23eb276e26ded41942a625b3e0aeda9cd","collapsed":true},"cell_type":"code","source":"learner.fit(lr, 3, cycle_len=1, cycle_mult=2)\nlearn.save(f'{sz}')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"49eb339bad4fd1fc803b751954d7d8c5e6e92a2d"},"cell_type":"code","source":"lrs = np.array([lr/9,lr/3,lr])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a34e1dd7845001b60aed672d05ac7abe639be04c","collapsed":true},"cell_type":"code","source":"learner.unfreeze()\nlearner.fit(lrs, 3, cycle_len=1, cycle_mult=2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"c5f77449dcb66c31a50228e3b9014fb9cd67ee2f"},"cell_type":"code","source":"learn.save(f'{sz}-lrs ')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"72d1feb7e58efa78f011be9337474b1794a12dcf"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"28187b0ae9752c008c3005e0b3c3b7805e99971e"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"295441e132b7af9b35e9a94f62095657f09faa07"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"400f30db66b4fdc06d5cf5978057c4019424527d"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"273a89ffcc5051ce07fe479dd1dfafc60167f3eb"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"c80e37cee3f918afa777a7e99840893982b00364"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"3bea88705bb9e9e937a3d518c7ed1f34b829c41a"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"620f89c11d9dcae1c396f966b7b9bb24d6de1de7"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"5ef0ad573eee6d234c374916af53ab291fe27d92"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"fe29ed335cd09a9f6f640f73fb0ded9ad9f83c7e"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"9fffcb705832eeb4b81e9c2d55cae44580ed653c"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"8a114c7b74e8c2d99a1ed5764fb287c262b8f233"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"84f50082d076c49b0fe0223741d1bb98418d3593"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"57f582a715f7b9bce81674db856ae8410a44e726"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"adb8e096e549d70700ac92a9a88e4938714e5644"},"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.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}