-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmobile_light_fpn.py
207 lines (152 loc) · 6.58 KB
/
mobile_light_fpn.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
207
# FPN + SPP for semantic segmentation
import torch
import torch.nn as nn
import torch.nn.functional as F
from MobileNetV1 import MobileNet
from base_model import resnet18
from config import config
class PoolAndAlign(nn.Module):
def __init__(self, in_planes, out_planes, scale, norm_layer=nn.BatchNorm2d):
super(PoolAndAlign, self).__init__()
self.conv = nn.Conv2d(in_planes, out_planes, 1, 1, bias=False)
self.bn = norm_layer(out_planes)
self.scale = scale
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
_, _, h, w = x.shape
x = F.adaptive_avg_pool2d(x, self.scale)
x = self.conv(x)
x = self.bn(x)
x = self.relu(x)
x = F.interpolate(x, (h,w), mode='bilinear', align_corners=True)
return x
class SpatailPyramidPooling(nn.Module):
'''
Spatial pyramid pooling
'''
def __init__(self, in_planes=1024, spp_planes=512, py_planes=128,
out_planes=256, rates=[1,2,3,6], norm_layer=nn.BatchNorm2d):
super(SpatailPyramidPooling, self).__init__()
self.num_scales = len(rates)
self.conv1 = nn.Conv2d(in_planes, spp_planes, 1, 1, bias=False)
self.spp_layers = [PoolAndAlign(spp_planes, py_planes, rates[i], norm_layer) for i in range(len(rates))]
self.spp_layers = nn.ModuleList(self.spp_layers)
self.conv2 = nn.Conv2d(spp_planes+spp_planes, out_planes, 1, 1, bias=False)
self.bn = norm_layer(out_planes)
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
x = self.conv1(x)
spatial_pyramids = [x]
for spp_layer in self.spp_layers:
spatial_pyramids.append(spp_layer(x))
x = torch.cat(spatial_pyramids, 1)
x = self.conv2(x)
x = self.bn(x)
x = self.relu(x)
return x
class LateralConnect(nn.Module):
'''
Lateral Connection
'''
def __init__(self, in_planes, lateral_planes, out_planes, norm_layer=nn.BatchNorm2d):
super(LateralConnect, self).__init__()
self.conv1 = nn.Conv2d(lateral_planes, in_planes, 1, 1, bias=False)
self.bn1 = norm_layer(in_planes)
self.conv2 = nn.Conv2d(in_planes, out_planes, 3, 1, 1, bias=False)
self.bn2 = norm_layer(out_planes)
self.relu = nn.ReLU(True)
def forward(self, x, lateral):
lateral = self.relu( self.bn1(self.conv1(lateral)) )
_, _, h, w = lateral.shape
x = F.interpolate(x, (h,w), mode='bilinear', align_corners=True)
x = x + lateral
return self.relu( self.bn2(self.conv2(x)) )
class Mobile_Light_FPN(nn.Module):
def __init__(self, out_planes, is_training,
criterion, pretrained_model=None,
norm_layer=nn.BatchNorm2d):
super(Mobile_Light_FPN, self).__init__()
# encoder
self.backbone = MobileNet(norm_layer=norm_layer)
# spp default pyramids: [1, 2, 4, 8]
self.spp = SpatailPyramidPooling(1024, 512, 128, 256, [1,2,4,8])
# decoder - lateral connections
self.decoder_configs = [
# n_features, n_laterals, n_output
[256, 512, 128],
[128, 256, 64],
[64, 128, 64]
]
decoder = []
for i in range(len(self.decoder_configs)):
in_planes, lateral_planes, out_planes = self.decoder_configs[i]
decoder.append(LateralConnect(in_planes, lateral_planes, out_planes))
# classifier
self.conv = nn.Conv2d(self.decoder_configs[-1][-1], out_planes, 3, 1, 1)
self.business_layer = []
self.is_training = is_training
self.decoder = nn.ModuleList(decoder)
self.business_layer.append(self.backbone)
self.business_layer.append(self.spp)
self.business_layer.append(self.decoder)
self.business_layer.append(self.conv)
if is_training:
self.criterion = criterion
def forward(self, data, label=None):
_, _, h, w = data.shape
encoder_layers = self.backbone(data)
x = self.spp(encoder_layers["layer5"])
x = self.decoder[0](x, encoder_layers["layer4"])
x = self.decoder[1](x, encoder_layers["layer3"])
x = self.decoder[2](x, encoder_layers["layer2"])
pred_out = F.interpolate(self.conv(x), size=(h,w), align_corners=True, mode="bilinear")
if self.is_training:
loss = self.criterion(pred_out, label)
return loss
return F.log_softmax(pred_out, dim=1)
class Res18_Light_FPN(nn.Module):
def __init__(self, out_planes, is_training,
criterion, pretrained_model=None,
norm_layer=nn.BatchNorm2d):
super(Res18_Light_FPN, self).__init__()
# encoder
self.backbone = resnet18(pretrained_model, norm_layer=norm_layer,
bn_eps=config.bn_eps,
bn_momentum=config.bn_momentum,
deep_stem=False, stem_width=64)
# spp default pyramids: [1, 2, 4, 8]
self.spp = SpatailPyramidPooling(512, 512, 128, 256, [1,2,4,8])
# decoder - lateral connections
self.decoder_configs = [
# n_features, n_laterals, n_output
[256, 256, 128],
[128, 128, 64],
[64, 64, 64]
]
decoder = []
for i in range(len(self.decoder_configs)):
in_planes, lateral_planes, out_planes = self.decoder_configs[i]
decoder.append(LateralConnect(in_planes, lateral_planes, out_planes))
# classifier
self.conv = nn.Conv2d(self.decoder_configs[-1][-1], out_planes, 3, 1, 1)
self.business_layer = []
self.is_training = is_training
self.decoder = nn.ModuleList(decoder)
self.business_layer.append(self.backbone)
self.business_layer.append(self.spp)
self.business_layer.append(self.decoder)
self.business_layer.append(self.conv)
if is_training:
self.criterion = criterion
def forward(self, data, label=None):
_, _, h, w = data.shape
encoder_layers = self.backbone(data)
x = self.spp(encoder_layers[3])
x = self.decoder[0](x, encoder_layers[2])
x = self.decoder[1](x, encoder_layers[1])
x = self.decoder[2](x, encoder_layers[0])
pred_out = F.interpolate(self.conv(x), size=(h,w), align_corners=True, mode="bilinear")
if self.is_training:
loss = self.criterion(pred_out, label)
return loss
return F.log_softmax(pred_out, dim=1)