Active Contour Loss based on minpooling & maxpooling
| 229 | |
| 230 | |
| 231 | class ACLoss3DV2(nn.Module): |
| 232 | """ |
| 233 | Active Contour Loss |
| 234 | based on minpooling & maxpooling |
| 235 | """ |
| 236 | |
| 237 | def __init__(self, miu=1.0, classes=3): |
| 238 | super(ACLoss3DV2, self).__init__() |
| 239 | |
| 240 | self.miu = miu |
| 241 | self.classes = classes |
| 242 | |
| 243 | def forward(self, predication, label): |
| 244 | min_pool_x = nn.functional.max_pool3d( |
| 245 | predication * -1, (3, 3, 3), 1, 1) * -1 |
| 246 | contour = torch.relu(nn.functional.max_pool3d( |
| 247 | min_pool_x, (3, 3, 3), 1, 1) - min_pool_x) |
| 248 | |
| 249 | # length |
| 250 | length = torch.sum(torch.abs(contour)) |
| 251 | |
| 252 | # region |
| 253 | label = label.float() |
| 254 | c_in = torch.ones_like(predication) |
| 255 | c_out = torch.zeros_like(predication) |
| 256 | region_in = torch.abs(torch.sum(predication * ((label - c_in) ** 2))) |
| 257 | region_out = torch.abs( |
| 258 | torch.sum((1 - predication) * ((label - c_out) ** 2))) |
| 259 | region = self.miu * region_in + region_out |
| 260 | |
| 261 | return region + length |
| 262 | |
| 263 | |
| 264 | class ACELoss3D(nn.Module): |