{"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":"#hide\n!pip install -Uqq fastbook\nimport fastbook\nfastbook.setup_book()","metadata":{"execution":{"iopub.status.busy":"2021-06-24T07:38:25.145964Z","iopub.execute_input":"2021-06-24T07:38:25.146666Z","iopub.status.idle":"2021-06-24T07:38:31.697479Z","shell.execute_reply.started":"2021-06-24T07:38:25.146613Z","shell.execute_reply":"2021-06-24T07:38:31.696334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#hide\nfrom fastbook import *\nfrom fastai.vision.widgets import *","metadata":{"execution":{"iopub.status.busy":"2021-06-24T07:38:31.699399Z","iopub.execute_input":"2021-06-24T07:38:31.699705Z","iopub.status.idle":"2021-06-24T07:38:31.709301Z","shell.execute_reply.started":"2021-06-24T07:38:31.699675Z","shell.execute_reply":"2021-06-24T07:38:31.70802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Download and Extract","metadata":{}},{"cell_type":"code","source":"# make sure to download kaggle.json from kaggle and place it under /root/.kaggle, or use `%env KAGGLE_CONFIG_DIR=/path/to/new/location`\n!kaggle competitions download -c understanding_cloud_organization","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_extract('understanding_cloud_organization.zip', dest='understanding_cloud_organization')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Investigating CAMVID Segmentation Images","metadata":{}},{"cell_type":"code","source":"path_x = untar_data(URLs.CAMVID_TINY, dest='understanding_cloud_organization/CAMVID/')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files_x = []\nfor i in get_image_files(path_x/'labels'):\n    t_img = tensor(Image.open(i))\n    files_x.append(t_img)\nprint(\"Unique Labels within Images\")    \nfiles_x[0].shape","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t = tensor(Image.open(get_image_files(path_x/'labels')[0])).reshape([96*128])\nt[0:3]=2\nt","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Trials","metadata":{}},{"cell_type":"code","source":"t1 = torch.zeros([21*14])\nencoded_pixels_s = \"16 1 30 1 44 1 58 1 72 1\"\nencoded_pixels_a = encoded_pixels_s.split()\npairs_a = [[int(encoded_pixels_a[i]), int(encoded_pixels_a[i + 1])] for i in range(0, len(encoded_pixels_a) - 1, 2)]\n\n\nfor pair in pairs_a:\n    start_px = pair[0]-1\n    length = pair[1]\n    t1[start_px:(start_px+length)] = 255\n\nt2 = t1.reshape([21, 14]).T    \npd.DataFrame(t2)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Cloud Label Images Preparation","metadata":{}},{"cell_type":"markdown","source":"### Initialize Variables","metadata":{}},{"cell_type":"markdown","source":"- The following folder should include the Kaggle's competition data (test_images, train_images, codes.txt, sample_submission.csv, train.csv)\n- the train_images_labels will be created under that folder location (CLOUD_PATH)","metadata":{}},{"cell_type":"code","source":"# THIS MIGHT NEED TO BE CHANGED ACCORDING TO THE LOCAL SETUP AND THE LOCATION OF THE DATA\nCLOUD_PATH = '../input/understanding_cloud_organization'\ncloud_path = Path(CLOUD_PATH)","metadata":{"execution":{"iopub.status.busy":"2021-06-24T07:41:51.740359Z","iopub.execute_input":"2021-06-24T07:41:51.740781Z","iopub.status.idle":"2021-06-24T07:41:51.746697Z","shell.execute_reply.started":"2021-06-24T07:41:51.74075Z","shell.execute_reply":"2021-06-24T07:41:51.745427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images_path = cloud_path/'train_images'\nlabel_images_path = Path('/kaggle/working/train_images_labels')","metadata":{"execution":{"iopub.status.busy":"2021-06-24T07:45:44.979215Z","iopub.execute_input":"2021-06-24T07:45:44.979729Z","iopub.status.idle":"2021-06-24T07:45:44.984396Z","shell.execute_reply.started":"2021-06-24T07:45:44.979687Z","shell.execute_reply":"2021-06-24T07:45:44.983387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#codes = list(pd.read_csv(cloud_path/'codes.txt', header=None)[0])\ncodes = ['Fish', 'Flower', 'Gravel', 'Sugar']","metadata":{"execution":{"iopub.status.busy":"2021-06-24T07:43:59.524225Z","iopub.execute_input":"2021-06-24T07:43:59.524616Z","iopub.status.idle":"2021-06-24T07:43:59.528447Z","shell.execute_reply.started":"2021-06-24T07:43:59.524575Z","shell.execute_reply":"2021-06-24T07:43:59.52772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(cloud_path/'train.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-24T07:44:02.874779Z","iopub.execute_input":"2021-06-24T07:44:02.875392Z","iopub.status.idle":"2021-06-24T07:44:08.329908Z","shell.execute_reply.started":"2021-06-24T07:44:02.875334Z","shell.execute_reply":"2021-06-24T07:44:08.329029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Clean Label Image folder (train_images_label) by renaming","metadata":{}},{"cell_type":"code","source":"# Clean the label images folder\n# os.rename(label_images_path, f'{label_images_path}_old_{time.time()}')\nlabel_images_path.mkdir(parents=True, exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2021-06-24T07:46:11.130473Z","iopub.execute_input":"2021-06-24T07:46:11.130814Z","iopub.status.idle":"2021-06-24T07:46:11.135277Z","shell.execute_reply.started":"2021-06-24T07:46:11.130784Z","shell.execute_reply":"2021-06-24T07:46:11.134332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# INITIAL ATTEMPT - WITH SINGLE CLASS PIXELS\n# from os import path\n# from torchvision import transforms\n\n# # Void class\n# void_class = 4 #codes.index('Void')\n\n\n# # Removes previoulsy generated files\n# previous_images = get_image_files(label_images_folder)\n# [os.unlink(f) for f in previous_images]\n\n# for idx, row in train_df.iterrows():\n#     train_img_fn = row.Image_Label[:row.Image_Label.index('_')]\n#     train_img_path = train_images_path/train_img_fn\n    \n    \n#     if path.exists(train_img_path):\n#         train_img_size = Image.open(train_img_path).size\n#         label_str = row.Image_Label[row.Image_Label.index('_')+1:]\n#         label_idx = codes.index(label_str)\n#         label_img_fn = train_img_fn.rsplit(\".\", 1)[0] + \".png\"\n#         label_img_path = label_images_path/label_img_fn\n#         if path.exists(label_img_path):\n#             # Opening Existing Image (label image created for a previous train.csv data record)\n#             label_img = Image.open(label_img_path)\n#             tensor_label_img = tensor(label_img)\n#             label_tensor_flat = tensor(label_img).T.reshape([1400 * 2100])\n#         else:\n#             # Creating a new label image\n#             label_tensor_flat = torch.tensor([void_class]*1400*2100, dtype=torch.uint8)\n            \n#         if not \"nan\" in str(row.EncodedPixels):\n#             encoded_pixels_a = row.EncodedPixels.split()\n#             encoded_pixels_pair_a = [[int(encoded_pixels_a[i]), int(encoded_pixels_a[i + 1])] for i in range(0, len(encoded_pixels_a) - 1, 2)]\n            \n#             for pair in encoded_pixels_pair_a:\n#                 start_px = pair[0]-1\n#                 length = pair[1]\n#                 label_tensor_flat[start_px:(start_px+length)] = label_idx\n        \n#         label_img = PILImage.create(label_tensor_flat.reshape([2100, 1400]).T, mode='L', size=train_img_size)\n#         label_img.save(label_img_path)\n        \n#     #break    \n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Create Label Images","metadata":{}},{"cell_type":"code","source":"from os import path\nfrom torchvision import transforms\n\ntrain_image_paths = get_image_files(train_images_path)\n\nfor train_img_path in train_image_paths:\n    \n    # get the train_image filename\n    train_image_fn = os.path.basename(train_img_path)\n    \n    # prepare label image path\n    label_img_fn = train_img_path.stem + \".png\"\n    label_img_path = label_images_path/label_img_fn\n    \n\n    #placeholder array for the different class tensors\n    tensor_a = []\n\n    # for all codes\n    for code in codes:\n        row = train_df[train_df.Image_Label == f'{train_image_fn}_{code}'].iloc[0]\n\n        # initialize the mask image tensor\n        mask_img_t = torch.zeros(1400*2100, dtype=torch.uint8)\n\n        if not \"nan\" in str(row.EncodedPixels):\n            encoded_pixels_a = row.EncodedPixels.split()\n            encoded_pixels_pair_a = [[int(encoded_pixels_a[i]), int(encoded_pixels_a[i + 1])] for i in range(0, len(encoded_pixels_a) - 1, 2)]\n\n            for pair in encoded_pixels_pair_a:\n                start_px = pair[0]-1\n                length = pair[1]\n                mask_img_t[start_px:(start_px+length)] = 1\n\n        # transposed mask image tensor        \n        mask_img_t_t = mask_img_t.reshape([2100, 1400]).T\n        tensor_a.append(mask_img_t_t)\n\n    mask_img_t_full = torch.stack(tensor_a)\n    \n    mask_img_t_full = mask_img_t_full.permute(1, 2, 0)\n    \n    label_img = PILImage.create(mask_img_t_full, mode='RGB')\n    label_img.save(label_img_path)\n    \n    tx = tensor(Image.open(label_img_path))\n    \n#     print(torch.unique(tx[:,:,3]))\n#     show_image(PILMask.create(tx[:,:,0]))\n#     show_image(PILMask.create(tx[:,:,1]))\n#     show_image(PILMask.create(tx[:,:,2]))\n#     show_image(PILMask.create(tx[:,:,3]))\n    \n#     break","metadata":{"execution":{"iopub.status.busy":"2021-06-24T07:47:23.814561Z","iopub.execute_input":"2021-06-24T07:47:23.815027Z","iopub.status.idle":"2021-06-24T07:48:16.462911Z","shell.execute_reply.started":"2021-06-24T07:47:23.814995Z","shell.execute_reply":"2021-06-24T07:48:16.46103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Show Random Image with Masks","metadata":{}},{"cell_type":"code","source":"label_fns = get_image_files(label_images_path)","metadata":{"execution":{"iopub.status.busy":"2021-06-24T07:48:20.241322Z","iopub.execute_input":"2021-06-24T07:48:20.241811Z","iopub.status.idle":"2021-06-24T07:48:20.246457Z","shell.execute_reply.started":"2021-06-24T07:48:20.24178Z","shell.execute_reply":"2021-06-24T07:48:20.245627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# img_fns = get_image_files(cloud_path/'train_images')\n\nlabel_fn = random.sample(label_fns, 1)[0]\n#label_fn = cloud_path/'train_images_labels'/'006c5a6.png'\n\nimg_fn = cloud_path/'train_images'/f'{label_fn.stem}.jpg'\n\nlabel_t = tensor(Image.open(label_fn))\n\ncloud_img = PILImage.create(img_fn)\n\nmask = PILMask.create(label_t[:,:, 0])\nmask1 = PILMask.create(label_t[:,:, 1])\nmask2 = PILMask.create(label_t[:,:, 2])\nmask3 = PILMask.create(label_t[:,:, 3])\n\n\n_,axs = plt.subplots(3,4, figsize=(21,14))\n\ncloud_img.show(ctx=axs[0][0], title='image')\nmask.show(ax=axs[1][0], title=f'Mask {codes[0]}')\nmask1.show(ax=axs[1][1], title=f'Mask {codes[1]}')\nmask2.show(ax=axs[1][2], title=f'Mask {codes[2]}')\nmask3.show(ax=axs[1][3], title=f'Mask {codes[3]}')\n\ncloud_img.show(ax=axs[2][0], title='superimposed')\nmask.show(ctx=axs[2][0]);\n\ncloud_img.show(ax=axs[2][1])\nmask1.show(ctx=axs[2][1]);\n\ncloud_img.show(ax=axs[2][2])\nmask2.show(ctx=axs[2][2]);\n\ncloud_img.show(ax=axs[2][3])\nmask3.show(ctx=axs[2][3]);","metadata":{"execution":{"iopub.status.busy":"2021-06-24T07:48:44.855304Z","iopub.execute_input":"2021-06-24T07:48:44.855639Z","iopub.status.idle":"2021-06-24T07:48:49.740276Z","shell.execute_reply.started":"2021-06-24T07:48:44.855609Z","shell.execute_reply":"2021-06-24T07:48:49.739222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Untested Yet - copy form lesson 1\n\n#path = untar_data(URLs.CAMVID_TINY)\ndls = SegmentationDataLoaders.from_label_func(\n    path, bs=8, fnames = get_image_files(path/\"train_image\"),\n    label_func = lambda o: path/'train_images_labels'/f'{o.stem}.png',\n    codes = np.loadtxt(path/'codes.txt', dtype=str)\n)\n\nlearn = unet_learner(dls, resnet18)\nlearn.fine_tune(20)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}