-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathTI_Loss.py
More file actions
179 lines (144 loc) · 7.08 KB
/
TI_Loss.py
File metadata and controls
179 lines (144 loc) · 7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import numpy as np
import torch
"""
The proposed topological interaction (TI) module encodes topological interactions by computing the critical voxels map. The critical voxels map contains the locations which induce errors in the topological interactions. The TI loss is introduced based on the topological interaction module.
"""
class TI_Loss(torch.nn.Module):
def __init__(self, dim, connectivity, inclusion, exclusion, min_thick=1):
"""
:param dim: 2 if 2D; 3 if 3D
:param connectivity: 4 or 8 for 2D; 6 or 26 for 3D
:param inclusion: list of [A,B] classes where A is completely surrounded by B.
:param exclusion: list of [A,C] classes where A and C exclude each other.
:param min_thick: Minimum thickness/separation between the two classes. Only used if connectivity is 8 for 2D or 26 for 3D
"""
super(TI_Loss, self).__init__()
self.dim = dim
self.connectivity = connectivity
self.min_thick = min_thick
self.interaction_list = []
self.sum_dim_list = None
self.conv_op = None
self.apply_nonlin = lambda x: torch.nn.functional.softmax(x, 1)
self.ce_loss_func = torch.nn.CrossEntropyLoss(reduction='none')
if self.dim == 2 :
self.sum_dim_list = [1,2,3]
self.conv_op = torch.nn.functional.conv2d
elif self.dim == 3 :
self.sum_dim_list = [1,2,3,4]
self.conv_op = torch.nn.functional.conv3d
self.set_kernel()
for inc in inclusion:
temp_pair = []
temp_pair.append(True) # type inclusion
temp_pair.append(inc[0])
temp_pair.append(inc[1])
self.interaction_list.append(temp_pair)
for exc in exclusion:
temp_pair = []
temp_pair.append(False) # type exclusion
temp_pair.append(exc[0])
temp_pair.append(exc[1])
self.interaction_list.append(temp_pair)
def set_kernel(self):
"""
Sets the connectivity kernel based on user's sepcification of dim, connectivity, min_thick
"""
k = 2 * self.min_thick + 1
if self.dim == 2:
if self.connectivity == 4:
np_kernel = np.array([[0,1,0],[1,1,1],[0,1,0]])
elif self.connectivity == 8:
np_kernel = np.ones((k, k))
elif self.dim == 3:
if self.connectivity == 6:
np_kernel = np.array([
[[0,0,0],[0,1,0],[0,0,0]],
[[0,1,0],[1,1,1],[0,1,0]],
[[0,0,0],[0,1,0],[0,0,0]]
])
elif self.connectivity == 26:
np_kernel = np.ones((k, k, k))
self.kernel = torch_kernel = torch.from_numpy(np.expand_dims(np.expand_dims(np_kernel,axis=0), axis=0))
def topological_interaction_module(self, P):
"""
Given a discrete segmentation map and the intended topological interactions, this module computes the critical voxels map.
:param P: Discrete segmentation map
:return: Critical voxels map
"""
for ind, interaction in enumerate(self.interaction_list):
interaction_type = interaction[0]
label_A = interaction[1]
label_C = interaction[2]
# Get Masks
mask_A = torch.where(P == label_A, 1.0, 0.0).double()
if interaction_type:
mask_C = torch.where(P == label_C, 1.0, 0.0).double()
mask_C = torch.logical_or(mask_C, mask_A).double()
mask_C = torch.logical_not(mask_C).double()
else:
mask_C = torch.where(P == label_C, 1.0, 0.0).double()
# Get Neighbourhood Information
neighbourhood_C = self.conv_op(mask_C, self.kernel.double(), padding='same')
neighbourhood_C = torch.where(neighbourhood_C >= 1.0, 1.0, 0.0)
neighbourhood_A = self.conv_op(mask_A, self.kernel.double(), padding='same')
neighbourhood_A = torch.where(neighbourhood_A >= 1.0, 1.0, 0.0)
# Get the pixels which induce errors
violating_A = neighbourhood_C * mask_A
violating_C = neighbourhood_A * mask_C
violating = violating_A + violating_C
violating = torch.where(violating >= 1.0, 1.0, 0.0)
if ind == 0:
critical_voxels_map = violating
else:
critical_voxels_map = torch.logical_or(critical_voxels_map, violating).double()
return critical_voxels_map
def forward(self, x, y):
"""
The forward function computes the TI loss value.
:param x: Likelihood map of shape: b, c, x, y(, z) with c = total number of classes
:param y: GT of shape: b, c, x, y(, z) with c=1. The GT should only contain values in [0,L) range where L is the total number of classes.
:return: TI loss value
"""
if x.device.type == "cuda":
self.kernel = self.kernel.cuda(x.device.index)
# Obtain discrete segmentation map
x_softmax = self.apply_nonlin(x)
P = torch.argmax(x_softmax, dim=1)
P = torch.unsqueeze(P.double(),dim=1)
del x_softmax
# Call the Topological Interaction Module
critical_voxels_map = self.topological_interaction_module(P)
# Compute the TI loss value
ce_tensor = torch.unsqueeze(self.ce_loss_func(x.double(),y[:,0].long()),dim=1)
ce_tensor[:,0] = ce_tensor[:,0] * torch.squeeze(critical_voxels_map, dim=1)
ce_loss_value = ce_tensor.sum(dim=self.sum_dim_list).mean()
return ce_loss_value
if __name__ == "__main__":
"""
Sample usage. In order to test the code, Input and GT are randomly populated with values.
Set the dim (2 for 2D; 3 for 3D) correctly to run relevant code.
The samples provided enforce the following interactions:
Enforce class 1 to be completely surrounded by class 2
Enforce class 2 to be excluded from class 3
Enforce class 3 to be excluded from class 4
"""
# Parameters for creating random input
num_classes = height = width = depth = 5
dim = 2
if dim == 2:
x = torch.rand(1,num_classes,height,width)
y = torch.randint(0, num_classes, (1,1,height,width))
ti_loss_weight = 1e-4
ti_loss_func = TI_Loss(dim=2, connectivity=4, inclusion=[[1,2]], exclusion=[[2,3],[3,4]])
ti_loss_value = ti_loss_func(x, y) if ti_loss_weight != 0 else 0
ti_loss_value = ti_loss_weight * ti_loss_value
print("ti_loss_value: ", ti_loss_value)
elif dim == 3:
x = torch.rand(1,num_classes,depth,height,width)
y = torch.randint(0, num_classes, (1,1,depth,height,width))
ti_loss_weight = 1e-6
ti_loss_func = TI_Loss(dim=3, connectivity=26, inclusion=[[1,2]], exclusion=[[2,3],[3,4]], min_thick=1)
ti_loss_value = ti_loss_func(x, y) if ti_loss_weight != 0 else 0
ti_loss_value = ti_loss_weight * ti_loss_value
print("ti_loss_value: ", ti_loss_value)