{"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.vision.all import *\nfrom fastcore.all import *\nfrom fastprogress import progress_bar\nUNIQUE_CLASSES = ['Fish', 'Flower', 'Gravel', 'Sugar']\n\nMASK_CODES = dict(zip(UNIQUE_CLASSES, (0b001, 0b010, 0b100, 0b1000)))\n\ndef get_combined_code(*class_names):\n    assert 1 <= len(class_names) <= 4\n    combined_code = 0\n    for class_name in class_names:\n        combined_code |= MASK_CODES[class_name]\n    label = \"+\".join(class_names)\n    return label, combined_code\n\nCOMBINED_MASK_CODES = [\n    (\"none\", 0),\n    *list(MASK_CODES.items()),\n    get_combined_code(\"Fish\", \"Flower\"),\n    get_combined_code(\"Fish\", \"Gravel\"),\n    get_combined_code(\"Fish\", \"Sugar\"),\n    get_combined_code(\"Flower\", \"Gravel\"),\n    get_combined_code(\"Flower\", \"Sugar\"),\n    get_combined_code(\"Gravel\", \"Sugar\"),\n    get_combined_code(\"Flower\",\"Gravel\", \"Sugar\",),\n    get_combined_code(\"Fish\",\"Gravel\", \"Sugar\"),\n    get_combined_code(\"Fish\", \"Flower\", \"Sugar\"),\n    get_combined_code(\"Fish\", \"Flower\",\"Gravel\"),\n    get_combined_code(\"Fish\", \"Flower\",\"Gravel\", \"Sugar\"),\n]","metadata":{"execution":{"iopub.status.busy":"2023-05-15T01:53:22.449041Z","iopub.execute_input":"2023-05-15T01:53:22.449412Z","iopub.status.idle":"2023-05-15T01:53:22.460881Z","shell.execute_reply.started":"2023-05-15T01:53:22.449383Z","shell.execute_reply":"2023-05-15T01:53:22.459654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = \"/kaggle/input/understanding_cloud_organization\"\ny = \"/kaggle/input/uc-pre\"\npath = Path(x)\nprint(path.ls())\nos.listdir(x)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-15T01:53:22.462989Z","iopub.execute_input":"2023-05-15T01:53:22.463613Z","iopub.status.idle":"2023-05-15T01:53:22.576791Z","shell.execute_reply.started":"2023-05-15T01:53:22.463575Z","shell.execute_reply":"2023-05-15T01:53:22.575857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"OUTPUT_DIR= Path(y)","metadata":{"execution":{"iopub.status.busy":"2023-05-15T01:53:22.578271Z","iopub.execute_input":"2023-05-15T01:53:22.579356Z","iopub.status.idle":"2023-05-15T01:53:22.583657Z","shell.execute_reply.started":"2023-05-15T01:53:22.579323Z","shell.execute_reply":"2023-05-15T01:53:22.582791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_items(source_dir: Path):\n    return get_image_files(source_dir.joinpath(\"images\"))[:2000]\n\ndef get_y(fn: Path):\n    return fn.parent.parent.joinpath(\"masks\").joinpath(f\"{fn.stem}.jpg.png\")\n","metadata":{"execution":{"iopub.status.busy":"2023-05-15T01:53:22.586288Z","iopub.execute_input":"2023-05-15T01:53:22.586762Z","iopub.status.idle":"2023-05-15T01:53:22.597053Z","shell.execute_reply.started":"2023-05-15T01:53:22.586728Z","shell.execute_reply":"2023-05-15T01:53:22.596145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seg = DataBlock(blocks=(ImageBlock, MaskBlock(COMBINED_MASK_CODES)),\n                get_items=get_items,\n                get_y=get_y,\n                splitter=RandomSplitter(),\n                item_tfms=[Resize(224, method=\"squish\")],\n#                 batch_tfms=[aug_transforms(size=(700,1050)),Normalize.from_stats(*imagenet_stats)]\n               )\n\ndls = seg.dataloaders(OUTPUT_DIR, bs=32)\n\nlearn = unet_learner(dls, resnet34, metrics=DiceMulti)\n# critation = BCELossFlat()\n# learn.loss = critation","metadata":{"execution":{"iopub.status.busy":"2023-05-15T01:53:37.024814Z","iopub.execute_input":"2023-05-15T01:53:37.025192Z","iopub.status.idle":"2023-05-15T01:53:44.729159Z","shell.execute_reply.started":"2023-05-15T01:53:37.025161Z","shell.execute_reply":"2023-05-15T01:53:44.728174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2023-05-15T01:53:44.732639Z","iopub.execute_input":"2023-05-15T01:53:44.733015Z","iopub.status.idle":"2023-05-15T01:53:48.337428Z","shell.execute_reply.started":"2023-05-15T01:53:44.732977Z","shell.execute_reply":"2023-05-15T01:53:48.336599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\ngc.collect()\ntorch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2023-05-15T01:53:48.338485Z","iopub.execute_input":"2023-05-15T01:53:48.339017Z","iopub.status.idle":"2023-05-15T01:53:48.561817Z","shell.execute_reply.started":"2023-05-15T01:53:48.338985Z","shell.execute_reply":"2023-05-15T01:53:48.560892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fine_tune(1)","metadata":{"execution":{"iopub.status.busy":"2023-05-15T01:53:48.564431Z","iopub.execute_input":"2023-05-15T01:53:48.565737Z","iopub.status.idle":"2023-05-15T01:58:21.793166Z","shell.execute_reply.started":"2023-05-15T01:53:48.565710Z","shell.execute_reply":"2023-05-15T01:58:21.792076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install GPUtil\nfrom GPUtil import showUtilization as gpu_usage\ngpu_usage()\n","metadata":{"execution":{"iopub.status.busy":"2023-05-15T01:58:21.794960Z","iopub.execute_input":"2023-05-15T01:58:21.796113Z","iopub.status.idle":"2023-05-15T01:58:35.957488Z","shell.execute_reply.started":"2023-05-15T01:58:21.796070Z","shell.execute_reply":"2023-05-15T01:58:35.956209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()\ntorch.cuda.empty_cache()\n\ngpu_usage()","metadata":{"execution":{"iopub.status.busy":"2023-05-15T01:58:35.959313Z","iopub.execute_input":"2023-05-15T01:58:35.960322Z","iopub.status.idle":"2023-05-15T01:58:36.314433Z","shell.execute_reply.started":"2023-05-15T01:58:35.960278Z","shell.execute_reply":"2023-05-15T01:58:36.313482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.freeze()\nlearn.fine_tune(10)","metadata":{"execution":{"iopub.status.busy":"2023-05-15T01:59:16.413625Z","iopub.execute_input":"2023-05-15T01:59:16.413999Z","iopub.status.idle":"2023-05-15T02:06:20.716518Z","shell.execute_reply.started":"2023-05-15T01:59:16.413966Z","shell.execute_reply":"2023-05-15T02:06:20.714023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.show_results(max_n= 4, figsize = (12,10))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.unfreeze()\nlearn.fit_one_cycle(10, slice(1e-5, 1e-4))","metadata":{"execution":{"iopub.status.busy":"2023-05-15T02:06:20.720302Z","iopub.status.idle":"2023-05-15T02:06:20.721145Z","shell.execute_reply.started":"2023-05-15T02:06:20.720839Z","shell.execute_reply":"2023-05-15T02:06:20.720866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.save('Stage-1')\nlearn.show_results(max_n= 4, figsize = (12,10))\nlearn.unfreeze() ","metadata":{"execution":{"iopub.status.busy":"2023-05-15T02:06:20.722457Z","iopub.status.idle":"2023-05-15T02:06:20.723157Z","shell.execute_reply.started":"2023-05-15T02:06:20.722903Z","shell.execute_reply":"2023-05-15T02:06:20.722926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lrs2=learn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2023-05-15T02:06:20.724378Z","iopub.status.idle":"2023-05-15T02:06:20.725084Z","shell.execute_reply.started":"2023-05-15T02:06:20.724821Z","shell.execute_reply":"2023-05-15T02:06:20.724843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr = lrs2.valley\nlr","metadata":{"execution":{"iopub.status.busy":"2023-05-15T02:06:20.726321Z","iopub.status.idle":"2023-05-15T02:06:20.727023Z","shell.execute_reply.started":"2023-05-15T02:06:20.726762Z","shell.execute_reply":"2023-05-15T02:06:20.726784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(10, slice(lr, 1e-3))","metadata":{"execution":{"iopub.status.busy":"2023-05-15T02:06:20.728321Z","iopub.status.idle":"2023-05-15T02:06:20.729069Z","shell.execute_reply.started":"2023-05-15T02:06:20.728805Z","shell.execute_reply":"2023-05-15T02:06:20.728828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.show_results(max_n= 4, figsize = (12,10))","metadata":{"execution":{"iopub.status.busy":"2023-05-15T02:06:20.730414Z","iopub.status.idle":"2023-05-15T02:06:20.731122Z","shell.execute_reply.started":"2023-05-15T02:06:20.730859Z","shell.execute_reply":"2023-05-15T02:06:20.730882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp = SegmentationInterpretation.from_learner(learn)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-05-15T02:06:20.718117Z","iopub.status.idle":"2023-05-15T02:06:20.718851Z","shell.execute_reply.started":"2023-05-15T02:06:20.718587Z","shell.execute_reply":"2023-05-15T02:06:20.718614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}