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

Function ACELoss

aceloss.py:89–164  ·  view source on GitHub ↗

Active Contour Loss based on total variations and mean curvature

(y_pred, y_true, u=1, a=1, b=1)

Source from the content-addressed store, hash-verified

87
88
89def ACELoss(y_pred, y_true, u=1, a=1, b=1):
90 """
91 Active Contour Loss
92 based on total variations and mean curvature
93 """
94 def first_derivative(input):
95 u = input
96 m = u.shape[2]
97 n = u.shape[3]
98
99 ci_0 = (u[:, :, 1, :] - u[:, :, 0, :]).unsqueeze(2)
100 ci_1 = u[:, :, 2:, :] - u[:, :, 0:m - 2, :]
101 ci_2 = (u[:, :, -1, :] - u[:, :, m - 2, :]).unsqueeze(2)
102 ci = torch.cat([ci_0, ci_1, ci_2], 2) / 2
103
104 cj_0 = (u[:, :, :, 1] - u[:, :, :, 0]).unsqueeze(3)
105 cj_1 = u[:, :, :, 2:] - u[:, :, :, 0:n - 2]
106 cj_2 = (u[:, :, :, -1] - u[:, :, :, n - 2]).unsqueeze(3)
107 cj = torch.cat([cj_0, cj_1, cj_2], 3) / 2
108
109 return ci, cj
110
111 def second_derivative(input, ci, cj):
112 u = input
113 m = u.shape[2]
114 n = u.shape[3]
115
116 cii_0 = (u[:, :, 1, :] + u[:, :, 0, :] -
117 2 * u[:, :, 0, :]).unsqueeze(2)
118 cii_1 = u[:, :, 2:, :] + u[:, :, :-2, :] - 2 * u[:, :, 1:-1, :]
119 cii_2 = (u[:, :, -1, :] + u[:, :, -2, :] -
120 2 * u[:, :, -1, :]).unsqueeze(2)
121 cii = torch.cat([cii_0, cii_1, cii_2], 2)
122
123 cjj_0 = (u[:, :, :, 1] + u[:, :, :, 0] -
124 2 * u[:, :, :, 0]).unsqueeze(3)
125 cjj_1 = u[:, :, :, 2:] + u[:, :, :, :-2] - 2 * u[:, :, :, 1:-1]
126 cjj_2 = (u[:, :, :, -1] + u[:, :, :, -2] -
127 2 * u[:, :, :, -1]).unsqueeze(3)
128
129 cjj = torch.cat([cjj_0, cjj_1, cjj_2], 3)
130
131 cij_0 = ci[:, :, :, 1:n]
132 cij_1 = ci[:, :, :, -1].unsqueeze(3)
133
134 cij_a = torch.cat([cij_0, cij_1], 3)
135 cij_2 = ci[:, :, :, 0].unsqueeze(3)
136 cij_3 = ci[:, :, :, 0:n - 1]
137 cij_b = torch.cat([cij_2, cij_3], 3)
138 cij = cij_a - cij_b
139
140 return cii, cjj, cij
141
142 def region(y_pred, y_true, u=1):
143 label = y_true.float()
144 c_in = torch.ones_like(y_pred)
145 c_out = torch.zeros_like(y_pred)
146 region_in = torch.abs(torch.sum(y_pred * ((label - c_in) ** 2)))

Callers 1

aceloss.pyFile · 0.85

Calls 2

regionFunction · 0.85
elasticaFunction · 0.85

Tested by

no test coverage detected