-
Notifications
You must be signed in to change notification settings - Fork 10
/
ladder_shufflenet.py
206 lines (167 loc) · 7.98 KB
/
ladder_shufflenet.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
import torch
import torch.nn as nn
def channel_shuffle(x, groups):
batchsize, num_channels, height, width = x.data.size()
channels_per_group = num_channels // groups
# reshape
x = x.view(batchsize, groups,
channels_per_group, height, width)
x = torch.transpose(x, 1, 2).contiguous()
# flatten
x = x.view(batchsize, -1, height, width)
return x
def depthwise_conv(i, o, kernel_size, stride=1, padding=0, bias=False, dilation=1):
return nn.Conv2d(i, o, kernel_size, stride, padding, bias=bias, groups=i, dilation=dilation)
class InvertedResidual(nn.Module):
def __init__(self, inp, oup, stride):
super(InvertedResidual, self).__init__()
if not (1 <= stride <= 3):
raise ValueError('illegal stride value')
self.stride = stride
branch_features = oup // 2
assert (self.stride != 1) or (inp == branch_features << 1)
if self.stride > 1:
self.branch1 = nn.Sequential(
depthwise_conv(inp, inp, kernel_size=3, stride=self.stride, padding=1),
nn.BatchNorm2d(inp),
nn.Conv2d(inp, branch_features, kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(branch_features),
nn.ReLU(inplace=True),
)
self.branch2 = nn.Sequential(
nn.Conv2d(inp if (self.stride > 1) else branch_features,
branch_features, kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(branch_features),
nn.ReLU(inplace=True),
depthwise_conv(branch_features, branch_features, kernel_size=5, stride=self.stride, padding=4, dilation=2),
nn.BatchNorm2d(branch_features),
nn.Conv2d(branch_features, branch_features, kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(branch_features),
nn.ReLU(inplace=True),
)
def forward(self, x):
if self.stride == 1:
x1, x2 = x.chunk(2, dim=1)
out = torch.cat((x1, self.branch2(x2)), dim=1)
else:
out = torch.cat((self.branch1(x), self.branch2(x)), dim=1)
out = channel_shuffle(out, 2)
return out
class InvertedResidualUpsample(nn.Module):
def __init__(self, inp, oup, stride):
super(InvertedResidualUpsample, self).__init__()
if not (1 <= stride <= 3):
raise ValueError('illegal stride value')
self.stride = stride
branch_features = oup // 2
assert (self.stride != 1) or (inp == branch_features << 1)
if self.stride > 1:
self.branch1 = nn.Sequential(
self.depthwise_conv_upsample(inp, inp, kernel_size=4, stride=self.stride, padding=1),
nn.BatchNorm2d(inp),
nn.Conv2d(inp, branch_features, kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(branch_features),
nn.ReLU(inplace=True),
)
self.branch2 = nn.Sequential(
nn.Conv2d(inp if (self.stride > 1) else branch_features,
branch_features, kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(branch_features),
nn.ReLU(inplace=True),
self.depthwise_conv_upsample(branch_features, branch_features, kernel_size=4, stride=self.stride, padding=1),
nn.BatchNorm2d(branch_features),
nn.Conv2d(branch_features, branch_features, kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(branch_features),
nn.ReLU(inplace=True),
)
else: # stride == 1
self.branch2 = nn.Sequential(
nn.Conv2d(inp if (self.stride > 1) else branch_features,
branch_features, kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(branch_features),
nn.ReLU(inplace=True),
# depthwise_conv(branch_features, branch_features, kernel_size=3, stride=self.stride, padding=1),
depthwise_conv(branch_features, branch_features, kernel_size=5, stride=self.stride, padding=4, dilation=2),
nn.BatchNorm2d(branch_features),
nn.Conv2d(branch_features, branch_features, kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(branch_features),
nn.ReLU(inplace=True),
)
@staticmethod
def depthwise_conv_upsample(i, o, kernel_size, stride, padding, bias=False):
return nn.ConvTranspose2d(i, o, kernel_size, stride, padding, bias=bias, groups=i)
def forward(self, x):
if self.stride == 1:
x1, x2 = x.chunk(2, dim=1)
out = torch.cat((x1, self.branch2(x2)), dim=1)
else:
out = torch.cat((self.branch1(x), self.branch2(x)), dim=1)
out = channel_shuffle(out, 2)
return out
class LadderModelAdd(nn.Module):
# Use resnet style add when merging layers
# Use 3 type pcm
def __init__(self, in_channels=1): # out: pcm paf mask
super(LadderModelAdd, self).__init__()
out_channels = 7 + 1 # 6 corner 1 neck 1 paf
self._stage_out_channels = [64, 64, 128, 256, 1024] # init, e1, e2, e3, e4
self._stage_in_channels_dec = [1024, 256, 128, 64, 64] # in: d4, d3, d2, d1, final
self._stage_out_channels_dec = [256, 128, 64, 64, out_channels] # out: d4, d3, d2, d1, final
self.initial = nn.Sequential(
nn.Conv2d(in_channels, self._stage_out_channels[0], 3, 1, 1, bias=False),
nn.BatchNorm2d(self._stage_out_channels[0]),
nn.ReLU(inplace=True),
)
# Encoder
input_channels = self._stage_out_channels[0]
stage_names = ['encoder{}'.format(i) for i in [1, 2, 3, 4]]
stages_repeats = [4, 4, 8, 4]
for name, repeats, output_channels in zip(
stage_names, stages_repeats, self._stage_out_channels[1:]):
seq = [InvertedResidual(input_channels, output_channels, 2)]
for i in range(repeats - 1):
seq.append(InvertedResidual(output_channels, output_channels, 1))
setattr(self, name, nn.Sequential(*seq))
input_channels = output_channels
# Decoder
stage_names = ['decoder{}'.format(i) for i in [4, 3, 2, 1]]
stages_repeats = [4, 8, 4, 4]
for name, repeats, input_channels, output_channels in zip(
stage_names, stages_repeats,self._stage_in_channels_dec[:4], self._stage_out_channels_dec[:4]):
seq = [InvertedResidualUpsample(input_channels, output_channels, 2)]
for i in range(repeats - 1):
seq.append(InvertedResidualUpsample(output_channels, output_channels, 1))
setattr(self, name, nn.Sequential(*seq))
# Final Block
input_channels = self._stage_in_channels_dec[-1]
output_channels = self._stage_out_channels_dec[-1]
self.final = nn.Sequential(
nn.Conv2d(input_channels, input_channels, 1, 1, 0, bias=False),
nn.BatchNorm2d(input_channels),
nn.ReLU(inplace=True),
nn.Conv2d(input_channels, output_channels, 1, 1, 0)
)
def forward(self, x):
init = self.initial(x)
e1 = self.encoder1(init)
e2 = self.encoder2(e1)
e3 = self.encoder3(e2)
e4 = self.encoder4(e3)
d4 = self.decoder4(e4)
d4_cat = torch.add(d4, e3)
d3 = self.decoder3(d4_cat)
d3_cat = torch.add(d3, e2)
d2 = self.decoder2(d3_cat)
d2_cat = torch.add(d2, e1)
d1 = self.decoder1(d2_cat)
final = self.final(d1)
# pcm, paf, spine-mask
res_dict = {"pcm": final[:, 0:7], "paf": final[:, 7:8]}
return res_dict
if __name__=="__main__":
import numpy as np
ladder = LadderModelAdd().cuda()
print(ladder)
input = np.zeros([2, 1, 256, 256], np.float32)
t_input = torch.from_numpy(input).cuda()
out = ladder(t_input)