-
Notifications
You must be signed in to change notification settings - Fork 2
/
my_networks.py
264 lines (217 loc) · 9.73 KB
/
my_networks.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn import init
import functools
import torchvision.models
import torchvision.models.resnet as resnet
import torch.optim as optim
from torch.autograd import Variable
import torchvision.models as models
class scr_net(torch.nn.Module):
def __init__(self, layers_to_drop=2, num_downsamp_to_drop=2):
super(scr_net, self).__init__()
model = models.resnet18(pretrained=True)
layers = list(model.children())[:-layers_to_drop]
for i in range(num_downsamp_to_drop):
block = layers[-i-1][0]
for l in block.children():
if type(l) is nn.Sequential:
for u in l:
if type(u) is nn.Conv2d and u.stride == (2,2):
u.stride = 1
elif type(l) is nn.Conv2d and l.stride == (2,2):
l.stride = 1
self.feat_extractor = nn.Sequential(*layers)
# self.regression = nn.Sequential(nn.Conv2d(512, 512, 1, 1, 0),
# nn.BatchNorm2d(512),
# nn.LeakyReLU(),
# nn.Conv2d(512, 3, 1, 1, 0)
# )
self.regression = nn.Sequential(nn.Conv2d(512, 512, 1, 1, 0),
nn.BatchNorm2d(512),
nn.LeakyReLU(),
nn.Conv2d(512, 256, 1, 1, 0),
nn.BatchNorm2d(256),
nn.LeakyReLU(),
nn.Conv2d(256, 3, 1, 1, 0)
)
def forward(self, x):
feat = self.feat_extractor(x)
sc = self.regression(feat)
return feat, sc
class scr_net_uncertain(torch.nn.Module):
def __init__(self, layers_to_drop=2, num_downsamp_to_drop=2):
super(scr_net_uncertain, self).__init__()
model = models.resnet18(pretrained=True)
layers = list(model.children())[:-layers_to_drop]
for i in range(num_downsamp_to_drop):
block = layers[-i-1][0]
for l in block.children():
if type(l) is nn.Sequential:
for u in l:
if type(u) is nn.Conv2d and u.stride == (2,2):
u.stride = 1
elif type(l) is nn.Conv2d and l.stride == (2,2):
l.stride = 1
self.feat_extractor = nn.Sequential(*layers)
self.regression = nn.Sequential(nn.Conv2d(512, 512, 1, 1, 0),
nn.BatchNorm2d(512),
nn.LeakyReLU(),
nn.Conv2d(512, 256, 1, 1, 0),
nn.BatchNorm2d(256),
nn.LeakyReLU(),
nn.Conv2d(256, 3, 1, 1, 0)
)
self.var = nn.Sequential(nn.Conv2d(512, 512, 1, 1, 0),
nn.BatchNorm2d(512),
nn.LeakyReLU(),
nn.Conv2d(512, 256, 1, 1, 0),
nn.BatchNorm2d(256),
nn.LeakyReLU(),
nn.Conv2d(256, 1, 1, 1, 0)
)
def forward(self, x):
feat = self.feat_extractor(x)
sc = self.regression(feat)
var = self.var(feat)
return feat, sc, var
def CreateDiscriminator(input_channel=512, lr=1e-4, restore_from=None):
learning_rate_D = lr#1e-4
discriminator = FCDiscriminator(input_channel)
optimizer = optim.Adam(discriminator.parameters(), lr=learning_rate_D, betas=(0.9, 0.99))
optimizer.zero_grad()
if restore_from is not None:
print('loading from pretrained')
discriminator.load_state_dict(torch.load(restore_from))
return discriminator, optimizer
class FCDiscriminator(nn.Module):
def __init__(self, input_channel, ndf = 64):
super(FCDiscriminator, self).__init__()
self.conv1 = nn.Conv2d(input_channel, ndf, kernel_size=4, stride=2, padding=1)
self.conv2 = nn.Conv2d(ndf, ndf*2, kernel_size=4, stride=2, padding=1)
self.conv3 = nn.Conv2d(ndf*2, ndf*4, kernel_size=4, stride=2, padding=1)
self.conv4 = nn.Conv2d(ndf*4, ndf*8, kernel_size=4, stride=2, padding=1)
self.classifier = nn.Conv2d(ndf*8, 1, kernel_size=4, stride=2, padding=1)
self.leaky_relu = nn.LeakyReLU(negative_slope=0.2, inplace=True)
self.bce_loss = nn.BCEWithLogitsLoss()
def forward(self, x, lbl):
x = self.conv1(x)
x = self.leaky_relu(x)
x = self.conv2(x)
x = self.leaky_relu(x)
x = self.conv3(x)
x = self.leaky_relu(x)
x = self.conv4(x)
x = self.leaky_relu(x)
x = self.classifier(x)
self.loss = self.bce_loss(x, Variable(torch.FloatTensor(x.data.size()).fill_(lbl)).cuda())
return x
def adjust_learning_rate(self, args, optimizer, i):
if args.model == 'DeepLab':
lr = args.learning_rate_D * ((1 - float(i) / args.num_steps) ** (args.power))
optimizer.param_groups[0]['lr'] = lr
if len(optimizer.param_groups) > 1:
optimizer.param_groups[1]['lr'] = lr * 10
else:
optimizer.param_groups[0]['lr'] = args.learning_rate_D * (0.1**(int(i/50000)))
if len(optimizer.param_groups) > 1:
optimizer.param_groups[1]['lr'] = args.learning_rate_D * (0.1**(int(i/50000))) * 2
class FeatureExtractor(torch.nn.Module):
def __init__(self,
basemodel='resnet18',
pretrained=True,
requires_grad=False,
layers_to_drop=1,
num_downsamp_to_drop=0,
):
super(FeatureExtractor, self).__init__()
model = getattr(torchvision.models, basemodel)(pretrained=pretrained)
layers = list(model.children())[:-layers_to_drop]
# unstride downsampling blocks
for i in range(num_downsamp_to_drop):
block = layers[-i-1][0]
for l in block.children():
if type(l) is nn.Sequential:
for u in l:
if type(u) is nn.Conv2d and u.stride == (2,2):
u.stride = 1
elif type(l) is nn.Conv2d and l.stride == (2,2):
l.stride = 1
self.layers = nn.ModuleList(layers)#.eval()
self.requires_grad_(requires_grad)
def forward(self, x):
for layer in self.layers:
x = layer(x)
x = x.squeeze(-1).squeeze(-1)
return x
class MultiHeadModel(nn.Module):
def __init__(self, feature_extractor, head_1):
super(MultiHeadModel, self).__init__()
self.f = feature_extractor
# [*self.heads] = heads
self.head_1 = head_1
# self.heads = nn.ModuleList(heads)
def forward(self,x):
x = self.f(x)
return self.head_1(x)
# return [h(x) for h in self.heads]
class TaskHead(nn.Module):
def __init__(self, in_planes, out_planes, planes=None,
layers=0, residual=False, global_avarage_pooling=True,
Block=lambda in_p, p: resnet.Bottleneck(in_p,p//4) ):
super(TaskHead, self).__init__()
planes = planes if planes is not None else in_planes
self.residual = residual
self.global_avarage_pooling = global_avarage_pooling
self.out_planes = out_planes
self.input_transform = nn.Conv2d(in_channels = in_planes,
out_channels = planes,
kernel_size = 1)
if planes is not None:
blocks = [Block(planes, planes) for block in range(layers)]
if self.residual:
self.blocks = nn.ModuleList(blocks)
def progressive_sum(x):
for l in self.blocks:
x = x + l(x)
return x
self.layers = progressive_sum
else:
self.layers = nn.Sequential(*blocks)
else:
self.layers = lambda x: x
outlist = []
if self.global_avarage_pooling:
outlist.append(nn.AdaptiveAvgPool2d(output_size = (1, 1)))
outlist.append( nn.Conv2d(in_channels = planes,
out_channels = self.out_planes,
kernel_size = 1
))
self.output_transform = nn.Sequential(*outlist)
def forward(self, x):
x = self.input_transform(x)
x = self.layers(x)
x = self.output_transform(x)
# x = nn.functional.upsample(x, scale_factor=8, mode='bilinear')
return x
class CoordRegressor2(TaskHead):
def __init__(self, in_planes, planes=None,
layers=0, residual=False,
Block=lambda in_p, p: resnet.Bottleneck(in_p,p//4) ):
super(CoordRegressor2, self).__init__(
in_planes=in_planes, out_planes=3, Block=Block,
planes=planes, layers=layers, residual=residual,
global_avarage_pooling=False,
)
def define_SCR():
extractor = FeatureExtractor(
basemodel='resnet18',
pretrained=True,
requires_grad=True,
layers_to_drop=2,
num_downsamp_to_drop=2)
regressor = CoordRegressor2(extractor(
torch.zeros(1,3,16,16)).shape[1])
net = MultiHeadModel(extractor, regressor)
return net