2 Copyright 2017 TensorFlow Authors and Kent Sommer 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 8 http://www.apache.org/licenses/LICENSE-2.0 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 21 from keras.layers.convolutional
import MaxPooling2D, Convolution2D, AveragePooling2D
22 from keras.layers
import Input, Dropout, Dense, Flatten, Activation
23 from keras.layers.normalization
import BatchNormalization
24 from keras.layers.merge
import concatenate
25 from keras
import regularizers
26 from keras
import initializers
27 from keras.models
import Model
29 from keras
import backend
as K
31 from keras.utils.layer_utils
import convert_all_kernels_in_model
32 from keras.utils.data_utils
import get_file
39 WEIGHTS_PATH =
'https://github.com/kentsommer/keras-inceptionV4/releases/download/2.1/inception-v4_weights_tf_dim_ordering_tf_kernels.h5' 40 WEIGHTS_PATH_NO_TOP =
'https://github.com/kentsommer/keras-inceptionV4/releases/download/2.1/inception-v4_weights_tf_dim_ordering_tf_kernels_notop.h5' 44 x = np.divide(x, 255.0)
45 x = np.subtract(x, 0.5)
46 x = np.multiply(x, 2.0)
50 def conv2d_bn(x, nb_filter, num_row, num_col,
51 padding=
'same', strides=(1, 1), use_bias=
False):
53 Utility function to apply conv + BN. 54 (Slightly modified from https://github.com/fchollet/keras/blob/master/keras/applications/inception_v3.py) 56 if K.image_data_format() ==
'channels_first':
60 x = Convolution2D(nb_filter, (num_row, num_col),
64 kernel_regularizer=regularizers.l2(0.00004),
65 kernel_initializer=initializers.VarianceScaling(scale=2.0, mode=
'fan_in', distribution=
'normal', seed=
None))(x)
66 x = BatchNormalization(axis=channel_axis, momentum=0.9997, scale=
False)(x)
67 x = Activation(
'relu')(x)
72 if K.image_data_format() ==
'channels_first':
86 branch_3 = AveragePooling2D((3,3), strides=(1,1), padding=
'same')(input)
89 x =
concatenate([branch_0, branch_1, branch_2, branch_3], axis=channel_axis)
94 if K.image_data_format() ==
'channels_first':
99 branch_0 =
conv2d_bn(input, 384, 3, 3, strides=(2,2), padding=
'valid')
102 branch_1 =
conv2d_bn(branch_1, 224, 3, 3)
103 branch_1 =
conv2d_bn(branch_1, 256, 3, 3, strides=(2,2), padding=
'valid')
105 branch_2 = MaxPooling2D((3,3), strides=(2,2), padding=
'valid')(input)
107 x =
concatenate([branch_0, branch_1, branch_2], axis=channel_axis)
112 if K.image_data_format() ==
'channels_first':
120 branch_1 =
conv2d_bn(branch_1, 224, 1, 7)
121 branch_1 =
conv2d_bn(branch_1, 256, 7, 1)
124 branch_2 =
conv2d_bn(branch_2, 192, 7, 1)
125 branch_2 =
conv2d_bn(branch_2, 224, 1, 7)
126 branch_2 =
conv2d_bn(branch_2, 224, 7, 1)
127 branch_2 =
conv2d_bn(branch_2, 256, 1, 7)
129 branch_3 = AveragePooling2D((3,3), strides=(1,1), padding=
'same')(input)
130 branch_3 =
conv2d_bn(branch_3, 128, 1, 1)
132 x =
concatenate([branch_0, branch_1, branch_2, branch_3], axis=channel_axis)
137 if K.image_data_format() ==
'channels_first':
143 branch_0 =
conv2d_bn(branch_0, 192, 3, 3, strides=(2, 2), padding=
'valid')
146 branch_1 =
conv2d_bn(branch_1, 256, 1, 7)
147 branch_1 =
conv2d_bn(branch_1, 320, 7, 1)
148 branch_1 =
conv2d_bn(branch_1, 320, 3, 3, strides=(2,2), padding=
'valid')
150 branch_2 = MaxPooling2D((3, 3), strides=(2, 2), padding=
'valid')(input)
152 x =
concatenate([branch_0, branch_1, branch_2], axis=channel_axis)
157 if K.image_data_format() ==
'channels_first':
165 branch_10 =
conv2d_bn(branch_1, 256, 1, 3)
166 branch_11 =
conv2d_bn(branch_1, 256, 3, 1)
167 branch_1 =
concatenate([branch_10, branch_11], axis=channel_axis)
171 branch_2 =
conv2d_bn(branch_2, 448, 3, 1)
172 branch_2 =
conv2d_bn(branch_2, 512, 1, 3)
173 branch_20 =
conv2d_bn(branch_2, 256, 1, 3)
174 branch_21 =
conv2d_bn(branch_2, 256, 3, 1)
175 branch_2 =
concatenate([branch_20, branch_21], axis=channel_axis)
177 branch_3 = AveragePooling2D((3, 3), strides=(1, 1), padding=
'same')(input)
178 branch_3 =
conv2d_bn(branch_3, 256, 1, 1)
180 x =
concatenate([branch_0, branch_1, branch_2, branch_3], axis=channel_axis)
185 if K.image_data_format() ==
'channels_first':
191 net =
conv2d_bn(input, 32, 3, 3, strides=(2,2), padding=
'valid')
192 net =
conv2d_bn(net, 32, 3, 3, padding=
'valid')
195 branch_0 = MaxPooling2D((3,3), strides=(2,2), padding=
'valid')(net)
197 branch_1 =
conv2d_bn(net, 96, 3, 3, strides=(2,2), padding=
'valid')
199 net =
concatenate([branch_0, branch_1], axis=channel_axis)
202 branch_0 =
conv2d_bn(branch_0, 96, 3, 3, padding=
'valid')
207 branch_1 =
conv2d_bn(branch_1, 96, 3, 3, padding=
'valid')
209 net =
concatenate([branch_0, branch_1], axis=channel_axis)
211 branch_0 =
conv2d_bn(net, 192, 3, 3, strides=(2,2), padding=
'valid')
212 branch_1 = MaxPooling2D((3,3), strides=(2,2), padding=
'valid')(net)
214 net =
concatenate([branch_0, branch_1], axis=channel_axis)
242 def inception_v4(num_classes, dropout_keep_prob, weights, input_shape, include_top, transfer_learning=None):
244 Creates the inception v4 network 247 num_classes: number of classes 248 dropout_keep_prob: float, the fraction to keep before final layer. 251 logits: the logits outputs of the model. 255 if K.image_data_format() ==
'channels_first':
256 inputs = Input((input_shape[2], input_shape[0], input_shape[1]))
258 inputs = Input(input_shape)
267 x = AveragePooling2D((8,8), padding=
'valid')(x)
268 x = Dropout(dropout_keep_prob)(x)
271 x = Dense(units=num_classes, activation=
'softmax')(x)
273 model = Model(inputs, x, name=
'inception_v4')
276 if weights ==
'imagenet':
277 if K.image_data_format() ==
'channels_first':
278 if K.backend() ==
'tensorflow':
279 warnings.warn(
'You are using the TensorFlow backend, yet you ' 280 'are using the Theano ' 281 'image data format convention ' 282 '(`image_data_format="channels_first"`). ' 283 'For best performance, set ' 284 '`image_data_format="channels_last"` in ' 286 'at ~/.keras/keras.json.')
288 weights_path = get_file(
289 'inception-v4_weights_tf_dim_ordering_tf_kernels.h5',
291 cache_subdir=
'models',
292 md5_hash=
'9fe79d77f793fe874470d84ca6ba4a3b')
294 weights_path = get_file(
295 'inception-v4_weights_tf_dim_ordering_tf_kernels_notop.h5',
297 cache_subdir=
'models',
298 md5_hash=
'9296b46b5971573064d12e4669110969')
299 model.load_weights(weights_path, by_name=
True)
302 if transfer_learning ==
'pretraining' or transfer_learning ==
'finetuning':
304 x = AveragePooling2D((8,8), padding=
'valid')(x)
305 x = Dropout(dropout_keep_prob)(x)
308 x = Dense(units=num_classes, activation=
'softmax')(x)
310 model = Model(inputs, x, name=
'inception_v4')
312 if transfer_learning ==
'finetuning':
313 for layer
in model.layers[:-4]:
314 layer.trainable =
False 319 def create_model(num_classes=1001, dropout_prob=0.2, weights=None, include_top=True, transfer_learning=None, input_shape=None):
320 return inception_v4(num_classes, dropout_prob, weights, input_shape, include_top, transfer_learning)
def inception_v4(num_classes, dropout_keep_prob, weights, input_shape, include_top, transfer_learning=None)
def inception_v4_base(input)
std::string concatenate(H const &h, T const &...t)
def block_reduction_b(input)
def block_reduction_a(input)
def create_model(num_classes=1001, dropout_prob=0.2, weights=None, include_top=True, transfer_learning=None, input_shape=None)
def conv2d_bn(x, nb_filter, num_row, num_col, padding='same', strides=(1, 1), use_bias=False)
def block_inception_a(input)
def block_inception_b(input)
def block_inception_c(input)