{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":61446,"databundleVersionId":6962461,"sourceType":"competition"},{"sourceId":6942858,"sourceType":"datasetVersion","datasetId":3987147}],"dockerImageVersionId":30626,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-19T06:33:21.198648Z","iopub.execute_input":"2023-12-19T06:33:21.199628Z","iopub.status.idle":"2023-12-19T06:33:23.459064Z","shell.execute_reply.started":"2023-12-19T06:33:21.199567Z","shell.execute_reply":"2023-12-19T06:33:23.458124Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install --upgrade certifi\n","metadata":{"execution":{"iopub.status.busy":"2023-12-19T06:54:18.453371Z","iopub.execute_input":"2023-12-19T06:54:18.454375Z","iopub.status.idle":"2023-12-19T06:54:31.150129Z","shell.execute_reply.started":"2023-12-19T06:54:18.454341Z","shell.execute_reply":"2023-12-19T06:54:31.148901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -q segmentation_models_pytorch","metadata":{"execution":{"iopub.status.busy":"2023-12-19T06:54:48.067005Z","iopub.execute_input":"2023-12-19T06:54:48.067794Z","iopub.status.idle":"2023-12-19T06:54:59.903342Z","shell.execute_reply.started":"2023-12-19T06:54:48.067758Z","shell.execute_reply":"2023-12-19T06:54:59.9022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=\"/kaggle/input/blood-vessel-segmentation/train\"\nprint(os.listdir(train))","metadata":{"execution":{"iopub.status.busy":"2023-12-19T05:51:20.115232Z","iopub.execute_input":"2023-12-19T05:51:20.115871Z","iopub.status.idle":"2023-12-19T05:51:20.121684Z","shell.execute_reply.started":"2023-12-19T05:51:20.115831Z","shell.execute_reply":"2023-12-19T05:51:20.120829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import certifi\nimport ssl\n\nssl_context = ssl.create_default_context(cafile=certifi.where())\nssl._create_default_https_context = ssl_context\n","metadata":{"execution":{"iopub.status.busy":"2023-12-19T06:58:39.082412Z","iopub.execute_input":"2023-12-19T06:58:39.083513Z","iopub.status.idle":"2023-12-19T06:58:39.099944Z","shell.execute_reply.started":"2023-12-19T06:58:39.083476Z","shell.execute_reply":"2023-12-19T06:58:39.099184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import ssl\nssl._create_default_https_context = ssl._create_unverified_context\n","metadata":{"execution":{"iopub.status.busy":"2023-12-19T07:00:00.803621Z","iopub.execute_input":"2023-12-19T07:00:00.804001Z","iopub.status.idle":"2023-12-19T07:00:00.80825Z","shell.execute_reply.started":"2023-12-19T07:00:00.803969Z","shell.execute_reply":"2023-12-19T07:00:00.807348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nconfig = tf.compat.v1.ConfigProto()\nconfig.gpu_options.allow_growth = True\nsession = tf.compat.v1.Session(config=config)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-19T05:54:33.968462Z","iopub.execute_input":"2023-12-19T05:54:33.968874Z","iopub.status.idle":"2023-12-19T05:54:52.796569Z","shell.execute_reply.started":"2023-12-19T05:54:33.968849Z","shell.execute_reply":"2023-12-19T05:54:52.795714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\nimport time\nfrom collections import defaultdict\nfrom matplotlib.patches import Rectangle\nimport torch\nimport torch.nn as nn\nfrom torch.utils.data import Dataset,DataLoader\nfrom torch.optim import lr_scheduler\nfrom torch.cuda import amp\nimport torch.optim as optim\nimport albumentations as A\nimport segmentation_models_pytorch as smp\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nimport torch.nn.functional as F\nfrom torchvision import transforms\nimport copy\nimport gc\nfrom torch.cuda import amp\nfrom tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2023-12-19T07:11:55.774003Z","iopub.execute_input":"2023-12-19T07:11:55.774372Z","iopub.status.idle":"2023-12-19T07:11:55.78124Z","shell.execute_reply.started":"2023-12-19T07:11:55.774342Z","shell.execute_reply":"2023-12-19T07:11:55.780332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def visualize_images(data, num_images=4):\n    images_folder = os.path.join(data, \"images\")\n    labels_folder = os.path.join(data, \"labels\")\n\n    count = 0\n\n    for file_name in os.listdir(images_folder):\n        if file_name.endswith(\".tif\"):\n            image_path = os.path.join(images_folder, file_name)\n            label_path = os.path.join(labels_folder, file_name)\n\n            if os.path.exists(label_path):\n                image = Image.open(image_path)\n                label = Image.open(label_path)\n\n                plt.figure(figsize=(10, 6))\n                plt.subplot(1, 2, 1)\n                plt.imshow(image)\n                plt.title('Image')\n                plt.grid(False)  # Turn off the grid\n\n                plt.subplot(1, 2, 2)\n                plt.imshow(label)\n                plt.title('Label')\n                plt.grid(False)  # Turn off the grid\n\n                plt.show()\n\n                count += 1\n\n                if count >= num_images:\n                    return\n            \ndata = \"/kaggle/input/blood-vessel-segmentation/train/kidney_1_dense\"\nvisualize_images(data, num_images=4)","metadata":{"execution":{"iopub.status.busy":"2023-12-19T05:54:59.115254Z","iopub.execute_input":"2023-12-19T05:54:59.116313Z","iopub.status.idle":"2023-12-19T05:55:01.7148Z","shell.execute_reply.started":"2023-12-19T05:54:59.116277Z","shell.execute_reply":"2023-12-19T05:55:01.713878Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data=\"/kaggle/input/blood-vessel-segmentation/train/kidney_3_sparse\"\nvisualize_images(data,num_images=4)","metadata":{"execution":{"iopub.status.busy":"2023-12-19T05:55:09.428336Z","iopub.execute_input":"2023-12-19T05:55:09.428985Z","iopub.status.idle":"2023-12-19T05:55:12.71484Z","shell.execute_reply.started":"2023-12-19T05:55:09.428942Z","shell.execute_reply":"2023-12-19T05:55:12.713811Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_patch_indices(image_shape,patch_size,stride):\n    max_indices=[image_shape[dim]-patch_size[dim]for dim in range(3)]\n    \n    start_indices=[random.randint(0,max_indices[dim])for dim in range(3)]\n    end_indices=[start_indices[dim]+patch_size[dim]for dim in range(3)]\n    \n    return start_indices,end_indices\n\ndef extract_patch(data,start_indices,end_indices):\n    return data[start_indices[0]:end_indices[0], start_indices[1]:end_indices[1], start_indices[2]:end_indices[2]]\n\n\ndef get_sorted_file_paths(directory, extension=None):\n    \"\"\" Returns a list of sorted file paths in the given directory with a specific extension. \"\"\"\n    file_paths = [os.path.join(directory, fname) for fname in sorted(os.listdir(directory)) if fname.endswith(extension)]\n    return file_paths\n\n\ndef load_data(image_dir, label_dir, image_ext='.tif', label_ext='.tif'):\n    \"\"\" Load and pair images and labels from directories. \"\"\"\n    image_paths = get_sorted_file_paths(image_dir, extension=image_ext)\n    label_paths = get_sorted_file_paths(label_dir, extension=label_ext)\n\n    # Create a dictionary of label paths with filenames without extension as keys\n    label_dict = {os.path.splitext(os.path.basename(path))[0]: path for path in label_paths}\n\n    paired_images = []\n    paired_labels = []\n\n    for img_path in image_paths:\n        img_filename_wo_ext = os.path.splitext(os.path.basename(img_path))[0]\n        if img_filename_wo_ext in label_dict:\n            paired_images.append(img_path)\n            paired_labels.append(label_dict[img_filename_wo_ext])\n\n    return paired_images, paired_labels","metadata":{"execution":{"iopub.status.busy":"2023-12-17T15:28:37.565868Z","iopub.execute_input":"2023-12-17T15:28:37.566243Z","iopub.status.idle":"2023-12-17T15:28:37.576983Z","shell.execute_reply.started":"2023-12-17T15:28:37.56621Z","shell.execute_reply":"2023-12-17T15:28:37.575946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimage_list=[\n    (\"/kaggle/input/blood-vessel-segmentation/train/kidney_1_dense/images\", \"/kaggle/input/blood-vessel-segmentation/train/kidney_1_dense/labels\"),\n    (\"/kaggle/input/blood-vessel-segmentation/train/kidney_2/images\", \"/kaggle/input/blood-vessel-segmentation/train/kidney_2/labels\"),\n    (\"/kaggle/input/blood-vessel-segmentation/train/kidney_3_sparse/images\", \"/kaggle/input/blood-vessel-segmentation/train/kidney_3_sparse/labels\"),\n    (\"/kaggle/input/blood-vessel-segmentation/train/kidney_3_sparse/images\", \"/kaggle/input/blood-vessel-segmentation/train/kidney_3_dense/labels\")\n]","metadata":{"execution":{"iopub.status.busy":"2023-12-19T05:59:34.077427Z","iopub.execute_input":"2023-12-19T05:59:34.078096Z","iopub.status.idle":"2023-12-19T05:59:34.083168Z","shell.execute_reply.started":"2023-12-19T05:59:34.078064Z","shell.execute_reply":"2023-12-19T05:59:34.082077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CFG:\n    seed  = 42\n    debug = True\n    exp_name = \"baseline\"\n    comment  = \"Deep labv3 + Unet-resnet50-512*512\"\n    model_name = \"Deeplabv3+UNET\"\n    backbone   = \"xception\"\n    ckpt_path = \"/kaggle/input/sennet-hoa-train-unet-simple-baseline/best_epoch.bin\"\n    valid_bs      = 32\n    train_bs      = 8\n    img_size      = [512, 512]\n    num_classes   = 1\n    epochs        = 15\n    n_accumulate  = max(1,64//train_bs)\n    lr            = 2e-2\n    scheduler     = \"CosineAnnealingLR\"\n    min_lr        = 1e-5\n    T_max         = int(2279/(train_bs * n_accumulate)*epochs)+50\n    T_0           = 25\n    warmup_epochs = 0\n    wd            = 1e-6\n    n_fold        = 7\n    device        = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n    \n    data_transforms = {\n        \"train\": A.Compose([\n            A.Resize(*img_size, interpolation=cv2.INTER_NEAREST),\n            A.HorizontalFlip(p=0.5),\n        ], p=1.0),\n        \n        \"valid\": A.Compose([\n            A.Resize(*img_size, interpolation=cv2.INTER_NEAREST),\n        ], p=1.0)\n    }\n    \n    dir_df = \"/kaggle/input/sennet-hoa-gt-data/gt.csv\"\n    data_root = \"/kaggle/input\"\n    train_groups = [\"kidney_1_dense\"]\n    valid_groups = [\"kidney_3_dense\"]\n    loss_func     = \"DiceLoss\"\n","metadata":{"execution":{"iopub.status.busy":"2023-12-19T07:08:03.895945Z","iopub.execute_input":"2023-12-19T07:08:03.896324Z","iopub.status.idle":"2023-12-19T07:08:03.905803Z","shell.execute_reply.started":"2023-12-19T07:08:03.896295Z","shell.execute_reply":"2023-12-19T07:08:03.904815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\ndef rle_encode(img):\n    '''\n    img: numpy array, 1 - mask, 0 - background\n    Returns run length as string formated\n    '''\n    pixels = img.flatten()\n    pixels = np.concatenate([[0], pixels, [0]])\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1\n    runs[1::2] -= runs[::2]\n    rle = ' '.join(str(x) for x in runs)\n    if rle=='':\n        rle = '1 0'\n    return rle\n\n","metadata":{"execution":{"iopub.status.busy":"2023-12-19T07:00:07.156276Z","iopub.execute_input":"2023-12-19T07:00:07.156873Z","iopub.status.idle":"2023-12-19T07:00:07.162854Z","shell.execute_reply.started":"2023-12-19T07:00:07.15684Z","shell.execute_reply":"2023-12-19T07:00:07.161904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_img(path):\n    img = cv2.imread(path, cv2.IMREAD_UNCHANGED)\n    img = np.tile(img[...,None], [1, 1, 3]) # gray to rgb\n    img = img.astype('float32') # original is uint16\n    mx = np.max(img)\n    if mx:\n        img/=mx # scale image to [0, 1]\n    return img\n\ndef load_msk(path):\n    msk = cv2.imread(path, cv2.IMREAD_UNCHANGED)\n    msk = msk.astype('float32')\n    msk/=255.0\n    return msk","metadata":{"execution":{"iopub.status.busy":"2023-12-19T07:00:08.87738Z","iopub.execute_input":"2023-12-19T07:00:08.877746Z","iopub.status.idle":"2023-12-19T07:00:08.88393Z","shell.execute_reply.started":"2023-12-19T07:00:08.877717Z","shell.execute_reply":"2023-12-19T07:00:08.883015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nclass BuildDataset(torch.utils.data.Dataset):\n    def __init__(self, img_paths, msk_paths=[], transforms=None):\n        self.img_paths  = img_paths\n        self.msk_paths  = msk_paths\n        self.transforms = transforms\n        \n    def __len__(self):\n        return len(self.img_paths)\n    \n    def __getitem__(self, index):\n        img_path  = self.img_paths[index]\n        img = load_img(img_path)\n        \n        if len(self.msk_paths)>0:\n            msk_path = self.msk_paths[index]\n            msk = load_msk(msk_path)\n            if self.transforms:\n                data = self.transforms(image=img, mask=msk)\n                img  = data['image']\n                msk  = data['mask']\n            img = np.transpose(img, (2, 0, 1))\n            return torch.tensor(img), torch.tensor(msk)\n        else:\n            orig_size = img.shape\n            if self.transforms:\n                data = self.transforms(image=img)\n                img  = data['image']\n            img = np.transpose(img, (2, 0, 1))\n            return torch.tensor(img), torch.tensor(np.array([orig_size[0], orig_size[1]]))\n\n","metadata":{"execution":{"iopub.status.busy":"2023-12-19T07:00:10.707207Z","iopub.execute_input":"2023-12-19T07:00:10.707606Z","iopub.status.idle":"2023-12-19T07:00:10.717514Z","shell.execute_reply.started":"2023-12-19T07:00:10.70756Z","shell.execute_reply":"2023-12-19T07:00:10.716619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\n\nDATASET_FOLDER = \"/kaggle/input/blood-vessel-segmentation\"\nfr_images = glob.glob(os.path.join(DATASET_FOLDER, \"test\", \"*\", \"*\", \"*.tif\"))\nprint(f\"found images: {len(fr_images)}\")\n","metadata":{"execution":{"iopub.status.busy":"2023-12-19T07:00:13.160215Z","iopub.execute_input":"2023-12-19T07:00:13.160564Z","iopub.status.idle":"2023-12-19T07:00:13.169303Z","shell.execute_reply.started":"2023-12-19T07:00:13.160535Z","shell.execute_reply":"2023-12-19T07:00:13.168353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset=BuildDataset(fr_images,[],transforms=CFG.data_transforms['valid'])\ntest_loader=DataLoader(test_dataset,batch_size=CFG.valid_bs,num_workers=4,shuffle=False,pin_memory=True)","metadata":{"execution":{"iopub.status.busy":"2023-12-19T07:00:14.800131Z","iopub.execute_input":"2023-12-19T07:00:14.800824Z","iopub.status.idle":"2023-12-19T07:00:14.805788Z","shell.execute_reply.started":"2023-12-19T07:00:14.800789Z","shell.execute_reply":"2023-12-19T07:00:14.804824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_groups = CFG.train_groups\nvalid_groups = CFG.valid_groups\ndir_df = pd.read_csv(CFG.dir_df)\ndir_df[\"img_path\"] = dir_df[\"img_path\"].apply(lambda x: os.path.join(CFG.data_root, x))\ndir_df[\"msk_path\"] = dir_df[\"msk_path\"].apply(lambda x: os.path.join(CFG.data_root, x))\ntrain_df = dir_df.query(\"group in @train_groups\").reset_index(drop=True)\nvalid_df = dir_df.query(\"group in @valid_groups\").reset_index(drop=True)\ntrain_img_paths = train_df[\"img_path\"].values.tolist()\ntrain_msk_paths = train_df[\"msk_path\"].values.tolist()\nvalid_img_paths = valid_df[\"img_path\"].values.tolist()\nvalid_msk_paths = valid_df[\"msk_path\"].values.tolist()\nif CFG.debug:\n    train_img_paths = train_img_paths[:CFG.train_bs*5]\n    train_msk_paths = train_msk_paths[:CFG.train_bs*5]\n    valid_img_paths = valid_img_paths[:CFG.valid_bs*3]\n    valid_msk_paths = valid_msk_paths[:CFG.valid_bs*3]","metadata":{"execution":{"iopub.status.busy":"2023-12-19T07:00:16.769283Z","iopub.execute_input":"2023-12-19T07:00:16.769655Z","iopub.status.idle":"2023-12-19T07:00:17.359688Z","shell.execute_reply.started":"2023-12-19T07:00:16.769622Z","shell.execute_reply":"2023-12-19T07:00:17.358857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = BuildDataset(train_img_paths, train_msk_paths, transforms=CFG.data_transforms['train'])\nvalid_dataset = BuildDataset(valid_img_paths, valid_msk_paths, transforms=CFG.data_transforms['valid'])\ntrain_loader = DataLoader(train_dataset, batch_size=CFG.train_bs, num_workers=0, shuffle=True, pin_memory=True, drop_last=False)\nvalid_loader = DataLoader(valid_dataset, batch_size=CFG.valid_bs, num_workers=0, shuffle=False, pin_memory=True)","metadata":{"execution":{"iopub.status.busy":"2023-12-19T07:00:18.733032Z","iopub.execute_input":"2023-12-19T07:00:18.733368Z","iopub.status.idle":"2023-12-19T07:00:18.739474Z","shell.execute_reply.started":"2023-12-19T07:00:18.733343Z","shell.execute_reply":"2023-12-19T07:00:18.738421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_ids = [random.randint(0, len(train_img_paths)) for _ in range(5)]\nfor sample_id in sample_ids:\n    data_name = train_df.loc[sample_id][\"id\"]\n    img, msk = train_dataset[sample_id]\n    img = img.permute((1, 2, 0)).numpy()*255.0\n    img = img.astype('uint8')\n    msk = (msk*255).numpy().astype('uint8')\n    plt.figure(figsize=(9, 4))\n    print(data_name)\n    plt.axis('off')\n    plt.subplot(1,3,1)\n    plt.imshow(img)\n    plt.subplot(1,3,2)\n    plt.imshow(msk)\n    plt.subplot(1,3,3)\n    plt.imshow(img, cmap='bone')\n    plt.imshow(msk, alpha=0.5)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-19T07:00:20.253164Z","iopub.execute_input":"2023-12-19T07:00:20.253528Z","iopub.status.idle":"2023-12-19T07:00:23.187505Z","shell.execute_reply.started":"2023-12-19T07:00:20.253497Z","shell.execute_reply":"2023-12-19T07:00:23.186646Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def build_model(backbone,num_classes,device):\n    model=smp.Unet(\n        encoder_name=backbone,\n        encoder_weights=\"imagenet\",\n        in_channels=3,\n        classes=num_classes,\n        activation=None,\n)\n    model.to(device)\n    return model","metadata":{"execution":{"iopub.status.busy":"2023-12-19T07:00:24.176679Z","iopub.execute_input":"2023-12-19T07:00:24.177024Z","iopub.status.idle":"2023-12-19T07:00:24.182442Z","shell.execute_reply.started":"2023-12-19T07:00:24.176999Z","shell.execute_reply":"2023-12-19T07:00:24.181498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = build_model(CFG.backbone, CFG.num_classes, CFG.device)","metadata":{"execution":{"iopub.status.busy":"2023-12-19T07:00:26.741309Z","iopub.execute_input":"2023-12-19T07:00:26.742141Z","iopub.status.idle":"2023-12-19T07:04:44.367347Z","shell.execute_reply.started":"2023-12-19T07:00:26.742108Z","shell.execute_reply":"2023-12-19T07:04:44.366342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DiceLoss = smp.losses.DiceLoss(mode='binary')\nBCELoss = smp.losses.SoftBCEWithLogitsLoss()\ndef criterion(y_pred, y_true):\n    if CFG.loss_func == \"DiceLoss\":\n        return DiceLoss(y_pred, y_true)\n    elif CFG.loss_func == \"BCELoss\":\n        y_true = y_true.unsqueeze(1)\n        return BCELoss(y_pred, y_true)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-19T07:06:48.466031Z","iopub.execute_input":"2023-12-19T07:06:48.467038Z","iopub.status.idle":"2023-12-19T07:06:48.473745Z","shell.execute_reply.started":"2023-12-19T07:06:48.466995Z","shell.execute_reply":"2023-12-19T07:06:48.472655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def dice_coef(y_true, y_pred, thr=0.5, dim=(2,3), epsilon=0.001):\n    y_true = y_true.unsqueeze(1).to(torch.float32)\n    y_pred = (y_pred>thr).to(torch.float32)\n    inter = (y_true*y_pred).sum(dim=dim)\n    den = y_true.sum(dim=dim) + y_pred.sum(dim=dim)\n    dice = ((2*inter+epsilon)/(den+epsilon)).mean(dim=(1,0))\n    return dice\n\ndef iou_coef(y_true, y_pred, thr=0.5, dim=(2,3), epsilon=0.001):\n    y_true = y_true.unsqueeze(1).to(torch.float32)\n    y_pred = (y_pred>thr).to(torch.float32)\n    inter = (y_true*y_pred).sum(dim=dim)\n    union = (y_true + y_pred - y_true*y_pred).sum(dim=dim)\n    iou = ((inter+epsilon)/(union+epsilon)).mean(dim=(1,0))\n    return iou","metadata":{"execution":{"iopub.status.busy":"2023-12-19T07:07:04.143536Z","iopub.execute_input":"2023-12-19T07:07:04.144505Z","iopub.status.idle":"2023-12-19T07:07:04.152681Z","shell.execute_reply.started":"2023-12-19T07:07:04.144465Z","shell.execute_reply":"2023-12-19T07:07:04.151656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def fetch_scheduler(optimizer):\n    if CFG.scheduler == 'CosineAnnealingLR':\n        scheduler = lr_scheduler.CosineAnnealingLR(optimizer,T_max=CFG.T_max, \n                                                   eta_min=CFG.min_lr)\n    elif CFG.scheduler == 'CosineAnnealingWarmRestarts':\n        scheduler = lr_scheduler.CosineAnnealingWarmRestarts(optimizer,T_0=CFG.T_0, \n                                                             eta_min=CFG.min_lr)\n    elif CFG.scheduler == 'ReduceLROnPlateau':\n        scheduler = lr_scheduler.ReduceLROnPlateau(optimizer,\n                                                   mode='min',\n                                                   factor=0.1,\n                                                   patience=7,\n                                                   threshold=0.0001,\n                                                   min_lr=CFG.min_lr,)\n    elif CFG.scheduer == 'ExponentialLR':\n        scheduler = lr_scheduler.ExponentialLR(optimizer, gamma=0.85)\n    elif CFG.scheduler == None:\n        return None\n        \n    return scheduler\noptimizer = optim.Adam(model.parameters(), lr=CFG.lr, weight_decay=CFG.wd)\nscheduler = fetch_scheduler(optimizer)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-19T07:08:09.713344Z","iopub.execute_input":"2023-12-19T07:08:09.713723Z","iopub.status.idle":"2023-12-19T07:08:09.723323Z","shell.execute_reply.started":"2023-12-19T07:08:09.713694Z","shell.execute_reply":"2023-12-19T07:08:09.722255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_optimizer = optim.Adam(model.parameters(), lr=CFG.lr, weight_decay=CFG.wd)\n_scheduler = fetch_scheduler(_optimizer)\nlr_list = []\nfor e in range(CFG.epochs):\n    for step in range(len(train_loader)):\n        lr_list.append(_optimizer.param_groups[0]['lr'])\n        if (step + 1) % CFG.n_accumulate == 0:\n            _optimizer.step()\n            _scheduler.step()\nplt.plot(np.array(range(len(lr_list))), np.array(lr_list))\nplt.show()\ndel _optimizer, _scheduler","metadata":{"execution":{"iopub.status.busy":"2023-12-19T07:08:21.92234Z","iopub.execute_input":"2023-12-19T07:08:21.922718Z","iopub.status.idle":"2023-12-19T07:08:22.118067Z","shell.execute_reply.started":"2023-12-19T07:08:21.922686Z","shell.execute_reply":"2023-12-19T07:08:22.117124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_one_epoch(model, optimizer, scheduler, dataloader, device, epoch):\n    model.train()\n    scaler = amp.GradScaler()\n    \n    dataset_size = 0\n    running_loss = 0.0\n    \n    pbar = tqdm(enumerate(dataloader), total=len(dataloader), desc='Train ')\n    for step, (images, masks) in pbar:         \n        images = images.to(device, dtype=torch.float)\n        masks  = masks.to(device, dtype=torch.float)\n        \n        batch_size = images.size(0)\n        \n        with amp.autocast(enabled=True):\n            y_pred = model(images)\n            loss   = criterion(y_pred, masks)\n            loss   = loss / CFG.n_accumulate\n            \n        scaler.scale(loss).backward()\n    \n        if (step + 1) % CFG.n_accumulate == 0:\n            scaler.step(optimizer)\n            scaler.update()\n\n            # zero the parameter gradients\n            optimizer.zero_grad()\n\n            if scheduler is not None:\n                scheduler.step()\n                \n        running_loss += (loss.item() * batch_size)\n        dataset_size += batch_size\n        \n        epoch_loss = running_loss / dataset_size\n        \n        mem = torch.cuda.memory_reserved() / 1E9 if torch.cuda.is_available() else 0\n        current_lr = optimizer.param_groups[0]['lr']\n        pbar.set_postfix( epoch=f'{epoch}',\n                          train_loss=f'{epoch_loss:0.4f}',\n                          lr=f'{current_lr:0.5f}',\n                          gpu_mem=f'{mem:0.2f} GB')\n    torch.cuda.empty_cache()\n    gc.collect()\n    return epoch_loss\n","metadata":{"execution":{"iopub.status.busy":"2023-12-19T07:11:17.332965Z","iopub.execute_input":"2023-12-19T07:11:17.333332Z","iopub.status.idle":"2023-12-19T07:11:17.343969Z","shell.execute_reply.started":"2023-12-19T07:11:17.333302Z","shell.execute_reply":"2023-12-19T07:11:17.342993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"@torch.no_grad()\ndef valid_one_epoch(model, dataloader, device, epoch):\n    model.eval()\n    \n    dataset_size = 0\n    running_loss = 0.0\n    \n    val_scores = []\n    \n    pbar = tqdm(enumerate(dataloader), total=len(dataloader), desc='Valid ')\n    for step, (images, masks) in pbar:        \n        images  = images.to(device, dtype=torch.float)\n        masks   = masks.to(device, dtype=torch.float)\n        \n        batch_size = images.size(0)\n        \n        y_pred  = model(images)\n        loss    = criterion(y_pred, masks)\n        \n        running_loss += (loss.item() * batch_size)\n        dataset_size += batch_size\n        \n        epoch_loss = running_loss / dataset_size\n        \n        y_pred = nn.Sigmoid()(y_pred)\n        val_dice = dice_coef(masks, y_pred).cpu().detach().numpy()\n        val_jaccard = iou_coef(masks, y_pred).cpu().detach().numpy()\n        val_scores.append([val_dice, val_jaccard])\n        \n        mem = torch.cuda.memory_reserved() / 1E9 if torch.cuda.is_available() else 0\n        current_lr = optimizer.param_groups[0]['lr']\n        pbar.set_postfix(valid_loss=f'{epoch_loss:0.4f}',\n                        lr=f'{current_lr:0.5f}',\n                        gpu_memory=f'{mem:0.2f} GB')\n    val_scores  = np.mean(val_scores, axis=0)\n    torch.cuda.empty_cache()\n    gc.collect()\n    return epoch_loss, val_scores","metadata":{"execution":{"iopub.status.busy":"2023-12-19T07:11:19.704098Z","iopub.execute_input":"2023-12-19T07:11:19.704821Z","iopub.status.idle":"2023-12-19T07:11:19.714588Z","shell.execute_reply.started":"2023-12-19T07:11:19.704786Z","shell.execute_reply":"2023-12-19T07:11:19.71375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def run_training(model, optimizer, scheduler, device, num_epochs):    \n    if torch.cuda.is_available():\n        print(\"cuda: {}\\n\".format(torch.cuda.get_device_name()))\n    \n    start = time.time()\n    best_model_wts = copy.deepcopy(model.state_dict())\n    best_loss      = np.inf\n    best_epoch     = -1\n    history = defaultdict(list)\n    \n    for epoch in range(1, num_epochs + 1): \n        gc.collect()\n        print(f'Epoch {epoch}/{num_epochs}', end='')\n        train_loss = train_one_epoch(model, optimizer, scheduler, \n                                           dataloader=train_loader, \n                                           device=CFG.device, epoch=epoch)\n        \n        val_loss, val_scores = valid_one_epoch(model, valid_loader, \n                                                 device=CFG.device, \n                                                 epoch=epoch)\n        val_dice, val_jaccard = val_scores\n        history['Train Loss'].append(train_loss)\n        history['Valid Loss'].append(val_loss)\n        history['Valid Dice'].append(val_dice)\n        history['Valid Jaccard'].append(val_jaccard)        \n        print(f'Valid Dice: {val_dice:0.4f} | Valid Jaccard: {val_jaccard:0.4f}')\n        print(f'Valid Loss: {val_loss}')\n        \n        # deep copy the model\n        if val_loss <= best_loss:\n            print(f\"Valid loss Improved ({best_loss} ---> {val_loss})\")\n            best_dice    = val_dice\n            best_jaccard = val_jaccard\n            best_loss = val_loss\n            best_epoch   = epoch\n            best_model_wts = copy.deepcopy(model.state_dict())\n            PATH = \"best_epoch.bin\"\n            torch.save(model.state_dict(), PATH)\n            print(f\"Model Saved\")\n            \n        last_model_wts = copy.deepcopy(model.state_dict())\n        PATH = \"last_epoch.bin\"\n        torch.save(model.state_dict(), PATH)\n            \n        print(); print()\n    \n    end = time.time()\n    time_elapsed = end - start\n    print('Training complete in {:.0f}h {:.0f}m {:.0f}s'.format(\n        time_elapsed // 3600, (time_elapsed % 3600) // 60, (time_elapsed % 3600) % 60))\n    print(\"Best Loss: {:.4f}\".format(best_loss))\n    \n    # load best model weights\n    model.load_state_dict(best_model_wts)\n    return model, history","metadata":{"execution":{"iopub.status.busy":"2023-12-19T07:13:46.165606Z","iopub.execute_input":"2023-12-19T07:13:46.166488Z","iopub.status.idle":"2023-12-19T07:13:46.179807Z","shell.execute_reply.started":"2023-12-19T07:13:46.166453Z","shell.execute_reply":"2023-12-19T07:13:46.178845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model, history = run_training(model, optimizer, scheduler,\n                                device=CFG.device,\n                                num_epochs=CFG.epochs)","metadata":{"execution":{"iopub.status.busy":"2023-12-19T07:13:49.559544Z","iopub.execute_input":"2023-12-19T07:13:49.559923Z","iopub.status.idle":"2023-12-19T07:17:02.014312Z","shell.execute_reply.started":"2023-12-19T07:13:49.559893Z","shell.execute_reply":"2023-12-19T07:17:02.0134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = BuildDataset(valid_img_paths, [], transforms=CFG.data_transforms['valid'])\nte_loader = DataLoader(test_dataset, batch_size=CFG.valid_bs, num_workers=0, shuffle=False, pin_memory=True)","metadata":{"execution":{"iopub.status.busy":"2023-12-19T07:19:18.640508Z","iopub.execute_input":"2023-12-19T07:19:18.641422Z","iopub.status.idle":"2023-12-19T07:19:18.646162Z","shell.execute_reply.started":"2023-12-19T07:19:18.641388Z","shell.execute_reply":"2023-12-19T07:19:18.645244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rles = []\npbar = tqdm(enumerate(te_loader), total=len(te_loader), desc='Inference ')\nfor step, (images, shapes) in pbar:\n    shapes = shapes.numpy()\n    images = images.to(CFG.device, dtype=torch.float)\n    with torch.no_grad():\n        preds = model(images)\n        preds = (nn.Sigmoid()(preds)>0.5).double()\n    preds = preds.cpu().numpy().astype(np.uint8)\n\n    for pred, shape in zip(preds, shapes):\n        pred = cv2.resize(pred[0], (shape[1], shape[0]), cv2.INTER_NEAREST)\n        rle = rle_encode(pred)\n        rles.append(rle)","metadata":{"execution":{"iopub.status.busy":"2023-12-19T07:19:20.412697Z","iopub.execute_input":"2023-12-19T07:19:20.413411Z","iopub.status.idle":"2023-12-19T07:19:38.400088Z","shell.execute_reply.started":"2023-12-19T07:19:20.413381Z","shell.execute_reply":"2023-12-19T07:19:38.399226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids = [f'{p.split(\"/\")[-3]}_{os.path.basename(p).split(\".\")[0]}' for p in valid_img_paths]\nsubmission = pd.DataFrame.from_dict({\n    \"id\": ids,\n    \"rle\": rles\n})\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-19T07:19:42.154793Z","iopub.execute_input":"2023-12-19T07:19:42.155157Z","iopub.status.idle":"2023-12-19T07:19:42.177248Z","shell.execute_reply.started":"2023-12-19T07:19:42.155124Z","shell.execute_reply":"2023-12-19T07:19:42.176285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"patch_size=[64,64,64]\nlabels=2\nsteps_per_epoch=60\nepochs=78\nvalidation_steps=20\n","metadata":{"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nclass AttentionBlock(tf.keras.layers.Layer):\n    def __init__(self,channels):\n        super(AttentionBlock,self).__init__() \n        self.convolution = tf.keras.layers.Conv2D(1, kernel_size=1)\n\n        \n    def forward(self,x):\n        attention=torch.sigmoid(self.convolution(x))\n        return attention *x\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2023-12-17T10:38:03.125218Z","iopub.execute_input":"2023-12-17T10:38:03.12637Z","iopub.status.idle":"2023-12-17T10:38:03.24928Z","shell.execute_reply.started":"2023-12-17T10:38:03.126332Z","shell.execute_reply":"2023-12-17T10:38:03.24829Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import layers,models\nclass UNetWithAttention(tf.keras.Model):\n    def __init__(self, in_channels, out_channels):\n        super(UNetWithAttention, self).__init__()\n\n        # Encoder\n        self.encoder_conv1 = layers.Conv2D(64, kernel_size=3, padding='same', activation='relu')\n        self.encoder_conv2 = layers.Conv2D(64, kernel_size=3, padding='same', activation='relu')\n        self.encoder_conv3 = layers.Conv2D(256, kernel_size=3, padding='same', activation='relu')\n        self.encoder_conv4 = layers.Conv2D(512, kernel_size=3, padding='same', activation='relu')\n        #self.encoder_conv5 = layers.Conv2D(1024, kernel_size=3, padding='same', activation='relu')\n\n        # Attention block\n        self.attention1 = AttentionBlock(1024)\n\n        # Decoder\n        self.decoder_conv5 = layers.Conv2D(512, kernel_size=3, padding='same', activation='relu')\n        #self.decoder_conv4 = layers.Conv2D(256, kernel_size=3, padding='same', activation='relu')\n        self.decoder_conv3 = layers.Conv2D(128, kernel_size=3, padding='same', activation='relu')\n        self.decoder_conv2 = layers.Conv2D(64, kernel_size=3, padding='same', activation='relu')\n        self.decoder_conv1 = layers.Conv2D(out_channels, kernel_size=3, padding='same', activation='relu')\n\n    def call(self, x):\n        # Encoder\n        x1 = self.encoder_conv1(x)\n        x2 = self.encoder_conv2(x1)\n        x3 = self.encoder_conv3(x2)\n        x4 = self.encoder_conv4(x3)\n       # x5 = self.encoder_conv5(x4)\n\n        # Attention\n        att1 = self.attention1(x4)\n\n        # Decoder\n        x5_att = self.decoder_conv5(att1)\n       # x4_att = self.decoder_conv4(x5_att)\n        x3_att = self.decoder_conv3(x4_att)\n        x2_att = self.decoder_conv2(x3_att)\n        x1_att = self.decoder_conv1(x2_att)\n\n        return x1_att\n# Example usage\n\"\"\"\nin_channels = 3  # Assuming RGB input\nout_channels = 1  # Assuming grayscale output\n#patch_size=[64,64,64]\nmodel = UNetWithAttention(in_channels, out_channels)\n\n# Generate random input tensor\n# Generate random input tensor with 3 channels\nbatch_size, channels,  height, width = 2, 3,256,256\ninput_tensor = torch.randn(batch_size, channels, height, width)\n\n\n# Forward pass\noutput_tensor = model(input_tensor)\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2023-12-17T11:09:07.729747Z","iopub.execute_input":"2023-12-17T11:09:07.730595Z","iopub.status.idle":"2023-12-17T11:09:07.745821Z","shell.execute_reply.started":"2023-12-17T11:09:07.730559Z","shell.execute_reply":"2023-12-17T11:09:07.744938Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install torchsummary","metadata":{"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install torchviz","metadata":{"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchsummary import summary\nfrom torchviz import make_dot\n# Create an instance of your model\nmodel = UNetWithAttention(in_channels=3, out_channels=1,patch_size=[64,64,64])\n\n# Print model summary\nsummary(model, input_size=(3, 64,64,64))  # Adjust input_size based on your actual input shape\n\n# Plot the model architecture\ninput_tensor = torch.randn(1, 3, 64,64,64)  # Adjust the size based on your actual input shape\noutput_tensor = model(input_tensor)\ngraph = make_dot(output_tensor, params=dict(model.named_parameters()))\ngraph.render(filename='unet_with_attention', format='png', cleanup=True)\ngraph","metadata":{"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nclass CustomDataset(Dataset):\n    def __init__(self,image_paths,label_paths,transform=None,augmentation=None):\n        self.image_paths=image_paths\n        self.label_paths=label_paths\n        self.transform=transform\n        self.augmentations=augmentations\n        \n    def __len__(self):\n        return len(self.image_paths)\n    \n    def __getitem__(self,index):\n        #image=cv2.cvtColor(cv2.imread(self.image_paths[index]),cv2.COLOR_BGR2RGB)\n        image=cv2.imread(self.image_paths[index])\n        #print(image.shape)\n        image=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)\n        label=cv2.imread(self.label_paths[index],cv2.IMREAD_GRAYSCALE)\n        if self.augmentations:\n            augmented = self.augmentations(image=image, mask=label)\n            image = augmented['image']\n            label = augmented['mask']\n        if self.transform:\n            image = self.transform(image)\n            label = self.transform(label)\n        \n        \n            \n        return {\"image\":image,\"label\":label}\n\"\"\"    \nseed=42\ntorch.manual_seed(seed)\ntorch.cuda.manual_seed(seed)\ntorch.backends.cudnn.deterministic=True\nrandom.seed(seed)\n\naugmentations = A.Compose([\n    A.HorizontalFlip(p=0.5),\n    A.VerticalFlip(p=0.5),\n   \n])\n\ntransform=transforms.Compose([transforms.ToTensor()])\n\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2023-12-17T15:52:57.134958Z","iopub.execute_input":"2023-12-17T15:52:57.135323Z","iopub.status.idle":"2023-12-17T15:52:57.14709Z","shell.execute_reply.started":"2023-12-17T15:52:57.135294Z","shell.execute_reply":"2023-12-17T15:52:57.146231Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"paired_images,paired_labels=load_data(image_list[0][0],image_list[0][1])\n#train_size=int(0.2 * len(paired_images))\n#train_images,train_labels=paired_images[:train_size],paired_labels[:train_size]\n#val_images,val_labels=paired_images[train_size:],paired_labels[train_size:]","metadata":{"execution":{"iopub.status.busy":"2023-12-17T15:52:59.635867Z","iopub.execute_input":"2023-12-17T15:52:59.636253Z","iopub.status.idle":"2023-12-17T15:52:59.669954Z","shell.execute_reply.started":"2023-12-17T15:52:59.636218Z","shell.execute_reply":"2023-12-17T15:52:59.669063Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]}]}