{"cells":[{"metadata":{},"cell_type":"markdown","source":"Implementation of the **U-Net Learner** from the **Fastai** library. Only 10% of the training data is used with **Resnet50** as a Encoder. The size of original image is reduced down to 4 times to reduce the trainig time. With only 10% of the whole data, decent dice score was obtained. The model can still be expanded since the unfreeze was not done on the pretrained model and original size was not used. \n"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd \nfrom fastai.vision import *\nfrom fastai import *\nimport zipfile\nimport torch.nn as nn\nimport os\n# print(os.listdir(\"../input/carvana-image-masking-challenge\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def unzip_file(path_name):\n    with zipfile.ZipFile(path_name, 'r') as zip_ref:\n        zip_ref.extractall(\"/kaggle/working/\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"unzip_file(\"/kaggle/input/carvana-image-masking-challenge/train_masks.zip\")\nunzip_file(\"/kaggle/input/carvana-image-masking-challenge/train.zip\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from subprocess import check_output\nprint(check_output([\"ls\", \"/kaggle/working/\"]).decode(\"utf8\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"path = Path(\"/kaggle/working/\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image_path = path/\"train\"\nmask_path = path/\"train_masks\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img = open_image(\"/kaggle/input/carvana-image-masking-challenge/29bb3ece3180_11.jpg\")\nimg.show(figsize = (5,5))\nimg.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"images = get_image_files(\"/kaggle/working/train\")\nimages.sort()\nimages[:5]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"masks = get_image_files(\"/kaggle/working/train_masks\")\nmasks.sort()\nmasks[:4]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"get_masks = lambda x: mask_path/f'{x.stem}_mask.gif'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"msk = open_mask(get_masks(images[1]), div = True)\nmsk.show(figsize = (10,7))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"> **Dataset**"},{"metadata":{"trusted":true},"cell_type":"code","source":"class LabelList(SegmentationLabelList):\n    def open(self,fn): \n        return open_mask(fn, div=True)\n    \nclass ItemList(ImageList):\n    _label_cls= LabelList","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Defining the dataset\nsrc_size = np.array(msk.shape[1:])\nsize = src_size//4\ncodes = [\"background\", \"car\"]\n# size = 224\nbs = 4\nsrc = (ItemList.from_folder(image_path)\n       .use_partial_data(sample_pct=0.1)\n       .split_by_rand_pct(0.2,42)\n       .label_from_func(get_masks, classes = codes))","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 = (7,7))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.show_batch(2,figsize = (7,7), ds_type = DatasetType.Valid)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"1. > **Model**\n(_I didnt know how to directly implant the pretrained in the kaggle kernel,So I First copy the path and then re-run the unet-learner method_)"},{"metadata":{"trusted":true},"cell_type":"code","source":"!cp '/kaggle/input/pretrained-pytorch-models/resnet50-19c8e357.pth' '/root/.cache/torch/checkpoints/resnet50-19c8e357.pth'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"wd = 1e-2\nlearn = unet_learner(data, models.resnet50, metrics = [dice], wd = wd).to_fp16()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.lr_find()\nlearn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"lr = 1e-03\nlearn.fit_one_cycle(3,slice(lr), pct_start = 0.9)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.save('stage-1')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true},"cell_type":"code","source":"learn.load('stage-1')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.unfreeze()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# common practice\nlrs = slice(lr/400, lr/4)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true},"cell_type":"code","source":"learn.fit_one_cycle(5, lrs, pct_start = 0.8)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":1}