forked from fangchangma/sparse-to-dense.pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blocks.py
158 lines (111 loc) · 4.59 KB
/
blocks.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
import torch.nn as nn
class ResidualConvUnit(nn.Module):
def __init__(self, features):
super(ResidualConvUnit, self).__init__()
self.conv1 = nn.Conv2d(
features, features, kernel_size=3, stride=1, padding=1, bias=True)
self.conv2 = nn.Conv2d(
features, features, kernel_size=3, stride=1, padding=1, bias=False)
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
out = self.relu(x)
out = self.conv1(out)
out = self.relu(out)
out = self.conv2(out)
return out + x
class MultiResolutionFusion(nn.Module):
def __init__(self, out_feats, *shapes):
super(MultiResolutionFusion, self).__init__()
_, max_size = max(shapes, key=lambda x: x[1])
for i, shape in enumerate(shapes):
feat, size = shape
if max_size % size != 0:
raise ValueError("max_size not divisble by shape {}".format(i))
scale_factor = max_size // size
if scale_factor != 1:
self.add_module("resolve{}".format(i), nn.Sequential(
nn.Conv2d(feat, out_feats, kernel_size=3,
stride=1, padding=1, bias=False),
nn.Upsample(scale_factor=scale_factor, mode='bilinear')#, align_corners=True)
))
else:
self.add_module(
"resolve{}".format(i),
nn.Conv2d(feat, out_feats, kernel_size=3,
stride=1, padding=1, bias=False)
)
def forward(self, *xs):
output = self.resolve0(xs[0])
for i, x in enumerate(xs[1:], 1):
output += self.__getattr__("resolve{}".format(i))(x)
return output
class ChainedResidualPool(nn.Module):
def __init__(self, feats):
super(ChainedResidualPool, self).__init__()
self.relu = nn.ReLU(inplace=True)
for i in range(1, 4):
self.add_module("block{}".format(i), nn.Sequential(
nn.MaxPool2d(kernel_size=5, stride=1, padding=2),
nn.Conv2d(feats, feats, kernel_size=3, stride=1, padding=1, bias=False)
))
def forward(self, x):
x = self.relu(x)
path = x
for i in range(1, 4):
path = self.__getattr__("block{}".format(i))(path)
x = x + path
return x
class ChainedResidualPoolImproved(nn.Module):
def __init__(self, feats):
super(ChainedResidualPoolImproved, self).__init__()
self.relu = nn.ReLU(inplace=True)
for i in range(1, 5):
self.add_module("block{}".format(i), nn.Sequential(
nn.Conv2d(feats, feats, kernel_size=3, stride=1, padding=1, bias=False),
nn.MaxPool2d(kernel_size=5, stride=1, padding=2)
))
def forward(self, x):
x = self.relu(x)
path = x
for i in range(1, 5):
path = self.__getattr__("block{}".format(i))(path)
x += path
return x
class BaseRefineNetBlock(nn.Module):
def __init__(self, features,
residual_conv_unit,
multi_resolution_fusion,
chained_residual_pool, *shapes):
super(BaseRefineNetBlock, self).__init__()
for i, shape in enumerate(shapes):
feats = shape[0]
self.add_module("rcu{}".format(i), nn.Sequential(
residual_conv_unit(feats),
residual_conv_unit(feats)
))
if len(shapes) != 1:
self.mrf = multi_resolution_fusion(features, *shapes)
else:
self.mrf = None
self.crp = chained_residual_pool(features)
self.output_conv = residual_conv_unit(features)
def forward(self, *xs):
rcu_xs = []
for i, x in enumerate(xs):
rcu_xs.append(self.__getattr__("rcu{}".format(i))(x))
if self.mrf is not None:
out = self.mrf(*rcu_xs)
else:
out = rcu_xs[0]
out = self.crp(out)
return self.output_conv(out)
class RefineNetBlock(BaseRefineNetBlock):
def __init__(self, features, *shapes):
super(RefineNetBlock, self).__init__(features, ResidualConvUnit,
MultiResolutionFusion,
ChainedResidualPool, *shapes)
class RefineNetBlockImprovedPooling(nn.Module):
def __init__(self, features, *shapes):
super(RefineNetBlockImprovedPooling, self).__init__(features, ResidualConvUnit,
MultiResolutionFusion,
ChainedResidualPoolImproved, *shapes)