-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathducknet.py
181 lines (146 loc) · 6.7 KB
/
ducknet.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
"""
Paper: Using DUCK-Net for Polyp Image Segmentation
Url: https://arxiv.org/abs/2311.02239
Create by: zh320
Date: 2024/11/08
"""
import torch
import torch.nn as nn
import torch.nn.functional as F
from .modules import conv1x1, ConvBNAct, Activation
from .model_registry import register_model
@register_model()
class DuckNet(nn.Module):
def __init__(self, num_class=1, n_channel=3, base_channel=17, act_type='relu'):
super().__init__()
self.down_stage1 = DownsampleBlock(n_channel, base_channel*2, act_type, fuse_channels=base_channel)
self.down_stage2 = DownsampleBlock(base_channel*2, base_channel*4, act_type)
self.down_stage3 = DownsampleBlock(base_channel*4, base_channel*8, act_type)
self.down_stage4 = DownsampleBlock(base_channel*8, base_channel*16, act_type)
self.down_stage5 = DownsampleBlock(base_channel*16, base_channel*32, act_type)
self.mid_stage = nn.Sequential(
ResidualBlock(base_channel*32, base_channel*32, act_type),
ResidualBlock(base_channel*32, base_channel*32, act_type),
ResidualBlock(base_channel*32, base_channel*16, act_type),
ResidualBlock(base_channel*16, base_channel*16, act_type),
)
self.up_stage5 = UpsampleBlock(base_channel*16, base_channel*8, act_type)
self.up_stage4 = UpsampleBlock(base_channel*8, base_channel*4, act_type)
self.up_stage3 = UpsampleBlock(base_channel*4, base_channel*2, act_type)
self.up_stage2 = UpsampleBlock(base_channel*2, base_channel, act_type)
self.up_stage1 = UpsampleBlock(base_channel, base_channel, act_type)
self.seg_head = conv1x1(base_channel, num_class)
def forward(self, x):
x1, x1_skip, x = self.down_stage1(x)
x2, x2_skip, x = self.down_stage2(x1+x, x)
x3, x3_skip, x = self.down_stage3(x2+x, x)
x4, x4_skip, x = self.down_stage4(x3+x, x)
x5, x5_skip, x = self.down_stage5(x4+x, x)
x = self.mid_stage(x5+x)
x = self.up_stage5(x, x5_skip)
x = self.up_stage4(x, x4_skip)
x = self.up_stage3(x, x3_skip)
x = self.up_stage2(x, x2_skip)
x = self.up_stage1(x, x1_skip)
x = self.seg_head(x)
return x
class DownsampleBlock(nn.Module):
def __init__(self, in_channels, out_channels, act_type, fuse_channels=None):
super().__init__()
fuse_channels = in_channels if fuse_channels is None else fuse_channels
self.duck = DUCK(in_channels, fuse_channels, act_type)
self.conv1 = ConvBNAct(fuse_channels, out_channels, 3, 2, act_type=act_type)
self.conv2 = ConvBNAct(in_channels, out_channels, 2, 2, act_type=act_type)
def forward(self, x1, x2=None):
if x2 is None:
x2 = self.conv2(x1)
else:
x2 = self.conv2(x2)
skip = self.duck(x1)
x1 = self.conv1(skip)
return x1, skip, x2
class UpsampleBlock(nn.Module):
def __init__(self, in_channels, out_channels, act_type):
super().__init__()
self.duck = DUCK(in_channels, out_channels, act_type)
def forward(self, x, residual):
size = residual.size()[2:]
x = F.interpolate(x, size, mode='nearest')
x += residual
x = self.duck(x)
return x
class ResidualBlock(nn.Module):
def __init__(self, in_channels, out_channels, act_type):
super().__init__()
self.upper_branch = conv1x1(in_channels, out_channels)
self.lower_branch = nn.Sequential(
ConvBNAct(in_channels, out_channels, 3, act_type=act_type),
ConvBNAct(out_channels, out_channels, 3, act_type=act_type),
)
self.bn = nn.Sequential(
nn.BatchNorm2d(out_channels),
Activation(act_type)
)
def forward(self, x):
x_up = self.upper_branch(x)
x_low = self.lower_branch(x)
x = x_up + x_low
x = self.bn(x)
return x
class DUCK(nn.Module):
def __init__(self, in_channels, out_channels, act_type, filter_size=6+1):
'''
Here I change the filter size of separated block to be odd number.
'''
super().__init__()
self.in_bn = nn.Sequential(
nn.BatchNorm2d(in_channels),
Activation(act_type)
)
self.branch1 = WidescopeBlock(in_channels, out_channels, act_type)
self.branch2 = MidscopeBlock(in_channels, out_channels, act_type)
self.branch3 = ResidualBlock(in_channels, out_channels, act_type)
self.branch4 = nn.Sequential(
ResidualBlock(in_channels, out_channels, act_type),
ResidualBlock(out_channels, out_channels, act_type),
)
self.branch5 = nn.Sequential(
ResidualBlock(in_channels, out_channels, act_type),
ResidualBlock(out_channels, out_channels, act_type),
ResidualBlock(out_channels, out_channels, act_type),
)
self.branch6 = SeparatedBlock(in_channels, out_channels, filter_size, act_type)
self.out_bn = nn.Sequential(
nn.BatchNorm2d(out_channels),
Activation(act_type)
)
def forward(self, x):
x = self.in_bn(x)
x1 = self.branch1(x)
x2 = self.branch2(x)
x3 = self.branch3(x)
x4 = self.branch4(x)
x5 = self.branch5(x)
x6 = self.branch6(x)
x = x1 + x2 + x3 + x4 + x5 + x6
x = self.out_bn(x)
return x
class MidscopeBlock(nn.Sequential):
def __init__(self, in_channels, out_channels, act_type):
super().__init__(
ConvBNAct(in_channels, out_channels, 3, act_type=act_type),
ConvBNAct(out_channels, out_channels, 3, dilation=2, act_type=act_type)
)
class WidescopeBlock(nn.Sequential):
def __init__(self, in_channels, out_channels, act_type):
super().__init__(
ConvBNAct(in_channels, out_channels, 3, act_type=act_type),
ConvBNAct(out_channels, out_channels, 3, dilation=2, act_type=act_type),
ConvBNAct(out_channels, out_channels, 3, dilation=3, act_type=act_type),
)
class SeparatedBlock(nn.Sequential):
def __init__(self, in_channels, out_channels, filter_size, act_type):
super().__init__(
ConvBNAct(in_channels, out_channels, (1, filter_size), act_type=act_type),
ConvBNAct(out_channels, out_channels, (filter_size, 1), act_type=act_type),
)