|
| 1 | +# Copyright (c) MONAI Consortium |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# Unless required by applicable law or agreed to in writing, software |
| 7 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | +# See the License for the specific language governing permissions and |
| 10 | +# limitations under the License. |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +import torch |
| 15 | +import torch.nn.functional as F |
| 16 | +from torch.nn.modules.loss import _Loss |
| 17 | + |
| 18 | + |
| 19 | +def soft_erode(img: torch.Tensor) -> torch.Tensor: # type: ignore |
| 20 | + """ |
| 21 | + Perform soft erosion on the input image |
| 22 | +
|
| 23 | + Args: |
| 24 | + img: the shape should be BCH(WD) |
| 25 | +
|
| 26 | + Adapted from: |
| 27 | + https://github.com/jocpae/clDice/blob/master/cldice_loss/pytorch/soft_skeleton.py#L6 |
| 28 | + """ |
| 29 | + if len(img.shape) == 4: |
| 30 | + p1 = -(F.max_pool2d(-img, (3, 1), (1, 1), (1, 0))) |
| 31 | + p2 = -(F.max_pool2d(-img, (1, 3), (1, 1), (0, 1))) |
| 32 | + return torch.min(p1, p2) # type: ignore |
| 33 | + elif len(img.shape) == 5: |
| 34 | + p1 = -(F.max_pool3d(-img, (3, 1, 1), (1, 1, 1), (1, 0, 0))) |
| 35 | + p2 = -(F.max_pool3d(-img, (1, 3, 1), (1, 1, 1), (0, 1, 0))) |
| 36 | + p3 = -(F.max_pool3d(-img, (1, 1, 3), (1, 1, 1), (0, 0, 1))) |
| 37 | + return torch.min(torch.min(p1, p2), p3) # type: ignore |
| 38 | + |
| 39 | + |
| 40 | +def soft_dilate(img: torch.Tensor) -> torch.Tensor: # type: ignore |
| 41 | + """ |
| 42 | + Perform soft dilation on the input image |
| 43 | +
|
| 44 | + Args: |
| 45 | + img: the shape should be BCH(WD) |
| 46 | +
|
| 47 | + Adapted from: |
| 48 | + https://github.com/jocpae/clDice/blob/master/cldice_loss/pytorch/soft_skeleton.py#L18 |
| 49 | + """ |
| 50 | + if len(img.shape) == 4: |
| 51 | + return F.max_pool2d(img, (3, 3), (1, 1), (1, 1)) # type: ignore |
| 52 | + elif len(img.shape) == 5: |
| 53 | + return F.max_pool3d(img, (3, 3, 3), (1, 1, 1), (1, 1, 1)) # type: ignore |
| 54 | + |
| 55 | + |
| 56 | +def soft_open(img: torch.Tensor) -> torch.Tensor: |
| 57 | + """ |
| 58 | + Wrapper function to perform soft opening on the input image |
| 59 | +
|
| 60 | + Args: |
| 61 | + img: the shape should be BCH(WD) |
| 62 | +
|
| 63 | + Adapted from: |
| 64 | + https://github.com/jocpae/clDice/blob/master/cldice_loss/pytorch/soft_skeleton.py#L25 |
| 65 | + """ |
| 66 | + eroded_image = soft_erode(img) |
| 67 | + dilated_image = soft_dilate(eroded_image) |
| 68 | + return dilated_image |
| 69 | + |
| 70 | + |
| 71 | +def soft_skel(img: torch.Tensor, iter_: int) -> torch.Tensor: |
| 72 | + """ |
| 73 | + Perform soft skeletonization on the input image |
| 74 | +
|
| 75 | + Adapted from: |
| 76 | + https://github.com/jocpae/clDice/blob/master/cldice_loss/pytorch/soft_skeleton.py#L29 |
| 77 | +
|
| 78 | + Args: |
| 79 | + img: the shape should be BCH(WD) |
| 80 | + iter_: number of iterations for skeletonization |
| 81 | +
|
| 82 | + Returns: |
| 83 | + skeletonized image |
| 84 | + """ |
| 85 | + img1 = soft_open(img) |
| 86 | + skel = F.relu(img - img1) |
| 87 | + for _ in range(iter_): |
| 88 | + img = soft_erode(img) |
| 89 | + img1 = soft_open(img) |
| 90 | + delta = F.relu(img - img1) |
| 91 | + skel = skel + F.relu(delta - skel * delta) |
| 92 | + return skel |
| 93 | + |
| 94 | + |
| 95 | +def soft_dice(y_true: torch.Tensor, y_pred: torch.Tensor, smooth: float = 1.0) -> torch.Tensor: |
| 96 | + """ |
| 97 | + Function to compute soft dice loss |
| 98 | +
|
| 99 | + Adapted from: |
| 100 | + https://github.com/jocpae/clDice/blob/master/cldice_loss/pytorch/cldice.py#L22 |
| 101 | +
|
| 102 | + Args: |
| 103 | + y_true: the shape should be BCH(WD) |
| 104 | + y_pred: the shape should be BCH(WD) |
| 105 | +
|
| 106 | + Returns: |
| 107 | + dice loss |
| 108 | + """ |
| 109 | + intersection = torch.sum((y_true * y_pred)[:, 1:, ...]) |
| 110 | + coeff = (2.0 * intersection + smooth) / (torch.sum(y_true[:, 1:, ...]) + torch.sum(y_pred[:, 1:, ...]) + smooth) |
| 111 | + soft_dice: torch.Tensor = 1.0 - coeff |
| 112 | + return soft_dice |
| 113 | + |
| 114 | + |
| 115 | +class SoftclDiceLoss(_Loss): |
| 116 | + """ |
| 117 | + Compute the Soft clDice loss defined in: |
| 118 | +
|
| 119 | + Shit et al. (2021) clDice -- A Novel Topology-Preserving Loss Function |
| 120 | + for Tubular Structure Segmentation. (https://arxiv.org/abs/2003.07311) |
| 121 | +
|
| 122 | + Adapted from: |
| 123 | + https://github.com/jocpae/clDice/blob/master/cldice_loss/pytorch/cldice.py#L7 |
| 124 | + """ |
| 125 | + |
| 126 | + def __init__(self, iter_: int = 3, smooth: float = 1.0) -> None: |
| 127 | + """ |
| 128 | + Args: |
| 129 | + iter_: Number of iterations for skeletonization |
| 130 | + smooth: Smoothing parameter |
| 131 | + """ |
| 132 | + super().__init__() |
| 133 | + self.iter = iter_ |
| 134 | + self.smooth = smooth |
| 135 | + |
| 136 | + def forward(self, y_true: torch.Tensor, y_pred: torch.Tensor) -> torch.Tensor: |
| 137 | + skel_pred = soft_skel(y_pred, self.iter) |
| 138 | + skel_true = soft_skel(y_true, self.iter) |
| 139 | + tprec = (torch.sum(torch.multiply(skel_pred, y_true)[:, 1:, ...]) + self.smooth) / ( |
| 140 | + torch.sum(skel_pred[:, 1:, ...]) + self.smooth |
| 141 | + ) |
| 142 | + tsens = (torch.sum(torch.multiply(skel_true, y_pred)[:, 1:, ...]) + self.smooth) / ( |
| 143 | + torch.sum(skel_true[:, 1:, ...]) + self.smooth |
| 144 | + ) |
| 145 | + cl_dice: torch.Tensor = 1.0 - 2.0 * (tprec * tsens) / (tprec + tsens) |
| 146 | + return cl_dice |
| 147 | + |
| 148 | + |
| 149 | +class SoftDiceclDiceLoss(_Loss): |
| 150 | + """ |
| 151 | + Compute the Soft clDice loss defined in: |
| 152 | +
|
| 153 | + Shit et al. (2021) clDice -- A Novel Topology-Preserving Loss Function |
| 154 | + for Tubular Structure Segmentation. (https://arxiv.org/abs/2003.07311) |
| 155 | +
|
| 156 | + Adapted from: |
| 157 | + https://github.com/jocpae/clDice/blob/master/cldice_loss/pytorch/cldice.py#L38 |
| 158 | + """ |
| 159 | + |
| 160 | + def __init__(self, iter_: int = 3, alpha: float = 0.5, smooth: float = 1.0) -> None: |
| 161 | + """ |
| 162 | + Args: |
| 163 | + iter_: Number of iterations for skeletonization |
| 164 | + smooth: Smoothing parameter |
| 165 | + alpha: Weighing factor for cldice |
| 166 | + """ |
| 167 | + super().__init__() |
| 168 | + self.iter = iter_ |
| 169 | + self.smooth = smooth |
| 170 | + self.alpha = alpha |
| 171 | + |
| 172 | + def forward(self, y_true: torch.Tensor, y_pred: torch.Tensor) -> torch.Tensor: |
| 173 | + dice = soft_dice(y_true, y_pred, self.smooth) |
| 174 | + skel_pred = soft_skel(y_pred, self.iter) |
| 175 | + skel_true = soft_skel(y_true, self.iter) |
| 176 | + tprec = (torch.sum(torch.multiply(skel_pred, y_true)[:, 1:, ...]) + self.smooth) / ( |
| 177 | + torch.sum(skel_pred[:, 1:, ...]) + self.smooth |
| 178 | + ) |
| 179 | + tsens = (torch.sum(torch.multiply(skel_true, y_pred)[:, 1:, ...]) + self.smooth) / ( |
| 180 | + torch.sum(skel_true[:, 1:, ...]) + self.smooth |
| 181 | + ) |
| 182 | + cl_dice = 1.0 - 2.0 * (tprec * tsens) / (tprec + tsens) |
| 183 | + total_loss: torch.Tensor = (1.0 - self.alpha) * dice + self.alpha * cl_dice |
| 184 | + return total_loss |
0 commit comments