{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"from fastai.data.all import *\nfrom fastai.vision.all import *\nimport torchvision.transforms as T\nimport gc","metadata":{"papermill":{"duration":7.174565,"end_time":"2023-07-04T15:24:13.791802","exception":false,"start_time":"2023-07-04T15:24:06.617237","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-07-07T11:15:21.877257Z","iopub.execute_input":"2023-07-07T11:15:21.87784Z","iopub.status.idle":"2023-07-07T11:15:29.130157Z","shell.execute_reply.started":"2023-07-07T11:15:21.877796Z","shell.execute_reply":"2023-07-07T11:15:29.129174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# pip install nbdev","metadata":{"execution":{"iopub.status.busy":"2023-07-07T11:15:29.132026Z","iopub.execute_input":"2023-07-07T11:15:29.132768Z","iopub.status.idle":"2023-07-07T11:15:29.1382Z","shell.execute_reply.started":"2023-07-07T11:15:29.132735Z","shell.execute_reply":"2023-07-07T11:15:29.136886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = Path('/kaggle/input/contrails-images-ash-color/contrails')","metadata":{"papermill":{"duration":0.013187,"end_time":"2023-07-04T15:24:13.809747","exception":false,"start_time":"2023-07-04T15:24:13.79656","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-07-07T11:15:29.139776Z","iopub.execute_input":"2023-07-07T11:15:29.14044Z","iopub.status.idle":"2023-07-07T11:15:29.152378Z","shell.execute_reply.started":"2023-07-07T11:15:29.140408Z","shell.execute_reply":"2023-07-07T11:15:29.151389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"codes = [\"Background\", \"Contrail\"]","metadata":{"papermill":{"duration":0.012011,"end_time":"2023-07-04T15:24:13.826078","exception":false,"start_time":"2023-07-04T15:24:13.814067","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-07-07T11:15:29.15515Z","iopub.execute_input":"2023-07-07T11:15:29.155528Z","iopub.status.idle":"2023-07-07T11:15:29.162736Z","shell.execute_reply.started":"2023-07-07T11:15:29.155497Z","shell.execute_reply":"2023-07-07T11:15:29.16188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_x(o): \n    return PILImage.create(T.ToPILImage()(T.ToTensor()(np.load(o)[...,:-1])))\ndef get_y(o): \n    return PILMask.create(np.load(o)[..., -1].astype('int8'))","metadata":{"execution":{"iopub.status.busy":"2023-07-07T11:15:29.164226Z","iopub.execute_input":"2023-07-07T11:15:29.165426Z","iopub.status.idle":"2023-07-07T11:15:29.175338Z","shell.execute_reply.started":"2023-07-07T11:15:29.165396Z","shell.execute_reply":"2023-07-07T11:15:29.174475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dblock = DataBlock(\n    blocks = (ImageBlock, MaskBlock(codes=codes)),\n    get_x = get_x,\n    get_y = get_y,\n    splitter  = RandomSplitter(),\n#     item_tfms = Resize(256),\n    batch_tfms = [*aug_transforms(), Normalize],\n)","metadata":{"execution":{"iopub.status.busy":"2023-07-07T11:15:29.176364Z","iopub.execute_input":"2023-07-07T11:15:29.17664Z","iopub.status.idle":"2023-07-07T11:15:29.190841Z","shell.execute_reply.started":"2023-07-07T11:15:29.176607Z","shell.execute_reply":"2023-07-07T11:15:29.189804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fnames = get_files(path)","metadata":{"papermill":{"duration":6.485503,"end_time":"2023-07-04T15:24:20.337179","exception":false,"start_time":"2023-07-04T15:24:13.851676","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-07-07T11:15:29.19196Z","iopub.execute_input":"2023-07-07T11:15:29.19227Z","iopub.status.idle":"2023-07-07T11:15:36.162409Z","shell.execute_reply.started":"2023-07-07T11:15:29.192233Z","shell.execute_reply":"2023-07-07T11:15:36.161436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = dblock.dataloaders(fnames[:1000], bs=16)","metadata":{"papermill":{"duration":3.539695,"end_time":"2023-07-04T15:24:23.881599","exception":false,"start_time":"2023-07-04T15:24:20.341904","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-07-07T11:15:36.163859Z","iopub.execute_input":"2023-07-07T11:15:36.164197Z","iopub.status.idle":"2023-07-07T11:15:42.792203Z","shell.execute_reply.started":"2023-07-07T11:15:36.164162Z","shell.execute_reply":"2023-07-07T11:15:42.791217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = unet_learner(dls, \n                     resnet34, \n                     loss_func=FocalLossFlat(axis=1),\n                     metrics=[Dice, foreground_acc],\n                    )","metadata":{"papermill":{"duration":2.646993,"end_time":"2023-07-04T15:24:34.674954","exception":false,"start_time":"2023-07-04T15:24:32.027961","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-07-07T11:15:42.793871Z","iopub.execute_input":"2023-07-07T11:15:42.794211Z","iopub.status.idle":"2023-07-07T11:15:45.109081Z","shell.execute_reply.started":"2023-07-07T11:15:42.79418Z","shell.execute_reply":"2023-07-07T11:15:45.108056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.unfreeze()","metadata":{"execution":{"iopub.status.busy":"2023-07-07T11:15:45.113188Z","iopub.execute_input":"2023-07-07T11:15:45.113484Z","iopub.status.idle":"2023-07-07T11:15:45.118363Z","shell.execute_reply.started":"2023-07-07T11:15:45.113459Z","shell.execute_reply":"2023-07-07T11:15:45.117158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# learn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2023-07-07T11:15:45.119933Z","iopub.execute_input":"2023-07-07T11:15:45.120633Z","iopub.status.idle":"2023-07-07T11:15:45.12979Z","shell.execute_reply.started":"2023-07-07T11:15:45.1206Z","shell.execute_reply":"2023-07-07T11:15:45.128739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(1, lr_max=slice(1e-03,1e-05))","metadata":{"papermill":{"duration":20.895747,"end_time":"2023-07-04T15:24:55.652496","exception":false,"start_time":"2023-07-04T15:24:34.756749","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-07-07T11:15:45.13136Z","iopub.execute_input":"2023-07-07T11:15:45.131686Z","iopub.status.idle":"2023-07-07T11:16:11.510678Z","shell.execute_reply.started":"2023-07-07T11:15:45.131656Z","shell.execute_reply":"2023-07-07T11:16:11.509543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.export(fname='focal.pkl')","metadata":{"execution":{"iopub.status.busy":"2023-07-07T11:16:11.512445Z","iopub.execute_input":"2023-07-07T11:16:11.51308Z","iopub.status.idle":"2023-07-07T11:16:12.347495Z","shell.execute_reply.started":"2023-07-07T11:16:11.513039Z","shell.execute_reply":"2023-07-07T11:16:12.344435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.show_results()","metadata":{"papermill":{"duration":2.199129,"end_time":"2023-07-04T15:24:57.926131","exception":false,"start_time":"2023-07-04T15:24:55.727002","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-07-07T11:16:12.348544Z","iopub.status.idle":"2023-07-07T11:16:12.349476Z","shell.execute_reply.started":"2023-07-07T11:16:12.34922Z","shell.execute_reply":"2023-07-07T11:16:12.349243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp = SegmentationInterpretation.from_learner(learn)","metadata":{"papermill":{"duration":4.044155,"end_time":"2023-07-04T15:25:02.06022","exception":true,"start_time":"2023-07-04T15:24:58.016065","status":"failed"},"tags":[],"execution":{"iopub.status.busy":"2023-07-07T11:16:12.350882Z","iopub.status.idle":"2023-07-07T11:16:12.351677Z","shell.execute_reply.started":"2023-07-07T11:16:12.35144Z","shell.execute_reply":"2023-07-07T11:16:12.351462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp.top_losses(k=3)","metadata":{"execution":{"iopub.status.busy":"2023-07-07T11:16:12.353141Z","iopub.status.idle":"2023-07-07T11:16:12.354071Z","shell.execute_reply.started":"2023-07-07T11:16:12.353815Z","shell.execute_reply":"2023-07-07T11:16:12.353837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp.show_results(interp.top_losses(k=3)[1])","metadata":{"execution":{"iopub.status.busy":"2023-07-07T11:16:12.355384Z","iopub.status.idle":"2023-07-07T11:16:12.356305Z","shell.execute_reply.started":"2023-07-07T11:16:12.35604Z","shell.execute_reply":"2023-07-07T11:16:12.356067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp.plot_top_losses(k=3)","metadata":{"execution":{"iopub.status.busy":"2023-07-07T11:16:12.358658Z","iopub.status.idle":"2023-07-07T11:16:12.359729Z","shell.execute_reply.started":"2023-07-07T11:16:12.359484Z","shell.execute_reply":"2023-07-07T11:16:12.359506Z"},"trusted":true},"execution_count":null,"outputs":[]}]}