-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_mlm.py
210 lines (170 loc) · 10.4 KB
/
model_mlm.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
import tensorflow as tf
from tensorflow.python.keras.models import Model
from transformers import TFBertModel, create_optimizer
from transformers.models.bert.modeling_tf_bert import TFBertMLMHead
class NeXtVLAD(tf.keras.layers.Layer):
def __init__(self, feature_size, cluster_size, output_size=1024, expansion=2, groups=8, dropout=0.2):
super().__init__()
self.feature_size = feature_size
self.cluster_size = cluster_size
self.expansion = expansion
self.groups = groups
self.new_feature_size = expansion * feature_size // groups
self.expand_dense = tf.keras.layers.Dense(self.expansion * self.feature_size)
# for group attention
self.attention_dense = tf.keras.layers.Dense(self.groups, activation=tf.nn.sigmoid)
# self.activation_bn = tf.keras.layers.BatchNormalization()
# for cluster weights
self.cluster_dense1 = tf.keras.layers.Dense(self.groups * self.cluster_size, activation=None, use_bias=False)
# self.cluster_dense2 = tf.keras.layers.Dense(self.cluster_size, activation=None, use_bias=False)
self.dropout = tf.keras.layers.Dropout(rate=dropout, seed=1)
self.fc = tf.keras.layers.Dense(output_size, activation=None)
def build(self, input_shape):
self.cluster_weights2 = self.add_weight(name="cluster_weights2",
shape=(1, self.new_feature_size, self.cluster_size),
initializer=tf.keras.initializers.glorot_normal, trainable=True)
self.built = True
def call(self, inputs, **kwargs):
image_embeddings, mask = inputs
_, num_segments, _ = image_embeddings.shape
if mask is not None: # in case num of images is less than num_segments
images_mask = tf.sequence_mask(mask, maxlen=num_segments)
images_mask = tf.cast(tf.expand_dims(images_mask, -1), tf.float32)
image_embeddings = tf.multiply(image_embeddings, images_mask)
inputs = self.expand_dense(image_embeddings) # b,32,1536*2
attention = self.attention_dense(inputs) # b,32,8
attention = tf.reshape(attention, [-1, num_segments * self.groups, 1]) # b,32*8,1
reshaped_input = tf.reshape(inputs, [-1, self.expansion * self.feature_size]) # b*32,1536*2
activation = self.cluster_dense1(reshaped_input) # b*32,8*64
# activation = self.activation_bn(activation)
activation = tf.reshape(activation, [-1, num_segments * self.groups, self.cluster_size]) # b,32*8,64
activation = tf.nn.softmax(activation, axis=-1) # shape: batch_size * (max_frame*groups) * cluster_size
activation = tf.multiply(activation, attention) # shape: batch_size * (max_frame*groups) * cluster_size
a_sum = tf.reduce_sum(activation, -2, keepdims=True) # shape: batch_size * 1 * cluster_size
a = tf.multiply(a_sum, self.cluster_weights2) # shape: batch_size * new_feature_size * cluster_size
activation = tf.transpose(activation, perm=[0, 2, 1]) # shape: batch_size * cluster_size * (max_frame*groups)
reshaped_input = tf.reshape(inputs, [-1, num_segments * self.groups, self.new_feature_size])
vlad = tf.matmul(activation, reshaped_input) # shape: batch_size * cluster_size * new_feature_size
vlad = tf.transpose(vlad, perm=[0, 2, 1]) # shape: batch_size * new_feature_size * cluster_size
vlad = tf.subtract(vlad, a)
vlad = tf.nn.l2_normalize(vlad, 1)
vlad = tf.reshape(vlad, [-1, self.cluster_size * self.new_feature_size])
vlad = self.dropout(vlad)
vlad = self.fc(vlad)
return vlad
class SENet(tf.keras.layers.Layer):
def __init__(self, channels, ratio=8, **kwargs):
super(SENet, self).__init__(**kwargs)
self.fc = tf.keras.Sequential([
tf.keras.layers.Dense(channels // ratio, activation='relu', kernel_initializer='he_normal', use_bias=False),
tf.keras.layers.Dense(channels, activation='sigmoid', kernel_initializer='he_normal', use_bias=False)
])
def call(self, inputs, **kwargs):
se = self.fc(inputs)
outputs = tf.math.multiply(inputs, se)
return outputs
class ConcatDenseSE(tf.keras.layers.Layer):
"""Fusion using Concate + Dense + SENet"""
def __init__(self, hidden_size, se_ratio, **kwargs):
super().__init__(**kwargs)
self.fusion = tf.keras.layers.Dense(hidden_size, activation='relu')
self.fusion_dropout = tf.keras.layers.Dropout(0.2)
self.enhance = SENet(channels=hidden_size, ratio=se_ratio)
def call(self, inputs, **kwargs):
embeddings = tf.concat(inputs, axis=1)
embeddings = self.fusion_dropout(embeddings)
embedding = self.fusion(embeddings)
embedding = self.enhance(embedding)
return embedding
class MultiModal(Model):
def __init__(self, config, *args, **kwargs):
super().__init__(*args, **kwargs)
self.bert = TFBertModel.from_pretrained(config.bert_dir)
self.nextvlad = NeXtVLAD(config.frame_embedding_size, config.vlad_cluster_size,
output_size=config.vlad_hidden_size, dropout=config.dropout)
self.fusion = ConcatDenseSE(config.hidden_size, config.se_ratio)
self.num_labels = config.num_labels
self.classifier = tf.keras.layers.Dense(self.num_labels, activation='sigmoid')
self.bert_optimizer, self.bert_lr = create_optimizer(init_lr=config.bert_lr,
num_train_steps=config.bert_total_steps,
num_warmup_steps=config.bert_warmup_steps)
self.optimizer, self.lr = create_optimizer(init_lr=config.lr,
num_train_steps=config.total_steps,
num_warmup_steps=config.warmup_steps)
self.bert_variables, self.num_bert, self.normal_variables, self.all_variables = None, None, None, None
def call(self, inputs, **kwargs):
bert_embedding = self.bert([inputs['input_ids'], inputs['mask']])[1]
frame_num = tf.reshape(inputs['num_frames'], [-1])
vision_embedding = self.nextvlad([inputs['frames'], frame_num])
vision_embedding = vision_embedding * tf.cast(tf.expand_dims(frame_num, -1) > 0, tf.float32)
final_embedding = self.fusion([vision_embedding, bert_embedding])
predictions = self.classifier(final_embedding)
return predictions, final_embedding
def get_variables(self):
if not self.all_variables: # is None, not initialized
self.bert_variables = self.bert.trainable_variables
self.num_bert = len(self.bert_variables)
self.normal_variables = self.nextvlad.trainable_variables + self.fusion.trainable_variables + \
self.classifier.trainable_variables
self.all_variables = self.bert_variables + self.normal_variables
return self.all_variables
def optimize(self, gradients):
bert_gradients = gradients[:self.num_bert]
self.bert_optimizer.apply_gradients(zip(bert_gradients, self.bert_variables))
normal_gradients = gradients[self.num_bert:]
self.optimizer.apply_gradients(zip(normal_gradients, self.normal_variables))
def shape_list(tensor):
"""
Deal with dynamic shape in tensorflow cleanly.
Args:
tensor (:obj:`tf.Tensor`): The tensor we want the shape of.
Returns:
:obj:`List[int]`: The shape of the tensor as a list.
"""
dynamic = tf.shape(tensor)
if tensor.shape == tf.TensorShape(None):
return dynamic
static = tensor.shape.as_list()
return [dynamic[i] if s is None else s for i, s in enumerate(static)]
class BERTforMaskedLM(Model):
def __init__(self, config, *args, **kwargs):
super().__init__(*args, **kwargs)
self.bert = TFBertModel.from_pretrained(config.bert_dir)
bert_config = self.bert.config
self.mlm = TFBertMLMHead(bert_config, input_embeddings=self.bert.bert.embeddings, name="mlm___cls")
self.bert_optimizer, self.bert_lr = create_optimizer(init_lr=config.bert_lr,
num_train_steps=config.bert_total_steps,
num_warmup_steps=config.bert_warmup_steps)
self.optimizer, self.lr = create_optimizer(init_lr=config.lr,
num_train_steps=config.total_steps,
num_warmup_steps=config.warmup_steps)
self.bert_variables, self.num_bert, self.normal_variables, self.all_variables = None, None, None, None
def call(self, inputs, training, **kwargs):
bert_embedding = self.bert([inputs['input_ids'], inputs['mask']]) # hidden_state
sequence_output = bert_embedding[0]
prediction_scores = self.mlm(sequence_output=sequence_output, training=training)
loss = (
None if inputs["mask_labels"] is None else self.compute_loss(labels=inputs["mask_labels"], logits=prediction_scores)
)
return prediction_scores, loss
def compute_loss(self, labels, logits):
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(
from_logits=True, reduction=tf.keras.losses.Reduction.NONE
)
# make sure only labels that are not equal to -100 affect the loss
active_loss = tf.not_equal(tf.reshape(labels, (-1,)), -100)
reduced_logits = tf.boolean_mask(tf.reshape(logits, (-1, shape_list(logits)[2])), active_loss)
labels = tf.boolean_mask(tf.reshape(labels, (-1,)), active_loss)
return loss_fn(labels, reduced_logits)
def get_variables(self):
if not self.all_variables: # is None, not initialized
self.bert_variables = self.bert.trainable_variables
self.num_bert = len(self.bert_variables)
self.normal_variables = self.mlm.trainable_variables
self.all_variables = self.bert_variables + self.normal_variables
return self.all_variables
def optimize(self, gradients):
bert_gradients = gradients[:self.num_bert]
self.bert_optimizer.apply_gradients(zip(bert_gradients, self.bert_variables))
normal_gradients = gradients[self.num_bert:]
self.optimizer.apply_gradients(zip(normal_gradients, self.normal_variables))