-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathu2net.py
201 lines (168 loc) · 6.42 KB
/
u2net.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
import torch
import torch.nn as nn
from model.s2block import S2Block
def init_weights(*modules):
for module in modules:
for m in module.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_in')
if m.bias is not None:
nn.init.constant_(m.bias, 0.0)
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1.0)
nn.init.constant_(m.bias, 0.0)
elif isinstance(m, nn.Linear):
# variance_scaling_initializer(m.weight)
nn.init.kaiming_normal_(m.weight, mode='fan_in')
if m.bias is not None:
nn.init.constant_(m.bias, 0.0)
class ResBlock(nn.Module):
def __init__(self, in_channels, hidden_channels, out_channels):
super().__init__()
self.conv0 = nn.Conv2d(in_channels, hidden_channels, 3, 1, 1)
self.conv1 = nn.Conv2d(hidden_channels, out_channels, 3, 1, 1)
self.relu = nn.LeakyReLU()
def forward(self, x):
rs1 = self.relu(self.conv0(x))
rs1 = self.conv1(rs1)
rs = torch.add(x, rs1)
return rs
class Upsample(nn.Module):
def __init__(self, n_feat):
super().__init__()
self.upsamle = nn.Sequential(
nn.Conv2d(n_feat, n_feat*16, 3, 1, 1, bias=False),
nn.PixelShuffle(4)
)
def forward(self, x):
return self.upsamle(x)
class Up(nn.Module):
def __init__(self, in_channels, out_channels, bilinear=False):
super().__init__()
if bilinear:
self.up = nn.Sequential(
nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True),
nn.Conv2d(in_channels, in_channels, 3, 1, 1, groups=in_channels),
nn.Conv2d(in_channels, out_channels, 1, 1, 0),
nn.LeakyReLU()
)
else:
self.up0 = nn.Sequential(
nn.ConvTranspose2d(in_channels, in_channels, 2, 2, 0),
nn.LeakyReLU(),
nn.Conv2d(in_channels, out_channels, 1, 1, 0),
nn.Conv2d(out_channels, out_channels, 3, 1, 1, groups=out_channels),
nn.LeakyReLU()
)
self.up1 = nn.Sequential(
nn.ConvTranspose2d(in_channels, out_channels, 2, 2, 0),
nn.LeakyReLU()
)
self.conv = nn.Conv2d(out_channels, out_channels, 3, 1, 1)
self.relu = nn.LeakyReLU()
def forward(self, x1, x2):
x1 = self.up1(x1)
x = x1 + x2
return self.relu(self.conv(x))
class Down(nn.Module):
def __init__(self, in_channels, out_channels):
super().__init__()
self.maxpool_conv = nn.Sequential(
nn.MaxPool2d(2),
nn.Conv2d(in_channels, out_channels, 1, 1, 0),
nn.Conv2d(out_channels, out_channels, 3, 1, 1, groups=out_channels),
nn.LeakyReLU()
)
self.conv = nn.Sequential(
nn.Conv2d(in_channels, in_channels, 2, 2, 0),
nn.LeakyReLU(),
nn.Conv2d(in_channels, out_channels, 1, 1, 0),
nn.Conv2d(out_channels, out_channels, 3, 1, 1, groups=out_channels),
nn.LeakyReLU()
)
def forward(self, x):
return self.conv(x)
class U2Net(nn.Module):
def __init__(self, dim, dim_head=16, se_ratio_mlp=0.5, se_ratio_rb=0.5):
super().__init__()
ms_dim = 8
pan_dim = 1
self.relu = nn.LeakyReLU()
self.upsample = Upsample(ms_dim)
self.raise_ms_dim = nn.Sequential(
nn.Conv2d(ms_dim, dim, 3, 1, 1),
nn.LeakyReLU()
)
self.raise_pan_dim = nn.Sequential(
nn.Conv2d(pan_dim, dim, 3, 1, 1),
nn.LeakyReLU()
)
self.to_hrms = nn.Sequential(
nn.Conv2d(dim, dim, 3, 1, 1),
nn.LeakyReLU(),
nn.Conv2d(dim, ms_dim, 3, 1, 1)
)
dim0 = dim
dim1 = int(dim0 * 2)
dim2 = int(dim1 * 2)
dim3 = dim1
dim4 = dim0
# layer 0
self.s2block0 = S2Block(dim0, dim0//dim_head, dim_head, int(dim0*se_ratio_mlp))
self.down0 = Down(dim0, dim1)
self.resblock0 = ResBlock(dim0, int(se_ratio_rb*dim0), dim0)
# layer 1
self.s2block1 = S2Block(dim1, dim1//dim_head, dim_head, int(dim1*se_ratio_mlp))
self.down1 = Down(dim1, dim2)
self.resblock1 = ResBlock(dim1, int(se_ratio_rb*dim1), dim1)
# layer 2
self.s2block2 = S2Block(dim2, dim2//dim_head, dim_head, int(dim2*se_ratio_mlp))
self.up0 = Up(dim2, dim3)
self.resblock2 = ResBlock(dim2, int(se_ratio_rb*dim2), dim2)
# layer 3
self.s2block3 = S2Block(dim3, dim3//dim_head, dim_head, int(dim3*se_ratio_mlp))
self.up1 = Up(dim3, dim4)
self.resblock3 = ResBlock(dim3, int(se_ratio_rb*dim3), dim3)
# layer 4
self.s2block4 = S2Block(dim4, dim4//dim_head, dim_head, int(dim4*se_ratio_mlp))
def forward(self, x, y):
x = self.upsample(x)
skip_c0 = x
x = self.raise_ms_dim(x)
y = self.raise_pan_dim(y)
# layer 0
x = self.s2block0(x, y) # 32 64 64
skip_c10 = x # 32 64 64
x = self.down0(x) # 64 32 32
y = self.resblock0(y) # 32 64 64
skip_c11 = y # 32 64 64
y = self.down0(y) # 64 32 32
# layer 1
x = self.s2block1(x, y) # 64 32 32
skip_c20 = x
x = self.down1(x) # 128 16 16
y = self.resblock1(y) # 64 32 32
skip_c21 = y # 64 32 32
y = self.down1(y) # 128 16 16
# layer 2
x = self.s2block2(x, y) # 128 16 16
x = self.up0(x, skip_c20) # 64 32 32
y = self.resblock2(y) # 128 16 16
y = self.up0(y, skip_c21) # 64 32 32
# layer 3
x = self.s2block3(x, y) # 64 32 32
x = self.up1(x, skip_c10) # 32 64 64
y = self.resblock3(y) # 64 32 32
y = self.up1(y, skip_c11) # 32 64 64
# layer 4
x = self.s2block4(x, y) # 32 64 64
output = self.to_hrms(x) + skip_c0 # 8 64 64
return output
def summaries(model, grad=False):
if grad:
from torchsummary import summary
summary(model, input_size=[(8, 16, 16), (1, 64, 64)], batch_size=1)
else:
for name, param in model.named_parameters():
if param.requires_grad:
print(name)