{"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":"<h1 style=\"font-family: Verdana; font-size: 28px; font-style: normal; font-weight: bold; text-decoration: none; text-transform: none; letter-spacing: 3px; background-color: #CCCCFF; color: black;\"><center><br>[Data Augmentation] 👀: Gaussian-Laplacian Pyramid Blending </center></h1>\n                                                      \n<center><img src = \"https://drive.google.com/uc?id=1pbIvjTlhGywfhiMTqcsdOB5LSHlklM90\"/></center>   \n\n<h5 style=\"text-align: center; font-family: Verdana; font-size: 12px; font-style: normal; font-weight: bold; text-decoration: None; text-transform: none; letter-spacing: 1px; color: black; background-color: #ffffff;\">CREATED BY: NGHI HUYNH and YUAN HONG</h5>","metadata":{}},{"cell_type":"markdown","source":"<p id=\"toc\"></p>\n<h2 class=\"list-group-item list-group-item-action active\" data-toggle=\"list\" style=\"font-family: Verdana; font-size: 24px; font-style: normal; font-weight: bold; text-decoration: none; text-transform: none; letter-spacing: 3px; background-color: #CCCCFF; color: black;\" role=\"tab\" aria-controls=\"home\"><center><br>CONTENTS</center></h2>\n\n<h3 style=\"text-indent: 10vw; font-family: Verdana; font-size: 16px; font-style: normal; font-weight: normal; text-decoration: none; text-transform: none; letter-spacing: 2px; color: black; background-color: #ffffff;\"><a href=\"#motive\">0&nbsp;&nbsp;&nbsp;&nbsp;MOTIVATION</a></h3>\n\n---\n\n<h3 style=\"text-indent: 10vw; font-family: Verdana; font-size: 16px; font-style: normal; font-weight: normal; text-decoration: none; text-transform: none; letter-spacing: 2px; color: black; background-color: #ffffff;\"><a href=\"#blending\">1&nbsp;&nbsp;&nbsp;&nbsp;GAUSSIAN-LAPLACIAN PYRAMID BLENDING</a></h3>\n\n---\n\n<h3 style=\"text-indent: 10vw; font-family: Verdana; font-size: 16px; font-style: normal; font-weight: normal; text-decoration: none; text-transform: none; letter-spacing: 2px; color: black; background-color: #ffffff;\"><a href=\"#demo\">2&nbsp;&nbsp;&nbsp;&nbsp;DEMO</a></h3>\n\n---\n","metadata":{}},{"cell_type":"markdown","source":"<div class=\"list-group\" id=\"list-tab\" role=\"tablist\">\n<h3 class=\"list-group-item list-group-item-action active\" data-toggle=\"list\" style='background:#F08080; border:0; color:black' role=\"tab\" aria-controls=\"home\"><center><br>If you find this notebook useful, do give me an upvote, it motivates me a lot.<br><br> This notebook is still a work in progress. Keep checking for further developments!😊</center></h3>","metadata":{}},{"cell_type":"markdown","source":"<a id=\"imports\"></a>\n\n<h2 style=\"font-family: Verdana; font-size: 24px; font-style: normal; font-weight: bold; text-decoration: none; text-transform: none; letter-spacing: 3px; background-color: #CCCCFF; color: black;\" id=\"motive\"><left><br>&nbsp;0. MOTIVATION <a href=\"#toc\">&#10514;</a><br></left> </h2>","metadata":{}},{"cell_type":"markdown","source":"While working on this challenge, I realize some problems that we are facing. Thus, I want to suggest a potential solution for those problems. I also create a discussion thread: [Data Augmentation: Gaussian-Laplacian Pyramid Blending](https://www.kaggle.com/competitions/hubmap-organ-segmentation/discussion/341235) to follow up.\n\nThere are **3** main problems:\n\n1. **[Stain Variations](https://www.ncbi.nlm.nih.gov/pmc/articles/PMC5374099/)**: \n    * Histopathological images (HIs) stained with **DAB + H** based on immunohistochemistry (IHC) technique leads to inter-batch variations. In DAB staining, brown chromogen reveals protein expression, while hematoxylin is for tissue counterstaining.\n\n    * HIs stained with **H&E**: hematoxylin highlights the nuclei with a blueish color, and eosin highlights the cytoplasm and extracellular matrix in pink\n    \n    * HIs stained with **PAS**: magenta to red color for PAS positive material, blue color for cell nuclei\n    \n    ![](https://drive.google.com/uc?id=1ZDlg8iveb1ZI92oHvwtrppsCCR3HmvMn)\n    \n    * **[Figure 1](https://www.ncbi.nlm.nih.gov/core/lw/2.0/html/tileshop_pmc/tileshop_pmc_inline.html?title=Click%20on%20image%20to%20zoom&p=PMC3&id=5374099_AHC16025f02.jpg)**: A-E (left-right, top-bottom): Results of hematoxylin and eosin (H&E), periodic acid-Schiff (PAS), and immunohistochemical staining (3,3'-diaminobenzidine and hematoxylin, DAB&H) of renal tissue from control rats. **A.** H&E staining. D, distal tubules; G, glomerulus; P, proximal tubules. **B.** PAS staining. Arrows, brush borders; BM, tubular basement membranes; D, distal tubules; P, proximal tubules. **C.** iNOS immunostaining (DAB&H). **D.** BAX immunostaining (DAB&H). **E.** VDR immunostaining (DAB&H).\n\n    => All techniques commonly face differences in intensity, saturation, and hue in the HIs. Color variations may introduce a bias to ML algorithms.\n\n2. **Data Imbalance**: DL models require very large datasets for training to avoid overfitting the models. We have some class imbalance for **spleen**, **lung** and **large intestine**. HI datasets are usually small due to expensive labeling. Data imbalance leads to impacts on supervised learning algorithms\n\n    ![](https://drive.google.com/uc?id=1Ah7dIzDK5MNvrQrPZoSYGLwBFo9pWy4V)\n\n\n3. **Inter- and Intra-Class Variability**: HIs are not only different between classes, but they are also different within classes\n\n    ![](https://drive.google.com/uc?id=12S_xNSa9g5MKapKj-TK6i7Ar_KdHU6oZ)\n\n=> Data augmentation has been actively used to circumvent these problems. In this notebook, I'll apply an image blending approach using the **Gaussian-Laplacian pyramid blending** technique to generate more data within classes. This technique aims to improve the generalization ability of ML algorithms dealing with HIs.\n","metadata":{}},{"cell_type":"markdown","source":"<a id=\"blending\"></a>\n\n<h2 style=\"font-family: Verdana; font-size: 24px; font-style: normal; font-weight: bold; text-decoration: none; text-transform: none; letter-spacing: 3px; background-color: #CCCCFF; color: black;\" id=\"blending\"><left><br>&nbsp;1. Gaussian-Laplacian Pyramid Blending <a href=\"#toc\">&#10514;</a><br></left> </h2>","metadata":{}},{"cell_type":"markdown","source":"# Background\n\n### Objective: \nGiven 2 histopathological images within the same class, and an image mask, blend the images in a seamless way\n\n<center><img src = \"https://drive.google.com/uc?id=1JUiPlQee5lpYf1JLMiz1A8BrRO2HoNHD\"></center> \n\n### Algorithm Overview:\n\n**1.** Build **Laplacian** pyramids $LA$ and $LB$ from images A and B\n\n**2.** Build a **Gaussian** pyramid $GR$ from selected region R (mask that says which pixels come from left and which from right)\n\n**3.** Form a **combined** pyramid $LS$ from $LA$ and $LB$ using nodes of $GR$ as weights:\n\n $$LS(i,j) = GR(i,j)*LA(i,j) + (1-GR(i,j))*LB(i,j)$$\n    \n**4.** Collapse the $LS$ pyramid to get the final blended image\n\n[**Reference**: Data Augmentation for Histopathological Images Based on Gaussian-Laplacian Pyramid Blending](https://arxiv.org/pdf/2002.00072.pdf)","metadata":{}},{"cell_type":"markdown","source":"# Imports","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport cv2\nimport imageio\nimport tifffile as tiff\nfrom PIL import Image\nimport numpy as np\nimport seaborn as sns\nimport scipy.signal as sig\nfrom scipy import misc\nimport matplotlib.pyplot as plt\nfrom scipy import ndimage\nimport cv2\nimport imageio\nfrom PIL import Image\nimport random","metadata":{"execution":{"iopub.status.busy":"2022-09-27T22:00:33.517051Z","iopub.execute_input":"2022-09-27T22:00:33.517594Z","iopub.status.idle":"2022-09-27T22:00:35.273259Z","shell.execute_reply.started":"2022-09-27T22:00:33.517487Z","shell.execute_reply":"2022-09-27T22:00:35.272218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Gaussian Kernel","metadata":{}},{"cell_type":"code","source":"kernel = (1.0/256)*np.array([[1, 4, 6, 4, 1],[4, 16, 24, 16, 4],[6, 24, 36, 24, 6],[4, 16, 24, 16, 4],[1, 4, 6, 4, 1]])","metadata":{"execution":{"iopub.status.busy":"2022-09-27T22:00:49.915761Z","iopub.execute_input":"2022-09-27T22:00:49.916831Z","iopub.status.idle":"2022-09-27T22:00:49.924941Z","shell.execute_reply.started":"2022-09-27T22:00:49.916773Z","shell.execute_reply":"2022-09-27T22:00:49.923231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(kernel, cmap='seismic')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-27T22:00:50.478224Z","iopub.execute_input":"2022-09-27T22:00:50.478648Z","iopub.status.idle":"2022-09-27T22:00:50.665862Z","shell.execute_reply.started":"2022-09-27T22:00:50.478612Z","shell.execute_reply":"2022-09-27T22:00:50.664713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kernel.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-27T22:00:52.664845Z","iopub.execute_input":"2022-09-27T22:00:52.665377Z","iopub.status.idle":"2022-09-27T22:00:52.674798Z","shell.execute_reply.started":"2022-09-27T22:00:52.665336Z","shell.execute_reply":"2022-09-27T22:00:52.673049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Gaussian-Laplacian blending","metadata":{}},{"cell_type":"code","source":"# https://github.com/twyunting/Laplacian-Pyramids\n\n\ndef interpolate(image):\n    \"\"\"\n    Interpolates an image with upsampling rate r = 2\n    \"\"\"\n    image_up = np.zeros((2*image.shape[0], 2*image.shape[1]))\n    # Upsample\n    image_up[::2, ::2] = image\n    # Blur (we need to scale this up since the kernel has unit area)\n    # (The length and width are both doubled, so the area is quadrupled)\n    #return sig.convolve2d(image_up, 4*kernel, 'same')\n    return ndimage.filters.convolve(image_up,4*kernel, mode='constant')\n                                \ndef decimate(image):\n    \"\"\"\n    Decimates an image with downsampling rate r=2.\n    \"\"\"\n    image_blur = ndimage.filters.convolve(image,kernel, mode='constant')\n    # Downsample\n    return image_blur[::2, ::2]                                \n               \n                                      \n# here is the constructions of pyramids\ndef pyramids(image):\n    \"\"\"\n    Constructs Gaussian and Laplacian pyramids.\n    Parameters :\n    image  : the original image (i.e. base of the pyramid)\n    Returns :\n    G   : the Gaussian pyramid\n    L   : the Laplacian pyramid\n    \"\"\"\n    # Initialize pyramids\n    G = [image, ]\n    L = []\n\n    # Build the Gaussian pyramid to maximum depth\n    while image.shape[0] >= 2 and image.shape[1] >= 2:\n        image = decimate(image)\n        G.append(image)\n\n   # Build the Laplacian pyramid\n    for i in range(len(G) - 1):\n        L.append(G[i] - interpolate(G[i + 1]))\n\n    return G[:-1], L\n\n\n# Build Gaussian pyramid and Laplacian pyramids from images A and B, also mask\n# Reference: https://becominghuman.ai/image-blending-using-laplacian-pyramids-2f8e9982077f\ndef pyramidBlending(A, B, mask):\n    [GA, LA] = pyramids(A)\n    [GB ,LB] = pyramids(B)\n    # Build a Gaussian pyramid GR from selected region R (mask that says which pixels come from left and which from right)\n    [Gmask, LMask] = pyramids(mask)\n    # Form a combined pyramid LS from LA and LB using nodes of GR as weights\n    # Equation: LS(i, j) = GR(I, j)*LA(I, j) + (1-GR(I, j)* LB(I, j))\n    # Collapse the LS pyramid to get the final blended image\n    blend = []\n    for i in range(len(LA)):\n        # LS = np.max(Gmask[i])*LA[i] + (1-np.max(Gmask[i]))*LB[i]\n        # make sure the color with in 255 (white)\n        LS = Gmask[i]/255*LA[i] + (1-Gmask[i]/255)*LB[i]\n        blend.append(LS)\n    return blend\n\n# reconstruct the pyramids as well as upsampling and add up with each level\ndef reconstruct(pyramid):\n    rows, cols = pyramid[0].shape\n    res = np.zeros((rows, cols + cols//2), dtype= np.double)\n    # start the smallest pyramid so we need to reverse the order\n    revPyramid = pyramid[::-1]\n    stack = revPyramid[0]\n    # start with the second index\n    for i in range(1, len(revPyramid)):\n        stack = interpolate(stack) + revPyramid[i] # upsampling simultaneously\n    return stack\n   \n# https://compvisionlab.wordpress.com/2013/05/13/image-blending-using-pyramid/\n# Besides pyramid Blending, we need to blend image's color\ndef colorBlending(img1, img2, mask, organ, i=0):\n    # split to 3 basic color, then using pyramidBlending and reconstruct it, respectively\n    img1R,img1G,img1B = cv2.split(img1)\n    img2R,img2G,img2B = cv2.split(img2)\n    # reconstruct each color channel and convert to uint8 to return proper img\n    R = reconstruct(pyramidBlending(img1R, img2R, mask))\n    G = reconstruct(pyramidBlending(img1G, img2G, mask))\n    B = reconstruct(pyramidBlending(img1B, img2B, mask))\n    output = cv2.merge((R, G, B))#.astype('uint8')\n    imageio.imsave(f\"output_{organ}_{i}.png\", output)\n    img = cv2.imread(f\"output_{organ}_{i}.png\")\n    #img = imageio.v2.imread(\"output.png\")\n    return img\n    \ndef Blending_mask(mask1, mask2, mask0, organ, i=0):\n#     mask2 = mask2.resize(mask1.size)\n    mask00 = imageio.imsave(\"output_mask.png\", mask0)\n    mask00 = Image.open(\"output_mask.png\")\n    output = Image.composite(mask1, mask2, mask00)\n    output = output.save(f\"output_mask_{organ}_{i}.png\")\n    output = Image.open(f\"output_mask_{organ}_{i}.png\")\n    return output\n","metadata":{"_kg_hide-input":false,"execution":{"iopub.status.busy":"2022-09-27T22:00:54.687792Z","iopub.execute_input":"2022-09-27T22:00:54.688313Z","iopub.status.idle":"2022-09-27T22:00:54.710382Z","shell.execute_reply.started":"2022-09-27T22:00:54.688275Z","shell.execute_reply":"2022-09-27T22:00:54.708905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"demo\"></a>\n\n<h2 style=\"font-family: Verdana; font-size: 24px; font-style: normal; font-weight: bold; text-decoration: none; text-transform: none; letter-spacing: 3px; background-color: #CCCCFF; color: black;\" id=\"demo\"><left><br>&nbsp;2. DEMO <a href=\"#toc\">&#10514;</a><br></left> </h2>","metadata":{}},{"cell_type":"code","source":"data_frame = pd.read_csv('../input/hubmap-organ-segmentation/train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-09-27T22:00:57.429643Z","iopub.execute_input":"2022-09-27T22:00:57.430131Z","iopub.status.idle":"2022-09-27T22:00:57.894504Z","shell.execute_reply.started":"2022-09-27T22:00:57.430095Z","shell.execute_reply":"2022-09-27T22:00:57.893104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# generate mask\nmask0 = np.zeros((512,512))\nmask0[:, 256:] = 255","metadata":{"execution":{"iopub.status.busy":"2022-09-27T22:00:57.896770Z","iopub.execute_input":"2022-09-27T22:00:57.897294Z","iopub.status.idle":"2022-09-27T22:00:57.905973Z","shell.execute_reply.started":"2022-09-27T22:00:57.897250Z","shell.execute_reply":"2022-09-27T22:00:57.904231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(mask0, cmap='gray')\nplt.title('Mask')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-27T22:00:58.603703Z","iopub.execute_input":"2022-09-27T22:00:58.604132Z","iopub.status.idle":"2022-09-27T22:00:58.831602Z","shell.execute_reply.started":"2022-09-27T22:00:58.604099Z","shell.execute_reply":"2022-09-27T22:00:58.830771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Helper functions","metadata":{}},{"cell_type":"code","source":"def read_image_mask(df, img_id):\n    image = imageio.v2.imread(str('../input/hubmap-organ-512512/train/' f\"{img_id}_0000.png\"))\n    mask = Image.open(str('../input/hubmap-organ-512512/masks/' f\"{img_id}_0000.png\"))\n    return image, mask\n\ndef highlight(row):\n    df = lambda x: ['background: #CCCCFF' if x.name in row\n                        else '' for i in x]\n    return df\n\ndef plot_blended_img(img_A, img_B, mask, title, organ):\n    plt.figure(figsize=(16,9))\n    \n    plt.subplot(1,4,1)\n    plt.imshow(img_A)\n    plt.title('Image A')\n    \n    plt.subplot(1,4,2)\n    plt.imshow(img_B)\n    plt.title('Image B')\n    \n    plt.subplot(1,4,3)\n    plt.imshow(mask,cmap='gray')\n    plt.title('Mask')\n    \n    plt.subplot(1,4,4)\n    blended_img = colorBlending(img_A, img_B, mask, organ=organ)\n    plt.imshow(blended_img)\n    plt.title('Blended Image')\n    \n    plt.suptitle(f'{title}', fontsize=20, y=0.75)\n    plt.tight_layout()\n    plt.show()\n    \ndef plot_blended_mask(mask_A, mask_B, mask, title, organ):\n    plt.figure(figsize=(16,9))\n    \n    plt.subplot(1,4,1)\n    plt.imshow(mask_A)\n    plt.title('Mask A')\n    \n    plt.subplot(1,4,2)\n    plt.imshow(mask_B)\n    plt.title('Mask B')\n    \n    plt.subplot(1,4,3)\n    plt.imshow(mask,cmap='gray')\n    plt.title('Mask')\n    \n    plt.subplot(1,4,4)\n    blended_mask = Blending_mask(mask_A, mask_B, mask, organ=organ )\n    plt.imshow(blended_mask)\n    plt.title('Blended Mask')\n    \n    plt.suptitle(f'{title}', fontsize=20, y=0.75)\n    plt.tight_layout()\n    plt.show()\n    \ndef list_ids_organ(df, o):\n    \"\"\"\n    Args: \n        df: dataframe\n        o: specific organ \n    \n    Returns:\n        list_ids: list of ids based on organ\n    \"\"\"\n    list_ids = list(df.loc[df['organ']==o].id)\n    return list_ids","metadata":{"_kg_hide-input":false,"execution":{"iopub.status.busy":"2022-09-27T22:01:01.503217Z","iopub.execute_input":"2022-09-27T22:01:01.504544Z","iopub.status.idle":"2022-09-27T22:01:01.519453Z","shell.execute_reply.started":"2022-09-27T22:01:01.504503Z","shell.execute_reply":"2022-09-27T22:01:01.517800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Metadata","metadata":{}},{"cell_type":"code","source":"# create an organ dataframe with ids only\norgans = {'prostate': [],'spleen': [],'lung': [],'kidney': [],'largeintestine': []}\nfor organ, ids in organs.items():\n    list_organ = list_ids_organ(data_frame, organ)\n    ids.append(list_organ)\ndf_organs = pd.DataFrame.from_dict(organs, orient='index').reset_index()\ndf_organs.columns = ['organ', 'ids']\ndf_organs.style.hide_index().apply(highlight([1,3]), axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-09-27T22:01:03.719620Z","iopub.execute_input":"2022-09-27T22:01:03.720060Z","iopub.status.idle":"2022-09-27T22:01:03.818502Z","shell.execute_reply.started":"2022-09-27T22:01:03.720025Z","shell.execute_reply":"2022-09-27T22:01:03.817057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Prostate","metadata":{}},{"cell_type":"code","source":"df_organs.style.hide_index().apply(highlight([0]), axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-09-27T22:01:06.621311Z","iopub.execute_input":"2022-09-27T22:01:06.621826Z","iopub.status.idle":"2022-09-27T22:01:06.639306Z","shell.execute_reply.started":"2022-09-27T22:01:06.621787Z","shell.execute_reply":"2022-09-27T22:01:06.637458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random.seed(10)\nrandom_prostate = random.sample(df_organs.loc[df_organs['organ'] == 'prostate'].ids[0], 2) #.ids[0] -> prostate\nimg_A, mask_A = read_image_mask(data_frame,random_prostate[0])\nimg_B, mask_B = read_image_mask(data_frame,random_prostate[1])\nimg_A.shape, img_B.shape, mask_A.size, mask_B.size","metadata":{"execution":{"iopub.status.busy":"2022-09-27T22:01:07.065390Z","iopub.execute_input":"2022-09-27T22:01:07.066636Z","iopub.status.idle":"2022-09-27T22:01:07.184646Z","shell.execute_reply.started":"2022-09-27T22:01:07.066565Z","shell.execute_reply":"2022-09-27T22:01:07.182786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_blended_img(img_A, img_B, mask0, 'Prostate Image', 'prostate')\nplot_blended_mask(mask_A, mask_B, mask0, 'Prostate Mask', 'prostate')","metadata":{"execution":{"iopub.status.busy":"2022-09-27T22:01:08.535373Z","iopub.execute_input":"2022-09-27T22:01:08.536210Z","iopub.status.idle":"2022-09-27T22:01:11.419491Z","shell.execute_reply.started":"2022-09-27T22:01:08.536166Z","shell.execute_reply":"2022-09-27T22:01:11.418340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Spleen","metadata":{}},{"cell_type":"code","source":"df_organs.style.hide_index().apply(highlight([1]), axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-09-27T22:01:11.421327Z","iopub.execute_input":"2022-09-27T22:01:11.421832Z","iopub.status.idle":"2022-09-27T22:01:11.437059Z","shell.execute_reply.started":"2022-09-27T22:01:11.421800Z","shell.execute_reply":"2022-09-27T22:01:11.435314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random.seed(10)\nrandom_spleen = random.sample(df_organs.loc[df_organs['organ'] == 'spleen'].ids[1], 2) #.ids[1] -> row 1: spleen\nimg_A, mask_A = read_image_mask(data_frame,random_spleen[0])\nimg_B, mask_B = read_image_mask(data_frame,random_spleen[1])\nimg_A.shape, img_B.shape, mask_A.size, mask_B.size","metadata":{"execution":{"iopub.status.busy":"2022-09-27T22:01:11.695966Z","iopub.execute_input":"2022-09-27T22:01:11.697019Z","iopub.status.idle":"2022-09-27T22:01:11.771132Z","shell.execute_reply.started":"2022-09-27T22:01:11.696968Z","shell.execute_reply":"2022-09-27T22:01:11.769562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_blended_img(img_A, img_B, mask0, 'Spleen Image', 'spleen')\nplot_blended_mask(mask_A, mask_B, mask0, 'Spleen Mask', 'spleen')","metadata":{"execution":{"iopub.status.busy":"2022-09-27T22:01:13.407800Z","iopub.execute_input":"2022-09-27T22:01:13.409272Z","iopub.status.idle":"2022-09-27T22:01:16.013161Z","shell.execute_reply.started":"2022-09-27T22:01:13.409215Z","shell.execute_reply":"2022-09-27T22:01:16.011749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Lung","metadata":{}},{"cell_type":"code","source":"df_organs.style.hide_index().apply(highlight([2]), axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-09-27T22:01:16.015785Z","iopub.execute_input":"2022-09-27T22:01:16.016158Z","iopub.status.idle":"2022-09-27T22:01:16.032314Z","shell.execute_reply.started":"2022-09-27T22:01:16.016109Z","shell.execute_reply":"2022-09-27T22:01:16.030618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random.seed(10)\nrandom_lung = random.sample(df_organs.loc[df_organs['organ'] == 'lung'].ids[2], 2)\nimg_A, mask_A = read_image_mask(data_frame,random_lung[0])\nimg_B, mask_B = read_image_mask(data_frame,random_lung[1])\nimg_A.shape, img_B.shape, mask_A.size, mask_B.size","metadata":{"execution":{"iopub.status.busy":"2022-09-27T22:01:16.034030Z","iopub.execute_input":"2022-09-27T22:01:16.034389Z","iopub.status.idle":"2022-09-27T22:01:16.114533Z","shell.execute_reply.started":"2022-09-27T22:01:16.034360Z","shell.execute_reply":"2022-09-27T22:01:16.113618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_blended_img(img_A, img_B, mask0, 'Lung Image', 'lung')\nplot_blended_mask(mask_A, mask_B, mask0, 'Lung Mask', 'lung')","metadata":{"execution":{"iopub.status.busy":"2022-09-27T22:01:18.200164Z","iopub.execute_input":"2022-09-27T22:01:18.200928Z","iopub.status.idle":"2022-09-27T22:01:21.105299Z","shell.execute_reply.started":"2022-09-27T22:01:18.200893Z","shell.execute_reply":"2022-09-27T22:01:21.104037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Kidney","metadata":{}},{"cell_type":"code","source":"df_organs.style.hide_index().apply(highlight([3]), axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-09-27T22:01:21.107609Z","iopub.execute_input":"2022-09-27T22:01:21.108686Z","iopub.status.idle":"2022-09-27T22:01:21.125042Z","shell.execute_reply.started":"2022-09-27T22:01:21.108522Z","shell.execute_reply":"2022-09-27T22:01:21.123562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random.seed(10)\nrandom_kidney = random.sample(df_organs.loc[df_organs['organ'] == 'kidney'].ids[3], 2)\nimg_A, mask_A = read_image_mask(data_frame,random_kidney[0])\nimg_B, mask_B = read_image_mask(data_frame,random_kidney[1])\nimg_A.shape, img_B.shape, mask_A.size, mask_B.size","metadata":{"execution":{"iopub.status.busy":"2022-09-27T22:01:22.246570Z","iopub.execute_input":"2022-09-27T22:01:22.247090Z","iopub.status.idle":"2022-09-27T22:01:22.332848Z","shell.execute_reply.started":"2022-09-27T22:01:22.247051Z","shell.execute_reply":"2022-09-27T22:01:22.331588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_blended_img(img_A, img_B, mask0, 'Kidney Image', 'kidney')\nplot_blended_mask(mask_A, mask_B, mask0, 'Kidney Mask', 'kidney')","metadata":{"execution":{"iopub.status.busy":"2022-09-27T22:01:22.838947Z","iopub.execute_input":"2022-09-27T22:01:22.840333Z","iopub.status.idle":"2022-09-27T22:01:25.088983Z","shell.execute_reply.started":"2022-09-27T22:01:22.840279Z","shell.execute_reply":"2022-09-27T22:01:25.087984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Large Intestine","metadata":{}},{"cell_type":"code","source":"df_organs.style.hide_index().apply(highlight([4]), axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-09-27T22:01:25.426103Z","iopub.execute_input":"2022-09-27T22:01:25.426597Z","iopub.status.idle":"2022-09-27T22:01:25.444889Z","shell.execute_reply.started":"2022-09-27T22:01:25.426564Z","shell.execute_reply":"2022-09-27T22:01:25.443113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random.seed(10)\nrandom_largeintestine = random.sample(df_organs.loc[df_organs['organ'] == 'largeintestine'].ids[4], 2)\nimg_A, mask_A = read_image_mask(data_frame,random_largeintestine[0])\nimg_B, mask_B = read_image_mask(data_frame,random_largeintestine[1])\nimg_A.shape, img_B.shape, mask_A.size, mask_B.size","metadata":{"execution":{"iopub.status.busy":"2022-09-27T22:01:27.676383Z","iopub.execute_input":"2022-09-27T22:01:27.677469Z","iopub.status.idle":"2022-09-27T22:01:27.760200Z","shell.execute_reply.started":"2022-09-27T22:01:27.677408Z","shell.execute_reply":"2022-09-27T22:01:27.758635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_blended_img(img_A, img_B, mask0, 'Large Intestine Image', 'largeintestine')\nplot_blended_mask(mask_A, mask_B, mask0, 'Large Intestine Mask', 'largeintestine')","metadata":{"execution":{"iopub.status.busy":"2022-09-27T22:01:28.141381Z","iopub.execute_input":"2022-09-27T22:01:28.141867Z","iopub.status.idle":"2022-09-27T22:01:30.726445Z","shell.execute_reply.started":"2022-09-27T22:01:28.141830Z","shell.execute_reply":"2022-09-27T22:01:30.722528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"=> These blending images and masks might help to augment the dataset and to balance the respective classes under investigation. 👀","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}