{"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><center>Sartorius: InDepth EDA + Explanation + Model + W&B</center></h1>\n                                                      \n<center><img src = \"https://wallpaperaccess.com/full/273943.jpg\" width = \"750\" height = \"500\"/></center>                                                                            ","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"markdown","source":"<h2 class=\"list-group-item list-group-item-action active\" data-toggle=\"list\" style='background:orange; border:0; color:white' role=\"tab\" aria-controls=\"home\"><center>Contents</center></h2>","metadata":{}},{"cell_type":"markdown","source":"1. [Competition Overview](#competition-overview)    \n2. [Alzheimer's Disease](#alzheimers-disease)  \n3. [Brain Tumor](#brain-tumor)  \n4. [Libraries](#libraries)  \n5. [Weights and Biases](#weights-and-biases)  \n6. [Global Config](#global-config)  \n7. [Load Datasets](#load-datasets)\n8. [Tabular Exploration](#tabular-exploration)  \n9. [Distribution Plots](#distribution-plots)\n10. [Image View](#image-view)  \n11. [Basic Image Exploration](#basic-image-exploration)\n12. [Intermediate Image Exploration](#intermediate-image-exploration)\n13. [Advanced Image Exploration](#advanced-image-exploration)\n14. [Instance Mask Viz](#instance-mask-viz)\n15. [U-Net Model](#unet-model)\n16. [References](#references)  ","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:maroon; border:0; color:white' role=\"tab\" aria-controls=\"home\"><center>If you find this notebook useful, do give me an upvote, it helps to keep up my motivation. This notebook will be updated frequently so keep checking for furthur developments.</center></h3>","metadata":{}},{"cell_type":"markdown","source":"<a id=\"competition-overview\"></a>\n<div class=\"list-group\" id=\"list-tab\" role=\"tablist\">\n<h2 class=\"list-group-item list-group-item-action active\" data-toggle=\"list\" style='background:orange; border:0; color:white' role=\"tab\" aria-controls=\"home\"><center>Competition Overview</center></h2>","metadata":{}},{"cell_type":"markdown","source":"## **<span style=\"color:orange;\">Description</span>**\n\n\nIn this competition, you’ll detect and delineate distinct objects of interest in biological images depicting neuronal cell types commonly used in the study of neurological disorders. More specifically, you'll use phase contrast microscopy images to train and test your model for instance segmentation of neuronal cells. Successful models will do this with a high level of accuracy.\n\nIf successful, you'll help further research in neurobiology thanks to the collection of robust quantitative data. Researchers may be able to use this to more easily measure the effects of disease and treatment conditions on neuronal cells. As a result, new drugs could be discovered to treat the millions of people with these leading causes of death and disability.\n\n---","metadata":{}},{"cell_type":"markdown","source":"## **<span style=\"color:orange;\">Evaluation Metric</span>**\n\nThis competition is evaluated on the **mean average precision** at different intersection over union  thresholds. The IoU of a proposed set of object pixels and a set of true object pixels is calculated as:\n\n`IoU(A,B)= (A∩B)/(A∪B)`\n\n---","metadata":{}},{"cell_type":"markdown","source":"> The competition uses a couple of medical terms which many people will be unfamiliar with. To easen out the process I am providing the foundations of the mentioned diseases and proposed treatments to give a stronger domain understanding.","metadata":{}},{"cell_type":"markdown","source":"<a id=\"alzheimers-disease\"></a>\n<div class=\"list-group\" id=\"list-tab\" role=\"tablist\">\n<h2 class=\"list-group-item list-group-item-action active\" data-toggle=\"list\" style='background:orange; border:0; color:white' role=\"tab\" aria-controls=\"home\"><center>Alzheimer's disease</center></h2>\n    \n<center><img src = \"https://www.alz.org/media/HomeOffice/Inline%20Image/dementia-vs-alzheimers-difference-inlineimage.jpg\" width = \"750\" height = \"500\"/></center>                                        ","metadata":{}},{"cell_type":"markdown","source":"## **<span style=\"color:orange;\">What is Alzheimer's Disease?</span>**\n\n- **Alzheimer's disease** is a progressive neurologic disorder that causes the brain to shrink (atrophy) and brain cells to die. \n- Alzheimer's disease is the most common cause of **dementia** — a continuous decline in thinking, behavioral and social skills that affects a person's ability to function independently.\n\n## **<span style=\"color:orange;\">What are the Key Symptoms?</span>**\n\n**Memory loss** is the key symptom of Alzheimer's disease. Early signs include difficulty remembering recent events or conversations. As the disease progresses, memory impairments worsen and other symptoms develop.  \n  \nAt first, a person with Alzheimer's disease may be aware of having **difficulty remembering things and organizing thoughts**. A family member or friend may be more likely to notice how the symptoms worsen.\n\nBrain changes associated with Alzheimer's disease lead to growing trouble with:\n- Memory\n- Thinking and Reasoning\n- Making Judgements and Decisions\n- Planning and Performing Familiar Tasks\n- Changes in Personality and Behaviour\n- Preserved Skills\n\n## **<span style=\"color:orange;\">What are the Causes?</span>**\n\nAt a basic level, **brain proteins** fail to function normally, which disrupts the work of brain cells (neurons) and triggers a series of toxic events. **Neurons are damaged**, lose connections to each other and eventually die.  \n  \nThe damage most often starts in the region of the brain that controls memory, but the process begins years before the first symptoms. The loss of neurons spreads in a somewhat predictable pattern to other regions of the brains. By the late stage of the disease, the brain has shrunk significantly.  \n  \nResearchers trying to understand the cause of Alzheimer's disease are focused on the role of two proteins:  \n  \n- **Plaques** - Beta-amyloid is a fragment of a larger protein. When these fragments cluster together, they appear to have a toxic effect on neurons and to disrupt cell-to-cell communication. These clusters form larger deposits called amyloid plaques, which also include other cellular debris.  \n  \n- **Tangles** - Tau proteins play a part in a neuron's internal support and transport system to carry nutrients and other essential materials. In Alzheimer's disease, tau proteins change shape and organize themselves into structures called neurofibrillary tangles. The tangles disrupt the transport system and are toxic to cells.\n\n## **<span style=\"color:orange;\">Additional Resources for better Understanding</span>**\n\n>- [What is Alzheimer's Disease? Symptoms & Causes | alz.org](https://www.alz.org/alzheimers-dementia/what-is-alzheimers)\n>- [Alzheimer's Disease Fact Sheet | National Institute on Aging](https://www.nia.nih.gov/health/alzheimers-disease-fact-sheet)\n>- [Alzheimer's disease: Symptoms, stages, causes, and treatments](https://www.medicalnewstoday.com/articles/159442)\n>- [Dementia and Alzheimer's Disease Health Center - WebMD](https://www.webmd.com/alzheimers/default.htm)\n\n---","metadata":{}},{"cell_type":"markdown","source":"<a id=\"brain-tumor\"></a>\n<div class=\"list-group\" id=\"list-tab\" role=\"tablist\">\n<h2 class=\"list-group-item list-group-item-action active\" data-toggle=\"list\" style='background:orange; border:0; color:white' role=\"tab\" aria-controls=\"home\"><center>Brain Tumor</center></h2>\n    \n<center><img src = \"https://cdn-prod.medicalnewstoday.com/content/images/articles/320/320427/an-illustration-of-a-brain-tumor.jpg\" width = \"750\" height = \"500\"/></center>                                        ","metadata":{}},{"cell_type":"markdown","source":"## **<span style=\"color:orange;\">What is Brain Tumor?<span>**\n\nA **brain tumor** is a mass or growth of abnormal cells in your brain.  \n  \nMany different types of brain tumors exist. Some brain tumors are **noncancerous (benign)**, and some brain tumors are **cancerous (malignant)**.   \n      \nBrain tumors can begin in your brain (primary brain tumors), or cancer can begin in other parts of your body and spread to your brain as secondary (metastatic) brain tumors\n\n## **<span style=\"color:orange;\">What are the Key Symptoms?</span>**\n\nGeneral signs and symptoms caused by brain tumors may include:\n\n- New onset or change in pattern of headaches\n- Headaches that gradually become more frequent and more severe\n- Unexplained nausea or vomiting\n- Vision problems, such as blurred vision, double vision or loss of peripheral vision\n- Gradual loss of sensation or movement in an arm or a leg\n- Difficulty with balance\n- Speech difficulties\n- Feeling very tired\n- Confusion in everyday matters\n- Difficulty making decisions\n- Inability to follow simple commands\n- Personality or behavior changes\n- Seizures, especially in someone who doesn't have a history of seizures\n- Hearing problems\n\n## **<span style=\"color:orange;\">What are the Causes?</span>**\n\nPrimary brain tumors originate in the brain itself or in tissues close to it, such as in the **brain-covering membranes (meninges), cranial nerves, pituitary gland or pineal gland** .\n  \nPrimary brain tumors begin when normal cells develop changes **(mutations)** in their DNA. A cell's DNA contains the instructions that tell a cell what to do. The mutations tell the cells to grow and divide rapidly and to continue living when healthy cells would die. The result is a mass of abnormal cells, which forms a tumor.\n  \nIn adults, primary brain tumors are much less common than are secondary brain tumors, in which cancer begins elsewhere and spreads to the brain.\n    \n## **<span style=\"color:orange;\">Additional Resources for better Understanding</span>**\n\n>- [Brain Tumor: Types, Risk Factors, and Symptoms - Healthline]()\n>- [Brain Tumor: Symptoms and Signs | Cancer.Net](https://www.cancer.net/cancer-types/brain-tumor/symptoms-and-signs)\n>- [Brain Cancer & Brain Tumor: Symptoms, Causes & Treatments](https://my.clevelandclinic.org/health/diseases/6149-brain-cancer-brain-tumor)\n>- [Brain Tumors—Patient Version - National Cancer Institute](https://www.cancer.gov/types/brain)\n\n---","metadata":{}},{"cell_type":"markdown","source":"<a id=\"libraries\"></a>\n<div class=\"list-group\" id=\"list-tab\" role=\"tablist\">\n<h2 class=\"list-group-item list-group-item-action active\" data-toggle=\"list\" style='background:orange; border:0; color:white' role=\"tab\" aria-controls=\"home\"><center>Libraries</center></h2>","metadata":{}},{"cell_type":"code","source":"%%capture\n!pip install ../input/segmentation-models-wheels/efficientnet_pytorch-0.6.3-py3-none-any.whl\n!pip install ../input/segmentation-models-wheels/pretrainedmodels-0.7.4-py3-none-any.whl\n!pip install ../input/segmentation-models-wheels/timm-0.3.2-py3-none-any.whl\n!pip install ../input/segmentation-models-wheels/segmentation_models_pytorch-0.1.3-py3-none-any.whl","metadata":{"execution":{"iopub.status.busy":"2021-10-20T06:47:25.918775Z","iopub.execute_input":"2021-10-20T06:47:25.919273Z","iopub.status.idle":"2021-10-20T06:48:03.532039Z","shell.execute_reply.started":"2021-10-20T06:47:25.919125Z","shell.execute_reply":"2021-10-20T06:48:03.530716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%sh\npip install -q --upgrade wandb\npip install -q timm","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-10-20T17:42:32.080360Z","iopub.execute_input":"2021-10-20T17:42:32.080761Z","iopub.status.idle":"2021-10-20T17:42:52.923392Z","shell.execute_reply.started":"2021-10-20T17:42:32.080670Z","shell.execute_reply":"2021-10-20T17:42:52.922176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\nimport os\nimport pdb\nimport time\nimport glob\nimport sys\nimport cv2\nimport imageio\nimport joblib\nimport math\nimport random\nimport wandb\nimport math\n\nimport numpy as np\nimport pandas as pd\n\nimport plotly.express as px\nimport matplotlib.pyplot as plt\nimport matplotlib.patches as patches\n\nplt.rcParams.update({'font.size': 18})\nplt.style.use('fivethirtyeight')\n\nimport seaborn as sns\nimport matplotlib\nfrom dask import bag, diagnostics \nfrom mpl_toolkits.mplot3d import Axes3D\n\nfrom termcolor import colored\n\nfrom tqdm.notebook import tqdm\n\n# import timm\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.nn import functional as F\nimport torch.backends.cudnn as cudnn\nfrom torch.optim.lr_scheduler import ReduceLROnPlateau\nfrom torch.utils.data import DataLoader, Dataset, sampler\n\nfrom sklearn.model_selection import KFold\n\nfrom albumentations import (HorizontalFlip, VerticalFlip, \n                            ShiftScaleRotate, Normalize, Resize, \n                            Compose, GaussNoise)\nfrom albumentations.pytorch import ToTensorV2\n\nimport warnings\nwarnings.simplefilter('ignore')\n\n# Activate pandas progress apply bar\ntqdm.pandas()","metadata":{"execution":{"iopub.status.busy":"2021-10-20T17:42:52.925995Z","iopub.execute_input":"2021-10-20T17:42:52.926322Z","iopub.status.idle":"2021-10-20T17:42:58.210676Z","shell.execute_reply.started":"2021-10-20T17:42:52.926280Z","shell.execute_reply":"2021-10-20T17:42:58.209552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Wandb Login\nimport wandb\nwandb.login()","metadata":{"execution":{"iopub.status.busy":"2021-10-20T17:42:58.212330Z","iopub.execute_input":"2021-10-20T17:42:58.212865Z","iopub.status.idle":"2021-10-20T17:43:08.495462Z","shell.execute_reply.started":"2021-10-20T17:42:58.212812Z","shell.execute_reply":"2021-10-20T17:43:08.494452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"weights-and-biases\"></a>\n<div class=\"list-group\" id=\"list-tab\" role=\"tablist\">\n<h2 class=\"list-group-item list-group-item-action active\" data-toggle=\"list\" style='background:orange; border:0; color:white' role=\"tab\" aria-controls=\"home\"><center>Weights and Biases (W&B)</center></h2>","metadata":{}},{"cell_type":"markdown","source":"<center><img src = \"https://i.imgur.com/1sm6x8P.png\" width = \"750\" height = \"500\"/></center>  \n  \n  \n**Weights & Biases** is the machine learning platform for developers to build better models faster. \n\nYou can use W&B's lightweight, interoperable tools to \n- quickly track experiments, \n- version and iterate on datasets, \n- evaluate model performance, \n- reproduce models, \n- visualize results and spot regressions, \n- and share findings with colleagues. \n\nSet up W&B in 5 minutes, then quickly iterate on your machine learning pipeline with the confidence that your datasets and models are tracked and versioned in a reliable system of record.\n\nIn this notebook I will use Weights and Biases's amazing features to perform wonderful visualizations and logging seamlessly. ","metadata":{}},{"cell_type":"markdown","source":"<a id=\"global-config\"></a>\n<div class=\"list-group\" id=\"list-tab\" role=\"tablist\">\n<h2 class=\"list-group-item list-group-item-action active\" data-toggle=\"list\" style='background:orange; border:0; color:white' role=\"tab\" aria-controls=\"home\"><center>Global Config</center></h2>","metadata":{}},{"cell_type":"code","source":"class config:\n    DIRECTORY_PATH = \"../input/sartorius-cell-instance-segmentation\"\n    TRAIN_CSV = DIRECTORY_PATH + \"/train.csv\"\n    TRAIN_PATH = DIRECTORY_PATH + \"/train\"\n    TEST_PATH = DIRECTORY_PATH + \"/test\"\n    TRAIN_SEMI_SUPERVISED_PATH = DIRECTORY_PATH + \"/train_semi_supervised\"\n    \n    SEED = 42\n    \n    RESNET_MEAN = (0.485, 0.456, 0.406)\n    RESNET_STD = (0.229, 0.224, 0.225)\n\n    # (336, 336)\n    IMAGE_RESIZE = (224, 224)\n\n    LEARNING_RATE = 5e-4\n    EPOCHS = 1","metadata":{"execution":{"iopub.status.busy":"2021-10-20T17:43:14.006901Z","iopub.execute_input":"2021-10-20T17:43:14.007689Z","iopub.status.idle":"2021-10-20T17:43:14.014262Z","shell.execute_reply.started":"2021-10-20T17:43:14.007645Z","shell.execute_reply":"2021-10-20T17:43:14.013298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# wandb config\nWANDB_CONFIG = {\n     'competition': 'Sartorius', \n              '_wandb_kernel': 'neuracort'\n    }","metadata":{"execution":{"iopub.status.busy":"2021-10-20T17:43:14.277438Z","iopub.execute_input":"2021-10-20T17:43:14.278155Z","iopub.status.idle":"2021-10-20T17:43:14.282117Z","shell.execute_reply.started":"2021-10-20T17:43:14.278113Z","shell.execute_reply":"2021-10-20T17:43:14.281475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def set_seed(seed=config.SEED):\n    random.seed(seed)\n    os.environ[\"PYTHONHASHSEED\"] = str(seed)\n    np.random.seed(seed)\n    \n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.cuda.manual_seed_all(seed)\n    torch.backends.cudnn.deterministic = True\n    torch.backends.cudnn.benchmark = False\n    \nset_seed()","metadata":{"execution":{"iopub.status.busy":"2021-10-20T17:43:25.834652Z","iopub.execute_input":"2021-10-20T17:43:25.834922Z","iopub.status.idle":"2021-10-20T17:43:25.845005Z","shell.execute_reply.started":"2021-10-20T17:43:25.834895Z","shell.execute_reply":"2021-10-20T17:43:25.843216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"load-datasets\"></a>\n<div class=\"list-group\" id=\"list-tab\" role=\"tablist\">\n<h2 class=\"list-group-item list-group-item-action active\" data-toggle=\"list\" style='background:orange; border:0; color:white' role=\"tab\" aria-controls=\"home\"><center>Load Datasets</center></h2>","metadata":{}},{"cell_type":"markdown","source":"## **<span style=\"color:orange;\">Understanding the Structure of the Dataset</span>**\n\n> ### **<span style=\"color:orange;\">Goal of Competition</span>**\n> \n> In this competition we are segmenting neuronal cells in images. The training annotations are provided as run length encoded masks, and the images are in PNG format. The number of images is small, but the number of annotated objects is quite high. The hidden test set is roughly 240 images.\n> \n> ### **<span style=\"color:orange;\">Files</span>**\n> \n> **train.csv** - IDs and masks for all training objects. None of this metadata is provided for the test set.\n> \n> - `id` - unique identifier for object\n> \n> - `annotation` - run length encoded pixels for the identified neuronal cell\n> \n> - `width` - source image width\n> \n> - `height` - source image height\n> \n> - `cell_type` - the cell line\n> \n> - `plate_time` - time plate was created\n> \n> - `sample_date` - date sample was created\n> \n> - `sample_id` - sample identifier\n> \n> - `elapsed_timedelta` - time since first image taken of sample\n> \n> **sample_submission.csv** - a sample submission file in the correct format\n> \n> **train** - train images in PNG format\n> \n> **test** - test images in PNG format. Only a few test set images are available for download; the remainder can only be accessed by your notebooks when you submit.\n> \n> **train_semi_supervised** - unlabeled images offered in case you want to use additional data for a semi-supervised approach.\n> \n> **LIVECell_dataset_2021** - A mirror of the data from the LIVECell dataset. LIVECell is the predecessor dataset to this competition. You will find extra data for the SH-SHY5Y cell line, plus several other cell lines not covered in the competition dataset that may be of interest for transfer learning.\n>\n>---","metadata":{}},{"cell_type":"code","source":"df_train = pd.read_csv(config.TRAIN_CSV)","metadata":{"execution":{"iopub.status.busy":"2021-10-20T17:45:26.643500Z","iopub.execute_input":"2021-10-20T17:45:26.644307Z","iopub.status.idle":"2021-10-20T17:45:27.308072Z","shell.execute_reply.started":"2021-10-20T17:45:26.644273Z","shell.execute_reply":"2021-10-20T17:45:27.307023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def getImagePaths(path):\n    \"\"\"\n    Function to Combine Directory Path with individual Image Paths\n    \n    parameters: path(string) - Path of directory\n    returns: image_names(string) - Full Image Path\n    \"\"\"\n    image_names = []\n    for dirname, _, filenames in os.walk(path):\n        for filename in tqdm(filenames):\n            fullpath = os.path.join(dirname, filename)\n            image_names.append(fullpath)\n    return image_names","metadata":{"execution":{"iopub.status.busy":"2021-10-20T06:54:58.896831Z","iopub.execute_input":"2021-10-20T06:54:58.897108Z","iopub.status.idle":"2021-10-20T06:54:58.903417Z","shell.execute_reply.started":"2021-10-20T06:54:58.897076Z","shell.execute_reply":"2021-10-20T06:54:58.902541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Get complete image paths for train and test datasets\ntrain_images_path = getImagePaths(config.TRAIN_PATH)\ntest_images_path = getImagePaths(config.TEST_PATH)\ntrain_semi_supervised_path = getImagePaths(config.TRAIN_SEMI_SUPERVISED_PATH)","metadata":{"execution":{"iopub.status.busy":"2021-10-20T06:54:59.078227Z","iopub.execute_input":"2021-10-20T06:54:59.078879Z","iopub.status.idle":"2021-10-20T06:54:59.811023Z","shell.execute_reply.started":"2021-10-20T06:54:59.078841Z","shell.execute_reply":"2021-10-20T06:54:59.810393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"tabular-exploration\"></a>\n<div class=\"list-group\" id=\"list-tab\" role=\"tablist\">\n<h2 class=\"list-group-item list-group-item-action active\" data-toggle=\"list\" style='background:orange; border:0; color:white' role=\"tab\" aria-controls=\"home\"><center>Tabular Exploration</center></h2>","metadata":{}},{"cell_type":"markdown","source":"## **<span style=\"color:orange;\">Train Head</span>**","metadata":{}},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-18T14:32:19.707927Z","iopub.execute_input":"2021-10-18T14:32:19.708683Z","iopub.status.idle":"2021-10-18T14:32:19.723447Z","shell.execute_reply.started":"2021-10-18T14:32:19.708631Z","shell.execute_reply":"2021-10-18T14:32:19.722547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **<span style=\"color:orange;\">Train Info</span>**","metadata":{}},{"cell_type":"code","source":"df_train.info()","metadata":{"execution":{"iopub.status.busy":"2021-10-18T14:32:23.530524Z","iopub.execute_input":"2021-10-18T14:32:23.530787Z","iopub.status.idle":"2021-10-18T14:32:23.572635Z","shell.execute_reply.started":"2021-10-18T14:32:23.530759Z","shell.execute_reply":"2021-10-18T14:32:23.571501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **<span style=\"color:orange;\">Train Dataset Size</span>**","metadata":{}},{"cell_type":"code","source":"print(f\"Training Dataset Shape: {colored(df_train.shape, 'yellow')}\")","metadata":{"execution":{"iopub.status.busy":"2021-10-18T14:32:27.439729Z","iopub.execute_input":"2021-10-18T14:32:27.440000Z","iopub.status.idle":"2021-10-18T14:32:27.445168Z","shell.execute_reply.started":"2021-10-18T14:32:27.439972Z","shell.execute_reply":"2021-10-18T14:32:27.444120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **<span style=\"color:orange;\">Column-wise Unique Values</span>**","metadata":{}},{"cell_type":"code","source":"for col in df_train.columns:\n    print(col + \":\" + colored(str(len(df_train[col].unique())), 'yellow'))","metadata":{"execution":{"iopub.status.busy":"2021-10-18T14:32:33.310538Z","iopub.execute_input":"2021-10-18T14:32:33.310810Z","iopub.status.idle":"2021-10-18T14:32:33.403552Z","shell.execute_reply.started":"2021-10-18T14:32:33.310782Z","shell.execute_reply":"2021-10-18T14:32:33.402615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **<span style=\"color:orange;\">Number of Images in Each Directory</span>**","metadata":{}},{"cell_type":"code","source":"print(f\"Number of train images: {colored(len(train_images_path), 'yellow')}\")\nprint(f\"Number of test images:  {colored(len(test_images_path), 'yellow')}\")","metadata":{"execution":{"iopub.status.busy":"2021-10-18T14:44:45.826261Z","iopub.execute_input":"2021-10-18T14:44:45.827275Z","iopub.status.idle":"2021-10-18T14:44:45.833063Z","shell.execute_reply.started":"2021-10-18T14:44:45.827194Z","shell.execute_reply":"2021-10-18T14:44:45.832092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"distribution-plots\"></a>\n<div class=\"list-group\" id=\"list-tab\" role=\"tablist\">\n<h2 class=\"list-group-item list-group-item-action active\" data-toggle=\"list\" style='background:orange; border:0; color:white' role=\"tab\" aria-controls=\"home\"><center>Distribution Plots</center></h2>","metadata":{}},{"cell_type":"code","source":"def plot_distribution(x):\n\n    fig = px.histogram(\n    df_train, \n    x = x,\n    width = 800,\n    height = 500,\n    )\n    \n    fig.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **<span style=\"color:orange;\">Cell Type Distribution</span>**","metadata":{}},{"cell_type":"code","source":"plot_distribution('cell_type')","metadata":{"execution":{"iopub.status.busy":"2021-10-18T14:58:43.449860Z","iopub.execute_input":"2021-10-18T14:58:43.450188Z","iopub.status.idle":"2021-10-18T14:58:43.864479Z","shell.execute_reply.started":"2021-10-18T14:58:43.450155Z","shell.execute_reply":"2021-10-18T14:58:43.863412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **<span style=\"color:orange;\">Plate Time Distribution</span>**","metadata":{}},{"cell_type":"code","source":"plot_distribution('plate_time')","metadata":{"execution":{"iopub.status.busy":"2021-10-18T15:01:22.575942Z","iopub.execute_input":"2021-10-18T15:01:22.577168Z","iopub.status.idle":"2021-10-18T15:01:23.003792Z","shell.execute_reply.started":"2021-10-18T15:01:22.577102Z","shell.execute_reply":"2021-10-18T15:01:23.002396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **<span style=\"color:orange;\">Elapsed TimeDelta Distribution</span>**","metadata":{}},{"cell_type":"code","source":"plot_distribution('elapsed_timedelta')","metadata":{"execution":{"iopub.status.busy":"2021-10-18T14:59:49.388344Z","iopub.execute_input":"2021-10-18T14:59:49.388648Z","iopub.status.idle":"2021-10-18T14:59:50.024464Z","shell.execute_reply.started":"2021-10-18T14:59:49.388614Z","shell.execute_reply":"2021-10-18T14:59:50.023837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"image-view\"></a>\n<div class=\"list-group\" id=\"list-tab\" role=\"tablist\">\n<h2 class=\"list-group-item list-group-item-action active\" data-toggle=\"list\" style='background:orange; border:0; color:white' role=\"tab\" aria-controls=\"home\"><center>Image View</center></h2>","metadata":{}},{"cell_type":"code","source":"def display_multiple_img(images_paths, rows, cols):\n    \"\"\"\n    Function to Display Images from Dataset.\n    \n    parameters: images_path(string) - Paths of Images to be displayed\n                rows(int) - No. of Rows in Output\n                cols(int) - No. of Columns in Output\n    \"\"\"\n    figure, ax = plt.subplots(nrows=rows,ncols=cols,figsize=(16,8) )\n    for ind,image_path in enumerate(images_paths):\n        image=cv2.imread(image_path)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) \n        try:\n            ax.ravel()[ind].imshow(image)\n            ax.ravel()[ind].set_axis_off()\n        except:\n            continue;\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-18T14:44:56.965634Z","iopub.execute_input":"2021-10-18T14:44:56.966079Z","iopub.status.idle":"2021-10-18T14:44:56.972273Z","shell.execute_reply.started":"2021-10-18T14:44:56.966040Z","shell.execute_reply":"2021-10-18T14:44:56.971381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **<span style=\"color:orange;\">Training Images</span>**","metadata":{}},{"cell_type":"code","source":"display_multiple_img(train_images_path[100:150], 5, 5)","metadata":{"execution":{"iopub.status.busy":"2021-10-18T14:45:03.226494Z","iopub.execute_input":"2021-10-18T14:45:03.226767Z","iopub.status.idle":"2021-10-18T14:45:06.277075Z","shell.execute_reply.started":"2021-10-18T14:45:03.226739Z","shell.execute_reply":"2021-10-18T14:45:06.274171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **<span style=\"color:orange;\">Training Semi Supervised Images</span>**","metadata":{}},{"cell_type":"code","source":"display_multiple_img(train_semi_supervised_path[100:150], 5, 5)","metadata":{"execution":{"iopub.status.busy":"2021-10-18T14:49:15.695904Z","iopub.execute_input":"2021-10-18T14:49:15.696196Z","iopub.status.idle":"2021-10-18T14:49:18.661822Z","shell.execute_reply.started":"2021-10-18T14:49:15.696164Z","shell.execute_reply":"2021-10-18T14:49:18.661167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **<span style=\"color:orange;\">Test Images</span>**","metadata":{}},{"cell_type":"code","source":"display_multiple_img(test_images_path, 1, 3)","metadata":{"execution":{"iopub.status.busy":"2021-10-18T14:46:19.457960Z","iopub.execute_input":"2021-10-18T14:46:19.458308Z","iopub.status.idle":"2021-10-18T14:46:19.938653Z","shell.execute_reply.started":"2021-10-18T14:46:19.458270Z","shell.execute_reply":"2021-10-18T14:46:19.937969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **<span style=\"color:orange;\">Mask Plots</span>**","metadata":{}},{"cell_type":"code","source":"def rle_decode(mask_rle, shape, color=1):\n    '''\n    mask_rle: run-length as string formated (start length)\n    shape: (height, width, channels) of array to return \n    color: color for the mask\n    Returns numpy array (mask)\n\n    '''\n    s = mask_rle.split()\n    \n    starts = list(map(lambda x: int(x) - 1, s[0::2]))\n    lengths = list(map(int, s[1::2]))\n    ends = [x + y for x, y in zip(starts, lengths)]\n    \n    img = np.zeros((shape[0] * shape[1], shape[2]), dtype=np.float32)\n            \n    for start, end in zip(starts, ends):\n        img[start : end] = color\n    \n    return img.reshape(shape)\n\ndef build_masks(df_train, image_id, input_shape):\n    height, width = input_shape\n    labels = df_train[df_train[\"id\"] == image_id][\"annotation\"].tolist()\n    mask = np.zeros((height, width))\n    for label in labels:\n        mask += rle_decode(label, shape=(height, width))\n    mask = mask.clip(0, 1)\n    return mask\n\ndef plot_masks(image_id, colors=True):\n    labels = df_train[df_train[\"id\"] == image_id][\"annotation\"].tolist()\n    cell_type = df_train[df_train[\"id\"] == image_id][\"cell_type\"].tolist()\n    cmap = {\"shsy5y\":(0,0,255),\"astro\":(0,255,0),\"cort\":(255,0,0)}\n\n    if colors:\n        mask = np.zeros((520, 704, 3))\n        for label,cell_type in zip(labels,cell_type):\n            c = cmap[cell_type]\n            mask += rle_decode(label, shape=(520, 704, 3), color=c)\n    else:\n        mask = np.zeros((520, 704, 1))\n        for label in labels:\n            mask += rle_decode(label, shape=(520, 704, 1))\n    mask = mask.clip(0, 1)\n\n    image = cv2.imread(f\"../input/sartorius-cell-instance-segmentation/train/{image_id}.png\")\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n\n    plt.figure(figsize=(16, 32))\n    plt.subplot(3, 1, 1)\n    plt.imshow(image)\n    plt.axis(\"off\")\n    plt.subplot(3, 1, 2)\n    plt.imshow(image)\n    plt.imshow(mask, alpha=0.5)\n    plt.axis(\"off\")\n    plt.subplot(3, 1, 3)\n    plt.imshow(mask)\n    plt.axis(\"off\")\n    \n    plt.show();","metadata":{"execution":{"iopub.status.busy":"2021-10-20T17:48:22.699200Z","iopub.execute_input":"2021-10-20T17:48:22.699515Z","iopub.status.idle":"2021-10-20T17:48:22.719642Z","shell.execute_reply.started":"2021-10-20T17:48:22.699473Z","shell.execute_reply":"2021-10-20T17:48:22.718770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_masks(\"ffdb3cc02eef\", colors=False)","metadata":{"execution":{"iopub.status.busy":"2021-10-19T17:58:04.809866Z","iopub.execute_input":"2021-10-19T17:58:04.810476Z","iopub.status.idle":"2021-10-19T17:58:06.170822Z","shell.execute_reply.started":"2021-10-19T17:58:04.810432Z","shell.execute_reply":"2021-10-19T17:58:06.169895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_masks(\"0030fd0e6378\", colors=False)","metadata":{"execution":{"iopub.status.busy":"2021-10-19T17:58:22.463426Z","iopub.execute_input":"2021-10-19T17:58:22.463935Z","iopub.status.idle":"2021-10-19T17:58:23.645151Z","shell.execute_reply.started":"2021-10-19T17:58:22.463894Z","shell.execute_reply":"2021-10-19T17:58:23.644159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_masks_all(image_ids, colors=True):\n    fig, ax = plt.subplots(len(image_ids),3,figsize=(16, 21))\n    for idx,image_id in enumerate(image_ids):\n        labels = df_train[df_train[\"id\"] == image_id][\"annotation\"].tolist()\n        cell_type = df_train[df_train[\"id\"] == image_id][\"cell_type\"].tolist()\n        cmap = {\"shsy5y\":(0,0,255),\"astro\":(0,255,0),\"cort\":(255,0,0)}\n\n        if colors:\n            mask = np.zeros((520, 704, 3))\n            for label,cell_t in zip(labels,cell_type):\n                c = cmap[cell_t]\n                mask += rle_decode(label, shape=(520, 704, 3), color=c)\n        else:\n            mask = np.zeros((520, 704, 1))\n            for label in labels:\n                mask += rle_decode(label, shape=(520, 704, 1))\n        mask = mask.clip(0, 1)\n\n        image = cv2.imread(f\"../input/sartorius-cell-instance-segmentation/train/{image_id}.png\")\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n\n        ax[idx,0].imshow(image)\n        ax[idx,0].set_title(f\"Image {image_id}: cell_type {cell_t} Original\")\n        plt.axis(\"off\")\n        \n        ax[idx,1].imshow(image)\n        ax[idx,1].imshow(mask, alpha=0.5)\n        ax[idx,1].set_title(f\"Image with mask\")\n        plt.axis(\"off\")\n        \n        ax[idx,2].imshow(mask)\n        ax[idx,2].set_title(f\"Only Mask\")\n        plt.axis(\"off\")\n    plt.tight_layout()\n    fig.suptitle(\"Annotations Colored by Cell_Type: {blue: shsy5y, green: astro, red: cort}\",fontsize=16)\n    plt.show();\n\nplot_masks_all(['042dc0e561a4', '04928f0866b0', '049f02e0f764', '085eb8fec206'])","metadata":{"execution":{"iopub.status.busy":"2021-10-19T17:58:51.907582Z","iopub.execute_input":"2021-10-19T17:58:51.908420Z","iopub.status.idle":"2021-10-19T17:58:55.536378Z","shell.execute_reply.started":"2021-10-19T17:58:51.908366Z","shell.execute_reply":"2021-10-19T17:58:55.535408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"basic-image-exploration\"></a>\n<div class=\"list-group\" id=\"list-tab\" role=\"tablist\">\n<h2 class=\"list-group-item list-group-item-action active\" data-toggle=\"list\" style='background:orange; border:0; color:white' role=\"tab\" aria-controls=\"home\"><center>Basic Image Exploration</center></h2>","metadata":{}},{"cell_type":"markdown","source":"## **<span style=\"color:orange;\">Dimensions and 2D Histogram</span>**\n\nJust look at image dimensions, confirm it's 3 band (RGB), byte scaled (0-255).","metadata":{}},{"cell_type":"code","source":"first = cv2.imread(train_images_path[0])\ndims = np.shape(first)\nprint(dims)","metadata":{"execution":{"iopub.status.busy":"2021-10-19T08:22:41.151213Z","iopub.execute_input":"2021-10-19T08:22:41.151441Z","iopub.status.idle":"2021-10-19T08:22:41.189657Z","shell.execute_reply.started":"2021-10-19T08:22:41.151413Z","shell.execute_reply":"2021-10-19T08:22:41.188932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.min(first), np.max(first)","metadata":{"execution":{"iopub.status.busy":"2021-10-19T08:23:21.094761Z","iopub.execute_input":"2021-10-19T08:23:21.095653Z","iopub.status.idle":"2021-10-19T08:23:21.108763Z","shell.execute_reply.started":"2021-10-19T08:23:21.095598Z","shell.execute_reply":"2021-10-19T08:23:21.107482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"For any image specific classification, clustering, etc. transforms we'll want to collapse spatial dimensions so that we have a matrix of pixels by color channels.","metadata":{}},{"cell_type":"code","source":"pixel_matrix = np.reshape(first, (dims[0] * dims[1], dims[2]))\nprint(np.shape(pixel_matrix))","metadata":{"execution":{"iopub.status.busy":"2021-10-19T08:23:40.376901Z","iopub.execute_input":"2021-10-19T08:23:40.377238Z","iopub.status.idle":"2021-10-19T08:23:40.383487Z","shell.execute_reply.started":"2021-10-19T08:23:40.377199Z","shell.execute_reply":"2021-10-19T08:23:40.382757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Scatter plots are a go to to look for clusters and separatbility in the data, but these are busy and don't reveal density well, so we switch to using 2d histograms instead. The data between bands is really correlated, typical with visible imagery and why most satellite image analysts prefer to at least have near infrared values.","metadata":{}},{"cell_type":"code","source":"_ = plt.hist2d(pixel_matrix[:,1], pixel_matrix[:,2], bins=(50,50))","metadata":{"execution":{"iopub.status.busy":"2021-10-19T08:23:56.202057Z","iopub.execute_input":"2021-10-19T08:23:56.202560Z","iopub.status.idle":"2021-10-19T08:23:56.471514Z","shell.execute_reply.started":"2021-10-19T08:23:56.202519Z","shell.execute_reply":"2021-10-19T08:23:56.470593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fifth = cv2.imread(train_images_path[4])\ndims = np.shape(fifth)\npixel_matrix5 = np.reshape(fifth, (dims[0] * dims[1], dims[2]))","metadata":{"execution":{"iopub.status.busy":"2021-10-19T08:24:04.021200Z","iopub.execute_input":"2021-10-19T08:24:04.021508Z","iopub.status.idle":"2021-10-19T08:24:04.046615Z","shell.execute_reply.started":"2021-10-19T08:24:04.021474Z","shell.execute_reply":"2021-10-19T08:24:04.045914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_ = plt.hist2d(pixel_matrix5[:,1], pixel_matrix5[:,2], bins=(50,50))","metadata":{"execution":{"iopub.status.busy":"2021-10-19T08:24:10.969986Z","iopub.execute_input":"2021-10-19T08:24:10.970615Z","iopub.status.idle":"2021-10-19T08:24:11.189019Z","shell.execute_reply.started":"2021-10-19T08:24:10.970576Z","shell.execute_reply":"2021-10-19T08:24:11.187937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(first)","metadata":{"execution":{"iopub.status.busy":"2021-10-19T08:24:17.606396Z","iopub.execute_input":"2021-10-19T08:24:17.606744Z","iopub.status.idle":"2021-10-19T08:24:17.929788Z","shell.execute_reply.started":"2021-10-19T08:24:17.606706Z","shell.execute_reply":"2021-10-19T08:24:17.929060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(fifth)","metadata":{"execution":{"iopub.status.busy":"2021-10-19T08:24:24.305816Z","iopub.execute_input":"2021-10-19T08:24:24.306200Z","iopub.status.idle":"2021-10-19T08:24:24.934553Z","shell.execute_reply.started":"2021-10-19T08:24:24.306164Z","shell.execute_reply":"2021-10-19T08:24:24.933359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"intermediate-image-exploration\"></a>\n<div class=\"list-group\" id=\"list-tab\" role=\"tablist\">\n<h2 class=\"list-group-item list-group-item-action active\" data-toggle=\"list\" style='background:orange; border:0; color:white' role=\"tab\" aria-controls=\"home\"><center>Intermediate Image Exploration</center></h2>","metadata":{}},{"cell_type":"markdown","source":"## **<span style=\"color:orange;\">K-Means Clustering</span>**\n","metadata":{}},{"cell_type":"code","source":"# simple k means clustering\nfrom sklearn import cluster\n\nkmeans = cluster.KMeans(5)\nclustered = kmeans.fit_predict(pixel_matrix)\n\ndims = np.shape(first)\nclustered_img = np.reshape(clustered, (dims[0], dims[1]))\nplt.imshow(clustered_img)","metadata":{"execution":{"iopub.status.busy":"2021-10-19T08:27:46.048530Z","iopub.execute_input":"2021-10-19T08:27:46.048980Z","iopub.status.idle":"2021-10-19T08:27:49.531643Z","shell.execute_reply.started":"2021-10-19T08:27:46.048938Z","shell.execute_reply":"2021-10-19T08:27:49.530473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ind0, ind1, ind2, ind3 = [np.where(clustered == x)[0] for x in [0, 1, 2, 3]]","metadata":{"execution":{"iopub.status.busy":"2021-10-19T08:28:27.478584Z","iopub.execute_input":"2021-10-19T08:28:27.479355Z","iopub.status.idle":"2021-10-19T08:28:27.490107Z","shell.execute_reply.started":"2021-10-19T08:28:27.479310Z","shell.execute_reply":"2021-10-19T08:28:27.488724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure()\nax = fig.add_subplot(111, projection='3d')\n\nplot_vals = [('r', 'o', ind0),\n             ('b', '^', ind1),\n             ('g', '8', ind2),\n             ('m', '*', ind3)]\n\nfor c, m, ind in plot_vals:\n    xs = pixel_matrix[ind, 0]\n    ys = pixel_matrix[ind, 1]\n    zs = pixel_matrix[ind, 2]\n    ax.scatter(xs, ys, zs, c=c, marker=m)\n\nax.set_xlabel('Blue channel')\nax.set_ylabel('green channel')\nax.set_zlabel('Red channel')","metadata":{"execution":{"iopub.status.busy":"2021-10-19T08:28:33.294556Z","iopub.execute_input":"2021-10-19T08:28:33.295206Z","iopub.status.idle":"2021-10-19T08:28:42.074970Z","shell.execute_reply.started":"2021-10-19T08:28:33.295154Z","shell.execute_reply":"2021-10-19T08:28:42.074235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# quick look at color value histograms for pixel matrix from first image\nsns.distplot(pixel_matrix[:,0], bins=12)\nsns.distplot(pixel_matrix[:,1], bins=12)\nsns.distplot(pixel_matrix[:,2], bins=12)","metadata":{"execution":{"iopub.status.busy":"2021-10-19T08:28:56.459787Z","iopub.execute_input":"2021-10-19T08:28:56.461181Z","iopub.status.idle":"2021-10-19T08:29:01.811768Z","shell.execute_reply.started":"2021-10-19T08:28:56.461128Z","shell.execute_reply":"2021-10-19T08:29:01.810710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **<span style=\"color:orange;\">Matching Features</span>**\n","metadata":{}},{"cell_type":"code","source":"img79_1, img79_2, img79_3, img79_4, img79_5 = [plt.imread(train_images_path[n]) for n in range(78, 83)]","metadata":{"execution":{"iopub.status.busy":"2021-10-19T08:31:27.443149Z","iopub.execute_input":"2021-10-19T08:31:27.443484Z","iopub.status.idle":"2021-10-19T08:31:27.503683Z","shell.execute_reply.started":"2021-10-19T08:31:27.443448Z","shell.execute_reply":"2021-10-19T08:31:27.502692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_list = (img79_1, img79_2, img79_3, img79_4, img79_5)\n\nplt.figure(figsize=(8,10))\nplt.imshow(img_list[0])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-19T08:33:52.973756Z","iopub.execute_input":"2021-10-19T08:33:52.974103Z","iopub.status.idle":"2021-10-19T08:33:53.340947Z","shell.execute_reply.started":"2021-10-19T08:33:52.974063Z","shell.execute_reply":"2021-10-19T08:33:53.339912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"advanced-image-exploration\"></a>\n<div class=\"list-group\" id=\"list-tab\" role=\"tablist\">\n<h2 class=\"list-group-item list-group-item-action active\" data-toggle=\"list\" style='background:orange; border:0; color:white' role=\"tab\" aria-controls=\"home\"><center>Advanced Image Exploration</center></h2>","metadata":{}},{"cell_type":"markdown","source":"## **<span style=\"color:orange;\">Rudimentary Transforms, Edge Detection, Texture</span>**","metadata":{}},{"cell_type":"code","source":"set144 = [plt.imread(train_images_path[n]) for n in (100, 200)]","metadata":{"execution":{"iopub.status.busy":"2021-10-19T17:55:29.645120Z","iopub.execute_input":"2021-10-19T17:55:29.645560Z","iopub.status.idle":"2021-10-19T17:55:29.707344Z","shell.execute_reply.started":"2021-10-19T17:55:29.645514Z","shell.execute_reply":"2021-10-19T17:55:29.706454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(set144[0])","metadata":{"execution":{"iopub.status.busy":"2021-10-19T08:35:52.374266Z","iopub.execute_input":"2021-10-19T08:35:52.374608Z","iopub.status.idle":"2021-10-19T08:35:52.684177Z","shell.execute_reply.started":"2021-10-19T08:35:52.374570Z","shell.execute_reply":"2021-10-19T08:35:52.683311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import skimage\nfrom skimage.feature import greycomatrix, greycoprops\nfrom skimage.filters import sobel","metadata":{"execution":{"iopub.status.busy":"2021-10-19T08:36:28.004965Z","iopub.execute_input":"2021-10-19T08:36:28.006178Z","iopub.status.idle":"2021-10-19T08:36:28.804405Z","shell.execute_reply.started":"2021-10-19T08:36:28.006113Z","shell.execute_reply":"2021-10-19T08:36:28.803274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **<span style=\"color:orange;\">Sobel Edge Detection</span>**\n\nA Sobel filter is one means of getting a basic edge magnitude/gradient image. Can be useful to threshold and find prominent linear features, etc. Several other similar filters in skimage.filters are also good edge detectors: roberts, scharr, etc. and you can control direction, i.e. use an anisotropic version.","metadata":{}},{"cell_type":"code","source":"# a sobel filter is a basic way to get an edge magnitude/gradient image\nfig = plt.figure(figsize=(8, 8))\nplt.imshow(sobel(set144[0][:150,:150]))","metadata":{"execution":{"iopub.status.busy":"2021-10-19T08:37:41.393004Z","iopub.execute_input":"2021-10-19T08:37:41.393454Z","iopub.status.idle":"2021-10-19T08:37:41.689517Z","shell.execute_reply.started":"2021-10-19T08:37:41.393421Z","shell.execute_reply":"2021-10-19T08:37:41.688511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from skimage.filters import sobel_h\n\n# can also apply sobel only across one direction.\nfig = plt.figure(figsize=(8, 8))\nplt.imshow(sobel_h(set144[0][:150,:150]), cmap='BuGn')","metadata":{"execution":{"iopub.status.busy":"2021-10-19T08:37:59.753318Z","iopub.execute_input":"2021-10-19T08:37:59.754427Z","iopub.status.idle":"2021-10-19T08:38:00.058937Z","shell.execute_reply.started":"2021-10-19T08:37:59.754372Z","shell.execute_reply":"2021-10-19T08:38:00.057626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"instance-mask-viz\"></a>\n<div class=\"list-group\" id=\"list-tab\" role=\"tablist\">\n<h2 class=\"list-group-item list-group-item-action active\" data-toggle=\"list\" style='background:orange; border:0; color:white' role=\"tab\" aria-controls=\"home\"><center>Instance Mask Viz</center></h2>","metadata":{}},{"cell_type":"code","source":"class_id2label = {\n    1: 'shsy5y',\n    2: 'cort', \n    3: 'astro'\n}\n\nclass_label2id = {v:k for k, v in class_id2label.items()}\n\n# Note the use of wandb.Image\ndef wandb_mask(bg_img, gt_mask):\n  return wandb.Image(bg_img, masks={\n      \"ground_truth\" : {\n          \"mask_data\" : gt_mask,\n          \"class_labels\": class_id2label\n      }\n    }\n  )","metadata":{"execution":{"iopub.status.busy":"2021-10-20T17:43:33.854180Z","iopub.execute_input":"2021-10-20T17:43:33.854437Z","iopub.status.idle":"2021-10-20T17:43:33.860370Z","shell.execute_reply.started":"2021-10-20T17:43:33.854408Z","shell.execute_reply":"2021-10-20T17:43:33.859538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"VISUALIZE_SAMPLES = 10\n\nids = df_train.id.unique()\nsample_idx = np.random.choice(len(ids), VISUALIZE_SAMPLES)\nsample_ids = ids[sample_idx]\n\n# Initialize W&B\nrun = wandb.init(project='sartorius', \n                 config= WANDB_CONFIG) # The config variable is to show that you can pass in any dict (hyperparameters)\n\nfor i in range(VISUALIZE_SAMPLES):\n    image_id = sample_ids[i]\n    sample_df = df_train[df_train[\"id\"] == image_id].reset_index(drop=True)\n                 \n    # Empty mask\n    mask = np.zeros((520, 704, 1))\n                 \n    # Fill mask\n    for j in range(len(sample_df)):\n        row = sample_df.loc[j]\n        mask += rle_decode(row.annotation, \n                           shape=(520, 704, 1))\n        \n    mask[np.where(mask>0)] = class_label2id[row.cell_type]\n    mask = np.squeeze(mask, axis=-1)\n            \n    # Log to W&B\n    image_path = f\"../input/sartorius-cell-instance-segmentation/train/{image_id}.png\"\n    wandb.log({f\"Segmentation Viz\" : [wandb_mask(image_path, mask)]})\n    \n# Close W&B run\nwandb.finish()","metadata":{"execution":{"iopub.status.busy":"2021-10-20T17:48:30.257617Z","iopub.execute_input":"2021-10-20T17:48:30.258140Z","iopub.status.idle":"2021-10-20T17:48:46.312908Z","shell.execute_reply.started":"2021-10-20T17:48:30.258106Z","shell.execute_reply":"2021-10-20T17:48:46.312053Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## [Check out the run page here $\\rightarrow$](https://wandb.ai/ishandutta/sartorius?workspace=user-ishandutta)  ","metadata":{}},{"cell_type":"markdown","source":"<a id=\"unet-model\"></a>\n<div class=\"list-group\" id=\"list-tab\" role=\"tablist\">\n<h2 class=\"list-group-item list-group-item-action active\" data-toggle=\"list\" style='background:orange; border:0; color:white' role=\"tab\" aria-controls=\"home\"><center>U-Net Model</center></h2>","metadata":{}},{"cell_type":"markdown","source":"## **<span style=\"color:orange;\">Dataset Class</span>**","metadata":{}},{"cell_type":"code","source":"class CellDataset(Dataset):\n    def __init__(self, df):\n        self.df = df\n        self.base_path = config.TRAIN_PATH\n        self.transforms = Compose([Resize(config.IMAGE_RESIZE[0], config.IMAGE_RESIZE[1]), \n                                   Normalize(mean=config.RESNET_MEAN, std=config.RESNET_STD, p=1), \n                                   HorizontalFlip(p=0.5),\n                                   VerticalFlip(p=0.5),\n                                   ToTensorV2()])\n        self.gb = self.df.groupby('id')\n        self.image_ids = df.id.unique().tolist()\n\n    def __getitem__(self, idx):\n        image_id = self.image_ids[idx]\n        df = self.gb.get_group(image_id)\n        annotations = df['annotation'].tolist()\n        image_path = os.path.join(self.base_path, image_id + \".png\")\n        image = cv2.imread(image_path)\n        mask = build_masks(df_train, image_id, input_shape=(520, 704))\n        mask = (mask >= 1).astype('float32')\n        augmented = self.transforms(image=image, mask=mask)\n        image = augmented['image']\n        mask = augmented['mask']\n        return image, mask.reshape((1, IMAGE_RESIZE[0], IMAGE_RESIZE[1]))\n\n    def __len__(self):\n        return len(self.image_ids)  ","metadata":{"execution":{"iopub.status.busy":"2021-10-20T06:59:28.376191Z","iopub.execute_input":"2021-10-20T06:59:28.377659Z","iopub.status.idle":"2021-10-20T06:59:28.390366Z","shell.execute_reply.started":"2021-10-20T06:59:28.377554Z","shell.execute_reply":"2021-10-20T06:59:28.389187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"references\"></a>\n<div class=\"list-group\" id=\"list-tab\" role=\"tablist\">\n<h2 class=\"list-group-item list-group-item-action active\" data-toggle=\"list\" style='background:orange; border:0; color:white' role=\"tab\" aria-controls=\"home\"><center>References</center></h2>","metadata":{}},{"cell_type":"markdown","source":">- [Alzheimer's disease](https://www.mayoclinic.org/diseases-conditions/alzheimers-disease/symptoms-causes/syc-20350447)\n>- [Brain tumor - Symptoms and causes - Mayo Clinic](https://www.mayoclinic.org/diseases-conditions/brain-tumor/symptoms-causes/syc-20350084#:~:text=A%20brain%20tumor%20is%20a,tumors%20are%20cancerous%20(malignant).)\n>- [🦠Cell Segmentation🦠 - Run Length Decoding](https://www.kaggle.com/ihelon/cell-segmentation-run-length-decoding)\n>- [🦠 Sartorius - Starter Baseline Torch U-net](https://www.kaggle.com/julian3833/sartorius-starter-baseline-torch-u-net)\n>- [Sartorius: Instance Mask Viz with W&B](https://www.kaggle.com/ayuraj/sartorius-instance-mask-viz-with-w-b)\n>\n>---","metadata":{}},{"cell_type":"markdown","source":"<h1><center>More Plots and Models coming soon!</center></h1>\n\n<center><img src = \"https://static.wixstatic.com/media/5f8fae_7581e21a24a1483085024f88b0949a9d~mv2.jpg/v1/fill/w_934,h_379,al_c,q_90/5f8fae_7581e21a24a1483085024f88b0949a9d~mv2.jpg\" width = \"750\" height = \"500\"/></center> ","metadata":{}},{"cell_type":"markdown","source":"--- \n\n## **<span style=\"color:orange;\">Let's have a Talk!</span>**\n> ### Reach out to me on [LinkedIn](https://www.linkedin.com/in/ishandutta0098)\n\n---","metadata":{}}]}