-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathvgg_cbcnn.py
191 lines (152 loc) · 8 KB
/
vgg_cbcnn.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
import numpy as np
import tensorflow as tf
from keras import backend as K
from keras import Input, Model
from keras.layers import Conv2D, MaxPooling2D, merge, Lambda, Dense, Reshape, regularizers
def compact_bilinear(tensors_list):
def _generate_sketch_matrix(rand_h, rand_s, output_dim):
"""
Return a sparse matrix used for tensor sketch operation in compact bilinear
pooling
Args:
rand_h: an 1D numpy array containing indices in interval `[0, output_dim)`.
rand_s: an 1D numpy array of 1 and -1, having the same shape as `rand_h`.
output_dim: the output dimensions of compact bilinear pooling.
Returns:
a sparse matrix of shape [input_dim, output_dim] for tensor sketch.
"""
# Generate a sparse matrix for tensor count sketch
rand_h = rand_h.astype(np.int64)
rand_s = rand_s.astype(np.float32)
assert (rand_h.ndim == 1 and rand_s.ndim == 1 and len(rand_h) == len(rand_s))
assert (np.all(rand_h >= 0) and np.all(rand_h < output_dim))
input_dim = len(rand_h)
indices = np.concatenate((np.arange(input_dim)[..., np.newaxis],
rand_h[..., np.newaxis]), axis=1)
sparse_sketch_matrix = tf.sparse_reorder(
tf.SparseTensor(indices, rand_s, [input_dim, output_dim]))
return sparse_sketch_matrix
bottom1, bottom2 = tensors_list
output_dim = 8192
# Static shapes are needed to construction count sketch matrix
input_dim1 = bottom1.get_shape().as_list()[-1]
input_dim2 = bottom2.get_shape().as_list()[-1]
# print (bottom1.get_shape().as_list())
# print (bottom2.get_shape().as_list())
# Step 0: Generate vectors and sketch matrix for tensor count sketch
# This is only done once during graph construction, and fixed during each
# operation
seed_h_1 = 1
seed_s_1 = 3
seed_h_2 = 5
seed_s_2 = 7
# Generate sparse_sketch_matrix1 using rand_h_1 and rand_s_1
np.random.seed(seed_h_1)
rand_h_1 = np.random.randint(output_dim, size=input_dim1)
np.random.seed(seed_s_1)
rand_s_1 = 2 * np.random.randint(2, size=input_dim1) - 1
sparse_sketch_matrix1 = _generate_sketch_matrix(rand_h_1, rand_s_1, output_dim)
# Generate sparse_sketch_matrix2 using rand_h_2 and rand_s_2
np.random.seed(seed_h_2)
rand_h_2 = np.random.randint(output_dim, size=input_dim2)
np.random.seed(seed_s_2)
rand_s_2 = 2 * np.random.randint(2, size=input_dim2) - 1
sparse_sketch_matrix2 = _generate_sketch_matrix(rand_h_2, rand_s_2, output_dim)
# Step 1: Flatten the input tensors and count sketch
bottom1_flat = tf.reshape(bottom1, [-1, input_dim1])
bottom2_flat = tf.reshape(bottom2, [-1, input_dim2])
# Essentially:
# sketch1 = bottom1 * sparse_sketch_matrix
# sketch2 = bottom2 * sparse_sketch_matrix
# But tensorflow only supports left multiplying a sparse matrix, so:
# sketch1 = (sparse_sketch_matrix.T * bottom1.T).T
# sketch2 = (sparse_sketch_matrix.T * bottom2.T).T
sketch1 = tf.transpose(tf.sparse_tensor_dense_matmul(sparse_sketch_matrix1,
bottom1_flat, adjoint_a=True, adjoint_b=True))
sketch2 = tf.transpose(tf.sparse_tensor_dense_matmul(sparse_sketch_matrix2,
bottom2_flat, adjoint_a=True, adjoint_b=True))
# Step 2: FFT
fft1 = tf.fft(tf.complex(real=sketch1, imag=tf.zeros_like(sketch1)))
fft2 = tf.fft(tf.complex(real=sketch2, imag=tf.zeros_like(sketch2)))
# Step 3: Elementwise product
fft_product = tf.multiply(fft1, fft2)
# Step 4: Inverse FFT and reshape back
# Compute output shape dynamically: [batch_size, height, width, output_dim]
cbp_flat = tf.real(tf.ifft(fft_product))
output_shape = tf.add(tf.multiply(tf.shape(bottom1), [1, 1, 1, 0]),
[0, 0, 0, output_dim])
cbp = tf.reshape(cbp_flat, output_shape)
# print (cbp.get_shape().as_list())
return cbp
def vgg_16_cbcnn(input_shape, no_classes, bilinear_output_dim, sum_pool=True, weight_decay_constant=5e-4,
multi_label=False, weights_path=None):
weights_regularizer = regularizers.l2(weight_decay_constant)
# Input layer
img_input = Input(shape=input_shape, name='spectr_input')
# Block 1
x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1',
kernel_regularizer=weights_regularizer)(img_input)
x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2',
kernel_regularizer=weights_regularizer)(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x)
# Block 2
x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1',
kernel_regularizer=weights_regularizer)(x)
x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2',
kernel_regularizer=weights_regularizer)(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x)
# Block 3
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1',
kernel_regularizer=weights_regularizer)(x)
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2',
kernel_regularizer=weights_regularizer)(x)
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3',
kernel_regularizer=weights_regularizer)(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x)
# Block 4
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1',
kernel_regularizer=weights_regularizer)(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2',
kernel_regularizer=weights_regularizer)(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3',
kernel_regularizer=weights_regularizer)(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x)
# Block 5
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1',
kernel_regularizer=weights_regularizer)(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2',
kernel_regularizer=weights_regularizer)(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3',
kernel_regularizer=weights_regularizer)(x)
# Merge using compact bilinear method
# dummy_tensor_for_output_dim = K.placeholder(shape=(bilinear_output_dim,))
compact_bilinear_arg_list = [x, x]
output_shape_x = x.get_shape().as_list()[1:]
output_shape_cb = (output_shape_x[0], output_shape_x[1], bilinear_output_dim,)
x = merge(compact_bilinear_arg_list, mode=compact_bilinear, name='compact_bilinear', output_shape=output_shape_cb)
# If sum_pool=True do a global sum pooling
if sum_pool:
# Since using tf. Hence 3rd would represent channels
x = Lambda(lambda x: K.sum(x, axis=[1, 2]))(x)
# Sign sqrt and L2 normalize result
x = Lambda(lambda x: K.sign(x) * K.sqrt(K.abs(x)))(x)
x = Lambda(lambda x: K.l2_normalize(x, axis=-1))(x)
# final dense layer
if not multi_label:
final_activation = 'softmax'
else:
final_activation = 'sigmoid'
x = Dense(no_classes, activation=final_activation, name='softmax_layer', kernel_regularizer=weights_regularizer)(x)
# Put together input and output to form model
model = Model(inputs=[img_input], outputs=[x])
if weights_path:
model.load_weights(weights_path, by_name=True)
return model
if __name__=='__main__':
input_shape = (448, 448, 3,)
no_classes = 128
bilinear_output_dim = 8192
vgg_weights_path = 'vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5'
model = vgg_16_cbcnn(input_shape, no_classes, bilinear_output_dim=bilinear_output_dim, sum_pool=True,
weights_path=vgg_weights_path)
print (model.summary())