MCPcopy Create free account
hub / github.com/HiLab-git/ACELoss / ACLoss

Class ACLoss

aceloss.py:8–53  ·  view source on GitHub ↗

Active Contour Loss based on sobel filter

Source from the content-addressed store, hash-verified

6
7
8class ACLoss(nn.Module):
9 """
10 Active Contour Loss
11 based on sobel filter
12 """
13
14 def __init__(self, miu=1.0, classes=3):
15 super(ACLoss, self).__init__()
16
17 self.miu = miu
18 self.classes = classes
19 sobel_x = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]])
20 sobel_y = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]])
21
22 self.sobel_x = nn.Parameter(torch.from_numpy(sobel_x).float().expand(self.classes, 1, 3, 3),
23 requires_grad=False)
24 self.sobel_y = nn.Parameter(torch.from_numpy(sobel_y).float().expand(self.classes, 1, 3, 3),
25 requires_grad=False)
26
27 self.diff_x = nn.Conv2d(self.classes, self.classes, groups=self.classes, kernel_size=3, stride=1, padding=1,
28 bias=False)
29 self.diff_x.weight = self.sobel_x
30 self.diff_y = nn.Conv2d(self.classes, self.classes, groups=self.classes, kernel_size=3, stride=1, padding=1,
31 bias=False)
32 self.diff_y.weight = self.sobel_y
33
34 def forward(self, predication, label):
35 grd_x = self.diff_x(predication)
36 grd_y = self.diff_y(predication)
37
38 # length
39 length = torch.sum(
40 torch.abs(torch.sqrt(grd_x ** 2 + grd_y ** 2 + 1e-8)))
41 length = (length - length.min()) / (length.max() - length.min() + 1e-8)
42 length = torch.sum(length)
43
44 # region
45 label = label.float()
46 c_in = torch.ones_like(predication)
47 c_out = torch.zeros_like(predication)
48 region_in = torch.abs(torch.sum(predication * ((label - c_in) ** 2)))
49 region_out = torch.abs(
50 torch.sum((1 - predication) * ((label - c_out) ** 2)))
51 region = self.miu * region_in + region_out
52
53 return region + length
54
55
56class ACLossV2(nn.Module):

Callers 1

aceloss.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected