{"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":"# <p style=\"font-family:JetBrains Mono; font-weight:bold; letter-spacing: 2px; color:#006600; font-size:140%; text-align:left;padding: 0px; border-bottom: 3px solid #003300\">HuBMAP</p>","metadata":{}},{"cell_type":"code","source":"import warnings\nwarnings.simplefilter('ignore')","metadata":{"execution":{"iopub.status.busy":"2023-08-07T09:56:36.487729Z","iopub.execute_input":"2023-08-07T09:56:36.488335Z","iopub.status.idle":"2023-08-07T09:56:36.529177Z","shell.execute_reply.started":"2023-08-07T09:56:36.488266Z","shell.execute_reply":"2023-08-07T09:56:36.527749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"border-radius:10px; border:#DEB887 solid; padding: 15px; background-color: #F3f9ed; font-size:100%; text-align:left\">\n\nThe $HuBMAP$ $-$ $Hacking$ $the$ $Human$ $Vasculature$ $competition$ is a $Kaggle$ $competition$ that challenges participants to `develop machine learning models to segment microvascular structures` in $2D$ `PAS-stained histology images` from healthy human kidney tissue slides. The goal of the competition is to `improve researchers' understanding of how the blood vessels are arranged in human tissues`.\n\nThe competition is hosted by the $Human$ $BioMolecular$ $Atlas$ $Program$ $HuBMAP$, which is a `global effort to create a comprehensive and open-access atlas of human cells`. $HuBMAP$ researchers are using the `latest molecular and cellular biology technologies` to `study the connections that cells have with each other` throughout the body.\n\nThe `microvascular structures` that are being segmented in this competition `include` \n* $Capillaries$\n* $Arterioles$\n* $Venules$\n\nThese structures are `very small` and `difficult to see with the naked eye`, so `automated segmentation methods are essential` for researchers to study them.\n\n**[National effort to focus on mapping human body on cellular level](https://www.purdue.edu/newsroom/releases/2019/Q4/national-effort-to-focus-on-mapping-human-body-on-cellular-level.html)**","metadata":{}},{"cell_type":"markdown","source":"# <p style=\"font-family:JetBrains Mono; font-weight:bold; letter-spacing: 2px; color:#FFC67D; font-size:140%; text-align:left;padding: 0px; border-bottom: 3px solid #FFC67D\">1 | Data 🚀</p>\n\n<div style=\"border-radius:10px; border:#FFC67D solid; padding: 15px; background-color: #F3f9ed; font-size:100%; text-align:left\">\n\nThe images are $256x256$ pixels in `size` and are in the `.tif` format. The images are a `diverse set` of images from `different patients` and `different tissue slides`.\n\nThe `ground truth segmentation` for the `images` in the data set was created by a `team of experts`. The experts used a `variety of techniques` to create the ground truth segmentation\n* Manual segmentation\n* Semi-automatic segmentation. \n\nThe ground truth segmentation is accurate and reliable, and it is essential for training machine learning models to segment microvascular structures.","metadata":{}},{"cell_type":"markdown","source":"# <p style=\"font-family:JetBrains Mono; font-weight:bold; letter-spacing: 2px; color:#00BFFF; font-size:140%; text-align:left;padding: 0px; border-bottom: 3px solid #00BFFF\">2 | Visualizing the Data 😎</p>","metadata":{}},{"cell_type":"code","source":"import cv2\nimport matplotlib.pyplot as plt\n\nimport tensorflow as tf\n\nimport albumentations as A\nfrom albumentations.pytorch import ToTensorV2","metadata":{"execution":{"iopub.status.busy":"2023-08-07T10:02:29.540659Z","iopub.execute_input":"2023-08-07T10:02:29.541104Z","iopub.status.idle":"2023-08-07T10:02:38.658199Z","shell.execute_reply.started":"2023-08-07T10:02:29.541069Z","shell.execute_reply":"2023-08-07T10:02:38.656853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"border-radius:10px; border:#00BFFF solid; padding: 15px; background-color: #F3f9ed; font-size:100%; text-align:left\">\n\nVisualizing the data is an integral part of image models. It gives us information on what we aare actually working, and also we do this for fun. Yayyyyyyyy\n\nLets asusme we take this iamge as sample ","metadata":{}},{"cell_type":"code","source":"sample_image = cv2.imread(\"/kaggle/input/hubmap-hacking-the-human-vasculature/test/72e40acccadf.tif\")","metadata":{"execution":{"iopub.status.busy":"2023-08-07T10:01:02.965603Z","iopub.execute_input":"2023-08-07T10:01:02.966047Z","iopub.status.idle":"2023-08-07T10:01:03.022250Z","shell.execute_reply.started":"2023-08-07T10:01:02.966016Z","shell.execute_reply":"2023-08-07T10:01:03.021178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"border-radius:10px; border:#00BFFF solid; padding: 15px; background-color: #F3f9ed; font-size:100%; text-align:left\">\n\nLets assume we want to train a model on the dataset we have. But it is not a good idea to directly train a big model on this data. \n\nWhen we are working with the images, it is advised to first preproces the images, before sending them to any model \n\nBy preporcessing we mostly mean that \n\n|_____|______\n|---|---\n|Resize|To ensure that all images are the same size.\n||To reduce the amount of data that needs to be processed.\n||To improve the accuracy of the model.\n|Mean|It helps to improve the stability of the model. \n||It helps to improve the performance of the model.\n||It helps to make the model more robust to changes in lighting.\n|STD|It helps to improve the stability of the training process.\n||It helps to improve the performance of the model. \n||It makes the model more interpretable.\n\nWe could have actually made a proper function from scratch to do these things, like this \n```\ndef preprocess(image):\n    \n    image = np.clip(image , width , height)\n\n    image = (image - image.mean()) / image.std()\n\n    return image\n```\n(this might be not correct, but just a example)\n\nBut we rather use a specialized library that is faster. The `code we just wrote` can be `very slow to work`. But `albumnetnation library` can be `fast enough`","metadata":{}},{"cell_type":"code","source":"A.Compose([\n        A.Resize(width = 512 , height = 512) , \n        A.Normalize(\n            mean = [0 , 0] , \n            std = [1 , 1] , \n            max_pixel_value = 255\n        ) , \n        ToTensorV2()\n    ])","metadata":{"execution":{"iopub.status.busy":"2023-08-07T10:01:45.622015Z","iopub.execute_input":"2023-08-07T10:01:45.623506Z","iopub.status.idle":"2023-08-07T10:01:45.634397Z","shell.execute_reply.started":"2023-08-07T10:01:45.623463Z","shell.execute_reply":"2023-08-07T10:01:45.633079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"border-radius:10px; border:#00BFFF solid; padding: 15px; background-color: #F3f9ed; font-size:100%; text-align:left\">\n\nThis is our `compose` block\n\nNow lets make a proper function `display` for viewing the image","metadata":{}},{"cell_type":"code","source":"def display(im , augments = False):\n\n    img = im\n    \n    if augments :\n        \n        img = A.Compose([\n        A.Resize(width = 512 , height = 512) , \n        A.Normalize(\n            mean = [0 , 0 , 0] , \n            std = [1 , 1 , 1] , \n            max_pixel_value = 255\n        ) , \n        ToTensorV2()\n    ])(image = im)[\"image\"]\n\n    # return image\n    \n    plt.imshow(tf.reshape(img , (512 , 512 , 3)))","metadata":{"execution":{"iopub.status.busy":"2023-08-07T10:02:38.660722Z","iopub.execute_input":"2023-08-07T10:02:38.661865Z","iopub.status.idle":"2023-08-07T10:02:38.670683Z","shell.execute_reply.started":"2023-08-07T10:02:38.661816Z","shell.execute_reply":"2023-08-07T10:02:38.669111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Test Image before preprocessing : \")\ndisplay(sample_image)","metadata":{"execution":{"iopub.status.busy":"2023-08-07T10:02:39.865452Z","iopub.execute_input":"2023-08-07T10:02:39.866652Z","iopub.status.idle":"2023-08-07T10:02:40.390245Z","shell.execute_reply.started":"2023-08-07T10:02:39.866585Z","shell.execute_reply":"2023-08-07T10:02:40.389222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Test Image before preprocessing : \")\ndisplay(sample_image , augments = True)","metadata":{"execution":{"iopub.status.busy":"2023-08-07T10:02:48.771217Z","iopub.execute_input":"2023-08-07T10:02:48.771629Z","iopub.status.idle":"2023-08-07T10:02:49.204146Z","shell.execute_reply.started":"2023-08-07T10:02:48.771599Z","shell.execute_reply":"2023-08-07T10:02:49.203153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"border-radius:10px; border:#00BFFF solid; padding: 15px; background-color: #F3f9ed; font-size:100%; text-align:left\">\n\n## $2.1$ $|$ $Test$ \n\nThanks to **[YASSINE ALOUINI](https://www.kaggle.com/yassinealouini)=>[Working with TIFF files](https://www.kaggle.com/code/yassinealouini/working-with-tiff-files)** for providing a simple way to work wit the `tif` files withing python environments\n\nI have change the way of input for the image from `rasterio` to `opencv`, though it was concluded in **[YASSINE ALOUINI](https://www.kaggle.com/yassinealouini)=>[Some Insights](https://www.kaggle.com/competitions/hubmap-organ-segmentation/discussion/333389)**that `rasterio` is the fastest way ","metadata":{}},{"cell_type":"code","source":"test_dir = \"/kaggle/input/hubmap-hacking-the-human-vasculature/test\"","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-08-07T10:03:33.416996Z","iopub.execute_input":"2023-08-07T10:03:33.417455Z","iopub.status.idle":"2023-08-07T10:03:33.423265Z","shell.execute_reply.started":"2023-08-07T10:03:33.417418Z","shell.execute_reply":"2023-08-07T10:03:33.421931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"border-radius:10px; border:#00BFFF solid; padding: 15px; background-color: #F3f9ed; font-size:100%; text-align:left\">\n\n## $2.2$ $|$ $Train$\n    \nWe also have a training data with us. The train folder only contains the image files in `tif` format. And the anootions are given in the `polygons.jsonl` format ","metadata":{}},{"cell_type":"code","source":"train_dir = \"/kaggle/input/hubmap-hacking-the-human-vasculature/train\"","metadata":{"execution":{"iopub.status.busy":"2023-08-07T10:07:27.306774Z","iopub.execute_input":"2023-08-07T10:07:27.307494Z","iopub.status.idle":"2023-08-07T10:07:27.313766Z","shell.execute_reply.started":"2023-08-07T10:07:27.307440Z","shell.execute_reply":"2023-08-07T10:07:27.312501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"border-radius:10px; border:#00BFFF solid; padding: 15px; background-color: #F3f9ed; font-size:100%; text-align:left\">\n\n## $2.3$ $|$ $Polygons$\n\nThis is a `json` file that basically contains the annotions to these data points","metadata":{}},{"cell_type":"code","source":"with open(\"/kaggle/input/hubmap-hacking-the-human-vasculature/sample_submission.csv\" , \"r\") as f:\n    k = list(f)\nprint(k[0])","metadata":{"execution":{"iopub.status.busy":"2023-08-07T10:04:48.964481Z","iopub.execute_input":"2023-08-07T10:04:48.964917Z","iopub.status.idle":"2023-08-07T10:04:48.972331Z","shell.execute_reply.started":"2023-08-07T10:04:48.964885Z","shell.execute_reply":"2023-08-07T10:04:48.971269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <p style=\"font-family:JetBrains Mono; font-weight:bold; letter-spacing: 2px; color:#32CD32; font-size:140%; text-align:left;padding: 0px; border-bottom: 3px solid #32CD32\">3 | DataLoader 📖</p>","metadata":{}},{"cell_type":"code","source":"import json\n\nfrom torch.utils.data import DataLoader, Dataset","metadata":{"execution":{"iopub.status.busy":"2023-08-07T10:07:48.152200Z","iopub.execute_input":"2023-08-07T10:07:48.152606Z","iopub.status.idle":"2023-08-07T10:07:48.159825Z","shell.execute_reply.started":"2023-08-07T10:07:48.152575Z","shell.execute_reply":"2023-08-07T10:07:48.157748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"border-radius:10px; border:#32CD32 solid; padding: 15px; background-color: #F3f9ed; font-size:100%; text-align:left\">\n\nA data loader is a tool that helps to load data into a deep learning model. It is used to break down the data into smaller batches, which can then be processed by the model more efficiently. Data loaders can also be used to shuffle the data, which can help to improve the performance of the model.\n\nThe `DataLoader` we are going to create here, is highly inspired by **[Thomas Rochefort-Beaudoin](https://www.kaggle.com/thomasrochefort)=>[HuBMAP : Simple PyTorch DataLoade](https://www.kaggle.com/code/thomasrochefort/hubmap-simple-pytorch-dataloader/notebook)**\n\n* We simply first read the `json files`\n* Then we get the images from the set\n* Then according to the coordinates in the `json_file`. We mask the blood vessels. \n* The we return the image and the mask\n\nBlood vessels are often masked in medical imaging competitions to make the task of identifying other objects in the image more challenging. This is because blood vessels can be very similar in appearance to other objects, such as tumors or lesions. Masking the blood vessels forces the model to focus on the other objects in the image, and to learn to distinguish them from the blood vessels. This can lead to more accurate and reliable detection of other objects in the image.\n\n","metadata":{}},{"cell_type":"code","source":"a = A.Compose([\n        A.Resize(width = 512 , height = 512) , \n        A.Normalize(\n            mean = [0 , 0 , 0] , \n            std = [1 , 1 , 1] , \n            max_pixel_value = 255\n        ) , \n        ToTensorV2()\n    ])","metadata":{"execution":{"iopub.status.busy":"2023-08-07T10:07:12.400036Z","iopub.execute_input":"2023-08-07T10:07:12.400442Z","iopub.status.idle":"2023-08-07T10:07:12.406866Z","shell.execute_reply.started":"2023-08-07T10:07:12.400411Z","shell.execute_reply":"2023-08-07T10:07:12.405724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class hubmapDataset(Dataset):\n    \n    def __init__(self, image_dir, labels_file , augments = False):\n        \n        with open(labels_file, 'r') as json_file:\n            self.json_labels = [json.loads(line) for line in json_file]\n\n        self.image_dir = image_dir\n#         self.transform = transform\n        self.augments = augments\n\n    __len__ = lambda self : len(self.json_labels)    \n        \n    def __getitem__(self, idx):\n        \n        image_path = os.path.join(self.image_dir, f\"{self.json_labels[idx]['id']}.tif\")\n        image = Image.open(image_path)\n        \n        if self.augments:\n            \n            image = a(image = image)[\"image\"]\n        \n        mask = np.zeros((512, 512), dtype=np.float32)\n\n        for annot in self.json_labels[idx]['annotations']:\n\n            cords = annot['coordinates']\n            \n            if annot['type'] == \"blood_vessel\":\n                \n                for cord in cords:\n                    \n                    rr, cc = np.array([i[1] for i in cord]), np.asarray([i[0] for i in cord])\n                    \n                    mask[rr, cc] = 1\n\n        image = torch.tensor(np.array(image), dtype=torch.float32).permute(2, 0, 1)  # Shape: [C, H, W]\n        mask = torch.tensor(mask, dtype=torch.float32)\n\n#         if self.transform:\n#             image = self.transform(image)\n\n        return image, mask","metadata":{"execution":{"iopub.status.busy":"2023-08-07T10:07:51.658898Z","iopub.execute_input":"2023-08-07T10:07:51.659322Z","iopub.status.idle":"2023-08-07T10:07:51.672652Z","shell.execute_reply.started":"2023-08-07T10:07:51.659288Z","shell.execute_reply":"2023-08-07T10:07:51.671290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"border-radius:10px; border:#32CD32 solid; padding: 15px; background-color: #F3f9ed; font-size:100%; text-align:left\">\n\nNow we first load our data ","metadata":{}},{"cell_type":"code","source":"train_dataset = hubmapDataset(image_dir = train_dir, \n                        labels_file = '../input/hubmap-hacking-the-human-vasculature/polygons.jsonl')\ntrain_dataset","metadata":{"execution":{"iopub.status.busy":"2023-08-07T10:07:52.742379Z","iopub.execute_input":"2023-08-07T10:07:52.742828Z","iopub.status.idle":"2023-08-07T10:07:57.869626Z","shell.execute_reply.started":"2023-08-07T10:07:52.742793Z","shell.execute_reply":"2023-08-07T10:07:57.868457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"border-radius:10px; border:#32CD32 solid; padding: 15px; background-color: #F3f9ed; font-size:100%; text-align:left\">\n\nNow we load this data into a dataloader, so that we get a manipulated data that we can pass in any pytorch pipleine","metadata":{}},{"cell_type":"code","source":"train_dataloader = DataLoader(train_dataset, batch_size = 4, shuffle = True)\ntrain_dataloader","metadata":{"execution":{"iopub.status.busy":"2023-05-31T06:16:01.865017Z","iopub.execute_input":"2023-05-31T06:16:01.868023Z","iopub.status.idle":"2023-05-31T06:16:01.878584Z","shell.execute_reply.started":"2023-05-31T06:16:01.867970Z","shell.execute_reply":"2023-05-31T06:16:01.876745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <p style=\"font-family:JetBrains Mono; font-weight:bold; letter-spacing: 2px; color:#F2C464; font-size:140%; text-align:left;padding: 0px; border-bottom: 3px solid #F2C464\">4 | Model Setup 🤖</p>","metadata":{}},{"cell_type":"code","source":"! pip install -q segmentation_models_pytorch","metadata":{"execution":{"iopub.status.busy":"2023-08-07T10:10:16.208090Z","iopub.execute_input":"2023-08-07T10:10:16.208493Z","iopub.status.idle":"2023-08-07T10:10:38.473480Z","shell.execute_reply.started":"2023-08-07T10:10:16.208464Z","shell.execute_reply":"2023-08-07T10:10:38.472006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\n\nimport segmentation_models_pytorch as smp","metadata":{"execution":{"iopub.status.busy":"2023-08-07T10:13:09.748072Z","iopub.execute_input":"2023-08-07T10:13:09.748594Z","iopub.status.idle":"2023-08-07T10:13:09.755088Z","shell.execute_reply.started":"2023-08-07T10:13:09.748553Z","shell.execute_reply":"2023-08-07T10:13:09.753821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"border-radius:10px; border:#F2C464 solid; padding: 15px; background-color: #F3f9ed; font-size:100%; text-align:left\">\n\nThe `model setup phase` of a deep learning project is the `process of defining the architecture of the model` and the `hyperparameters` that will be used to train it.\n\n# $4.1$ $|$ $Efficient$ $Net$ $B-7$ ✨\n\n$EfficientNet$ $B-7$ is a deep learning model that was developed by `Google AI` in $2019$. It is a `large model` that is designed for `image classification` tasks. $EfficientNet B-7$ achieves `state-of-the-art accuracy on ImageNet`, while being `more efficient` than previous models.\n\n$EfficientNet$ $B-7$ is based on the $EfficientNet$ architecture, which was proposed in the paper \"EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks\" by `Mingxing Tan` and `Quoc V. Le`. The $EfficientNet$ architecture is a `compound scaling method` that `uniformly scales all dimensions` of `depth/width/resolution` using a simple yet highly effective `compound coefficient`.\n\n$EfficientNet$ $B-7$ has $237$ layers and $1.54M$ parameters. It is trained on the `ImageNet` dataset, $EfficientNet$ $B-7$ achieves an accuracy of $84.4$% on the $ImageNet$ validation set.\n\n$EfficientNet$ $B-7$ is a powerful model that can be used for a variety of `image classification` tasks. It is more `accurate and efficient` than previous models, making it a good choice for a variety of applications.\n\nThe model setup is highly inspired by **[\nIMVision](https://www.kaggle.com/imvision12)=>[[Training] - Hubmap EfficientNet](https://www.kaggle.com/code/imvision12/training-hubmap-efficientnet)**","metadata":{}},{"cell_type":"code","source":"u_net = smp.Unet(encoder_name = \"efficientnet-b3\" , encoder_weights = \"imagenet\" , activation = \"sigmoid\")\n\n# u_net = u_net.cuda()","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-08-07T10:11:10.312025Z","iopub.execute_input":"2023-08-07T10:11:10.312519Z","iopub.status.idle":"2023-08-07T10:11:10.621494Z","shell.execute_reply.started":"2023-08-07T10:11:10.312481Z","shell.execute_reply":"2023-08-07T10:11:10.620070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"u_net.train()\ntrain_loss = 0 \nscore = 0","metadata":{"execution":{"iopub.status.busy":"2023-08-07T10:11:16.748101Z","iopub.execute_input":"2023-08-07T10:11:16.748979Z","iopub.status.idle":"2023-08-07T10:11:16.756900Z","shell.execute_reply.started":"2023-08-07T10:11:16.748938Z","shell.execute_reply":"2023-08-07T10:11:16.755937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"border-radius:10px; border:#F2C464 solid; padding: 15px; background-color: #F3f9ed; font-size:100%; text-align:left\">\n\n# $7.2$ $|$ $Deep$ $Lab$ $V3$\n\n$DeepLab V3$ is a `semantic segmentation` architecture that `improves` upon $DeepLabV2$ with `several modifications`. To handle the problem of `segmenting objects at multiple scales`, modules are designed which employ `atrous convolution` in cascade or in `parallel to capture multi-scale context by adopting multiple atrous rates`. Furthermore, the $Atrous$ $Spatial$ $Pyramid$ $Pooling$ module from $DeepLabV2$ augmented with `image-level` features `encoding global context` and further `boost performance`. $DeepLab$ $V3$ has been shown to `achieve state-of-the-art` results on a variety of `semantic segmentation benchmarks`, including the $Cityscapes$. $DeepLab$ $V3$ is a powerful tool for `semantic segmentation`","metadata":{}},{"cell_type":"code","source":"deep_lab_v3 = torch.hub.load('pytorch/vision:v0.10.0' , 'deeplabv3_resnet50', pretrained=True)\n\n# deep_lab_v3 = deep_lab_v3.cuda()","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-08-07T10:13:24.377834Z","iopub.execute_input":"2023-08-07T10:13:24.378243Z","iopub.status.idle":"2023-08-07T10:13:25.829585Z","shell.execute_reply.started":"2023-08-07T10:13:24.378214Z","shell.execute_reply":"2023-08-07T10:13:25.828678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"deep_lab_v3 = deep_lab_v3.train()","metadata":{"execution":{"iopub.status.busy":"2023-08-07T10:13:31.303366Z","iopub.execute_input":"2023-08-07T10:13:31.304200Z","iopub.status.idle":"2023-08-07T10:13:31.312038Z","shell.execute_reply.started":"2023-08-07T10:13:31.304144Z","shell.execute_reply":"2023-08-07T10:13:31.310612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <p style=\"font-family:JetBrains Mono; font-weight:bold; letter-spacing: 2px; color:#FF69B4; font-size:140%; text-align:left;padding: 0px; border-bottom: 3px solid #FF69B4\">5 | Training Arguments 🛺</p>","metadata":{}},{"cell_type":"code","source":"import torch.nn as nn","metadata":{"execution":{"iopub.status.busy":"2023-08-07T10:15:55.812840Z","iopub.execute_input":"2023-08-07T10:15:55.813268Z","iopub.status.idle":"2023-08-07T10:15:55.819099Z","shell.execute_reply.started":"2023-08-07T10:15:55.813233Z","shell.execute_reply":"2023-08-07T10:15:55.817714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"border-radius:10px; border:#FF69B4 solid; padding: 15px; background-color: #F3f9ed; font-size:100%; text-align:left\">\n\n## $5.1$ $|$ $Losses$ \n\nLoss functions are used to measure the difference between the predicted output of a model and the ground truth. The loss function is used to calculate the gradient of the error with respect to the model parameters, which is then used to update the parameters using an optimization algorithm.\n\n## $5.1.1$ $|$ $DiceLoss$\n\nDice loss is a loss function used in image segmentation tasks. It is a measure of the similarity between two sets of data, and it is often used to train deep learning models for image segmentation.\n\n```\nDice loss = 1 - 2 * (intersection / (union + smooth))\n```\n\n## $5.1.2$ $|$ $Soft$ $BCE$ $With$ $Logits$ $Loss$\n\nSoftBCEWithLogitsLoss is a loss function that is used for semantic segmentation tasks. It is a smooth version of the binary cross-entropy loss function, which is more robust to outliers and can help to improve the performance of segmentation models.\n\n```\nloss = -(y * log(p) + (1 - y) * log(1 - p))\n\n```","metadata":{}},{"cell_type":"code","source":"class CustomLoss(nn.Module):\n    \n    def __init__(self):\n        \n        super(CustomLoss,self).__init__()\n        \n        self.diceloss = smp.losses.DiceLoss(mode='binary')\n        self.binloss = smp.losses.SoftBCEWithLogitsLoss(reduction = 'mean' , smooth_factor = 0.1)\n\n    def forward(self, output, mask):\n        \n        output = torch.squeeze(output)\n        mask = torch.squeeze(mask)\n        \n        dice = self.diceloss(output , mask)\n        bce = self.binloss(output , mask)\n        \n        loss = dice * 0.7 + bce * 0.3\n        \n        return loss","metadata":{"execution":{"iopub.status.busy":"2023-08-07T10:15:59.432494Z","iopub.execute_input":"2023-08-07T10:15:59.432949Z","iopub.status.idle":"2023-08-07T10:15:59.441242Z","shell.execute_reply.started":"2023-08-07T10:15:59.432905Z","shell.execute_reply":"2023-08-07T10:15:59.439985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class DiceCoef(nn.Module):\n    \n    def __init__(self, weight=None, size_average=True):\n    \n        super().__init__()\n\n    def forward(self, y_pred, y_true, smooth=1.):\n        \n        y_true = y_true.view(-1)\n        y_pred = y_pred.view(-1)\n        \n        y_pred = torch.round((y_pred - y_pred.min()) / (y_pred.max() - y_pred.min()))\n        \n        intersection = (y_true * y_pred).sum()\n        \n        dice = (2.0 * intersection + smooth)/(y_true.sum() + y_pred.sum() + smooth)\n        \n        return dice","metadata":{"execution":{"iopub.status.busy":"2023-08-07T10:16:00.989284Z","iopub.execute_input":"2023-08-07T10:16:00.989952Z","iopub.status.idle":"2023-08-07T10:16:00.999041Z","shell.execute_reply.started":"2023-08-07T10:16:00.989920Z","shell.execute_reply":"2023-08-07T10:16:00.997701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss_func = CustomLoss()\ndice_coe = DiceCoef()","metadata":{"execution":{"iopub.status.busy":"2023-08-07T10:16:02.533761Z","iopub.execute_input":"2023-08-07T10:16:02.534181Z","iopub.status.idle":"2023-08-07T10:16:02.539746Z","shell.execute_reply.started":"2023-08-07T10:16:02.534148Z","shell.execute_reply":"2023-08-07T10:16:02.538715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"border-radius:10px; border:#FF69B4 solid; padding: 15px; background-color: #F3f9ed; font-size:100%; text-align:left\">\n\n## $5.2$ $|$ $Optimizer$ \n\nAdam is an optimization algorithm that can be used to train deep learning models. It is a stochastic gradient descent (SGD) method that uses adaptive estimation of first-order and second-order moments. This makes it more efficient than SGD, as it does not need to recalculate the learning rate for each weight update.","metadata":{}},{"cell_type":"code","source":"optimizer_unet = torch.optim.Adam([\n    {'params': u_net.decoder.parameters(), 'lr': 5e-5}, \n    {'params': u_net.encoder.parameters(), 'lr': 8e-5},  \n])","metadata":{"execution":{"iopub.status.busy":"2023-08-07T10:16:48.072854Z","iopub.execute_input":"2023-08-07T10:16:48.073285Z","iopub.status.idle":"2023-08-07T10:16:48.082852Z","shell.execute_reply.started":"2023-08-07T10:16:48.073254Z","shell.execute_reply":"2023-08-07T10:16:48.081541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer_dlv3 = torch.optim.Adam(deep_lab_v3.parameters(), lr=0.001)","metadata":{"execution":{"iopub.status.busy":"2023-08-07T10:16:49.420021Z","iopub.execute_input":"2023-08-07T10:16:49.420453Z","iopub.status.idle":"2023-08-07T10:16:49.429952Z","shell.execute_reply.started":"2023-08-07T10:16:49.420420Z","shell.execute_reply":"2023-08-07T10:16:49.428738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <p style=\"font-family:JetBrains Mono; font-weight:bold; letter-spacing: 2px; color:#FFA07A; font-size:140%; text-align:left;padding: 0px; border-bottom: 3px solid #FFA07A\">6 | Training 🚃</p>\n\n<div style=\"border-radius:10px; border:#FFA07A solid; padding: 15px; background-color: #F3f9ed; font-size:100%; text-align:left\">\n\nI don't know the exact reason but even after reducing the `batch_size` to $1$, the `GPU` was facing `out of the memmory error`\n```\ntrain_dataloader = DataLoader(train_dataset, batch_size = 1 , shuffle = True)\n```\nAnt thus I will be commneting out the `training code`. I tried this to run on  colab and it ran perfectly. Dont know why this is happening. \n\n## $6.1$ $|$ $U-Net$ $Training$\n\n```\nfor epoch in tqdm.notebook.tqdm(range(5)):   \n    torch.cuda.empty_cache()     \n    model.train()\n    train_loss = 0\n    score = 0\n        \n    for data in tqdm.notebook.tqdm(train_dataloader ,total = len(train_dataloader)):\n        torch.cuda.empty_cache()\n        optimizer.zero_grad()\n        img, mask = data\n\n        img = img.to(\"cuda\")\n        mask = mask.to(\"cuda\")\n\n        outputs = model(img)  \n\n        loss =  loss_func(outputs , mask)\n        \n        loss.backward()\n        optimizer.step()\n\n        train_loss += loss.item()\n```\n```\n---------------------------------------------------------------------------\nOutOfMemoryError                          Traceback (most recent call last)\nCell In[31], line 15\n     12 img = img.to(\"cuda\")\n     13 mask = mask.to(\"cuda\")\n---> 15 outputs = model(img)  \n     17 loss =  loss_func(outputs , mask)\n     19 loss.backward()\n\nFile /opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py:1501, in Module._call_impl(self, *args, **kwargs)\n   1496 # If we don't have any hooks, we want to skip the rest of the logic in\n   1497 # this function, and just call forward.\n   1498 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks\n   1499         or _global_backward_pre_hooks or _global_backward_hooks\n   1500         or _global_forward_hooks or _global_forward_pre_hooks):\n-> 1501     return forward_call(*args, **kwargs)\n   1502 # Do not call functions when jit is used\n   1503 full_backward_hooks, non_full_backward_hooks = [], []\n\nFile /opt/conda/lib/python3.10/site-packages/segmentation_models_pytorch/base/model.py:29, in SegmentationModel.forward(self, x)\n     25 \"\"\"Sequentially pass `x` trough model`s encoder, decoder and heads\"\"\"\n     27 self.check_input_shape(x)\n---> 29 features = self.encoder(x)\n     30 decoder_output = self.decoder(*features)\n     32 masks = self.segmentation_head(decoder_output)\n\nFile /opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py:1501, in Module._call_impl(self, *args, **kwargs)\n   1496 # If we don't have any hooks, we want to skip the rest of the logic in\n   1497 # this function, and just call forward.\n   1498 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks\n   1499         or _global_backward_pre_hooks or _global_backward_hooks\n   1500         or _global_forward_hooks or _global_forward_pre_hooks):\n-> 1501     return forward_call(*args, **kwargs)\n   1502 # Do not call functions when jit is used\n   1503 full_backward_hooks, non_full_backward_hooks = [], []\n\nFile /opt/conda/lib/python3.10/site-packages/segmentation_models_pytorch/encoders/efficientnet.py:73, in EfficientNetEncoder.forward(self, x)\n     71             drop_connect = drop_connect_rate * block_number / len(self._blocks)\n     72             block_number += 1.0\n---> 73             x = module(x, drop_connect)\n     75     features.append(x)\n     77 return features\n\nFile /opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py:1501, in Module._call_impl(self, *args, **kwargs)\n   1496 # If we don't have any hooks, we want to skip the rest of the logic in\n   1497 # this function, and just call forward.\n   1498 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks\n   1499         or _global_backward_pre_hooks or _global_backward_hooks\n   1500         or _global_forward_hooks or _global_forward_pre_hooks):\n-> 1501     return forward_call(*args, **kwargs)\n   1502 # Do not call functions when jit is used\n   1503 full_backward_hooks, non_full_backward_hooks = [], []\n\nFile /opt/conda/lib/python3.10/site-packages/efficientnet_pytorch/model.py:111, in MBConvBlock.forward(self, inputs, drop_connect_rate)\n    109 x = self._depthwise_conv(x)\n    110 x = self._bn1(x)\n--> 111 x = self._swish(x)\n    113 # Squeeze and Excitation\n    114 if self.has_se:\n\nFile /opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py:1501, in Module._call_impl(self, *args, **kwargs)\n   1496 # If we don't have any hooks, we want to skip the rest of the logic in\n   1497 # this function, and just call forward.\n   1498 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks\n   1499         or _global_backward_pre_hooks or _global_backward_hooks\n   1500         or _global_forward_hooks or _global_forward_pre_hooks):\n-> 1501     return forward_call(*args, **kwargs)\n   1502 # Do not call functions when jit is used\n   1503 full_backward_hooks, non_full_backward_hooks = [], []\n\nFile /opt/conda/lib/python3.10/site-packages/efficientnet_pytorch/utils.py:80, in MemoryEfficientSwish.forward(self, x)\n     79 def forward(self, x):\n---> 80     return SwishImplementation.apply(x)\n\nFile /opt/conda/lib/python3.10/site-packages/torch/autograd/function.py:506, in Function.apply(cls, *args, **kwargs)\n    503 if not torch._C._are_functorch_transforms_active():\n    504     # See NOTE: [functorch vjp and autograd interaction]\n    505     args = _functorch.utils.unwrap_dead_wrappers(args)\n--> 506     return super().apply(*args, **kwargs)  # type: ignore[misc]\n    508 if cls.setup_context == _SingleLevelFunction.setup_context:\n    509     raise RuntimeError(\n    510         'In order to use an autograd.Function with functorch transforms '\n    511         '(vmap, grad, jvp, jacrev, ...), it must override the setup_context '\n    512         'staticmethod. For more details, please see '\n    513         'https://pytorch.org/docs/master/notes/extending.func.html style=\"color:rgb(175,0,0)\">')\n\nFile /opt/conda/lib/python3.10/site-packages/efficientnet_pytorch/utils.py:67, in SwishImplementation.forward(ctx, i)\n     65 @staticmethod\n     66 def forward(ctx, i):\n---> 67     result = i * torch.sigmoid(i)\n     68     ctx.save_for_backward(i)\n     69     return result\n\nOutOfMemoryError: CUDA out of memory. Tried to allocate 40.00 MiB (GPU 0; 15.90 GiB total capacity; 319.37 MiB already allocated; 7.75 MiB free; 326.00 MiB reserved in total by PyTorch) If reserved memory is >> allocated memory try setting max_split_size_mb to avoid fragmentation.  See documentation for Memory Management and PYTORCH_CUDA_ALLOC_CONF\n```\n    \n## $6.2$ $|$ $Deep$ $Lab$ $V3$ $Training$\n\n```\n#wandb.watch(deep_lab_v3 , log = \"all\" , log_freq = 10)\n\nfor epoch in tqdm.notebook.tqdm(range(5)):   \n    \n    torch.cuda.empty_cache()     \n\n    train_loss = 0\n    score = 0\n        \n    for data in tqdm.notebook.tqdm(dataloader ,total = len(dataloader)):\n        torch.cuda.empty_cache()\n        \n        img, mask = data\n\n        img = img.to(\"cuda\")\n        mask = mask.to(\"cuda\")\n\n        outputs = deep_lab_v3(img)[\"out\"][: , -1 , : , :] \n\n        loss =  loss_func(outputs , mask)\n        \n        loss.backward()\n        optimizer.step()\n\n        train_loss += loss.item()\n```","metadata":{}},{"cell_type":"markdown","source":"# <p style=\"font-family:JetBrains Mono; font-weight:bold; letter-spacing: 2px; color:#87CEEB; font-size:140%; text-align:left;padding: 0px; border-bottom: 3px solid #87CEEB\">7 | Results Visualization 🚃</p>","metadata":{}},{"cell_type":"code","source":"from IPython.display import IFrame","metadata":{"execution":{"iopub.status.busy":"2023-08-07T10:21:04.720001Z","iopub.execute_input":"2023-08-07T10:21:04.720505Z","iopub.status.idle":"2023-08-07T10:21:04.725881Z","shell.execute_reply.started":"2023-08-07T10:21:04.720466Z","shell.execute_reply":"2023-08-07T10:21:04.724604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"border-radius:10px; border:#87CEEB solid; padding: 15px; background-color: #F3f9ed; font-size:100%; text-align:left\">\n\n\nThough we couldnt train model on Kaggle server. We can still see its output. I ran the notebook on Colab and connected it with `wandb`. Below is the `wandb artifact` for the model. ","metadata":{}},{"cell_type":"code","source":"IFrame(\"https://wandb.ai/ayushsinghal659/uncategorized/reports/HuBMAP--Vmlldzo0NDgyNDQ4\" , 1300 , 400)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-08-07T10:21:08.298531Z","iopub.execute_input":"2023-08-07T10:21:08.298979Z","iopub.status.idle":"2023-08-07T10:21:08.307790Z","shell.execute_reply.started":"2023-08-07T10:21:08.298947Z","shell.execute_reply":"2023-08-07T10:21:08.306199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IFrame(\"https://wandb.ai/ayushsinghal659/DeepLabV3/reports/HuBMAP-DEEP-LAB-V3--Vmlldzo0NTE3ODk0\" , 1300 , 400)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-08-07T10:21:12.601521Z","iopub.execute_input":"2023-08-07T10:21:12.602187Z","iopub.status.idle":"2023-08-07T10:21:12.608969Z","shell.execute_reply.started":"2023-08-07T10:21:12.602152Z","shell.execute_reply":"2023-08-07T10:21:12.607880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <p style=\"font-family:JetBrains Mono; font-weight:bold; letter-spacing: 2px; color:#7A288A; font-size:140%; text-align:left;padding: 0px; border-bottom: 3px solid #7A288A\">8 | Ending 🫡</p>\n\n<div style=\"border-radius:10px; border:#7A288A solid; padding: 15px; background-color: #F3f9ed; font-size:100%; text-align:left\">\n\n**THAT IT FOR TODAY GUYS**\n\n**WE WILL GO DEEPER INTO THE DATA IN THE UPCOMING VERSIONS**\n\n**PLEASE COMMENT YOUR THOUGHTS, HIHGLY APPRICIATED**\n\n**DONT FORGET TO MAKE AN UPVOTE, IF YOU LIKED MY WORK :)**\n\n<img src = \"https://i.imgflip.com/19aadg.jpg\">\n\n**PEACE OUT !!!! :)**","metadata":{}}]}