forked from ss7krd/Usher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inception_v4.py
244 lines (176 loc) · 7.07 KB
/
inception_v4.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
from keras.layers import Input, merge, Dropout, Dense, Flatten, Activation
from keras.layers.convolutional import MaxPooling2D, Convolution2D, AveragePooling2D
from keras.layers.normalization import BatchNormalization
from keras.models import Model
from keras import backend as K
from keras.utils.data_utils import get_file
"""
Implementation of Inception Network v4 [Inception Network v4 Paper](http://arxiv.org/pdf/1602.07261v1.pdf) in Keras.
"""
def conv_block(x, nb_filter, nb_row, nb_col, border_mode='same', subsample=(1, 1), bias=False):
if K.image_dim_ordering() == "th":
channel_axis = 1
else:
channel_axis = -1
x = Convolution2D(nb_filter, nb_row, nb_col, subsample=subsample, border_mode=border_mode, bias=bias)(x)
x = BatchNormalization(axis=channel_axis)(x)
x = Activation('relu')(x)
return x
def inception_stem(input):
if K.image_dim_ordering() == "th":
channel_axis = 1
else:
channel_axis = -1
# Input Shape is 299 x 299 x 3 (th) or 3 x 299 x 299 (th)
x = conv_block(input, 32, 3, 3, subsample=(2, 2), border_mode='valid')
x = conv_block(x, 32, 3, 3, border_mode='valid')
x = conv_block(x, 64, 3, 3)
x1 = MaxPooling2D((3, 3), strides=(2, 2), border_mode='valid')(x)
x2 = conv_block(x, 96, 3, 3, subsample=(2, 2), border_mode='valid')
x = merge([x1, x2], mode='concat', concat_axis=channel_axis)
x1 = conv_block(x, 64, 1, 1)
x1 = conv_block(x1, 96, 3, 3, border_mode='valid')
x2 = conv_block(x, 64, 1, 1)
x2 = conv_block(x2, 64, 1, 7)
x2 = conv_block(x2, 64, 7, 1)
x2 = conv_block(x2, 96, 3, 3, border_mode='valid')
x = merge([x1, x2], mode='concat', concat_axis=channel_axis)
x1 = conv_block(x, 192, 3, 3, subsample=(2, 2), border_mode='valid')
x2 = MaxPooling2D((3, 3), strides=(2, 2), border_mode='valid')(x)
x = merge([x1, x2], mode='concat', concat_axis=channel_axis)
return x
def inception_A(input):
if K.image_dim_ordering() == "th":
channel_axis = 1
else:
channel_axis = -1
a1 = conv_block(input, 96, 1, 1)
a2 = conv_block(input, 64, 1, 1)
a2 = conv_block(a2, 96, 3, 3)
a3 = conv_block(input, 64, 1, 1)
a3 = conv_block(a3, 96, 3, 3)
a3 = conv_block(a3, 96, 3, 3)
a4 = AveragePooling2D((3, 3), strides=(1, 1), border_mode='same')(input)
a4 = conv_block(a4, 96, 1, 1)
m = merge([a1, a2, a3, a4], mode='concat', concat_axis=channel_axis)
return m
def inception_B(input):
if K.image_dim_ordering() == "th":
channel_axis = 1
else:
channel_axis = -1
b1 = conv_block(input, 384, 1, 1)
b2 = conv_block(input, 192, 1, 1)
b2 = conv_block(b2, 224, 1, 7)
b2 = conv_block(b2, 256, 7, 1)
b3 = conv_block(input, 192, 1, 1)
b3 = conv_block(b3, 192, 7, 1)
b3 = conv_block(b3, 224, 1, 7)
b3 = conv_block(b3, 224, 7, 1)
b3 = conv_block(b3, 256, 1, 7)
b4 = AveragePooling2D((3, 3), strides=(1, 1), border_mode='same')(input)
b4 = conv_block(b4, 128, 1, 1)
m = merge([b1, b2, b3, b4], mode='concat', concat_axis=channel_axis)
return m
def inception_C(input):
if K.image_dim_ordering() == "th":
channel_axis = 1
else:
channel_axis = -1
c1 = conv_block(input, 256, 1, 1)
c2 = conv_block(input, 384, 1, 1)
c2_1 = conv_block(c2, 256, 1, 3)
c2_2 = conv_block(c2, 256, 3, 1)
c2 = merge([c2_1, c2_2], mode='concat', concat_axis=channel_axis)
c3 = conv_block(input, 384, 1, 1)
c3 = conv_block(c3, 448, 3, 1)
c3 = conv_block(c3, 512, 1, 3)
c3_1 = conv_block(c3, 256, 1, 3)
c3_2 = conv_block(c3, 256, 3, 1)
c3 = merge([c3_1, c3_2], mode='concat', concat_axis=channel_axis)
c4 = AveragePooling2D((3, 3), strides=(1, 1), border_mode='same')(input)
c4 = conv_block(c4, 256, 1, 1)
m = merge([c1, c2, c3, c4], mode='concat', concat_axis=channel_axis)
return m
def reduction_A(input):
if K.image_dim_ordering() == "th":
channel_axis = 1
else:
channel_axis = -1
r1 = conv_block(input, 384, 3, 3, subsample=(2, 2), border_mode='valid')
r2 = conv_block(input, 192, 1, 1)
r2 = conv_block(r2, 224, 3, 3)
r2 = conv_block(r2, 256, 3, 3, subsample=(2, 2), border_mode='valid')
r3 = MaxPooling2D((3, 3), strides=(2, 2), border_mode='valid')(input)
m = merge([r1, r2, r3], mode='concat', concat_axis=channel_axis)
return m
def reduction_B(input):
if K.image_dim_ordering() == "th":
channel_axis = 1
else:
channel_axis = -1
r1 = conv_block(input, 192, 1, 1)
r1 = conv_block(r1, 192, 3, 3, subsample=(2, 2), border_mode='valid')
r2 = conv_block(input, 256, 1, 1)
r2 = conv_block(r2, 256, 1, 7)
r2 = conv_block(r2, 320, 7, 1)
r2 = conv_block(r2, 320, 3, 3, subsample=(2, 2), border_mode='valid')
r3 = MaxPooling2D((3, 3), strides=(2, 2), border_mode='valid')(input)
m = merge([r1, r2, r3], mode='concat', concat_axis=channel_axis)
return m
def create_inception_v4(nb_classes=1001, load_weights=True):
'''
Creates a inception v4 network
:param nb_classes: number of classes.txt
:return: Keras Model with 1 input and 1 output
'''
if K.image_dim_ordering() == 'th':
init = Input((3, 299, 299))
else:
init = Input((299, 299, 3))
# Input Shape is 299 x 299 x 3 (tf) or 3 x 299 x 299 (th)
x = inception_stem(init)
# 4 x Inception A
for i in range(4):
x = inception_A(x)
# Reduction A
x = reduction_A(x)
# 7 x Inception B
for i in range(7):
x = inception_B(x)
# Reduction B
x = reduction_B(x)
# 3 x Inception C
for i in range(3):
x = inception_C(x)
# Average Pooling
x = AveragePooling2D((8, 8))(x)
# Dropout
x = Dropout(0.8)(x)
x = Flatten()(x)
# Output
out = Dense(output_dim=nb_classes, activation='softmax')(x)
model = Model(init, out, name='Inception-v4')
if load_weights:
if K.backend() == "theano":
if K.image_dim_ordering() == "th":
weights = get_file('inception_v4_weights_th_dim_ordering_th_kernels.h5', TH_BACKEND_TH_DIM_ORDERING,
cache_subdir='models')
else:
weights = get_file('inception_v4_weights_tf_dim_ordering_th_kernels.h5', TH_BACKEND_TF_DIM_ORDERING,
cache_subdir='models')
else:
if K.image_dim_ordering() == "th":
weights = get_file('inception_v4_weights_th_dim_ordering_tf_kernels.h5', TF_BACKEND_TH_DIM_ORDERING,
cache_subdir='models')
else:
weights = get_file('inception_v4_weights_tf_dim_ordering_tf_kernels.h5', TH_BACKEND_TF_DIM_ORDERING,
cache_subdir='models')
model.load_weights(weights)
print("Model weights loaded.")
return model
if __name__ == "__main__":
# from keras.utils.visualize_util import plot
inception_v4 = create_inception_v4(load_weights=True)
# inception_v4.summary()
# plot(inception_v4, to_file="Inception-v4.png", show_shapes=True)