{"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":"markdown","source":"* Using fastai V2 for this notebook. \n* This is a working onprogress. \n* Inference Notebook will be publish later. ","metadata":{}},{"cell_type":"code","source":"from fastai.vision.all import *\nfrom fastai.callback.all import *\nfrom fastai.basics import *\nimport numpy as np","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-03T07:12:18.752548Z","iopub.execute_input":"2022-07-03T07:12:18.753164Z","iopub.status.idle":"2022-07-03T07:12:22.420815Z","shell.execute_reply.started":"2022-07-03T07:12:18.753046Z","shell.execute_reply":"2022-07-03T07:12:22.416842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = Path(\"../input/hubmap-2022-256x256\")\n\npath_img = path/\"train\"\npath_mask = path/\"masks\"","metadata":{"execution":{"iopub.status.busy":"2022-07-03T07:12:22.422747Z","iopub.execute_input":"2022-07-03T07:12:22.423336Z","iopub.status.idle":"2022-07-03T07:12:22.446940Z","shell.execute_reply.started":"2022-07-03T07:12:22.423292Z","shell.execute_reply":"2022-07-03T07:12:22.441275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(path_img.ls()),len(path_mask.ls())\n","metadata":{"execution":{"iopub.status.busy":"2022-07-03T07:12:22.449790Z","iopub.execute_input":"2022-07-03T07:12:22.450137Z","iopub.status.idle":"2022-07-03T07:12:23.167747Z","shell.execute_reply.started":"2022-07-03T07:12:22.450101Z","shell.execute_reply":"2022-07-03T07:12:23.166756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fnames = get_image_files(path_img)\nfnames[:3]\n","metadata":{"execution":{"iopub.status.busy":"2022-07-03T07:12:23.170422Z","iopub.execute_input":"2022-07-03T07:12:23.171014Z","iopub.status.idle":"2022-07-03T07:12:25.191128Z","shell.execute_reply.started":"2022-07-03T07:12:23.170977Z","shell.execute_reply":"2022-07-03T07:12:25.190230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lbl_names = get_image_files(path_mask)","metadata":{"execution":{"iopub.status.busy":"2022-07-03T07:12:25.192363Z","iopub.execute_input":"2022-07-03T07:12:25.192720Z","iopub.status.idle":"2022-07-03T07:12:26.902757Z","shell.execute_reply.started":"2022-07-03T07:12:25.192683Z","shell.execute_reply":"2022-07-03T07:12:26.901784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_f = fnames[0]\nImage.open(img_f)","metadata":{"execution":{"iopub.status.busy":"2022-07-03T07:12:26.905184Z","iopub.execute_input":"2022-07-03T07:12:26.905804Z","iopub.status.idle":"2022-07-03T07:12:26.944882Z","shell.execute_reply.started":"2022-07-03T07:12:26.905765Z","shell.execute_reply":"2022-07-03T07:12:26.943889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#get_y_fn = lambda x: path_mask/f'{x.stem}_{x.suffix}'\n#mask = open_mask()\n\n#def label_fn(x): return path_mask/f'{x.stem}_{x.suffix}'\n\ndef label_fn(x): return path_mask/x.name","metadata":{"execution":{"iopub.status.busy":"2022-07-03T07:12:26.945881Z","iopub.execute_input":"2022-07-03T07:12:26.947025Z","iopub.status.idle":"2022-07-03T07:12:26.951695Z","shell.execute_reply.started":"2022-07-03T07:12:26.946995Z","shell.execute_reply":"2022-07-03T07:12:26.950816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_f","metadata":{"execution":{"iopub.status.busy":"2022-07-03T07:12:26.953242Z","iopub.execute_input":"2022-07-03T07:12:26.953886Z","iopub.status.idle":"2022-07-03T07:12:26.964148Z","shell.execute_reply.started":"2022-07-03T07:12:26.953849Z","shell.execute_reply":"2022-07-03T07:12:26.962906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#get_y_fn(img_f)\n\nlabel_fn(img_f)","metadata":{"execution":{"iopub.status.busy":"2022-07-03T07:12:26.965990Z","iopub.execute_input":"2022-07-03T07:12:26.966752Z","iopub.status.idle":"2022-07-03T07:12:26.973107Z","shell.execute_reply.started":"2022-07-03T07:12:26.966717Z","shell.execute_reply":"2022-07-03T07:12:26.972066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"codes = np.array(['A', 'B'], dtype='<U17')","metadata":{"execution":{"iopub.status.busy":"2022-07-03T07:12:26.977589Z","iopub.execute_input":"2022-07-03T07:12:26.978169Z","iopub.status.idle":"2022-07-03T07:12:26.983458Z","shell.execute_reply.started":"2022-07-03T07:12:26.978123Z","shell.execute_reply":"2022-07-03T07:12:26.982330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = SegmentationDataLoaders.from_label_func(\n    path, bs=8, fnames = fnames, label_func = label_fn , codes = codes\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-03T07:12:26.984997Z","iopub.execute_input":"2022-07-03T07:12:26.985684Z","iopub.status.idle":"2022-07-03T07:12:29.657862Z","shell.execute_reply.started":"2022-07-03T07:12:26.985519Z","shell.execute_reply":"2022-07-03T07:12:29.655979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch(max_n=6)","metadata":{"execution":{"iopub.status.busy":"2022-07-03T07:12:29.660271Z","iopub.execute_input":"2022-07-03T07:12:29.660867Z","iopub.status.idle":"2022-07-03T07:12:30.181457Z","shell.execute_reply.started":"2022-07-03T07:12:29.660827Z","shell.execute_reply":"2022-07-03T07:12:30.180509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = unet_learner(dls, resnet34 , metrics=Dice())\nlearn.fine_tune(4)","metadata":{"execution":{"iopub.status.busy":"2022-07-03T07:12:30.182848Z","iopub.execute_input":"2022-07-03T07:12:30.183887Z","iopub.status.idle":"2022-07-03T07:19:23.202746Z","shell.execute_reply.started":"2022-07-03T07:12:30.183842Z","shell.execute_reply":"2022-07-03T07:19:23.201524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.model_dir='/kaggle/working/'","metadata":{"execution":{"iopub.status.busy":"2022-07-03T07:19:23.207392Z","iopub.execute_input":"2022-07-03T07:19:23.210141Z","iopub.status.idle":"2022-07-03T07:19:23.217382Z","shell.execute_reply.started":"2022-07-03T07:19:23.210097Z","shell.execute_reply":"2022-07-03T07:19:23.216140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.save('stage-1')","metadata":{"execution":{"iopub.status.busy":"2022-07-03T07:19:23.218790Z","iopub.execute_input":"2022-07-03T07:19:23.219450Z","iopub.status.idle":"2022-07-03T07:19:24.289566Z","shell.execute_reply.started":"2022-07-03T07:19:23.219413Z","shell.execute_reply":"2022-07-03T07:19:24.288470Z"},"trusted":true},"execution_count":null,"outputs":[]}]}