forked from facebookresearch/clevr-iep
-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathfilmed_net.py
311 lines (275 loc) · 12.3 KB
/
filmed_net.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/usr/bin/env python3
import math
import ipdb as pdb
import pprint
from termcolor import colored
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import torchvision.models
from vr.models.layers import init_modules, GlobalAveragePool, Flatten
from vr.models.layers import build_classifier, build_stem
import vr.programs
class FiLM(nn.Module):
"""
A Feature-wise Linear Modulation Layer from
'FiLM: Visual Reasoning with a General Conditioning Layer'
"""
def forward(self, x, gammas, betas):
gammas = gammas.unsqueeze(2).unsqueeze(3).expand_as(x)
betas = betas.unsqueeze(2).unsqueeze(3).expand_as(x)
return (gammas * x) + betas
class FiLMedNet(nn.Module):
def __init__(self, vocab, feature_dim=(1024, 14, 14),
stem_num_layers=2,
stem_batchnorm=False,
stem_kernel_size=3,
stem_stride=1,
stem_padding=None,
num_modules=4,
module_num_layers=1,
module_dim=128,
module_residual=True,
module_batchnorm=False,
module_batchnorm_affine=False,
module_dropout=0,
module_input_proj=1,
module_kernel_size=3,
classifier_proj_dim=512,
classifier_downsample='maxpool2',
classifier_fc_layers=(1024,),
classifier_batchnorm=False,
classifier_dropout=0,
condition_method='bn-film',
condition_pattern=[],
use_gamma=True,
use_beta=True,
use_coords=1,
debug_every=float('inf'),
print_verbose_every=float('inf'),
verbose=True,
):
super(FiLMedNet, self).__init__()
num_answers = len(vocab['answer_idx_to_token'])
self.stem_times = []
self.module_times = []
self.classifier_times = []
self.timing = False
self.num_modules = num_modules
self.module_num_layers = module_num_layers
self.module_batchnorm = module_batchnorm
self.module_dim = module_dim
self.condition_method = condition_method
self.use_gamma = use_gamma
self.use_beta = use_beta
self.use_coords_freq = use_coords
self.debug_every = debug_every
self.print_verbose_every = print_verbose_every
# Initialize helper variables
self.stem_use_coords = (stem_stride == 1) and (self.use_coords_freq > 0)
self.condition_pattern = condition_pattern
if len(condition_pattern) == 0:
self.condition_pattern = []
for i in range(self.module_num_layers * self.num_modules):
self.condition_pattern.append(self.condition_method != 'concat')
else:
self.condition_pattern = [i > 0 for i in self.condition_pattern]
self.extra_channel_freq = self.use_coords_freq
self.block = FiLMedResBlock
self.num_cond_maps = 2 * self.module_dim if self.condition_method == 'concat' else 0
self.fwd_count = 0
self.num_extra_channels = 2 if self.use_coords_freq > 0 else 0
if self.debug_every <= -1:
self.print_verbose_every = 1
module_H = feature_dim[1] // (stem_stride ** stem_num_layers) # Rough calc: work for main cases
module_W = feature_dim[2] // (stem_stride ** stem_num_layers) # Rough calc: work for main cases
self.coords = coord_map((module_H, module_W))
self.default_weight = Variable(torch.ones(1, 1, self.module_dim)).type(torch.cuda.FloatTensor)
self.default_bias = Variable(torch.zeros(1, 1, self.module_dim)).type(torch.cuda.FloatTensor)
# Initialize stem
stem_feature_dim = feature_dim[0] + self.stem_use_coords * self.num_extra_channels
self.stem = build_stem(stem_feature_dim, module_dim,
num_layers=stem_num_layers, with_batchnorm=stem_batchnorm,
kernel_size=stem_kernel_size, stride=stem_stride, padding=stem_padding)
# Initialize FiLMed network body
self.function_modules = {}
self.vocab = vocab
for fn_num in range(self.num_modules):
with_cond = self.condition_pattern[self.module_num_layers * fn_num:
self.module_num_layers * (fn_num + 1)]
mod = self.block(module_dim, with_residual=module_residual, with_batchnorm=module_batchnorm,
with_cond=with_cond,
dropout=module_dropout,
num_extra_channels=self.num_extra_channels,
extra_channel_freq=self.extra_channel_freq,
with_input_proj=module_input_proj,
num_cond_maps=self.num_cond_maps,
kernel_size=module_kernel_size,
batchnorm_affine=module_batchnorm_affine,
num_layers=self.module_num_layers,
condition_method=condition_method,
debug_every=self.debug_every)
self.add_module(str(fn_num), mod)
self.function_modules[fn_num] = mod
# Initialize output classifier
self.classifier = build_classifier(module_dim + self.num_extra_channels, module_H, module_W,
num_answers, classifier_fc_layers, classifier_proj_dim,
classifier_downsample, with_batchnorm=classifier_batchnorm,
dropout=classifier_dropout)
init_modules(self.modules())
def forward(self, x, film, save_activations=False):
# Initialize forward pass and externally viewable activations
self.fwd_count += 1
if save_activations:
self.feats = None
self.module_outputs = []
self.cf_input = None
if self.debug_every <= -2:
pdb.set_trace()
# Prepare FiLM layers
gammas = None
betas = None
if self.condition_method == 'concat':
# Use parameters usually used to condition via FiLM instead to condition via concatenation
cond_params = film[:,:,:2*self.module_dim]
cond_maps = cond_params.unsqueeze(3).unsqueeze(4).expand(cond_params.size() + x.size()[-2:])
else:
gammas, betas = torch.split(film[:,:,:2*self.module_dim], self.module_dim, dim=-1)
if not self.use_gamma:
gammas = self.default_weight.expand_as(gammas)
if not self.use_beta:
betas = self.default_bias.expand_as(betas)
# Propagate up image features CNN
batch_coords = None
if self.use_coords_freq > 0:
batch_coords = self.coords.unsqueeze(0).expand(torch.Size((x.size(0), *self.coords.size())))
if self.stem_use_coords:
x = torch.cat([x, batch_coords], 1)
feats = self.stem(x)
if save_activations:
self.feats = feats
N, _, H, W = feats.size()
# Propagate up the network from low-to-high numbered blocks
module_inputs = Variable(torch.zeros(feats.size()).unsqueeze(1).expand(
N, self.num_modules, self.module_dim, H, W)).type(torch.cuda.FloatTensor)
module_inputs[:,0] = feats
for fn_num in range(self.num_modules):
if self.condition_method == 'concat':
layer_output = self.function_modules[fn_num](module_inputs[:,fn_num],
extra_channels=batch_coords, cond_maps=cond_maps[:,fn_num])
else:
layer_output = self.function_modules[fn_num](module_inputs[:,fn_num],
gammas[:,fn_num,:], betas[:,fn_num,:], batch_coords)
# Store for future computation
if save_activations:
self.module_outputs.append(layer_output)
if fn_num == (self.num_modules - 1):
final_module_output = layer_output
else:
module_inputs_updated = module_inputs.clone()
module_inputs_updated[:,fn_num+1] = module_inputs_updated[:,fn_num+1] + layer_output
module_inputs = module_inputs_updated
if self.debug_every <= -2:
pdb.set_trace()
# Run the final classifier over the resultant, post-modulated features.
if self.use_coords_freq > 0:
final_module_output = torch.cat([final_module_output, batch_coords], 1)
if save_activations:
self.cf_input = final_module_output
out = self.classifier(final_module_output)
if ((self.fwd_count % self.debug_every) == 0) or (self.debug_every <= -1):
pdb.set_trace()
return out
class FiLMedResBlock(nn.Module):
def __init__(self, in_dim, out_dim=None, with_residual=True, with_batchnorm=True,
with_cond=[False], dropout=0, num_extra_channels=0, extra_channel_freq=1,
with_input_proj=0, num_cond_maps=0, kernel_size=3, batchnorm_affine=False,
num_layers=1, condition_method='bn-film', debug_every=float('inf')):
if out_dim is None:
out_dim = in_dim
super(FiLMedResBlock, self).__init__()
self.with_residual = with_residual
self.with_batchnorm = with_batchnorm
self.with_cond = with_cond
self.dropout = dropout
self.extra_channel_freq = 0 if num_extra_channels == 0 else extra_channel_freq
self.with_input_proj = with_input_proj # Kernel size of input projection
self.num_cond_maps = num_cond_maps
self.kernel_size = kernel_size
self.batchnorm_affine = batchnorm_affine
self.num_layers = num_layers
self.condition_method = condition_method
self.debug_every = debug_every
if self.with_input_proj % 2 == 0:
raise(NotImplementedError)
if self.kernel_size % 2 == 0:
raise(NotImplementedError)
if self.num_layers >= 2:
raise(NotImplementedError)
if self.condition_method == 'block-input-film' and self.with_cond[0]:
self.film = FiLM()
if self.with_input_proj:
self.input_proj = nn.Conv2d(in_dim + (num_extra_channels if self.extra_channel_freq >= 1 else 0),
in_dim, kernel_size=self.with_input_proj, padding=self.with_input_proj // 2)
self.conv1 = nn.Conv2d(in_dim + self.num_cond_maps +
(num_extra_channels if self.extra_channel_freq >= 2 else 0),
out_dim, kernel_size=self.kernel_size,
padding=self.kernel_size // 2)
if self.condition_method == 'conv-film' and self.with_cond[0]:
self.film = FiLM()
if self.with_batchnorm:
self.bn1 = nn.BatchNorm2d(out_dim, affine=((not self.with_cond[0]) or self.batchnorm_affine))
if self.condition_method == 'bn-film' and self.with_cond[0]:
self.film = FiLM()
if dropout > 0:
self.drop = nn.Dropout2d(p=self.dropout)
if ((self.condition_method == 'relu-film' or self.condition_method == 'block-output-film')
and self.with_cond[0]):
self.film = FiLM()
init_modules(self.modules())
def forward(self, x, gammas=None, betas=None, extra_channels=None, cond_maps=None):
if self.debug_every <= -2:
pdb.set_trace()
if self.condition_method == 'block-input-film' and self.with_cond[0]:
x = self.film(x, gammas, betas)
# ResBlock input projection
if self.with_input_proj:
if extra_channels is not None and self.extra_channel_freq >= 1:
x = torch.cat([x, extra_channels], 1)
x = F.relu(self.input_proj(x))
out = x
# ResBlock body
if cond_maps is not None:
out = torch.cat([out, cond_maps], 1)
if extra_channels is not None and self.extra_channel_freq >= 2:
out = torch.cat([out, extra_channels], 1)
out = self.conv1(out)
if self.condition_method == 'conv-film' and self.with_cond[0]:
out = self.film(out, gammas, betas)
if self.with_batchnorm:
out = self.bn1(out)
if self.condition_method == 'bn-film' and self.with_cond[0]:
out = self.film(out, gammas, betas)
if self.dropout > 0:
out = self.drop(out)
out = F.relu(out)
if self.condition_method == 'relu-film' and self.with_cond[0]:
out = self.film(out, gammas, betas)
# ResBlock remainder
if self.with_residual:
out = x + out
if self.condition_method == 'block-output-film' and self.with_cond[0]:
out = self.film(out, gammas, betas)
return out
def coord_map(shape, start=-1, end=1):
"""
Gives, a 2d shape tuple, returns two mxn coordinate maps,
Ranging min-max in the x and y directions, respectively.
"""
m, n = shape
x_coord_row = torch.linspace(start, end, steps=n).type(torch.cuda.FloatTensor)
y_coord_row = torch.linspace(start, end, steps=m).type(torch.cuda.FloatTensor)
x_coords = x_coord_row.unsqueeze(0).expand(torch.Size((m, n))).unsqueeze(0)
y_coords = y_coord_row.unsqueeze(1).expand(torch.Size((m, n))).unsqueeze(0)
return Variable(torch.cat([x_coords, y_coords], 0))