Functions | Variables
se_inception_v3 Namespace Reference

Functions

def _conv2d_bn (x, filters, num_row, num_col, padding='same', strides=(1, 1), name=None)
 
def SEInceptionV3 (include_top=True, weights=None, input_tensor=None, input_shape=None, pooling=None, classes=1000)
 
def preprocess_input (x)
 

Variables

string WEIGHTS_PATH = ''
 
string WEIGHTS_PATH_NO_TOP = ''
 

Detailed Description

Squeeze and Excite Inception V3 model

Major portions of this code is adapted from the applications folder of Keras.

Note that the input image format for this model is different than for
the VGG16 and ResNet models (299x299 instead of 224x224),
and that the input preprocessing function is also different (same as Xception).

# Reference
- [Rethinking the Inception Architecture for Computer Vision](http://arxiv.org/abs/1512.00567)
- []() # added when paper is published on Arxiv

Function Documentation

def se_inception_v3._conv2d_bn (   x,
  filters,
  num_row,
  num_col,
  padding = 'same',
  strides = (1, 1),
  name = None 
)
private
Utility function to apply conv + BN.

# Arguments
    x: input tensor.
    filters: filters in `Conv2D`.
    num_row: height of the convolution kernel.
    num_col: width of the convolution kernel.
    padding: padding mode in `Conv2D`.
    strides: strides in `Conv2D`.
    name: name of the ops; will become `name + '_conv'`
        for the convolution and `name + '_bn'` for the
        batch norm layer.

# Returns
    Output tensor after applying `Conv2D` and `BatchNormalization`.

Definition at line 50 of file se_inception_v3.py.

50  name=None):
51  """Utility function to apply conv + BN.
52 
53  # Arguments
54  x: input tensor.
55  filters: filters in `Conv2D`.
56  num_row: height of the convolution kernel.
57  num_col: width of the convolution kernel.
58  padding: padding mode in `Conv2D`.
59  strides: strides in `Conv2D`.
60  name: name of the ops; will become `name + '_conv'`
61  for the convolution and `name + '_bn'` for the
62  batch norm layer.
63 
64  # Returns
65  Output tensor after applying `Conv2D` and `BatchNormalization`.
66  """
67  if name is not None:
68  bn_name = name + '_bn'
69  conv_name = name + '_conv'
70  else:
71  bn_name = None
72  conv_name = None
73  if K.image_data_format() == 'channels_first':
74  bn_axis = 1
75  else:
76  bn_axis = 3
77  x = Conv2D(
78  filters, (num_row, num_col),
79  strides=strides,
80  padding=padding,
81  use_bias=False,
82  name=conv_name)(x)
83  x = BatchNormalization(axis=bn_axis, scale=False, name=bn_name)(x)
84  x = Activation('relu', name=name)(x)
85  return x
86 
87 
def se_inception_v3.preprocess_input (   x)

Definition at line 385 of file se_inception_v3.py.

386  x /= 255.
387  x -= 0.5
388  x *= 2.
389  return x
390 
def se_inception_v3.SEInceptionV3 (   include_top = True,
  weights = None,
  input_tensor = None,
  input_shape = None,
  pooling = None,
  classes = 1000 
)
Instantiates the Squeeze and Excite Inception v3 architecture.

# Arguments
    include_top: whether to include the fully-connected
        layer at the top of the network.
    weights: one of `None` (random initialization)
        or "imagenet" (pre-training on ImageNet).
    input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
        to use as image input for the model.
    input_shape: optional shape tuple, only to be specified
        if `include_top` is False (otherwise the input shape
        has to be `(299, 299, 3)` (with `channels_last` data format)
        or `(3, 299, 299)` (with `channels_first` data format).
        It should have exactly 3 inputs channels,
        and width and height should be no smaller than 139.
        E.g. `(150, 150, 3)` would be one valid value.
    pooling: Optional pooling mode for feature extraction
        when `include_top` is `False`.
        - `None` means that the output of the model will be
            the 4D tensor output of the
            last convolutional layer.
        - `avg` means that global average pooling
            will be applied to the output of the
            last convolutional layer, and thus
            the output of the model will be a 2D tensor.
        - `max` means that global max pooling will
            be applied.
    classes: optional number of classes to classify images
        into, only to be specified if `include_top` is True, and
        if no `weights` argument is specified.

# Returns
    A Keras model instance.

# Raises
    ValueError: in case of invalid argument for `weights`,
        or invalid input shape.

Definition at line 93 of file se_inception_v3.py.

93  classes=1000):
94  """Instantiates the Squeeze and Excite Inception v3 architecture.
95 
96  # Arguments
97  include_top: whether to include the fully-connected
98  layer at the top of the network.
99  weights: one of `None` (random initialization)
100  or "imagenet" (pre-training on ImageNet).
101  input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
102  to use as image input for the model.
103  input_shape: optional shape tuple, only to be specified
104  if `include_top` is False (otherwise the input shape
105  has to be `(299, 299, 3)` (with `channels_last` data format)
106  or `(3, 299, 299)` (with `channels_first` data format).
107  It should have exactly 3 inputs channels,
108  and width and height should be no smaller than 139.
109  E.g. `(150, 150, 3)` would be one valid value.
110  pooling: Optional pooling mode for feature extraction
111  when `include_top` is `False`.
112  - `None` means that the output of the model will be
113  the 4D tensor output of the
114  last convolutional layer.
115  - `avg` means that global average pooling
116  will be applied to the output of the
117  last convolutional layer, and thus
118  the output of the model will be a 2D tensor.
119  - `max` means that global max pooling will
120  be applied.
121  classes: optional number of classes to classify images
122  into, only to be specified if `include_top` is True, and
123  if no `weights` argument is specified.
124 
125  # Returns
126  A Keras model instance.
127 
128  # Raises
129  ValueError: in case of invalid argument for `weights`,
130  or invalid input shape.
131  """
132  if weights not in {'imagenet', None}:
133  raise ValueError('The `weights` argument should be either '
134  '`None` (random initialization) or `imagenet` '
135  '(pre-training on ImageNet).')
136 
137  if weights == 'imagenet' and include_top and classes != 1000:
138  raise ValueError('If using `weights` as imagenet with `include_top`'
139  ' as true, `classes` should be 1000')
140 
141  # Determine proper input shape
142  input_shape = _obtain_input_shape(
143  input_shape,
144  default_size=299,
145  min_size=139,
146  data_format=K.image_data_format(),
147  require_flatten=include_top)
148 
149  if input_tensor is None:
150  img_input = Input(shape=input_shape)
151  else:
152  if not K.is_keras_tensor(input_tensor):
153  img_input = Input(tensor=input_tensor, shape=input_shape)
154  else:
155  img_input = input_tensor
156 
157  if K.image_data_format() == 'channels_first':
158  channel_axis = 1
159  else:
160  channel_axis = 3
161 
162  x = _conv2d_bn(img_input, 32, 3, 3, strides=(2, 2), padding='valid')
163  x = _conv2d_bn(x, 32, 3, 3, padding='valid')
164  x = _conv2d_bn(x, 64, 3, 3)
165  x = MaxPooling2D((3, 3), strides=(2, 2))(x)
166 
167  x = _conv2d_bn(x, 80, 1, 1, padding='valid')
168  x = _conv2d_bn(x, 192, 3, 3, padding='valid')
169  x = MaxPooling2D((3, 3), strides=(2, 2))(x)
170 
171  # mixed 0, 1, 2: 35 x 35 x 256
172  branch1x1 = _conv2d_bn(x, 64, 1, 1)
173 
174  branch5x5 = _conv2d_bn(x, 48, 1, 1)
175  branch5x5 = _conv2d_bn(branch5x5, 64, 5, 5)
176 
177  branch3x3dbl = _conv2d_bn(x, 64, 1, 1)
178  branch3x3dbl = _conv2d_bn(branch3x3dbl, 96, 3, 3)
179  branch3x3dbl = _conv2d_bn(branch3x3dbl, 96, 3, 3)
180 
181  branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x)
182  branch_pool = _conv2d_bn(branch_pool, 32, 1, 1)
183  x = layers.concatenate(
184  [branch1x1, branch5x5, branch3x3dbl, branch_pool],
185  axis=channel_axis,
186  name='mixed0')
187 
188  # squeeze and excite block
189  x = squeeze_excite_block(x)
190 
191  # mixed 1: 35 x 35 x 256
192  branch1x1 = _conv2d_bn(x, 64, 1, 1)
193 
194  branch5x5 = _conv2d_bn(x, 48, 1, 1)
195  branch5x5 = _conv2d_bn(branch5x5, 64, 5, 5)
196 
197  branch3x3dbl = _conv2d_bn(x, 64, 1, 1)
198  branch3x3dbl = _conv2d_bn(branch3x3dbl, 96, 3, 3)
199  branch3x3dbl = _conv2d_bn(branch3x3dbl, 96, 3, 3)
200 
201  branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x)
202  branch_pool = _conv2d_bn(branch_pool, 64, 1, 1)
203  x = layers.concatenate(
204  [branch1x1, branch5x5, branch3x3dbl, branch_pool],
205  axis=channel_axis,
206  name='mixed1')
207 
208  # squeeze and excite block
209  x = squeeze_excite_block(x)
210 
211  # mixed 2: 35 x 35 x 256
212  branch1x1 = _conv2d_bn(x, 64, 1, 1)
213 
214  branch5x5 = _conv2d_bn(x, 48, 1, 1)
215  branch5x5 = _conv2d_bn(branch5x5, 64, 5, 5)
216 
217  branch3x3dbl = _conv2d_bn(x, 64, 1, 1)
218  branch3x3dbl = _conv2d_bn(branch3x3dbl, 96, 3, 3)
219  branch3x3dbl = _conv2d_bn(branch3x3dbl, 96, 3, 3)
220 
221  branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x)
222  branch_pool = _conv2d_bn(branch_pool, 64, 1, 1)
223  x = layers.concatenate(
224  [branch1x1, branch5x5, branch3x3dbl, branch_pool],
225  axis=channel_axis,
226  name='mixed2')
227 
228  # squeeze and excite block
229  x = squeeze_excite_block(x)
230 
231  # mixed 3: 17 x 17 x 768
232  branch3x3 = _conv2d_bn(x, 384, 3, 3, strides=(2, 2), padding='valid')
233 
234  branch3x3dbl = _conv2d_bn(x, 64, 1, 1)
235  branch3x3dbl = _conv2d_bn(branch3x3dbl, 96, 3, 3)
236  branch3x3dbl = _conv2d_bn(
237  branch3x3dbl, 96, 3, 3, strides=(2, 2), padding='valid')
238 
239  branch_pool = MaxPooling2D((3, 3), strides=(2, 2))(x)
240  x = layers.concatenate(
241  [branch3x3, branch3x3dbl, branch_pool], axis=channel_axis, name='mixed3')
242 
243  # squeeze and excite block
244  x = squeeze_excite_block(x)
245 
246  # mixed 4: 17 x 17 x 768
247  branch1x1 = _conv2d_bn(x, 192, 1, 1)
248 
249  branch7x7 = _conv2d_bn(x, 128, 1, 1)
250  branch7x7 = _conv2d_bn(branch7x7, 128, 1, 7)
251  branch7x7 = _conv2d_bn(branch7x7, 192, 7, 1)
252 
253  branch7x7dbl = _conv2d_bn(x, 128, 1, 1)
254  branch7x7dbl = _conv2d_bn(branch7x7dbl, 128, 7, 1)
255  branch7x7dbl = _conv2d_bn(branch7x7dbl, 128, 1, 7)
256  branch7x7dbl = _conv2d_bn(branch7x7dbl, 128, 7, 1)
257  branch7x7dbl = _conv2d_bn(branch7x7dbl, 192, 1, 7)
258 
259  branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x)
260  branch_pool = _conv2d_bn(branch_pool, 192, 1, 1)
261  x = layers.concatenate(
262  [branch1x1, branch7x7, branch7x7dbl, branch_pool],
263  axis=channel_axis,
264  name='mixed4')
265 
266  # squeeze and excite block
267  x = squeeze_excite_block(x)
268 
269  # mixed 5, 6: 17 x 17 x 768
270  for i in range(2):
271  branch1x1 = _conv2d_bn(x, 192, 1, 1)
272 
273  branch7x7 = _conv2d_bn(x, 160, 1, 1)
274  branch7x7 = _conv2d_bn(branch7x7, 160, 1, 7)
275  branch7x7 = _conv2d_bn(branch7x7, 192, 7, 1)
276 
277  branch7x7dbl = _conv2d_bn(x, 160, 1, 1)
278  branch7x7dbl = _conv2d_bn(branch7x7dbl, 160, 7, 1)
279  branch7x7dbl = _conv2d_bn(branch7x7dbl, 160, 1, 7)
280  branch7x7dbl = _conv2d_bn(branch7x7dbl, 160, 7, 1)
281  branch7x7dbl = _conv2d_bn(branch7x7dbl, 192, 1, 7)
282 
283  branch_pool = AveragePooling2D(
284  (3, 3), strides=(1, 1), padding='same')(x)
285  branch_pool = _conv2d_bn(branch_pool, 192, 1, 1)
286  x = layers.concatenate(
287  [branch1x1, branch7x7, branch7x7dbl, branch_pool],
288  axis=channel_axis,
289  name='mixed' + str(5 + i))
290 
291  # squeeze and excite block
292  x = squeeze_excite_block(x)
293 
294  # mixed 7: 17 x 17 x 768
295  branch1x1 = _conv2d_bn(x, 192, 1, 1)
296 
297  branch7x7 = _conv2d_bn(x, 192, 1, 1)
298  branch7x7 = _conv2d_bn(branch7x7, 192, 1, 7)
299  branch7x7 = _conv2d_bn(branch7x7, 192, 7, 1)
300 
301  branch7x7dbl = _conv2d_bn(x, 192, 1, 1)
302  branch7x7dbl = _conv2d_bn(branch7x7dbl, 192, 7, 1)
303  branch7x7dbl = _conv2d_bn(branch7x7dbl, 192, 1, 7)
304  branch7x7dbl = _conv2d_bn(branch7x7dbl, 192, 7, 1)
305  branch7x7dbl = _conv2d_bn(branch7x7dbl, 192, 1, 7)
306 
307  branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x)
308  branch_pool = _conv2d_bn(branch_pool, 192, 1, 1)
309  x = layers.concatenate(
310  [branch1x1, branch7x7, branch7x7dbl, branch_pool],
311  axis=channel_axis,
312  name='mixed7')
313 
314  # squeeze and excite block
315  x = squeeze_excite_block(x)
316 
317  # mixed 8: 8 x 8 x 1280
318  branch3x3 = _conv2d_bn(x, 192, 1, 1)
319  branch3x3 = _conv2d_bn(branch3x3, 320, 3, 3,
320  strides=(2, 2), padding='valid')
321 
322  branch7x7x3 = _conv2d_bn(x, 192, 1, 1)
323  branch7x7x3 = _conv2d_bn(branch7x7x3, 192, 1, 7)
324  branch7x7x3 = _conv2d_bn(branch7x7x3, 192, 7, 1)
325  branch7x7x3 = _conv2d_bn(
326  branch7x7x3, 192, 3, 3, strides=(2, 2), padding='valid')
327 
328  branch_pool = MaxPooling2D((3, 3), strides=(2, 2))(x)
329  x = layers.concatenate(
330  [branch3x3, branch7x7x3, branch_pool], axis=channel_axis, name='mixed8')
331 
332  # squeeze and excite block
333  x = squeeze_excite_block(x)
334 
335  # mixed 9: 8 x 8 x 2048
336  for i in range(2):
337  branch1x1 = _conv2d_bn(x, 320, 1, 1)
338 
339  branch3x3 = _conv2d_bn(x, 384, 1, 1)
340  branch3x3_1 = _conv2d_bn(branch3x3, 384, 1, 3)
341  branch3x3_2 = _conv2d_bn(branch3x3, 384, 3, 1)
342  branch3x3 = layers.concatenate(
343  [branch3x3_1, branch3x3_2], axis=channel_axis, name='mixed9_' + str(i))
344 
345  branch3x3dbl = _conv2d_bn(x, 448, 1, 1)
346  branch3x3dbl = _conv2d_bn(branch3x3dbl, 384, 3, 3)
347  branch3x3dbl_1 = _conv2d_bn(branch3x3dbl, 384, 1, 3)
348  branch3x3dbl_2 = _conv2d_bn(branch3x3dbl, 384, 3, 1)
349  branch3x3dbl = layers.concatenate(
350  [branch3x3dbl_1, branch3x3dbl_2], axis=channel_axis)
351 
352  branch_pool = AveragePooling2D(
353  (3, 3), strides=(1, 1), padding='same')(x)
354  branch_pool = _conv2d_bn(branch_pool, 192, 1, 1)
355  x = layers.concatenate(
356  [branch1x1, branch3x3, branch3x3dbl, branch_pool],
357  axis=channel_axis,
358  name='mixed' + str(9 + i))
359 
360  # squeeze and excite block
361  x = squeeze_excite_block(x)
362 
363  if include_top:
364  # Classification block
365  x = GlobalAveragePooling2D(name='avg_pool')(x)
366  x = Dense(classes, activation='softmax', name='predictions')(x)
367  else:
368  if pooling == 'avg':
369  x = GlobalAveragePooling2D()(x)
370  elif pooling == 'max':
371  x = GlobalMaxPooling2D()(x)
372 
373  # Ensure that the model takes into account
374  # any potential predecessors of `input_tensor`.
375  if input_tensor is not None:
376  inputs = get_source_inputs(input_tensor)
377  else:
378  inputs = img_input
379  # Create model.
380  model = Model(inputs, x, name='inception_v3')
381 
382  return model
383 
384 
def squeeze_excite_block(input, ratio=16)
Definition: se.py:5
def _obtain_input_shape(input_shape, default_size, min_size, data_format, require_flatten, weights=None)
def _conv2d_bn(x, filters, num_row, num_col, padding='same', strides=(1, 1), name=None)
static QCString str

Variable Documentation

string se_inception_v3.WEIGHTS_PATH = ''

Definition at line 40 of file se_inception_v3.py.

string se_inception_v3.WEIGHTS_PATH_NO_TOP = ''

Definition at line 41 of file se_inception_v3.py.