-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGradEdgeLoss3D.py
220 lines (187 loc) · 6.21 KB
/
GradEdgeLoss3D.py
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
"""
Created on Tue Jul 10 2022
Last Modified on Thu Apr 6 2023
@author: Agamdeep Chopra, [email protected]
@affiliation: University of Washington, Seattle WA
@reference: Thevenot, A. (2022, February 17). Implement canny edge detection
from scratch with PyTorch. Medium. Retrieved July 10, 2022, from
https://towardsdatascience.com/implement-canny-edge-detection-from-scratch-with-pytorch-a1cccfa58bed
"""
from numpy import asarray, float32
from torch import from_numpy, sum as tsum, stack, cat, float32 as tfloat32
from torch.nn import Module, Conv3d, L1Loss
from torch.nn.functional import pad as tpad
def get_sobel_kernel3D(n1=1, n2=2, n3=2):
'''
Returns 3D Sobel kernels Sx, Sy, Sz, & diagonal kernels for edge detection.
Parameters
----------
n1 : int, optional
Kernel value 1 (default 1).
n2 : int, optional
Kernel value 2 (default 2).
n3 : int, optional
Kernel value 3 (default 2).
Returns
-------
list
List of all the 3D Sobel kernels (Sx, Sy, Sz, diagonal kernels).
'''
Sx = asarray(
[[[-n1, 0, n1],
[-n2, 0, n2],
[-n1, 0, n1]],
[[-n2, 0, n2],
[-n3*n2, 0, n3*n2],
[-n2, 0, n2]],
[[-n1, 0, n1],
[-n2, 0, n2],
[-n1, 0, n1]]])
Sy = asarray(
[[[-n1, -n2, -n1],
[0, 0, 0],
[n1, n2, n1]],
[[-n2, -n3*n2, -n2],
[0, 0, 0],
[n2, n3*n2, n2]],
[[-n1, -n2, -n1],
[0, 0, 0],
[n1, n2, n1]]])
Sz = asarray(
[[[-n1, -n2, -n1],
[-n2, -n3*n2, -n2],
[-n1, -n2, -n1]],
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]],
[[n1, n2, n1],
[n2, n3*n2, n2],
[n1, n2, n1]]])
Sd11 = asarray(
[[[0, n1, n2],
[-n1, 0, n1],
[-n2, -n1, 0]],
[[0, n2, n2*n3],
[-n2, 0, n2],
[-n2*n3, -n2, 0]],
[[0, n1, n2],
[-n1, 0, n1],
[-n2, -n1, 0]]])
Sd12 = asarray(
[[[-n2, -n1, 0],
[-n1, 0, n1],
[0, n1, n2]],
[[-n2*n3, -n2, 0],
[-n2, 0, n2],
[0, n2, n2*n3]],
[[-n2, -n1, 0],
[-n1, 0, n1],
[0, n1, n2]]])
Sd21 = Sd11.T
Sd22 = Sd12.T
Sd31 = asarray([-S.T for S in Sd11.T])
Sd32 = asarray([S.T for S in Sd12.T])
return [Sx, Sy, Sz, Sd11, Sd12, Sd21, Sd22, Sd31, Sd32]
class GradEdge3D():
'''
Implements Sobel edge detection for 3D images using PyTorch.
Parameters
----------
n1 : int, optional
Filter size for the first dimension (default is 1).
n2 : int, optional
Filter size for the second dimension (default is 2).
n3 : int, optional
Filter size for the third dimension (default is 2).
'''
def __init__(self, n1=1, n2=2, n3=2):
super(GradEdge3D, self).__init__()
k_sobel = 3
S = get_sobel_kernel3D(n1, n2, n3)
self.sobel_filters = []
# Initialize Sobel filters for edge detection
for s in S:
sobel_filter = Conv3d(
in_channels=1, out_channels=1, stride=1,
kernel_size=k_sobel, padding=k_sobel // 2, bias=False)
sobel_filter.weight.data = from_numpy(
s.astype(float32)).reshape(1, 1, k_sobel, k_sobel, k_sobel)
sobel_filter = sobel_filter.to(dtype=tfloat32)
self.sobel_filters.append(sobel_filter)
def __call__(self, img, a=1):
'''
Perform edge detection on the given 3D image.
Parameters
----------
img : torch.Tensor
3D input tensor of shape (B, C, x, y, z).
a : int, optional
Padding size (default is 1).
Returns
-------
torch.Tensor
Tensor containing the gradient magnitudes of the edges.
'''
pad = (a, a, a, a, a, a)
B, C, H, W, D = img.shape
img = tpad(img, pad, mode='reflect')
# Calculate gradient magnitude of edges
grad_mag = (1 / C) * tsum(stack([tsum(cat(
[s.to(img.device)(img[:, c:c+1]) for c in range(C)],
dim=1) + 1e-6, dim=1) ** 2 for s in self.sobel_filters],
dim=1) + 1e-6, dim=1) ** 0.5
grad_mag = grad_mag[:, a:-a, a:-a, a:-a]
num = grad_mag - grad_mag.min()
dnm = grad_mag.max() - grad_mag.min() + 1e-6
norm_grad_mag = num / dnm
return norm_grad_mag.view(B, 1, H, W, D)
class GMELoss3D(Module):
'''
Implements Gradient Magnitude Edge Loss for 3D image data.
Parameters
----------
n1 : int
Filter size for the first dimension.
n2 : int
Filter size for the second dimension.
n3 : int
Filter size for the third dimension.
lam_errors : list
List of tuples (weight, loss function) for computing error.
reduction : str
Reduction method for loss ('sum' or 'mean').
'''
def __init__(self, n1=1, n2=2, n3=2,
lam_errors=[(1.0, L1Loss())], reduction='sum'):
super(GMELoss3D, self).__init__()
self.edge_filter = GradEdge3D(n1, n2, n3)
self.lam_errors = lam_errors
self.reduction = reduction
def forward(self, x, y):
'''
Compute the loss based on the edges detected in the input tensors.
Parameters
----------
x : torch.Tensor
Input tensor of shape (B, C, x, y, z).
y : torch.Tensor
Target tensor of shape (B, C, x, y, z).
Returns
-------
torch.Tensor
The computed loss value.
'''
assert x.shape == y.shape, 'Inputs must be of the same shape'
assert x.device == y.device, 'Inputs must be on the same device'
edge_x = self.edge_filter(x)
edge_y = self.edge_filter(y)
if self.reduction == 'sum':
error = 1e-6 + sum([lam * err_func(edge_x, edge_y)
for lam, err_func in self.lam_errors])
else:
error = 1e-6 + (
sum(
[lam * err_func(
edge_x, edge_y) for lam, err_func in self.lam_errors]
) / len(self.lam_errors))
return error