2 Based on https://gist.github.com/joelouismarino/a2ede9ab3928f999575423b9887abd14 5 from scipy.misc
import imread, imresize
7 from keras.layers
import Input, Dense, Convolution2D, MaxPooling2D, AveragePooling2D, ZeroPadding2D, Dropout, Flatten, merge, Reshape, Activation
8 from keras.models
import Model
9 from keras.regularizers
import l2
10 from keras.optimizers
import SGD
11 from googlenet_custom_layers
import PoolHelper,LRN
14 def googlenet(input_shape=[500,500,3], classes=13, weights_path=None):
17 input = Input(shape=input_shape)
19 conv1_7x7_s2 = Convolution2D(64,7,7,subsample=(2,2),border_mode=
'same',activation=
'relu',name=
'conv1/7x7_s2',W_regularizer=l2(0.0002))(input)
21 conv1_zero_pad = ZeroPadding2D(padding=(1, 1))(conv1_7x7_s2)
25 pool1_3x3_s2 = MaxPooling2D(pool_size=(3,3),strides=(2,2),border_mode=
'valid',name=
'pool1/3x3_s2')(pool1_helper)
27 pool1_norm1 =
LRN(name=
'pool1/norm1')(pool1_3x3_s2)
29 conv2_3x3_reduce = Convolution2D(64,1,1,border_mode=
'same',activation=
'relu',name=
'conv2/3x3_reduce',W_regularizer=l2(0.0002))(pool1_norm1)
31 conv2_3x3 = Convolution2D(192,3,3,border_mode=
'same',activation=
'relu',name=
'conv2/3x3',W_regularizer=l2(0.0002))(conv2_3x3_reduce)
33 conv2_norm2 =
LRN(name=
'conv2/norm2')(conv2_3x3)
35 conv2_zero_pad = ZeroPadding2D(padding=(1, 1))(conv2_norm2)
39 pool2_3x3_s2 = MaxPooling2D(pool_size=(3,3),strides=(2,2),border_mode=
'valid',name=
'pool2/3x3_s2')(pool2_helper)
42 inception_3a_1x1 = Convolution2D(64,1,1,border_mode=
'same',activation=
'relu',name=
'inception_3a/1x1',W_regularizer=l2(0.0002))(pool2_3x3_s2)
44 inception_3a_3x3_reduce = Convolution2D(96,1,1,border_mode=
'same',activation=
'relu',name=
'inception_3a/3x3_reduce',W_regularizer=l2(0.0002))(pool2_3x3_s2)
46 inception_3a_3x3 = Convolution2D(128,3,3,border_mode=
'same',activation=
'relu',name=
'inception_3a/3x3',W_regularizer=l2(0.0002))(inception_3a_3x3_reduce)
48 inception_3a_5x5_reduce = Convolution2D(16,1,1,border_mode=
'same',activation=
'relu',name=
'inception_3a/5x5_reduce',W_regularizer=l2(0.0002))(pool2_3x3_s2)
50 inception_3a_5x5 = Convolution2D(32,5,5,border_mode=
'same',activation=
'relu',name=
'inception_3a/5x5',W_regularizer=l2(0.0002))(inception_3a_5x5_reduce)
52 inception_3a_pool = MaxPooling2D(pool_size=(3,3),strides=(1,1),border_mode=
'same',name=
'inception_3a/pool')(pool2_3x3_s2)
54 inception_3a_pool_proj = Convolution2D(32,1,1,border_mode=
'same',activation=
'relu',name=
'inception_3a/pool_proj',W_regularizer=l2(0.0002))(inception_3a_pool)
56 inception_3a_output = merge([inception_3a_1x1,inception_3a_3x3,inception_3a_5x5,inception_3a_pool_proj],mode=
'concat',concat_axis=1,name=
'inception_3a/output')
59 inception_3b_1x1 = Convolution2D(128,1,1,border_mode=
'same',activation=
'relu',name=
'inception_3b/1x1',W_regularizer=l2(0.0002))(inception_3a_output)
61 inception_3b_3x3_reduce = Convolution2D(128,1,1,border_mode=
'same',activation=
'relu',name=
'inception_3b/3x3_reduce',W_regularizer=l2(0.0002))(inception_3a_output)
63 inception_3b_3x3 = Convolution2D(192,3,3,border_mode=
'same',activation=
'relu',name=
'inception_3b/3x3',W_regularizer=l2(0.0002))(inception_3b_3x3_reduce)
65 inception_3b_5x5_reduce = Convolution2D(32,1,1,border_mode=
'same',activation=
'relu',name=
'inception_3b/5x5_reduce',W_regularizer=l2(0.0002))(inception_3a_output)
67 inception_3b_5x5 = Convolution2D(96,5,5,border_mode=
'same',activation=
'relu',name=
'inception_3b/5x5',W_regularizer=l2(0.0002))(inception_3b_5x5_reduce)
69 inception_3b_pool = MaxPooling2D(pool_size=(3,3),strides=(1,1),border_mode=
'same',name=
'inception_3b/pool')(inception_3a_output)
71 inception_3b_pool_proj = Convolution2D(64,1,1,border_mode=
'same',activation=
'relu',name=
'inception_3b/pool_proj',W_regularizer=l2(0.0002))(inception_3b_pool)
73 inception_3b_output = merge([inception_3b_1x1,inception_3b_3x3,inception_3b_5x5,inception_3b_pool_proj],mode=
'concat',concat_axis=1,name=
'inception_3b/output')
76 inception_3b_output_zero_pad = ZeroPadding2D(padding=(1, 1))(inception_3b_output)
78 pool3_helper =
PoolHelper()(inception_3b_output_zero_pad)
80 pool3_3x3_s2 = MaxPooling2D(pool_size=(3,3),strides=(2,2),border_mode=
'valid',name=
'pool3/3x3_s2')(pool3_helper)
83 inception_4a_1x1 = Convolution2D(192,1,1,border_mode=
'same',activation=
'relu',name=
'inception_4a/1x1',W_regularizer=l2(0.0002))(pool3_3x3_s2)
85 inception_4a_3x3_reduce = Convolution2D(96,1,1,border_mode=
'same',activation=
'relu',name=
'inception_4a/3x3_reduce',W_regularizer=l2(0.0002))(pool3_3x3_s2)
87 inception_4a_3x3 = Convolution2D(208,3,3,border_mode=
'same',activation=
'relu',name=
'inception_4a/3x3',W_regularizer=l2(0.0002))(inception_4a_3x3_reduce)
89 inception_4a_5x5_reduce = Convolution2D(16,1,1,border_mode=
'same',activation=
'relu',name=
'inception_4a/5x5_reduce',W_regularizer=l2(0.0002))(pool3_3x3_s2)
91 inception_4a_5x5 = Convolution2D(48,5,5,border_mode=
'same',activation=
'relu',name=
'inception_4a/5x5',W_regularizer=l2(0.0002))(inception_4a_5x5_reduce)
93 inception_4a_pool = MaxPooling2D(pool_size=(3,3),strides=(1,1),border_mode=
'same',name=
'inception_4a/pool')(pool3_3x3_s2)
95 inception_4a_pool_proj = Convolution2D(64,1,1,border_mode=
'same',activation=
'relu',name=
'inception_4a/pool_proj',W_regularizer=l2(0.0002))(inception_4a_pool)
97 inception_4a_output = merge([inception_4a_1x1,inception_4a_3x3,inception_4a_5x5,inception_4a_pool_proj],mode=
'concat',concat_axis=1,name=
'inception_4a/output')
100 loss1_ave_pool = AveragePooling2D(pool_size=(5,5),strides=(3,3),name=
'loss1/ave_pool')(inception_4a_output)
102 loss1_conv = Convolution2D(128,1,1,border_mode=
'same',activation=
'relu',name=
'loss1/conv',W_regularizer=l2(0.0002))(loss1_ave_pool)
104 loss1_flat = Flatten()(loss1_conv)
106 loss1_fc = Dense(1024,activation=
'relu',name=
'loss1/fc',W_regularizer=l2(0.0002))(loss1_flat)
108 loss1_drop_fc = Dropout(0.7)(loss1_fc)
110 loss1_classifier = Dense(1000,name=
'loss1/classifier',W_regularizer=l2(0.0002))(loss1_drop_fc)
112 loss1_classifier_act = Activation(
'softmax')(loss1_classifier)
115 inception_4b_1x1 = Convolution2D(160,1,1,border_mode=
'same',activation=
'relu',name=
'inception_4b/1x1',W_regularizer=l2(0.0002))(inception_4a_output)
117 inception_4b_3x3_reduce = Convolution2D(112,1,1,border_mode=
'same',activation=
'relu',name=
'inception_4b/3x3_reduce',W_regularizer=l2(0.0002))(inception_4a_output)
119 inception_4b_3x3 = Convolution2D(224,3,3,border_mode=
'same',activation=
'relu',name=
'inception_4b/3x3',W_regularizer=l2(0.0002))(inception_4b_3x3_reduce)
121 inception_4b_5x5_reduce = Convolution2D(24,1,1,border_mode=
'same',activation=
'relu',name=
'inception_4b/5x5_reduce',W_regularizer=l2(0.0002))(inception_4a_output)
123 inception_4b_5x5 = Convolution2D(64,5,5,border_mode=
'same',activation=
'relu',name=
'inception_4b/5x5',W_regularizer=l2(0.0002))(inception_4b_5x5_reduce)
125 inception_4b_pool = MaxPooling2D(pool_size=(3,3),strides=(1,1),border_mode=
'same',name=
'inception_4b/pool')(inception_4a_output)
127 inception_4b_pool_proj = Convolution2D(64,1,1,border_mode=
'same',activation=
'relu',name=
'inception_4b/pool_proj',W_regularizer=l2(0.0002))(inception_4b_pool)
129 inception_4b_output = merge([inception_4b_1x1,inception_4b_3x3,inception_4b_5x5,inception_4b_pool_proj],mode=
'concat',concat_axis=1,name=
'inception_4b_output')
132 inception_4c_1x1 = Convolution2D(128,1,1,border_mode=
'same',activation=
'relu',name=
'inception_4c/1x1',W_regularizer=l2(0.0002))(inception_4b_output)
134 inception_4c_3x3_reduce = Convolution2D(128,1,1,border_mode=
'same',activation=
'relu',name=
'inception_4c/3x3_reduce',W_regularizer=l2(0.0002))(inception_4b_output)
136 inception_4c_3x3 = Convolution2D(256,3,3,border_mode=
'same',activation=
'relu',name=
'inception_4c/3x3',W_regularizer=l2(0.0002))(inception_4c_3x3_reduce)
138 inception_4c_5x5_reduce = Convolution2D(24,1,1,border_mode=
'same',activation=
'relu',name=
'inception_4c/5x5_reduce',W_regularizer=l2(0.0002))(inception_4b_output)
140 inception_4c_5x5 = Convolution2D(64,5,5,border_mode=
'same',activation=
'relu',name=
'inception_4c/5x5',W_regularizer=l2(0.0002))(inception_4c_5x5_reduce)
142 inception_4c_pool = MaxPooling2D(pool_size=(3,3),strides=(1,1),border_mode=
'same',name=
'inception_4c/pool')(inception_4b_output)
144 inception_4c_pool_proj = Convolution2D(64,1,1,border_mode=
'same',activation=
'relu',name=
'inception_4c/pool_proj',W_regularizer=l2(0.0002))(inception_4c_pool)
146 inception_4c_output = merge([inception_4c_1x1,inception_4c_3x3,inception_4c_5x5,inception_4c_pool_proj],mode=
'concat',concat_axis=1,name=
'inception_4c/output')
149 inception_4d_1x1 = Convolution2D(112,1,1,border_mode=
'same',activation=
'relu',name=
'inception_4d/1x1',W_regularizer=l2(0.0002))(inception_4c_output)
151 inception_4d_3x3_reduce = Convolution2D(144,1,1,border_mode=
'same',activation=
'relu',name=
'inception_4d/3x3_reduce',W_regularizer=l2(0.0002))(inception_4c_output)
153 inception_4d_3x3 = Convolution2D(288,3,3,border_mode=
'same',activation=
'relu',name=
'inception_4d/3x3',W_regularizer=l2(0.0002))(inception_4d_3x3_reduce)
155 inception_4d_5x5_reduce = Convolution2D(32,1,1,border_mode=
'same',activation=
'relu',name=
'inception_4d/5x5_reduce',W_regularizer=l2(0.0002))(inception_4c_output)
157 inception_4d_5x5 = Convolution2D(64,5,5,border_mode=
'same',activation=
'relu',name=
'inception_4d/5x5',W_regularizer=l2(0.0002))(inception_4d_5x5_reduce)
159 inception_4d_pool = MaxPooling2D(pool_size=(3,3),strides=(1,1),border_mode=
'same',name=
'inception_4d/pool')(inception_4c_output)
161 inception_4d_pool_proj = Convolution2D(64,1,1,border_mode=
'same',activation=
'relu',name=
'inception_4d/pool_proj',W_regularizer=l2(0.0002))(inception_4d_pool)
163 inception_4d_output = merge([inception_4d_1x1,inception_4d_3x3,inception_4d_5x5,inception_4d_pool_proj],mode=
'concat',concat_axis=1,name=
'inception_4d/output')
166 loss2_ave_pool = AveragePooling2D(pool_size=(5,5),strides=(3,3),name=
'loss2/ave_pool')(inception_4d_output)
168 loss2_conv = Convolution2D(128,1,1,border_mode=
'same',activation=
'relu',name=
'loss2/conv',W_regularizer=l2(0.0002))(loss2_ave_pool)
170 loss2_flat = Flatten()(loss2_conv)
172 loss2_fc = Dense(1024,activation=
'relu',name=
'loss2/fc',W_regularizer=l2(0.0002))(loss2_flat)
174 loss2_drop_fc = Dropout(0.7)(loss2_fc)
176 loss2_classifier = Dense(1000,name=
'loss2/classifier',W_regularizer=l2(0.0002))(loss2_drop_fc)
178 loss2_classifier_act = Activation(
'softmax')(loss2_classifier)
181 inception_4e_1x1 = Convolution2D(256,1,1,border_mode=
'same',activation=
'relu',name=
'inception_4e/1x1',W_regularizer=l2(0.0002))(inception_4d_output)
183 inception_4e_3x3_reduce = Convolution2D(160,1,1,border_mode=
'same',activation=
'relu',name=
'inception_4e/3x3_reduce',W_regularizer=l2(0.0002))(inception_4d_output)
185 inception_4e_3x3 = Convolution2D(320,3,3,border_mode=
'same',activation=
'relu',name=
'inception_4e/3x3',W_regularizer=l2(0.0002))(inception_4e_3x3_reduce)
187 inception_4e_5x5_reduce = Convolution2D(32,1,1,border_mode=
'same',activation=
'relu',name=
'inception_4e/5x5_reduce',W_regularizer=l2(0.0002))(inception_4d_output)
189 inception_4e_5x5 = Convolution2D(128,5,5,border_mode=
'same',activation=
'relu',name=
'inception_4e/5x5',W_regularizer=l2(0.0002))(inception_4e_5x5_reduce)
191 inception_4e_pool = MaxPooling2D(pool_size=(3,3),strides=(1,1),border_mode=
'same',name=
'inception_4e/pool')(inception_4d_output)
193 inception_4e_pool_proj = Convolution2D(128,1,1,border_mode=
'same',activation=
'relu',name=
'inception_4e/pool_proj',W_regularizer=l2(0.0002))(inception_4e_pool)
195 inception_4e_output = merge([inception_4e_1x1,inception_4e_3x3,inception_4e_5x5,inception_4e_pool_proj],mode=
'concat',concat_axis=1,name=
'inception_4e/output')
198 inception_4e_output_zero_pad = ZeroPadding2D(padding=(1, 1))(inception_4e_output)
200 pool4_helper =
PoolHelper()(inception_4e_output_zero_pad)
202 pool4_3x3_s2 = MaxPooling2D(pool_size=(3,3),strides=(2,2),border_mode=
'valid',name=
'pool4/3x3_s2')(pool4_helper)
205 inception_5a_1x1 = Convolution2D(256,1,1,border_mode=
'same',activation=
'relu',name=
'inception_5a/1x1',W_regularizer=l2(0.0002))(pool4_3x3_s2)
207 inception_5a_3x3_reduce = Convolution2D(160,1,1,border_mode=
'same',activation=
'relu',name=
'inception_5a/3x3_reduce',W_regularizer=l2(0.0002))(pool4_3x3_s2)
209 inception_5a_3x3 = Convolution2D(320,3,3,border_mode=
'same',activation=
'relu',name=
'inception_5a/3x3',W_regularizer=l2(0.0002))(inception_5a_3x3_reduce)
211 inception_5a_5x5_reduce = Convolution2D(32,1,1,border_mode=
'same',activation=
'relu',name=
'inception_5a/5x5_reduce',W_regularizer=l2(0.0002))(pool4_3x3_s2)
213 inception_5a_5x5 = Convolution2D(128,5,5,border_mode=
'same',activation=
'relu',name=
'inception_5a/5x5',W_regularizer=l2(0.0002))(inception_5a_5x5_reduce)
215 inception_5a_pool = MaxPooling2D(pool_size=(3,3),strides=(1,1),border_mode=
'same',name=
'inception_5a/pool')(pool4_3x3_s2)
217 inception_5a_pool_proj = Convolution2D(128,1,1,border_mode=
'same',activation=
'relu',name=
'inception_5a/pool_proj',W_regularizer=l2(0.0002))(inception_5a_pool)
219 inception_5a_output = merge([inception_5a_1x1,inception_5a_3x3,inception_5a_5x5,inception_5a_pool_proj],mode=
'concat',concat_axis=1,name=
'inception_5a/output')
222 inception_5b_1x1 = Convolution2D(384,1,1,border_mode=
'same',activation=
'relu',name=
'inception_5b/1x1',W_regularizer=l2(0.0002))(inception_5a_output)
224 inception_5b_3x3_reduce = Convolution2D(192,1,1,border_mode=
'same',activation=
'relu',name=
'inception_5b/3x3_reduce',W_regularizer=l2(0.0002))(inception_5a_output)
226 inception_5b_3x3 = Convolution2D(384,3,3,border_mode=
'same',activation=
'relu',name=
'inception_5b/3x3',W_regularizer=l2(0.0002))(inception_5b_3x3_reduce)
228 inception_5b_5x5_reduce = Convolution2D(48,1,1,border_mode=
'same',activation=
'relu',name=
'inception_5b/5x5_reduce',W_regularizer=l2(0.0002))(inception_5a_output)
230 inception_5b_5x5 = Convolution2D(128,5,5,border_mode=
'same',activation=
'relu',name=
'inception_5b/5x5',W_regularizer=l2(0.0002))(inception_5b_5x5_reduce)
232 inception_5b_pool = MaxPooling2D(pool_size=(3,3),strides=(1,1),border_mode=
'same',name=
'inception_5b/pool')(inception_5a_output)
234 inception_5b_pool_proj = Convolution2D(128,1,1,border_mode=
'same',activation=
'relu',name=
'inception_5b/pool_proj',W_regularizer=l2(0.0002))(inception_5b_pool)
236 inception_5b_output = merge([inception_5b_1x1,inception_5b_3x3,inception_5b_5x5,inception_5b_pool_proj],mode=
'concat',concat_axis=1,name=
'inception_5b/output')
239 pool5_7x7_s1 = AveragePooling2D(pool_size=(7,7),strides=(1,1),name=
'pool5/7x7_s2')(inception_5b_output)
241 loss3_flat = Flatten()(pool5_7x7_s1)
243 pool5_drop_7x7_s1 = Dropout(0.4)(loss3_flat)
245 loss3_classifier = Dense(classes,name=
'loss3/classifier',W_regularizer=l2(0.0002))(pool5_drop_7x7_s1)
247 loss3_classifier_act = Activation(
'softmax',name=
'prob')(loss3_classifier)
250 model = Model(input=input, output=[loss1_classifier_act,loss2_classifier_act,loss3_classifier_act])
253 model.load_weights(weights_path)
def googlenet(input_shape=[500, classes=13, weights_path=None)