-
Notifications
You must be signed in to change notification settings - Fork 9
/
loupe_keras.py
428 lines (327 loc) · 17.9 KB
/
loupe_keras.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
""" This code is modified from the following paper.
Learnable mOdUle for Pooling fEatures (LOUPE)
Contains a collection of models (NetVLAD, NetRVLAD, NetFV and Soft-DBoW)
which enables pooling of a list of features into a single compact
representation.
Reference:
Learnable pooling method with Context Gating for video classification
Antoine Miech, Ivan Laptev, Josef Sivic
"""
import math
import tensorflow as tf
import tensorflow.contrib.slim as slim
import numpy as np
from keras import initializers, layers
import keras.backend as K
import sys
# Keras version
class ContextGating(layers.Layer):
"""Creates a NetVLAD class.
"""
def __init__(self, **kwargs):
super(ContextGating, self).__init__(**kwargs)
def build(self, input_shape):
# Create a trainable weight variable for this layer.
self.gating_weights = self.add_weight(name='kernel_W1',
shape=(input_shape[-1], input_shape[-1]),
initializer=tf.random_normal_initializer(stddev=1 / math.sqrt(input_shape[-1])),
trainable=True)
self.gating_biases = self.add_weight(name='kernel_B1',
shape=(input_shape[-1],),
initializer=tf.random_normal_initializer(stddev=1 / math.sqrt(input_shape[-1])),
trainable=True)
super(ContextGating, self).build(input_shape) # Be sure to call this at the end
def call(self, inputs):
"""
In Keras, there are two way to do matrix multiplication (dot product)
1) K.dot : AxB -> when A has batchsize and B doesn't, use K.dot
2) tf.matmul: AxB -> when A and B both have batchsize, use tf.matmul
Error example: Use tf.matmul when A has batchsize (3 dim) and B doesn't (2 dim)
ValueError: Shape must be rank 2 but is rank 3 for 'net_vlad_1/MatMul' (op: 'MatMul') with input shapes: [?,21,64], [64,3]
tf.matmul might still work when the dim of A is (?,64), but this is too confusing.
Just follow the above rules.
"""
gates = K.dot(inputs, self.gating_weights)
gates += self.gating_biases
gates = tf.sigmoid(gates)
activation = tf.multiply(inputs,gates)
return activation
def compute_output_shape(self, input_shape):
return tuple(input_shape)
class NetVLAD(layers.Layer):
"""Creates a NetVLAD class.
"""
def __init__(self, feature_size, max_samples, cluster_size, output_dim, **kwargs):
self.feature_size = feature_size
self.max_samples = max_samples
self.output_dim = output_dim
self.cluster_size = cluster_size
super(NetVLAD, self).__init__(**kwargs)
def build(self, input_shape):
# Create a trainable weight variable for this layer.
self.cluster_weights = self.add_weight(name='kernel_W1',
shape=(self.feature_size, self.cluster_size),
initializer=tf.random_normal_initializer(stddev=1 / math.sqrt(self.feature_size)),
trainable=True)
self.cluster_biases = self.add_weight(name='kernel_B1',
shape=(self.cluster_size,),
initializer=tf.random_normal_initializer(stddev=1 / math.sqrt(self.feature_size)),
trainable=True)
self.cluster_weights2 = self.add_weight(name='kernel_W2',
shape=(1,self.feature_size, self.cluster_size),
initializer=tf.random_normal_initializer(stddev=1 / math.sqrt(self.feature_size)),
trainable=True)
self.hidden1_weights = self.add_weight(name='kernel_H1',
shape=(self.cluster_size*self.feature_size, self.output_dim),
initializer=tf.random_normal_initializer(stddev=1 / math.sqrt(self.cluster_size)),
trainable=True)
super(NetVLAD, self).build(input_shape) # Be sure to call this at the end
def call(self, reshaped_input):
"""Forward pass of a NetVLAD block.
Args:
reshaped_input: If your input is in that form:
'batch_size' x 'max_samples' x 'feature_size'
It should be reshaped in the following form:
'batch_size*max_samples' x 'feature_size'
by performing:
reshaped_input = tf.reshape(input, [-1, features_size])
Returns:
vlad: the pooled vector of size: 'batch_size' x 'output_dim'
"""
"""
In Keras, there are two way to do matrix multiplication (dot product)
1) K.dot : AxB -> when A has batchsize and B doesn't, use K.dot
2) tf.matmul: AxB -> when A and B both have batchsize, use tf.matmul
Error example: Use tf.matmul when A has batchsize (3 dim) and B doesn't (2 dim)
ValueError: Shape must be rank 2 but is rank 3 for 'net_vlad_1/MatMul' (op: 'MatMul') with input shapes: [?,21,64], [64,3]
tf.matmul might still work when the dim of A is (?,64), but this is too confusing.
Just follow the above rules.
"""
activation = K.dot(reshaped_input, self.cluster_weights)
activation += self.cluster_biases
activation = tf.nn.softmax(activation)
activation = tf.reshape(activation,
[-1, self.max_samples, self.cluster_size])
a_sum = tf.reduce_sum(activation,-2,keep_dims=True)
a = tf.multiply(a_sum,self.cluster_weights2)
activation = tf.transpose(activation,perm=[0,2,1])
reshaped_input = tf.reshape(reshaped_input,[-1,
self.max_samples, self.feature_size])
vlad = tf.matmul(activation,reshaped_input)
vlad = tf.transpose(vlad,perm=[0,2,1])
vlad = tf.subtract(vlad,a)
vlad = tf.nn.l2_normalize(vlad,1)
vlad = tf.reshape(vlad,[-1, self.cluster_size*self.feature_size])
vlad = tf.nn.l2_normalize(vlad,1)
vlad = K.dot(vlad, self.hidden1_weights)
return vlad
def compute_output_shape(self, input_shape):
return tuple([None, self.output_dim])
class NetRVLAD(layers.Layer):
"""Creates a NetRVLAD class (Residual-less NetVLAD).
"""
def __init__(self, feature_size, max_samples, cluster_size, output_dim, **kwargs):
self.feature_size = feature_size
self.max_samples = max_samples
self.output_dim = output_dim
self.cluster_size = cluster_size
super(NetRVLAD, self).__init__(**kwargs)
def build(self, input_shape):
# Create a trainable weight variable for this layer.
self.cluster_weights = self.add_weight(name='kernel_W1',
shape=(self.feature_size, self.cluster_size),
initializer=tf.random_normal_initializer(stddev=1 / math.sqrt(self.feature_size)),
trainable=True)
self.cluster_biases = self.add_weight(name='kernel_B1',
shape=(self.cluster_size,),
initializer=tf.random_normal_initializer(stddev=1 / math.sqrt(self.feature_size)),
trainable=True)
self.hidden1_weights = self.add_weight(name='kernel_H1',
shape=(self.cluster_size*self.feature_size, self.output_dim),
initializer=tf.random_normal_initializer(stddev=1 / math.sqrt(self.cluster_size)),
trainable=True)
super(NetRVLAD, self).build(input_shape) # Be sure to call this at the end
def call(self, reshaped_input):
"""Forward pass of a NetRVLAD block.
Args:
reshaped_input: If your input is in that form:
'batch_size' x 'max_samples' x 'feature_size'
It should be reshaped in the following form:
'batch_size*max_samples' x 'feature_size'
by performing:
reshaped_input = tf.reshape(input, [-1, features_size])
Returns:
vlad: the pooled vector of size: 'batch_size' x 'output_dim'
"""
"""
In Keras, there are two way to do matrix multiplication (dot product)
1) K.dot : AxB -> when A has batchsize and B doesn't, use K.dot
2) tf.matmul: AxB -> when A and B both have batchsize, use tf.matmul
Error example: Use tf.matmul when A has batchsize (3 dim) and B doesn't (2 dim)
ValueError: Shape must be rank 2 but is rank 3 for 'net_vlad_1/MatMul' (op: 'MatMul') with input shapes: [?,21,64], [64,3]
tf.matmul might still work when the dim of A is (?,64), but this is too confusing.
Just follow the above rules.
"""
activation = K.dot(reshaped_input, self.cluster_weights)
activation += self.cluster_biases
activation = tf.nn.softmax(activation)
activation = tf.reshape(activation,
[-1, self.max_samples, self.cluster_size])
activation = tf.transpose(activation,perm=[0,2,1])
reshaped_input = tf.reshape(reshaped_input,[-1,
self.max_samples, self.feature_size])
vlad = tf.matmul(activation,reshaped_input)
vlad = tf.transpose(vlad,perm=[0,2,1])
vlad = tf.nn.l2_normalize(vlad,1)
vlad = tf.reshape(vlad,[-1, self.cluster_size*self.feature_size])
vlad = tf.nn.l2_normalize(vlad,1)
vlad = K.dot(vlad, self.hidden1_weights)
return vlad
def compute_output_shape(self, input_shape):
return tuple([None, self.output_dim])
class SoftDBoW(layers.Layer):
"""Creates a Soft Deep Bag-of-Features class.
"""
def __init__(self, feature_size, max_samples, cluster_size, output_dim, **kwargs):
self.feature_size = feature_size
self.max_samples = max_samples
self.output_dim = output_dim
self.cluster_size = cluster_size
super(SoftDBoW, self).__init__(**kwargs)
def build(self, input_shape):
# Create a trainable weight variable for this layer.
self.cluster_weights = self.add_weight(name='kernel_W1',
shape=(self.feature_size, self.cluster_size),
initializer=tf.random_normal_initializer(stddev=1 / math.sqrt(self.feature_size)),
trainable=True)
self.cluster_biases = self.add_weight(name='kernel_B1',
shape=(self.cluster_size,),
initializer=tf.random_normal_initializer(stddev=1 / math.sqrt(self.feature_size)),
trainable=True)
self.hidden1_weights = self.add_weight(name='kernel_H1',
shape=(self.cluster_size, self.output_dim),
initializer=tf.random_normal_initializer(stddev=1 / math.sqrt(self.cluster_size)),
trainable=True)
super(SoftDBoW, self).build(input_shape) # Be sure to call this at the end
def call(self, reshaped_input):
"""Forward pass of a Soft-DBoW block.
Args:
reshaped_input: If your input is in that form:
'batch_size' x 'max_samples' x 'feature_size'
It should be reshaped in the following form:
'batch_size*max_samples' x 'feature_size'
by performing:
reshaped_input = tf.reshape(input, [-1, features_size])
Returns:
vlad: the pooled vector of size: 'batch_size' x 'output_dim'
"""
"""
In Keras, there are two way to do matrix multiplication (dot product)
1) K.dot : AxB -> when A has batchsize and B doesn't, use K.dot
2) tf.matmul: AxB -> when A and B both have batchsize, use tf.matmul
Error example: Use tf.matmul when A has batchsize (3 dim) and B doesn't (2 dim)
ValueError: Shape must be rank 2 but is rank 3 for 'net_vlad_1/MatMul' (op: 'MatMul') with input shapes: [?,21,64], [64,3]
tf.matmul might still work when the dim of A is (?,64), but this is too confusing.
Just follow the above rules.
"""
activation = K.dot(reshaped_input, self.cluster_weights)
activation += self.cluster_biases
activation = tf.nn.softmax(activation)
activation = tf.reshape(activation,
[-1, self.max_samples, self.cluster_size])
bow = tf.reduce_sum(activation,1)
bow = tf.nn.l2_normalize(bow,1)
bow = K.dot(bow, self.hidden1_weights)
return bow
def compute_output_shape(self, input_shape):
return tuple([None, self.output_dim])
class NetFV(layers.Layer):
"""Creates a NetVLAD class.
"""
def __init__(self, feature_size, max_samples, cluster_size, output_dim, **kwargs):
self.feature_size = feature_size
self.max_samples = max_samples
self.output_dim = output_dim
self.cluster_size = cluster_size
super(NetFV, self).__init__(**kwargs)
def build(self, input_shape):
# Create a trainable weight variable for this layer.
self.cluster_weights = self.add_weight(name='kernel_W1',
shape=(self.feature_size, self.cluster_size),
initializer=tf.random_normal_initializer(stddev=1 / math.sqrt(self.feature_size)),
trainable=True)
self.covar_weights = self.add_weight(name='kernel_C1',
shape=(self.feature_size, self.cluster_size),
initializer=tf.random_normal_initializer(stddev=1 / math.sqrt(self.feature_size)),
trainable=True)
self.cluster_biases = self.add_weight(name='kernel_B1',
shape=(self.cluster_size,),
initializer=tf.random_normal_initializer(stddev=1 / math.sqrt(self.feature_size)),
trainable=True)
self.cluster_weights2 = self.add_weight(name='kernel_W2',
shape=(1,self.feature_size, self.cluster_size),
initializer=tf.random_normal_initializer(stddev=1 / math.sqrt(self.feature_size)),
trainable=True)
self.hidden1_weights = self.add_weight(name='kernel_H1',
shape=(2*self.cluster_size*self.feature_size, self.output_dim),
initializer=tf.random_normal_initializer(stddev=1 / math.sqrt(self.cluster_size)),
trainable=True)
super(NetFV, self).build(input_shape) # Be sure to call this at the end
def call(self, reshaped_input):
"""Forward pass of a NetFV block.
Args:
reshaped_input: If your input is in that form:
'batch_size' x 'max_samples' x 'feature_size'
It should be reshaped in the following form:
'batch_size*max_samples' x 'feature_size'
by performing:
reshaped_input = tf.reshape(input, [-1, features_size])
Returns:
vlad: the pooled vector of size: 'batch_size' x 'output_dim'
"""
"""
In Keras, there are two way to do matrix multiplication (dot product)
1) K.dot : AxB -> when A has batchsize and B doesn't, use K.dot
2) tf.matmul: AxB -> when A and B both have batchsize, use tf.matmul
Error example: Use tf.matmul when A has batchsize (3 dim) and B doesn't (2 dim)
ValueError: Shape must be rank 2 but is rank 3 for 'net_vlad_1/MatMul' (op: 'MatMul') with input shapes: [?,21,64], [64,3]
tf.matmul might still work when the dim of A is (?,64), but this is too confusing.
Just follow the above rules.
"""
covar_weights = tf.square(self.covar_weights)
eps = tf.constant([1e-6])
covar_weights = tf.add(covar_weights,eps)
activation = K.dot(reshaped_input, self.cluster_weights)
activation += self.cluster_biases
activation = tf.nn.softmax(activation)
activation = tf.reshape(activation,
[-1, self.max_samples, self.cluster_size])
a_sum = tf.reduce_sum(activation,-2,keep_dims=True)
a = tf.multiply(a_sum,self.cluster_weights2)
activation = tf.transpose(activation,perm=[0,2,1])
reshaped_input = tf.reshape(reshaped_input,[-1,
self.max_samples, self.feature_size])
fv1 = tf.matmul(activation,reshaped_input)
fv1 = tf.transpose(fv1,perm=[0,2,1])
# computing second order FV
a2 = tf.multiply(a_sum,tf.square(self.cluster_weights2))
b2 = tf.multiply(fv1,self.cluster_weights2)
fv2 = tf.matmul(activation,tf.square(reshaped_input))
fv2 = tf.transpose(fv2,perm=[0,2,1])
fv2 = tf.add_n([a2,fv2,tf.scalar_mul(-2,b2)])
fv2 = tf.divide(fv2,tf.square(covar_weights))
fv2 = tf.subtract(fv2,a_sum)
fv2 = tf.reshape(fv2,[-1,self.cluster_size*self.feature_size])
fv2 = tf.nn.l2_normalize(fv2,1)
fv2 = tf.reshape(fv2,[-1,self.cluster_size*self.feature_size])
fv2 = tf.nn.l2_normalize(fv2,1)
fv1 = tf.subtract(fv1,a)
fv1 = tf.divide(fv1,covar_weights)
fv1 = tf.nn.l2_normalize(fv1,1)
fv1 = tf.reshape(fv1,[-1,self.cluster_size*self.feature_size])
fv1 = tf.nn.l2_normalize(fv1,1)
fv = tf.concat([fv1,fv2],1)
fv = K.dot(fv, self.hidden1_weights)
return fv
def compute_output_shape(self, input_shape):
return tuple([None, self.output_dim])