-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathmodels.py
373 lines (322 loc) · 15.7 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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
"""Defines the shifting attention referring relationship model.
"""
from config import parse_args
from keras import backend as K
from keras.applications.resnet50 import ResNet50
from keras.applications.vgg19 import VGG19
from keras.layers import Dense, Input, Activation
from keras.layers.convolutional import Conv2D
from keras.layers.core import Dropout
from keras.layers.core import Lambda
from keras.layers.core import Reshape
from keras.layers.embeddings import Embedding
from keras.layers.merge import Concatenate
from keras.layers.merge import Multiply
from keras.models import Model
import numpy as np
class ReferringRelationshipsModel():
"""Given a relationship, this model locatlizes them.
"""
def __init__(self, args):
"""Constructor for ReferringRelationshipModel.
Args:
args: the arguments specified by `config.py`.
"""
self.input_dim = args.input_dim
self.feat_map_dim = args.feat_map_dim
self.hidden_dim = args.hidden_dim
self.num_objects = args.num_objects
self.num_predicates = args.num_predicates
self.dropout = args.dropout
self.use_subject = args.use_subject
self.use_predicate = args.use_predicate
self.use_object = args.use_object
self.nb_conv_att_map = args.nb_conv_att_map
self.nb_conv_im_map = args.nb_conv_im_map
self.cnn = args.cnn
self.feat_map_layer = args.feat_map_layer
self.conv_im_kernel = args.conv_im_kernel
self.conv_predicate_kernel = args.conv_predicate_kernel
self.conv_predicate_channels = args.conv_predicate_channels
self.model = args.model
self.use_internal_loss = args.use_internal_loss
self.internal_loss_weight = args.internal_loss_weight
self.iterations = args.iterations
self.attention_conv_kernel = args.attention_conv_kernel
self.refinement_conv_kernel = args.refinement_conv_kernel
self.output_dim = args.output_dim
self.embedding_dim = args.embedding_dim
self.finetune_cnn = args.finetune_cnn
# Discovery. Create a general object attention embedding option.
if args.discovery:
self.num_objects += 1
def build_model(self):
"""Chooses which model based on the arguments.
"""
if self.model == "co-occurrence":
if self.use_predicate:
raise ValueError('co-occurrence model must be run with '
'--use-predicate False.')
return self.build_vrd()
elif self.model == "vrd":
if not self.use_predicate:
raise ValueError('vrd model must be run with '
'--use-predicate True.')
return self.build_vrd()
elif self.model == "ssas":
return self.build_ssas()
else:
raise ValueError('Model argument not recognized. Model options: '
'ssas, co-occurrence, vrd.')
def build_ssas(self):
"""Focusing on the subject and object iteratively.
Returns:
The Keras model.
"""
# Create Inputs.
input_im = Input(shape=(self.input_dim, self.input_dim, 3))
input_subj = Input(shape=(1,))
input_obj = Input(shape=(1,))
if self.use_predicate:
input_pred = Input(shape=(self.num_predicates,))
inputs=[input_im, input_subj, input_pred, input_obj]
else:
inputs=[input_im, input_subj, input_obj]
# Generate the embedding and image feature layers.
im_features = self.build_image_model(input_im)
subj_obj_embedding = self.build_embedding_layer(
self.num_objects, self.embedding_dim)
embedded_subject = subj_obj_embedding(input_subj)
embedded_subject = Dense(self.hidden_dim, activation="relu")(
embedded_subject)
embedded_subject = Dropout(self.dropout)(embedded_subject)
embedded_object = subj_obj_embedding(input_obj)
embedded_object = Dense(self.hidden_dim, activation="relu")(
embedded_object)
embedded_object = Dropout(self.dropout)(embedded_object)
# Initial attention over the subject and object.
subject_att = self.attend(im_features, embedded_subject, name='subject-att-0')
object_att = self.attend(im_features, embedded_object, name='object-att-0')
# Create the predicate conv layers.
if self.use_predicate:
predicate_masks = Reshape((1, 1, self.num_predicates))(input_pred)
predicate_modules = self.build_conv_modules(
basename='conv{}-predicate{}')
inverse_predicate_modules = self.build_conv_modules(
basename='conv{}-inv-predicate{}')
# Save the initial predictions when using the internal loss.
if self.use_internal_loss:
subject_outputs = [subject_att]
object_outputs = [object_att]
# Iterate!
for iteration in range(self.iterations):
predicate_att = self.shift_conv_attention(subject_att, predicate_modules, predicate_masks)
predicate_att = Lambda(lambda x: x, name='shift-{}'.format(iteration+1))(predicate_att)
new_image_features = Multiply()([im_features, predicate_att])
new_object_att = self.attend(new_image_features, embedded_object, name='object-att-{}'.format(iteration+1))
inv_predicate_att = self.shift_conv_attention(object_att, inverse_predicate_modules, predicate_masks)
inv_predicate_att = Lambda(lambda x: x, name='inv-shift-{}'.format(iteration+1))(inv_predicate_att)
new_image_features = Multiply()([im_features, inv_predicate_att])
new_subject_att = self.attend(new_image_features, embedded_subject, name='subject-att-{}'.format(iteration+1))
if self.use_internal_loss:
object_outputs.append(new_object_att)
subject_outputs.append(new_subject_att)
object_att = new_object_att
subject_att = new_subject_att
# Collect the subject and objects regions.
if self.use_internal_loss and self.iterations > 0:
internal_weights = np.array([self.internal_loss_weight**i for i in range(len(subject_outputs))])
internal_weights = internal_weights / internal_weights.sum()
# Multiply with the internal losses.
subject_outputs = Lambda(lambda x: [internal_weights[i]*x[i] for i in range(len(x))])(subject_outputs)
object_outputs = Lambda(lambda x: [internal_weights[i]*x[i] for i in range(len(x))])(object_outputs)
# Concatenate all the internal outputs.
subject_att = Concatenate(axis=3)(subject_outputs)
object_att = Concatenate(axis=3)(object_outputs)
# Sum across the internal values.
subject_att = Lambda(lambda x: K.mean(x, axis=3, keepdims=True))(subject_att)
object_att = Lambda(lambda x: K.mean(x, axis=3, keepdims=True))(object_att)
subject_att = Activation("tanh")(subject_att)
object_att = Activation("tanh")(object_att)
subject_regions = Reshape((self.output_dim * self.output_dim,), name="subject")(subject_att)
object_regions = Reshape((self.output_dim * self.output_dim,), name="object")(object_att)
# Create and output the model.
model = Model(inputs=inputs, outputs=[subject_regions, object_regions])
return model
def build_conv_modules(self, basename):
"""Creates the convolution modules used to shift attention.
Args:
basename: String representing the name of the conv.
Returns:
A list of length `self.num_predicates` convolution modules.
"""
predicate_modules = []
for k in range(self.num_predicates):
predicate_module_group = []
for i in range(self.nb_conv_att_map-1):
predicate_conv = Conv2D(
self.conv_predicate_channels, self.conv_predicate_kernel,
strides=(1, 1), padding='same', use_bias=False,
activation='relu',
name=basename.format(i, k))
predicate_module_group.append(predicate_conv)
# last conv with only one channel
predicate_conv = Conv2D(
1, self.conv_predicate_kernel,
strides=(1, 1), padding='same', use_bias=False,
activation='relu',
name=basename.format(self.nb_conv_att_map-1, k))
predicate_module_group.append(predicate_conv)
predicate_modules.append(predicate_module_group)
return predicate_modules
def shift_conv_attention(self, att, merged_modules, predicate_masks):
"""Takes an intial attention map and shifts it using the predicate convs.
Args:
att: An initial attention by the object or the subject.
merged_modules: A list containing `self.num_predicate` elements
where each element is a list of `self.nb_conv_att_map` convs.
predicate_masks: A helpful tensor indicating which predicate
is involved for relationship element in the batch.
Returns:
The shifted attention.
"""
conv_outputs = []
for group in merged_modules:
att_map = att
for conv_module in group:
att_map = conv_module(att_map)
conv_outputs.append(att_map)
merged_output = Concatenate(axis=3)(conv_outputs)
predicate_att = Multiply()([predicate_masks, merged_output])
predicate_att = Lambda(lambda x: K.sum(x, axis=3, keepdims=True))(predicate_att)
predicate_att = Activation("tanh")(predicate_att)
return predicate_att
def build_vrd(self):
"""Initializes the baseline model that uses predicates.
Returns:
The Keras model.
"""
# Setup the inputs.
input_im = Input(shape=(self.input_dim, self.input_dim, 3))
relationship_inputs = []
num_classes = []
input_sub = Input(shape=(1,))
relationship_inputs.append(input_sub)
num_classes.append(self.num_objects)
if self.use_predicate:
input_pred = Input(shape=(1,))
relationship_inputs.append(input_pred)
num_classes.append(self.num_predicates)
input_obj = Input(shape=(1,))
relationship_inputs.append(input_obj)
num_classes.append(self.num_objects)
# Grab the image features.
im_features = self.build_image_model(input_im)
# Embed the relationship.
rel_features = self.build_relationship_model(relationship_inputs, num_classes)
rel_features = Dropout(self.dropout)(rel_features)
subjects_features = Dense(self.hidden_dim, activation="relu")(rel_features)
objects_features = Dense(self.hidden_dim, activation="relu")(rel_features)
subjects_features = Dropout(self.dropout)(subjects_features)
objects_features = Dropout(self.dropout)(objects_features)
# Attend over the image regions.
subject_att = self.attend(im_features, subjects_features)
object_att = self.attend(im_features, objects_features)
subject_att = Activation("tanh")(subject_att)
object_att = Activation("tanh")(object_att)
# Output the predictions.
subject_regions = Reshape((self.output_dim * self.output_dim,), name="subject")(subject_att)
object_regions = Reshape((self.output_dim * self.output_dim,), name="object")(object_att)
model_inputs = [input_im] + relationship_inputs
model = Model(inputs=model_inputs, outputs=[subject_regions, object_regions])
return model
def build_image_model(self, input_im):
"""Grab the image features.
Finetunes the image feature layers if self.finetune_cnn = True.
Args:
input_im: The input image to the model.
Returns:
The image feature map.
"""
if self.cnn == "resnet":
base_model = ResNet50(
weights='imagenet', include_top=False,
input_shape=(self.input_dim, self.input_dim, 3))
elif self.cnn == "vgg":
base_model = VGG19(
weights='imagenet', include_top=False,
input_shape=(self.input_dim, self.input_dim, 3))
else:
raise ValueError('--cnn must be [resnet, vgg] but got {}'.format(
self.cnn))
if self.finetune_cnn:
for layer in base_model.layers:
layer.trainable = True
layer.training = True
else:
for layer in base_model.layers:
layer.trainable = False
layer.training = False
output = base_model.get_layer(self.feat_map_layer).output
image_branch = Model(inputs=base_model.input, outputs=output)
im_features = image_branch(input_im)
im_features = Dropout(self.dropout)(im_features)
for i in range(self.nb_conv_im_map):
im_features = Conv2D(self.hidden_dim, self.conv_im_kernel,
strides=(1, 1), padding='same',
activation='relu')(im_features)
im_features = Dropout(self.dropout)(im_features)
return im_features
def build_embedding_layer(self, num_categories, emb_dim):
"""Creates an embedding layer.
Args:
num_categories: The number of categories we want embeddings for.
emb_dim: The dimensions in the embedding.
Returns:
An embedding layer.
"""
return Embedding(num_categories, emb_dim, input_length=1)
def attend(self, feature_map, query, name=None):
"""Uses the embedded query to attend over the image features.
Args:
feature_map: The image features to attend over.
query: The embedding of the category used as the attention query.
name: The name of the layer.
Returns:
The attention weights over the feature map.
"""
query = Reshape((1, 1, self.hidden_dim,))(query)
attention_weights = Multiply()([feature_map, query])
attention_weights = Lambda(lambda x: K.sum(x, axis=3, keepdims=True))(attention_weights)
attention_weights = Activation("relu", name=name)(attention_weights)
return attention_weights
def build_relationship_model(self, relationship_inputs, num_classes):
"""Converts the input relationship into a feature space.
Args:
relationship_inputs: A list of inputs to the model. Can contains
input_sub, input_pred or input_obj depending on the args.
num_classes: A list containing how many categories each input can
take in the relatinoship_inputs list. Used to initialize the
embedding layer.
Returns:
The feature representation for the relationship.
"""
embeddings = []
for rel_input, num_categories in zip(relationship_inputs, num_classes):
embedding_layer = Embedding(num_categories,
self.embedding_dim,
input_length=1)
embeddings.append(embedding_layer(rel_input))
# Concatenate the inputs if there are more than 1.
if len(embeddings) > 1:
concatenated_inputs = Concatenate(axis=2)(embeddings)
else:
concatenated_inputs = embeddings[0]
concatenated_inputs = Dropout(self.dropout)(concatenated_inputs)
return concatenated_inputs
if __name__ == "__main__":
# This file can be executed to make sure the model compiles.
args = parse_args()
rel = ReferringRelationshipsModel(args)
model = rel.build_model()
model.summary()