-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathDFF2d.py
44 lines (33 loc) · 1.36 KB
/
DFF2d.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
import torch
import torch.nn as nn
#论文:D-Net: Dynamic Large Kernel with Dynamic Feature Fusion for Volumetric Medical Image Segmentation
#论文地址:https://arxiv.org/abs/2403.10674
class DFF(nn.Module):
def __init__(self, dim):
super().__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.conv_atten = nn.Sequential(
nn.Conv2d(dim * 2, dim * 2, kernel_size=1, bias=False),
nn.Sigmoid()
)
self.conv_redu = nn.Conv2d(dim * 2, dim, kernel_size=1, bias=False)
self.conv1 = nn.Conv2d(dim, 1, kernel_size=1, stride=1, bias=True)
self.conv2 = nn.Conv2d(dim, 1, kernel_size=1, stride=1, bias=True)
self.nonlin = nn.Sigmoid()
def forward(self, x, skip):
output = torch.cat([x, skip], dim=1)
att = self.conv_atten(self.avg_pool(output))
output = output * att
output = self.conv_redu(output)
att = self.conv1(x) + self.conv2(skip)
att = self.nonlin(att)
output = output * att
return output
if __name__ == '__main__':
x = torch.randn(1, 48, 128, 128)
skip = torch.randn(1, 48, 128, 128)
block = DFF(48)
output = block(x, skip)
print("Input shape (x):", x.size())
print("Input shape (skip):", skip.size())
print("Output shape:", output.size())