-
Notifications
You must be signed in to change notification settings - Fork 101
/
models.py
225 lines (187 loc) · 8.26 KB
/
models.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.models
import collections
import math
class Unpool(nn.Module):
# Unpool: 2*2 unpooling with zero padding
def __init__(self, num_channels, stride=2):
super(Unpool, self).__init__()
self.num_channels = num_channels
self.stride = stride
# create kernel [1, 0; 0, 0]
self.weights = torch.autograd.Variable(torch.zeros(num_channels, 1, stride, stride).cuda()) # currently not compatible with running on CPU
self.weights[:,:,0,0] = 1
def forward(self, x):
return F.conv_transpose2d(x, self.weights, stride=self.stride, groups=self.num_channels)
def weights_init(m):
# Initialize filters with Gaussian random weights
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
if m.bias is not None:
m.bias.data.zero_()
elif isinstance(m, nn.ConvTranspose2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.in_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
if m.bias is not None:
m.bias.data.zero_()
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
class Decoder(nn.Module):
# Decoder is the base class for all decoders
names = ['deconv2', 'deconv3', 'upconv', 'upproj']
def __init__(self):
super(Decoder, self).__init__()
self.layer1 = None
self.layer2 = None
self.layer3 = None
self.layer4 = None
def forward(self, x):
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
return x
class DeConv(Decoder):
def __init__(self, in_channels, kernel_size):
assert kernel_size>=2, "kernel_size out of range: {}".format(kernel_size)
super(DeConv, self).__init__()
def convt(in_channels):
stride = 2
padding = (kernel_size - 1) // 2
output_padding = kernel_size % 2
assert -2 - 2*padding + kernel_size + output_padding == 0, "deconv parameters incorrect"
module_name = "deconv{}".format(kernel_size)
return nn.Sequential(collections.OrderedDict([
(module_name, nn.ConvTranspose2d(in_channels,in_channels//2,kernel_size,
stride,padding,output_padding,bias=False)),
('batchnorm', nn.BatchNorm2d(in_channels//2)),
('relu', nn.ReLU(inplace=True)),
]))
self.layer1 = convt(in_channels)
self.layer2 = convt(in_channels // 2)
self.layer3 = convt(in_channels // (2 ** 2))
self.layer4 = convt(in_channels // (2 ** 3))
class UpConv(Decoder):
# UpConv decoder consists of 4 upconv modules with decreasing number of channels and increasing feature map size
def upconv_module(self, in_channels):
# UpConv module: unpool -> 5*5 conv -> batchnorm -> ReLU
upconv = nn.Sequential(collections.OrderedDict([
('unpool', Unpool(in_channels)),
('conv', nn.Conv2d(in_channels,in_channels//2,kernel_size=5,stride=1,padding=2,bias=False)),
('batchnorm', nn.BatchNorm2d(in_channels//2)),
('relu', nn.ReLU()),
]))
return upconv
def __init__(self, in_channels):
super(UpConv, self).__init__()
self.layer1 = self.upconv_module(in_channels)
self.layer2 = self.upconv_module(in_channels//2)
self.layer3 = self.upconv_module(in_channels//4)
self.layer4 = self.upconv_module(in_channels//8)
class UpProj(Decoder):
# UpProj decoder consists of 4 upproj modules with decreasing number of channels and increasing feature map size
class UpProjModule(nn.Module):
# UpProj module has two branches, with a Unpool at the start and a ReLu at the end
# upper branch: 5*5 conv -> batchnorm -> ReLU -> 3*3 conv -> batchnorm
# bottom branch: 5*5 conv -> batchnorm
def __init__(self, in_channels):
super(UpProj.UpProjModule, self).__init__()
out_channels = in_channels//2
self.unpool = Unpool(in_channels)
self.upper_branch = nn.Sequential(collections.OrderedDict([
('conv1', nn.Conv2d(in_channels,out_channels,kernel_size=5,stride=1,padding=2,bias=False)),
('batchnorm1', nn.BatchNorm2d(out_channels)),
('relu', nn.ReLU()),
('conv2', nn.Conv2d(out_channels,out_channels,kernel_size=3,stride=1,padding=1,bias=False)),
('batchnorm2', nn.BatchNorm2d(out_channels)),
]))
self.bottom_branch = nn.Sequential(collections.OrderedDict([
('conv', nn.Conv2d(in_channels,out_channels,kernel_size=5,stride=1,padding=2,bias=False)),
('batchnorm', nn.BatchNorm2d(out_channels)),
]))
self.relu = nn.ReLU()
def forward(self, x):
x = self.unpool(x)
x1 = self.upper_branch(x)
x2 = self.bottom_branch(x)
x = x1 + x2
x = self.relu(x)
return x
def __init__(self, in_channels):
super(UpProj, self).__init__()
self.layer1 = self.UpProjModule(in_channels)
self.layer2 = self.UpProjModule(in_channels//2)
self.layer3 = self.UpProjModule(in_channels//4)
self.layer4 = self.UpProjModule(in_channels//8)
def choose_decoder(decoder, in_channels):
# iheight, iwidth = 10, 8
if decoder[:6] == 'deconv':
assert len(decoder)==7
kernel_size = int(decoder[6])
return DeConv(in_channels, kernel_size)
elif decoder == "upproj":
return UpProj(in_channels)
elif decoder == "upconv":
return UpConv(in_channels)
else:
assert False, "invalid option for decoder: {}".format(decoder)
class ResNet(nn.Module):
def __init__(self, layers, decoder, output_size, in_channels=3, pretrained=True):
if layers not in [18, 34, 50, 101, 152]:
raise RuntimeError('Only 18, 34, 50, 101, and 152 layer model are defined for ResNet. Got {}'.format(layers))
super(ResNet, self).__init__()
pretrained_model = torchvision.models.__dict__['resnet{}'.format(layers)](pretrained=pretrained)
if in_channels == 3:
self.conv1 = pretrained_model._modules['conv1']
self.bn1 = pretrained_model._modules['bn1']
else:
self.conv1 = nn.Conv2d(in_channels, 64, kernel_size=7, stride=2, padding=3, bias=False)
self.bn1 = nn.BatchNorm2d(64)
weights_init(self.conv1)
weights_init(self.bn1)
self.output_size = output_size
self.relu = pretrained_model._modules['relu']
self.maxpool = pretrained_model._modules['maxpool']
self.layer1 = pretrained_model._modules['layer1']
self.layer2 = pretrained_model._modules['layer2']
self.layer3 = pretrained_model._modules['layer3']
self.layer4 = pretrained_model._modules['layer4']
# clear memory
del pretrained_model
# define number of intermediate channels
if layers <= 34:
num_channels = 512
elif layers >= 50:
num_channels = 2048
self.conv2 = nn.Conv2d(num_channels,num_channels//2,kernel_size=1,bias=False)
self.bn2 = nn.BatchNorm2d(num_channels//2)
self.decoder = choose_decoder(decoder, num_channels//2)
# setting bias=true doesn't improve accuracy
self.conv3 = nn.Conv2d(num_channels//32,1,kernel_size=3,stride=1,padding=1,bias=False)
self.bilinear = nn.Upsample(size=self.output_size, mode='bilinear', align_corners=True)
# weight init
self.conv2.apply(weights_init)
self.bn2.apply(weights_init)
self.decoder.apply(weights_init)
self.conv3.apply(weights_init)
def forward(self, x):
# resnet
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.conv2(x)
x = self.bn2(x)
# decoder
x = self.decoder(x)
x = self.conv3(x)
x = self.bilinear(x)
return x