-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmodel.py
155 lines (131 loc) · 5.66 KB
/
model.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
from keras.models import Sequential, Model
import keras.backend as K
from keras.utils.np_utils import to_categorical
from keras.layers import Input, merge
from keras.layers.core import Flatten, Dense, Dropout, Reshape
from keras.layers.core import TimeDistributedDense, RepeatVector, Permute, Lambda
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.layers.convolutional import ZeroPadding2D
from keras.optimizers import SGD
import sys
sys.path.append( "../deep-learning-models/")
from vgg19 import VGG19
def repeat_1(x):
"""Wrap keras backend repeat."""
return K.repeat_elements(x, 32, 2)
def sum_(x):
"""Wrap keras backend sum."""
return K.sum(x, axis=1)
def hieratt_network(w, query_in_size, query_embed_size, nb_classes):
"""Try to reproduce Hierarchical attention networks.
https://arxiv.org/abs/1606.02393
"""
input_image = Input(shape=(3,w,w)) # w by w color image
input_question = Input(shape=(query_in_size,)) # question vector
w = w/2
# Feature map 1
f_1 = Convolution2D(32, 3, 3, activation='relu',
border_mode='same')(input_image)
f_1 = Convolution2D(32, 3, 3, activation='relu',
border_mode='same')(input_image)
f_1 = MaxPooling2D((2,2), strides=(2,2))(f_1)
# f_1 = Dropout(0.25)(f_1)
num_features = f_1.shape[2] * 2
# Attention 1
# Create num_feature by num_channels "feature columns".
f_1 = Reshape((32, w*w))(f_1)
f_1 = Permute((2,1))(f_1)
q_1 = Dense(query_embed_size, activation='relu')(input_question) # Encode question
# Add question embedding to each feature column
q_1 = RepeatVector(w*w)(q_1)
q_f = merge([f_1, q_1], 'concat')
# Estimate and apply attention per feature
att_1 = TimeDistributedDense(1, activation="sigmoid")(q_f)
att_1 = Lambda(repeat_1, output_shape=(w*w, 32))(att_1)
att_1 = merge([f_1, att_1], 'mul')
# Reshape to the original feature map from previous layer
att_1 = Permute((2,1))(att_1)
f_1_att = Reshape((32, w, w))(att_1)
# Feature map 2
f_2 = Convolution2D(32, 3, 3, activation='relu',
border_mode='same')(f_1_att)
f_2 = Convolution2D(32, 3, 3, activation='relu',
border_mode='same')(f_2)
f_2 = MaxPooling2D((2,2), strides=(2,2))(f_2)
# f_2 = Dropout(0.25)(f_2)
w = w/2
# Attention 2
f_2 = Reshape((32, w*w))(f_2)
f_2 = Permute((2,1))(f_2)
q_2 = Dense(query_embed_size, activation='relu')(input_question)
q_2 = RepeatVector(w*w)(q_2)
q_f = merge([f_2, q_2], 'concat')
att_2 = TimeDistributedDense(1, activation="sigmoid")(q_f)
att_2 = Lambda(repeat_1, output_shape=(w*w, 32))(att_2)
att_2 = merge([f_2, att_2], 'mul')
att_2 = Permute((2,1))(att_2)
f_2_att = Reshape((32, w, w))(att_2)
# Feature map 3
f_3 = Convolution2D(32, 3, 3, activation='relu',
border_mode='same')(f_2_att)
f_3 = Convolution2D(32, 3, 3, activation='relu',
border_mode='same')(f_3)
f_3 = MaxPooling2D((2,2), strides=(2,2))(f_3)
# f_3 = Dropout(0.25)(f_3)
w = w/2
# Attention 3
f_3 = Reshape((32, w*w))(f_3)
f_3 = Permute((2,1))(f_3)
q_3 = Dense(query_embed_size, activation='relu')(input_question)
q_3 = RepeatVector(w*w)(q_3)
q_f = merge([f_3, q_3], 'concat')
att_3 = TimeDistributedDense(1, activation="sigmoid")(q_f)
att_3 = Lambda(repeat_1, output_shape=(w*w, 32))(att_3)
att_3 = merge([f_3, att_3], 'mul')
att_3 = Permute((2,1))(att_3)
f_3_att = Reshape((32, w, w))(att_3)
# Feature map 4
f_4 = Convolution2D(32, 3, 3, activation='relu',
border_mode='same')(f_3_att)
f_4 = Convolution2D(32, 3, 3, activation='relu',
border_mode='same')(f_4)
f_4 = MaxPooling2D((2,2), strides=(2,2))(f_4)
# f_4 = Dropout(0.25)(f_4)
w = w/2
# Attention 4
f_4 = Reshape((32, w*w))(f_4)
f_4 = Permute((2,1))(f_4)
q_4 = Dense(query_embed_size, activation="relu")(input_question)
q_4 = RepeatVector(w*w)(q_4)
q_f = merge([f_4, q_4], 'concat')
att_4 = TimeDistributedDense(1, activation="softmax")(q_f)
att_4 = Lambda(repeat_1, output_shape=(w*w, 32))(att_4)
att_4 = merge([f_4, att_4], 'mul')
f_att = Lambda(sum_, output_shape=(32,))(att_4)
probs = Dense(nb_classes, activation="softmax")(f_att)
model = Model(input=[input_image, input_question], output=probs)
return model
def VGG19_hieratt(query_in_size, query_embed_size, nb_classes):
"""Stack hierarchical attention on pre-trained VGG19.
Requires https://github.com/fchollet/deep-learning-models"""
base_model = VGG19(weights='imagenet')
input_image = base_model.input
input_question = Input(shape=(query_in_size,)) # question vector
# Model up to 3rd block
f_1 = Model(input=img_in, output=base_model.get_layer('block3_pool').output)
f_1 = f_1(img_in)
f_1 = Reshape((256, 28*28))(f_1)
f_1 = Permute((2,1))(f_1)
q_1 = Dense(query_embed_size, activation='relu')(input_question) # Encode question
# Add question embedding to each feature column
q_1 = RepeatVector(28*28)(q_1)
q_f = merge([f_1, q_1], 'concat')
# Estimate and apply attention per feature
att_1 = TimeDistributedDense(1, activation="sigmoid")(q_f)
att_1 = Lambda(repeat_1, output_shape=(28*28, 256))(att_1)
att_1 = merge([f_1, att_1], 'mul')
# Reshape to the original feature map from previous layer
att_1 = Permute((2,1))(att_1)
f_1_att = Reshape((256, 28, 28))(att_1)
model = Model(input=[img_in, input_question], output=f_1_att)
print model.summary()