{"cells":[{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"%reload_ext autoreload\n%autoreload 2\n%matplotlib inline\n\nfrom fastai.vision import *\nfrom fastai.callbacks.hooks import *\nfrom fastai.utils.mem import *\n\nimport shutil\nimport pathlib\nimport pandas as pd","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Define Paths"},{"metadata":{"trusted":true},"cell_type":"code","source":"path = pathlib.Path('../input')\n\npath_img = path/'train_v2'\npath_label = path/'train_ship_segmentations_v2.csv'\npath_img_test = path/'test_v2'\n\nprint(len(path_img.ls()))\nprint(len(path_img_test.ls()))\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Get images"},{"metadata":{"trusted":true},"cell_type":"code","source":"fnames = get_image_files(path_img)\n\nimg_f = fnames[44]\nimg = open_image(img_f)\nimg.show(figsize=(5,5))\nimg.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"! mkdir small_train\n\nfnames_small = fnames[:200]\nsmall_train_path = pathlib.Path('small_train/')\n\nfor fn in fnames_small:\n    to_file = small_train_path/fn.name\n    shutil.copy(str(fn), str(to_file))\n\nfnames_small = get_image_files(small_train_path)\n\nimg_f = fnames_small[42]\nimg = open_image(img_f)\nimg.show(figsize=(5,5))\nimg.shape","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Load in CSV with labels"},{"metadata":{"trusted":true},"cell_type":"code","source":"label_df = pd.read_csv(path_label)\nlabel_df[:10]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Define function for mapping masks"},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_seg(x_path, x_size=(768, 768)):\n    rte_list = label_df.loc[label_df['ImageId'] == x_path.name]['EncodedPixels'].tolist()\n    if len(rte_list) == 1 and not isinstance(rte_list[0], str):\n        return open_mask_rle(\"\", x_size)\n    else:\n        mask = FloatTensor(rle_decode(\" \".join(rte_list), x_size).astype(np.uint8))\n        mask = mask.view(x_size[1], x_size[0], -1)\n        return ImageSegment(mask.permute(2,1,0))\n\n    \n\nimg_f = fnames_small[3]\n\nmask = get_seg(img_f)\nimg = open_image(img_f)\nimg.show(figsize=(5,5), y=mask, title='masked')\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Setup datasets"},{"metadata":{"trusted":true},"cell_type":"code","source":"src_size = array([768, 768])\nsize = src_size//4\n\nfree = gpu_mem_get_free_no_cache()\n# the max size of bs depends on the available GPU RAM\nif free > 8200: bs=8\nelse:           bs=4\nprint(f\"using bs={bs}, have {free}MB of GPU RAM free\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"src = (SegmentationItemList.from_folder(small_train_path)\n       .split_by_rand_pct(0.2)\n       .label_from_func(get_seg, classes=array(['Backgroun', 'Vessel'], dtype='<U17')))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data = (src.transform(get_transforms(), size=size, tfm_y=True)\n        .databunch(bs=bs)\n        .normalize(imagenet_stats))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.show_batch(2, figsize=(10,7))","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}