封面 《時計仕掛けのレイライン -陽炎に彷徨う魔女-》

前言

最近看到了动态蛇形卷积这一模块,感觉其描述的按血管卷积和适合细长结构比较适合我的场景,因此阅读了解一下,并记录一下论文笔记。

结构

尽管现在有SAM、Universal Model等优秀模型,许多分割任务只需要在这上面进行简单微调即可,但是在一些复杂领域,大模型暂时没有很好的覆盖,例如一些复杂结构管状结构,如(3D血管、气管)。整个动态蛇形卷积受到可形变卷积(Deformable Convolution)的启发,其将可形变卷积的偏移量从固定的偏移量变成了动态的偏移量,从而使得其可以更好的适应细长结构。

可形变卷积效果如下,可以看到通过一个卷积计算特征图的偏移量,看起代码可以看到这些偏置只有一个范围约束,形变范围非常的自由,而蛇形卷积作者认为这样子容易让模型丢失占比小的细小结构,对于细长管状结构分割任务来说是一个巨大挑战。
可形变卷积

其提出对可形变卷积增加一个连续性的约束,使其像蛇一样连续移动,一节节如蛇一样移动,从而使得其可以更好的适应细长结构,每个卷积采用一个方向作为基准自由摆动。
蛇形卷积
x方向的蛇形卷积,可以看到通过约束,相邻两个感受野y坐标相差±1\pm1

Ki±c={(xi+c,yi+c)=(xi+c,yi+Σii+cΔy)(xic,yic)=(xic,yi+ΣiciΔy) K_{i\pm c}= \left\{ \begin {aligned} (x_{i+c}, y_{i+c}) = (x_i+c, y_i + \Sigma _{i}^{i+c} \Delta y) \\ (x_{i-c}, y_{i-c}) = (x_i-c, y_i + \Sigma _{i-c}^{i} \Delta y) \\ \end {aligned} \right.

同理y方向蛇形卷积公式如下

Kj±c={(xj+c,yj+c)=(xj+Σjj+cΔx,yj+c),(xjc,yjc)=(xj+ΣjcjΔx,yjc), K_{j\pm c}=\left \{ \begin {aligned} (x_{j+c}, y_{j+c}) = (x_{j} + \Sigma _{j}^{j+c} \Delta x, y_j+c), \\ (x_{j-c}, y_{j-c}) = (x_{j} + \Sigma _{j-c}^{j} \Delta x, y_j-c), \\ \end {aligned} \right.

蛇形卷积和其他卷积的对比效果如下,可以看到感受野的不同非常明显,且看在实际图像上的感受野,和可形变卷积比跟适合细长管状结构。

卷积感受野对比

卷积效果对比,一共三层卷积729个点(红色)的感受野,黄色为卷积核的位置

代码

动态蛇形卷积的代码如下,是直接从官方库修改,官方代码中在蛇形卷积中有一些重复代码,但是为了还原效果进行验证还先不改

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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
# -*- coding: utf-8 -*-
import torch
from torch import nn
import warnings
from monai.networks.blocks.convolutions import Convolution


class DSConv3d(nn.Module):
def __init__(
self,
in_ch: int,
out_ch: int,
kernel_size: int = 3,
extend_scope: int = 1,
morph: int = 0,
if_offset: bool = True,
):
super(DSConv3d, self).__init__()
assert out_ch >= 4, "out_ch must be larger than 4"
self.offset_conv = nn.Conv3d(in_ch, 3 * 2 * kernel_size, 3, padding=1)
self.bn = nn.BatchNorm3d(3 * 2 * kernel_size)
self.kernel_size = kernel_size

self.if_offset = if_offset
self.morph = morph
self.extend_scope = extend_scope

self.dcn_conv_x = nn.Conv3d(
in_ch,
out_ch,
kernel_size=(1, 1, kernel_size),
stride=(1, 1, kernel_size),
padding=0,
) #
self.dcn_conv_y = nn.Conv3d(
in_ch,
out_ch,
kernel_size=(1, kernel_size, 1),
stride=(1, kernel_size, 1),
padding=0,
) #
self.dcn_conv_z = nn.Conv3d(
in_ch,
out_ch,
kernel_size=(kernel_size, 1, 1),
stride=(kernel_size, 1, 1),
padding=0,
) #

self.dcn_conv = nn.Conv3d(
in_ch, out_ch, kernel_size=kernel_size, stride=kernel_size, padding=0
)
self.gn = nn.GroupNorm(out_ch // 4, out_ch)
self.relu = nn.ReLU(inplace=True)

def forward(self, f):
offset = self.offset_conv(f)
offset = self.bn(offset)
offset = torch.tanh(offset)
input_shape = f.shape

dcn = DCN3d(input_shape, self.kernel_size, self.extend_scope, self.morph)
deformed_feature = dcn.deform_conv(f, offset, self.if_offset)
if self.morph == 0:
x = self.dcn_conv_x(deformed_feature)
x = self.gn(x)
x = self.relu(x)
return x
elif self.morph == 1:
x = self.dcn_conv_y(deformed_feature)
x = self.gn(x)
x = self.relu(x)
return x
else:
x = self.dcn_conv_z(deformed_feature)
x = self.gn(x)
x = self.relu(x)
return x


class DCN3d(object):
def __init__(self, input_shape, kernel_size, extend_scope, morph):
self.num_points = kernel_size
self.depth = input_shape[2]
self.width = input_shape[3]
self.height = input_shape[4]
self.morph = morph
self.extend_scope = extend_scope # offset (-1 ~ 1) * extend_scope
self.num_batch = input_shape[0] # (N,C,D,W,H)
self.num_channels = input_shape[1]

"""
input: offset [N,3*K,D,W,H]
output: [N,1,K*D,W,H] coordinate map
output: [N,1,K,K*W,H] coordinate map
output: [N,1,D,W,K*H] coordinate map
"""

def _coordinate_map_3D(self, offset, if_offset):
device = offset.device
# offset
offset1, offset2 = torch.split(offset, 3 * self.num_points, dim=1)
z_offset1, y_offset1, x_offset1 = torch.split(offset1, self.num_points, dim=1)
z_offset2, y_offset2, x_offset2 = torch.split(offset2, self.num_points, dim=1)

z_center = torch.arange(0, self.depth).repeat([self.width * self.height])
z_center = z_center.reshape(self.width, self.height, self.depth)
z_center = z_center.permute(2, 1, 0)
z_center = z_center.reshape([-1, self.depth, self.width, self.height])
z_center = z_center.repeat([self.num_points, 1, 1, 1]).float()
z_center = z_center.unsqueeze(0)

y_center = torch.arange(0, self.width).repeat([self.height * self.depth])
y_center = y_center.reshape(self.height, self.depth, self.width)
y_center = y_center.permute(1, 2, 0)
y_center = y_center.reshape([-1, self.depth, self.width, self.height])
y_center = y_center.repeat([self.num_points, 1, 1, 1]).float()
y_center = y_center.unsqueeze(0)

x_center = torch.arange(0, self.height).repeat([self.depth * self.width])
x_center = x_center.reshape(self.depth, self.width, self.height)
x_center = x_center.permute(0, 1, 2)
x_center = x_center.reshape([-1, self.depth, self.width, self.height])
x_center = x_center.repeat([self.num_points, 1, 1, 1]).float()
x_center = x_center.unsqueeze(0)

if self.morph == 0:
z = torch.linspace(0, 0, 1)
y = torch.linspace(0, 0, 1)
x = torch.linspace(
-int(self.num_points // 2),
int(self.num_points // 2),
int(self.num_points),
)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
z, y, x = torch.meshgrid(z, y, x)
z_spread = z.reshape(-1, 1)
y_spread = y.reshape(-1, 1)
x_spread = x.reshape(-1, 1)

z_grid = z_spread.repeat([1, self.depth * self.width * self.height])
z_grid = z_grid.reshape(
[self.num_points, self.depth, self.width, self.height]
)
z_grid = z_grid.unsqueeze(0) # [N,K,D,W,H]

y_grid = y_spread.repeat([1, self.depth * self.width * self.height])
y_grid = y_grid.reshape(
[self.num_points, self.depth, self.width, self.height]
)
y_grid = y_grid.unsqueeze(0) # [N,K,D,W,H]

x_grid = x_spread.repeat([1, self.depth * self.width * self.height])
x_grid = x_grid.reshape(
[self.num_points, self.depth, self.width, self.height]
)
x_grid = x_grid.unsqueeze(0) # [N,K,D,W,H]

z_new = z_center + z_grid
y_new = y_center + y_grid
x_new = x_center + x_grid # [N,K,D,W,H]

z_new = z_new.repeat(self.num_batch, 1, 1, 1, 1)
y_new = y_new.repeat(self.num_batch, 1, 1, 1, 1)
x_new = x_new.repeat(self.num_batch, 1, 1, 1, 1)

z_new = z_new.to(device)
y_new = y_new.to(device)
x_new = x_new.to(device)

z_offset1_new = z_offset1.detach().clone()
y_offset1_new = y_offset1.detach().clone()

if if_offset:
z_offset1_new = z_offset1_new.permute(1, 0, 2, 3, 4)
y_offset1_new = y_offset1_new.permute(1, 0, 2, 3, 4)
z_offset1 = z_offset1.permute(1, 0, 2, 3, 4)
y_offset1 = y_offset1.permute(1, 0, 2, 3, 4)
center = int(self.num_points // 2)
z_offset1_new[center] = 0
y_offset1_new[center] = 0
for index in range(1, center + 1):
z_offset1_new[center + index] = (
z_offset1_new[center + index - 1] + z_offset1[center + index]
)
z_offset1_new[center - index] = (
z_offset1_new[center - index + 1] + z_offset1[center - index]
)
y_offset1_new[center + index] = (
y_offset1_new[center + index - 1] + y_offset1[center + index]
)
y_offset1_new[center - index] = (
y_offset1_new[center - index + 1] + y_offset1[center - index]
)
z_offset1_new = z_offset1_new.permute(1, 0, 2, 3, 4).to(device)
y_offset1_new = y_offset1_new.permute(1, 0, 2, 3, 4).to(device)
z_new = z_new.add(z_offset1_new.mul(self.extend_scope))
y_new = y_new.add(y_offset1_new.mul(self.extend_scope))

z_new = z_new.reshape(
[
self.num_batch,
1,
1,
self.num_points,
self.depth,
self.width,
self.height,
]
)
z_new = z_new.permute(0, 4, 1, 5, 2, 6, 3)
z_new = z_new.reshape(
[
self.num_batch,
self.depth,
self.width,
self.num_points * self.height,
]
)

y_new = y_new.reshape(
[
self.num_batch,
1,
1,
self.num_points,
self.depth,
self.width,
self.height,
]
)
y_new = y_new.permute(0, 4, 1, 5, 2, 6, 3)
y_new = y_new.reshape(
[
self.num_batch,
self.depth,
self.width,
self.num_points * self.height,
]
)

x_new = x_new.reshape(
[
self.num_batch,
1,
1,
self.num_points,
self.depth,
self.width,
self.height,
]
)
x_new = x_new.permute(0, 4, 1, 5, 2, 6, 3)
x_new = x_new.reshape(
[
self.num_batch,
self.depth,
self.width,
self.num_points * self.height,
]
)
return z_new, y_new, x_new

elif self.morph == 1:
z = torch.linspace(0, 0, 1)
y = torch.linspace(
-int(self.num_points // 2),
int(self.num_points // 2),
int(self.num_points),
)
x = torch.linspace(0, 0, 1)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
z, y, x = torch.meshgrid(z, y, x)
z_spread = z.reshape(-1, 1)
y_spread = y.reshape(-1, 1)
x_spread = x.reshape(-1, 1)

z_grid = z_spread.repeat([1, self.depth * self.width * self.height])
z_grid = z_grid.reshape(
[self.num_points, self.depth, self.width, self.height]
)
z_grid = z_grid.unsqueeze(0) # [N*K,D,W,H]

y_grid = y_spread.repeat([1, self.depth * self.width * self.height])
y_grid = y_grid.reshape(
[self.num_points, self.depth, self.width, self.height]
)
y_grid = y_grid.unsqueeze(0) # [N*K*K*K,D,W,H]

x_grid = x_spread.repeat([1, self.depth * self.width * self.height])
x_grid = x_grid.reshape(
[self.num_points, self.depth, self.width, self.height]
)
x_grid = x_grid.unsqueeze(0) # [N*K*K*K,D,W,H]

z_new = z_center + z_grid
y_new = y_center + y_grid
x_new = x_center + x_grid # [N*K*K*K,D,W,H]

z_new = z_new.repeat(self.num_batch, 1, 1, 1, 1)
y_new = y_new.repeat(self.num_batch, 1, 1, 1, 1)
x_new = x_new.repeat(self.num_batch, 1, 1, 1, 1)

z_new = z_new.to(device)
y_new = y_new.to(device)
x_new = x_new.to(device)
x_offset1_new = x_offset1.detach().clone()
z_offset2_new = z_offset2.detach().clone()

if if_offset:
x_offset1_new = x_offset1_new.permute(1, 0, 2, 3, 4)
z_offset2_new = z_offset2_new.permute(1, 0, 2, 3, 4)
x_offset1 = x_offset1.permute(1, 0, 2, 3, 4)
z_offset2 = z_offset2.permute(1, 0, 2, 3, 4)
center = int(self.num_points // 2)
x_offset1_new[center] = 0
z_offset2_new[center] = 0
for index in range(1, center + 1):
x_offset1_new[center + index] = (
x_offset1_new[center + index - 1] + x_offset1[center + index]
)
x_offset1_new[center - index] = (
x_offset1_new[center - index + 1] + x_offset1[center - index]
)
z_offset2_new[center + index] = (
z_offset2_new[center + index - 1] + z_offset2[center + index]
)
z_offset2_new[center - index] = (
z_offset2_new[center - index + 1] + z_offset2[center - index]
)
x_offset1_new = x_offset1_new.permute(1, 0, 2, 3, 4).to(device)
z_offset2_new = z_offset2_new.permute(1, 0, 2, 3, 4).to(device)
z_new = z_new.add(z_offset2_new.mul(self.extend_scope))
x_new = x_new.add(x_offset1_new.mul(self.extend_scope))
z_new = z_new.reshape(
[
self.num_batch,
1,
self.num_points,
1,
self.depth,
self.width,
self.height,
]
)
z_new = z_new.permute(0, 4, 1, 5, 2, 6, 3)
z_new = z_new.reshape(
[self.num_batch, self.depth, self.num_points * self.width, self.height]
)
y_new = y_new.reshape(
[
self.num_batch,
1,
self.num_points,
1,
self.depth,
self.width,
self.height,
]
)
y_new = y_new.permute(0, 4, 1, 5, 2, 6, 3)
y_new = y_new.reshape(
[self.num_batch, self.depth, self.num_points * self.width, self.height]
)
x_new = x_new.reshape(
[
self.num_batch,
1,
self.num_points,
1,
self.depth,
self.width,
self.height,
]
)
x_new = x_new.permute(0, 4, 1, 5, 2, 6, 3)
x_new = x_new.reshape(
[self.num_batch, self.depth, self.num_points * self.width, self.height]
)
return z_new, y_new, x_new

else:
z = torch.linspace(
-int(self.num_points // 2),
int(self.num_points // 2),
int(self.num_points),
)
y = torch.linspace(0, 0, 1)
x = torch.linspace(0, 0, 1)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
z, y, x = torch.meshgrid(z, y, x)
z_spread = z.reshape(-1, 1)
y_spread = y.reshape(-1, 1)
x_spread = x.reshape(-1, 1)

z_grid = z_spread.repeat([1, self.depth * self.width * self.height])
z_grid = z_grid.reshape(
[self.num_points, self.depth, self.width, self.height]
)
z_grid = z_grid.unsqueeze(0) # [N*K,D,W,H]

y_grid = y_spread.repeat([1, self.depth * self.width * self.height])
y_grid = y_grid.reshape(
[self.num_points, self.depth, self.width, self.height]
)
y_grid = y_grid.unsqueeze(0) # [N*K*K*K,D,W,H]

x_grid = x_spread.repeat([1, self.depth * self.width * self.height])
x_grid = x_grid.reshape(
[self.num_points, self.depth, self.width, self.height]
)
x_grid = x_grid.unsqueeze(0) # [N*K*K*K,D,W,H]

z_new = z_center + z_grid
y_new = y_center + y_grid
x_new = x_center + x_grid # [N*K*K*K,D,W,H]

z_new = z_new.repeat(self.num_batch, 1, 1, 1, 1)
y_new = y_new.repeat(self.num_batch, 1, 1, 1, 1)
x_new = x_new.repeat(self.num_batch, 1, 1, 1, 1)

z_new = z_new.to(device)
y_new = y_new.to(device)
x_new = x_new.to(device)
x_offset2_new = x_offset2.detach().clone()
y_offset2_new = y_offset2.detach().clone()

if if_offset:
x_offset2_new = x_offset2_new.permute(1, 0, 2, 3, 4)
y_offset2_new = y_offset2_new.permute(1, 0, 2, 3, 4)
x_offset2 = x_offset2.permute(1, 0, 2, 3, 4)
y_offset2 = y_offset2.permute(1, 0, 2, 3, 4)
center = int(self.num_points // 2)
x_offset2_new[center] = 0
x_offset2_new[center] = 0
for index in range(1, center + 1):
x_offset2_new[center + index] = (
x_offset2_new[center + index - 1] + x_offset2[center + index]
)
x_offset2_new[center - index] = (
x_offset2_new[center - index + 1] + x_offset2[center - index]
)
y_offset2_new[center + index] = (
y_offset2_new[center + index - 1] + y_offset2[center + index]
)
y_offset2_new[center - index] = (
y_offset2_new[center - index + 1] + y_offset2[center - index]
)
x_offset2_new = x_offset2_new.permute(1, 0, 2, 3, 4).to(device)
y_offset2_new = y_offset2_new.permute(1, 0, 2, 3, 4).to(device)
x_new = x_new.add(x_offset2_new.mul(self.extend_scope))
y_new = y_new.add(y_offset2_new.mul(self.extend_scope))

z_new = z_new.reshape(
[
self.num_batch,
self.num_points,
1,
1,
self.depth,
self.width,
self.height,
]
)
z_new = z_new.permute(0, 4, 1, 5, 2, 6, 3)
z_new = z_new.reshape(
[self.num_batch, self.num_points * self.depth, self.width, self.height]
)

y_new = y_new.reshape(
[
self.num_batch,
self.num_points,
1,
1,
self.depth,
self.width,
self.height,
]
)
y_new = y_new.permute(0, 4, 1, 5, 2, 6, 3)
y_new = y_new.reshape(
[self.num_batch, self.num_points * self.depth, self.width, self.height]
)

x_new = x_new.reshape(
[
self.num_batch,
self.num_points,
1,
1,
self.depth,
self.width,
self.height,
]
)
x_new = x_new.permute(0, 4, 1, 5, 2, 6, 3)
x_new = x_new.reshape(
[self.num_batch, self.num_points * self.depth, self.width, self.height]
)
return z_new, y_new, x_new

"""
input: input feature map [N,C,D,W,H];coordinate map [N,K*D,K*W,K*H]
output: [N,1,K*D,K*W,K*H] deformed feature map
"""

def _bilinear_interpolate_3D(self, input_feature, z, y, x):
device = input_feature.device
z = z.reshape([-1]).float()
y = y.reshape([-1]).float()
x = x.reshape([-1]).float() # [N*KD*KW*KH]

zero = torch.zeros([]).int()
max_z = self.depth - 1
max_y = self.width - 1
max_x = self.height - 1

# find 8 grid locations
z0 = torch.floor(z).int()
z1 = z0 + 1
y0 = torch.floor(y).int()
y1 = y0 + 1
x0 = torch.floor(x).int()
x1 = x0 + 1

# clip out coordinates exceeding feature map volume以外的点
z0 = torch.clamp(z0, zero, max_z)
z1 = torch.clamp(z1, zero, max_z)
y0 = torch.clamp(y0, zero, max_y)
y1 = torch.clamp(y1, zero, max_y)
x0 = torch.clamp(x0, zero, max_x)
x1 = torch.clamp(x1, zero, max_x) # [N*KD*KW*KH]

# convert input_feature and coordinate X, Y to 3D,for gathering
# input_feature_flat = input_feature.reshape([-1, self.num_channels]) # [N*D*W*H, C]
input_feature_flat = input_feature.flatten()
input_feature_flat = input_feature_flat.reshape(
self.num_batch, self.num_channels, self.depth, self.width, self.height
)
input_feature_flat = input_feature_flat.permute(0, 2, 3, 4, 1)
input_feature_flat = input_feature_flat.reshape(-1, self.num_channels)
dimension = self.height * self.width * self.depth

base = torch.arange(self.num_batch) * dimension
base = base.reshape([-1, 1]).float() # [N,1]

repeat = torch.ones(
[self.num_points * self.depth * self.width * self.height]
).unsqueeze(0)
repeat = repeat.float() # [1,D*W*H*K*K*K]

base = torch.matmul(
base, repeat
) # [N,1] * [1,D*W*H*K*K*K] ==> [N,D*W*H*K*K*K]
base = base.reshape([-1]) # [D*W*H*K*K*K]

base = base.to(device)

base_z0 = base + z0 * self.height * self.width
base_z1 = base + z1 * self.height * self.width
base_y0 = base + y0 * self.height
base_y1 = base + y1 * self.height

# top rectangle of the neighbourhood volume
index_a0 = base_y0 + base_z0 - base + x0
index_b0 = base_y0 + base_z1 - base + x0
index_c0 = base_y0 + base_z0 - base + x1
index_d0 = base_y0 + base_z1 - base + x1 # [N*KD*KW*KH]

# bottom rectangle of the neighbourhood volume
index_a1 = base_y1 + base_z0 - base + x0
index_b1 = base_y1 + base_z1 - base + x0
index_c1 = base_y1 + base_z0 - base + x1
index_d1 = base_y1 + base_z1 - base + x1 # [N*KD*KW*KH]

# get 8 grid values ([N*D*W*H,C], [N*D*W*H*27])
value_a0 = input_feature_flat[index_a0.type(torch.int64)]
value_b0 = input_feature_flat[index_b0.type(torch.int64)]
value_c0 = input_feature_flat[index_c0.type(torch.int64)]
value_d0 = input_feature_flat[index_d0.type(torch.int64)]
value_a1 = input_feature_flat[index_a1.type(torch.int64)]
value_b1 = input_feature_flat[index_b1.type(torch.int64)]
value_c1 = input_feature_flat[index_c1.type(torch.int64)]
value_d1 = input_feature_flat[index_d1.type(torch.int64)] # [N*KD*KW*KH, C]

# find 8 grid locations
z0 = torch.floor(z).int()
z1 = z0 + 1
y0 = torch.floor(y).int()
y1 = y0 + 1
x0 = torch.floor(x).int()
x1 = x0 + 1

# clip out coordinates exceeding feature map volume以外的点
z0 = torch.clamp(z0, zero, max_z + 1)
z1 = torch.clamp(z1, zero, max_z + 1)
y0 = torch.clamp(y0, zero, max_y + 1)
y1 = torch.clamp(y1, zero, max_y + 1)
x0 = torch.clamp(x0, zero, max_x + 1)
x1 = torch.clamp(x1, zero, max_x + 1) # [N*KD*KW*KH]

x0_float = x0.float()
x1_float = x1.float()
y0_float = y0.float()
y1_float = y1.float()
z0_float = z0.float()
z1_float = z1.float()

vol_a0 = ((z1_float - z) * (y1_float - y) * (x1_float - x)).unsqueeze(-1)
vol_b0 = ((z - z0_float) * (y1_float - y) * (x1_float - x)).unsqueeze(-1)
vol_c0 = ((z1_float - z) * (y1_float - y) * (x - x0_float)).unsqueeze(-1)
vol_d0 = ((z - z0_float) * (y1_float - y) * (x - x0_float)).unsqueeze(-1)
vol_a1 = ((z1_float - z) * (y - y0_float) * (x1_float - x)).unsqueeze(-1)
vol_b1 = ((z - z0_float) * (y - y0_float) * (x1_float - x)).unsqueeze(-1)
vol_c1 = ((z1_float - z) * (y - y0_float) * (x - x0_float)).unsqueeze(-1)
vol_d1 = ((z - z0_float) * (y - y0_float) * (x - x0_float)).unsqueeze(
-1
) # [N*KD*KW*KH, C]

outputs = (
value_a0 * vol_a0
+ value_b0 * vol_b0
+ value_c0 * vol_c0
+ value_d0 * vol_d0
+ value_a1 * vol_a1
+ value_b1 * vol_b1
+ value_c1 * vol_c1
+ value_d1 * vol_d1
)

if self.morph == 0:
outputs = outputs.reshape(
[
self.num_batch,
self.depth,
self.width,
self.num_points * self.height,
self.num_channels,
]
)
outputs = outputs.permute(0, 4, 1, 2, 3)
elif self.morph == 1:
outputs = outputs.reshape(
[
self.num_batch,
self.depth,
self.num_points * self.width,
self.height,
self.num_channels,
]
)
outputs = outputs.permute(0, 4, 1, 2, 3)
else:
outputs = outputs.reshape(
[
self.num_batch,
self.num_points * self.depth,
self.width,
self.height,
self.num_channels,
]
)
outputs = outputs.permute(0, 4, 1, 2, 3)
return outputs

def deform_conv(self, input, offset, if_offset):
z, y, x = self._coordinate_map_3D(offset, if_offset)
deformed_feature = self._bilinear_interpolate_3D(input, z, y, x)
return deformed_feature


class DSConv3dBlock(nn.Module):
def __init__(
self,
in_channels: int,
out_channels: int,
kernel_size: int = 3,
extend_scope: int = 1,
if_offset: bool = True,
res_block: bool = False,
dropout_rate: float = 0.0,
) -> None:
super(DSConv3dBlock, self).__init__()

self.res_block = res_block

self.conv = Convolution(
spatial_dims=3,
in_channels=in_channels,
out_channels=out_channels,
dropout=dropout_rate,
)
self.ds_conv_x = DSConv3d(
in_channels, out_channels, kernel_size, extend_scope, 0, if_offset
)
self.ds_conv_y = DSConv3d(
in_channels, out_channels, kernel_size, extend_scope, 1, if_offset
)
self.ds_conv_z = DSConv3d(
in_channels, out_channels, kernel_size, extend_scope, 2, if_offset
)

self.out_conv = Convolution(
spatial_dims=3,
in_channels=4 * out_channels,
out_channels=out_channels,
dropout=dropout_rate,
)

def forward(self, inp):
residual = inp
conv_out = self.conv(inp)
x = self.ds_conv_x(inp)
y = self.ds_conv_y(inp)
z = self.ds_conv_z(inp)

out = torch.cat([conv_out, x, y, z], dim=1)
out = self.out_conv(out)

if self.res_block:
out += residual

return out

实验结果

笔者在ISLES2017数据集上采用了DSCNet,其中UNERT和SegResNet的Dice系数都是0.5,而DSCNet效果是0.6,提升了0.1,但是考虑到ISLES2017数据集本身就很小,该效果需要进一步验证。

result

参考文献