{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"colab":{"provenance":[]},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":4732950,"sourceType":"datasetVersion","datasetId":2738852}],"dockerImageVersionId":30665,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import torch\ntorch.cuda.empty_cache()\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport shutil\n\nfrom fastai.basics import *\nfrom fastai.vision.all import *\nfrom fastai.callback.all import *\n","metadata":{"id":"fe4db4ea","execution":{"iopub.status.busy":"2024-03-11T05:18:15.145114Z","iopub.execute_input":"2024-03-11T05:18:15.145388Z","iopub.status.idle":"2024-03-11T05:18:26.689006Z","shell.execute_reply.started":"2024-03-11T05:18:15.145363Z","shell.execute_reply":"2024-03-11T05:18:26.688195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path_im = Path('/kaggle/input/flood-area-segmentation/Image')\n\npath_lbl = Path('/kaggle/input/flood-area-segmentation/Mask')\nsource_dir2=Path('/kaggle/input/flood-area-segmentation/Mask')","metadata":{"id":"03bfd516","execution":{"iopub.status.busy":"2024-03-11T05:18:26.690946Z","iopub.execute_input":"2024-03-11T05:18:26.691298Z","iopub.status.idle":"2024-03-11T05:18:26.696232Z","shell.execute_reply.started":"2024-03-11T05:18:26.691267Z","shell.execute_reply":"2024-03-11T05:18:26.695137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fnames = get_image_files(path_im)\nlabel_names = get_image_files(path_lbl)","metadata":{"id":"9a9fa123","execution":{"iopub.status.busy":"2024-03-11T05:18:26.697504Z","iopub.execute_input":"2024-03-11T05:18:26.698200Z","iopub.status.idle":"2024-03-11T05:18:26.883621Z","shell.execute_reply.started":"2024-03-11T05:18:26.698168Z","shell.execute_reply":"2024-03-11T05:18:26.882952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nget_msk = lambda o: source_dir2/f'{o.stem}.png' #o is the filename of the mask\n\n","metadata":{"id":"76707706","execution":{"iopub.status.busy":"2024-03-11T05:18:26.885929Z","iopub.execute_input":"2024-03-11T05:18:26.886343Z","iopub.status.idle":"2024-03-11T05:18:26.890447Z","shell.execute_reply.started":"2024-03-11T05:18:26.886311Z","shell.execute_reply":"2024-03-11T05:18:26.889508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fnames[0]","metadata":{"id":"207b4678","outputId":"3708c923-06f5-44bd-82c3-8b1778addbc9","execution":{"iopub.status.busy":"2024-03-11T05:18:26.891526Z","iopub.execute_input":"2024-03-11T05:18:26.891866Z","iopub.status.idle":"2024-03-11T05:18:26.903030Z","shell.execute_reply.started":"2024-03-11T05:18:26.891833Z","shell.execute_reply":"2024-03-11T05:18:26.902197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_names[0]","metadata":{"id":"8d580fe5","outputId":"c7d6b3f8-4f84-42cb-f660-2a6a4c144a5d","execution":{"iopub.status.busy":"2024-03-11T05:18:26.904277Z","iopub.execute_input":"2024-03-11T05:18:26.904538Z","iopub.status.idle":"2024-03-11T05:18:26.913138Z","shell.execute_reply.started":"2024-03-11T05:18:26.904516Z","shell.execute_reply":"2024-03-11T05:18:26.912288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_fn = fnames[4]\nimg= PILImage.create(img_fn)\n\nimg.show()","metadata":{"id":"07419f92","outputId":"25cc4ae1-fe2e-4184-ff81-775259712849","execution":{"iopub.status.busy":"2024-03-11T05:18:26.914238Z","iopub.execute_input":"2024-03-11T05:18:26.914492Z","iopub.status.idle":"2024-03-11T05:18:30.780576Z","shell.execute_reply.started":"2024-03-11T05:18:26.914470Z","shell.execute_reply":"2024-03-11T05:18:30.779694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask = PILMask.create(get_msk(img_fn))\nmask.show(alpha=1)","metadata":{"id":"ad16c6a7","outputId":"1dbdc593-0710-47e6-de73-38a21faae2fe","execution":{"iopub.status.busy":"2024-03-11T05:18:30.781849Z","iopub.execute_input":"2024-03-11T05:18:30.782148Z","iopub.status.idle":"2024-03-11T05:18:32.395303Z","shell.execute_reply.started":"2024-03-11T05:18:30.782124Z","shell.execute_reply":"2024-03-11T05:18:32.394420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(tensor(mask))\n\n","metadata":{"id":"3028b217","outputId":"f3f70acc-b321-451c-c51b-2cf367349da0","execution":{"iopub.status.busy":"2024-03-11T05:18:32.396495Z","iopub.execute_input":"2024-03-11T05:18:32.396801Z","iopub.status.idle":"2024-03-11T05:18:32.476683Z","shell.execute_reply.started":"2024-03-11T05:18:32.396777Z","shell.execute_reply":"2024-03-11T05:18:32.475645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"codes = ['n','y']","metadata":{"id":"a4143e7a","execution":{"iopub.status.busy":"2024-03-11T05:18:32.480856Z","iopub.execute_input":"2024-03-11T05:18:32.481211Z","iopub.status.idle":"2024-03-11T05:18:32.485681Z","shell.execute_reply.started":"2024-03-11T05:18:32.481182Z","shell.execute_reply":"2024-03-11T05:18:32.484533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sz= mask.shape; sz","metadata":{"id":"ca86fc79","outputId":"4fab64f5-b450-47b9-b865-1c77dfc65350","execution":{"iopub.status.busy":"2024-03-11T05:18:32.487045Z","iopub.execute_input":"2024-03-11T05:18:32.487380Z","iopub.status.idle":"2024-03-11T05:18:32.499819Z","shell.execute_reply.started":"2024-03-11T05:18:32.487349Z","shell.execute_reply":"2024-03-11T05:18:32.499026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"half = tuple(int(x/2) for x in sz); half","metadata":{"id":"6fa5117a","outputId":"3c69d5c9-c00c-4ed6-8b79-39d7d6bfbea0","execution":{"iopub.status.busy":"2024-03-11T05:18:32.502337Z","iopub.execute_input":"2024-03-11T05:18:32.502627Z","iopub.status.idle":"2024-03-11T05:18:32.508985Z","shell.execute_reply.started":"2024-03-11T05:18:32.502603Z","shell.execute_reply":"2024-03-11T05:18:32.508005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cancer = DataBlock(blocks=(ImageBlock, MaskBlock(codes)),\nget_items=get_image_files,\nsplitter=RandomSplitter(valid_pct=0.2),\nget_y = get_msk,\nitem_tfms=Resize(244),\nbatch_tfms=[*aug_transforms(size=(244,244)), Normalize.from_stats(*imagenet_stats),  IntToFloatTensor(div_mask=255)\n])\n","metadata":{"id":"3b8d47fc","execution":{"iopub.status.busy":"2024-03-11T05:18:32.510010Z","iopub.execute_input":"2024-03-11T05:18:32.512237Z","iopub.status.idle":"2024-03-11T05:18:32.761137Z","shell.execute_reply.started":"2024-03-11T05:18:32.512207Z","shell.execute_reply":"2024-03-11T05:18:32.760110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"source_p=Path('/kaggle/input/flood-area-segmentation/Image')\n\ndls =  cancer.dataloaders(source_p,bs=4)","metadata":{"id":"69c1c127","execution":{"iopub.status.busy":"2024-03-11T05:18:32.762420Z","iopub.execute_input":"2024-03-11T05:18:32.762739Z","iopub.status.idle":"2024-03-11T05:18:33.725078Z","shell.execute_reply.started":"2024-03-11T05:18:32.762713Z","shell.execute_reply":"2024-03-11T05:18:33.724123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch()","metadata":{"id":"bb893bc9","outputId":"18201d9f-cdc7-4cdc-afe0-4b1944742d6d","execution":{"iopub.status.busy":"2024-03-11T05:18:33.726752Z","iopub.execute_input":"2024-03-11T05:18:33.727148Z","iopub.status.idle":"2024-03-11T05:18:34.446946Z","shell.execute_reply.started":"2024-03-11T05:18:33.727114Z","shell.execute_reply":"2024-03-11T05:18:34.445874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.vocab = codes\n\ndls.vocab","metadata":{"id":"7ec45198","outputId":"111911fd-5ae5-40f0-f716-a3decd96160c","execution":{"iopub.status.busy":"2024-03-11T05:18:34.448154Z","iopub.execute_input":"2024-03-11T05:18:34.448505Z","iopub.status.idle":"2024-03-11T05:18:34.455661Z","shell.execute_reply.started":"2024-03-11T05:18:34.448475Z","shell.execute_reply":"2024-03-11T05:18:34.454658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"name2id = {v:k for k,v in enumerate(codes)}","metadata":{"id":"dd19b4e7","execution":{"iopub.status.busy":"2024-03-11T05:18:34.457018Z","iopub.execute_input":"2024-03-11T05:18:34.457336Z","iopub.status.idle":"2024-03-11T05:18:34.464897Z","shell.execute_reply.started":"2024-03-11T05:18:34.457306Z","shell.execute_reply":"2024-03-11T05:18:34.463842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"name2id","metadata":{"id":"a430073d","outputId":"bb0e0440-2d01-44e6-c9df-7c1d38ab0b49","execution":{"iopub.status.busy":"2024-03-11T05:18:34.466195Z","iopub.execute_input":"2024-03-11T05:18:34.466549Z","iopub.status.idle":"2024-03-11T05:18:34.478324Z","shell.execute_reply.started":"2024-03-11T05:18:34.466520Z","shell.execute_reply":"2024-03-11T05:18:34.477331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"opt=Adam","metadata":{"id":"44bfdf66","execution":{"iopub.status.busy":"2024-03-11T05:18:34.479460Z","iopub.execute_input":"2024-03-11T05:18:34.480094Z","iopub.status.idle":"2024-03-11T05:18:34.488761Z","shell.execute_reply.started":"2024-03-11T05:18:34.480062Z","shell.execute_reply":"2024-03-11T05:18:34.487887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = unet_learner(dls, resnet34)\n","metadata":{"id":"b1c721c5","execution":{"iopub.status.busy":"2024-03-11T05:21:46.727536Z","iopub.execute_input":"2024-03-11T05:21:46.728541Z","iopub.status.idle":"2024-03-11T05:22:00.950778Z","shell.execute_reply.started":"2024-03-11T05:21:46.728500Z","shell.execute_reply":"2024-03-11T05:22:00.949720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.summary()","metadata":{"id":"fcf090e5","outputId":"142542df-ea9a-4d45-856e-9739e060b503","execution":{"iopub.status.busy":"2024-03-11T05:18:37.996195Z","iopub.execute_input":"2024-03-11T05:18:37.996489Z","iopub.status.idle":"2024-03-11T05:18:38.926452Z","shell.execute_reply.started":"2024-03-11T05:18:37.996465Z","shell.execute_reply":"2024-03-11T05:18:38.925330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\nos.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'max_split_size_mb:50'\n","metadata":{"execution":{"iopub.status.busy":"2024-03-11T05:18:38.927893Z","iopub.execute_input":"2024-03-11T05:18:38.928266Z","iopub.status.idle":"2024-03-11T05:18:38.933308Z","shell.execute_reply.started":"2024-03-11T05:18:38.928233Z","shell.execute_reply":"2024-03-11T05:18:38.932225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fine_tune(8)","metadata":{"id":"09606895","outputId":"03dd7b23-39d2-4e39-ceda-0dc56893ec39","execution":{"iopub.status.busy":"2024-03-11T05:22:53.173413Z","iopub.execute_input":"2024-03-11T05:22:53.174297Z","iopub.status.idle":"2024-03-11T05:24:39.620905Z","shell.execute_reply.started":"2024-03-11T05:22:53.174260Z","shell.execute_reply":"2024-03-11T05:24:39.619376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.show_results()","metadata":{"id":"2a5e3634","outputId":"62f51c2a-3d5a-4f77-d00e-b6ebac17b91f","execution":{"iopub.status.busy":"2024-03-11T05:20:54.588118Z","iopub.execute_input":"2024-03-11T05:20:54.588414Z","iopub.status.idle":"2024-03-11T05:20:55.854768Z","shell.execute_reply.started":"2024-03-11T05:20:54.588386Z","shell.execute_reply":"2024-03-11T05:20:55.853869Z"},"trusted":true},"execution_count":null,"outputs":[]}]}