Functions
se_inception_resnet_v2 Namespace Reference

Functions

def preprocess_input (x)
 
def conv2d_bn (x, filters, kernel_size, strides=1, padding='same', activation='relu', use_bias=False, name=None)
 
def inception_resnet_block (x, scale, block_type, block_idx, activation='relu')
 
def SEInceptionResNetV2 (include_top=True, weights=None, input_tensor=None, input_shape=None, pooling=None, classes=1000)
 

Function Documentation

def se_inception_resnet_v2.conv2d_bn (   x,
  filters,
  kernel_size,
  strides = 1,
  padding = 'same',
  activation = 'relu',
  use_bias = False,
  name = None 
)
Utility function to apply conv + BN.
# Arguments
    x: input tensor.
    filters: filters in `Conv2D`.
    kernel_size: kernel size as in `Conv2D`.
    padding: padding mode in `Conv2D`.
    activation: activation in `Conv2D`.
    strides: strides in `Conv2D`.
    name: name of the ops; will become `name + '_ac'` for the activation
        and `name + '_bn'` for the batch norm layer.
# Returns
    Output tensor after applying `Conv2D` and `BatchNormalization`.

Definition at line 60 of file se_inception_resnet_v2.py.

60  name=None):
61  """Utility function to apply conv + BN.
62  # Arguments
63  x: input tensor.
64  filters: filters in `Conv2D`.
65  kernel_size: kernel size as in `Conv2D`.
66  padding: padding mode in `Conv2D`.
67  activation: activation in `Conv2D`.
68  strides: strides in `Conv2D`.
69  name: name of the ops; will become `name + '_ac'` for the activation
70  and `name + '_bn'` for the batch norm layer.
71  # Returns
72  Output tensor after applying `Conv2D` and `BatchNormalization`.
73  """
74  x = Conv2D(filters,
75  kernel_size,
76  strides=strides,
77  padding=padding,
78  use_bias=use_bias,
79  name=name)(x)
80  if not use_bias:
81  bn_axis = 1 if K.image_data_format() == 'channels_first' else 3
82  bn_name = None if name is None else name + '_bn'
83  x = BatchNormalization(axis=bn_axis, scale=False, name=bn_name)(x)
84  if activation is not None:
85  ac_name = None if name is None else name + '_ac'
86  x = Activation(activation, name=ac_name)(x)
87  return x
88 
89 
def se_inception_resnet_v2.inception_resnet_block (   x,
  scale,
  block_type,
  block_idx,
  activation = 'relu' 
)
Adds a Inception-ResNet block with Squeeze and Excitation block at the end.
This function builds 3 types of Inception-ResNet blocks mentioned
in the paper, controlled by the `block_type` argument (which is the
block name used in the official TF-slim implementation):
    - Inception-ResNet-A: `block_type='block35'`
    - Inception-ResNet-B: `block_type='block17'`
    - Inception-ResNet-C: `block_type='block8'`
# Arguments
    x: input tensor.
    scale: scaling factor to scale the residuals (i.e., the output of
        passing `x` through an inception module) before adding them
        to the shortcut branch. Let `r` be the output from the residual branch,
        the output of this block will be `x + scale * r`.
    block_type: `'block35'`, `'block17'` or `'block8'`, determines
        the network structure in the residual branch.
    block_idx: an `int` used for generating layer names. The Inception-ResNet blocks
        are repeated many times in this network. We use `block_idx` to identify
        each of the repetitions. For example, the first Inception-ResNet-A block
        will have `block_type='block35', block_idx=0`, ane the layer names will have
        a common prefix `'block35_0'`.
    activation: activation function to use at the end of the block
        (see [activations](../activations.md)).
        When `activation=None`, no activation is applied
        (i.e., "linear" activation: `a(x) = x`).
# Returns
    Output tensor for the block.
# Raises
    ValueError: if `block_type` is not one of `'block35'`,
        `'block17'` or `'block8'`.

Definition at line 90 of file se_inception_resnet_v2.py.

90 def inception_resnet_block(x, scale, block_type, block_idx, activation='relu'):
91  """Adds a Inception-ResNet block with Squeeze and Excitation block at the end.
92  This function builds 3 types of Inception-ResNet blocks mentioned
93  in the paper, controlled by the `block_type` argument (which is the
94  block name used in the official TF-slim implementation):
95  - Inception-ResNet-A: `block_type='block35'`
96  - Inception-ResNet-B: `block_type='block17'`
97  - Inception-ResNet-C: `block_type='block8'`
98  # Arguments
99  x: input tensor.
100  scale: scaling factor to scale the residuals (i.e., the output of
101  passing `x` through an inception module) before adding them
102  to the shortcut branch. Let `r` be the output from the residual branch,
103  the output of this block will be `x + scale * r`.
104  block_type: `'block35'`, `'block17'` or `'block8'`, determines
105  the network structure in the residual branch.
106  block_idx: an `int` used for generating layer names. The Inception-ResNet blocks
107  are repeated many times in this network. We use `block_idx` to identify
108  each of the repetitions. For example, the first Inception-ResNet-A block
109  will have `block_type='block35', block_idx=0`, ane the layer names will have
110  a common prefix `'block35_0'`.
111  activation: activation function to use at the end of the block
112  (see [activations](../activations.md)).
113  When `activation=None`, no activation is applied
114  (i.e., "linear" activation: `a(x) = x`).
115  # Returns
116  Output tensor for the block.
117  # Raises
118  ValueError: if `block_type` is not one of `'block35'`,
119  `'block17'` or `'block8'`.
120  """
121  if block_type == 'block35':
122  branch_0 = conv2d_bn(x, 32, 1)
123  branch_1 = conv2d_bn(x, 32, 1)
124  branch_1 = conv2d_bn(branch_1, 32, 3)
125  branch_2 = conv2d_bn(x, 32, 1)
126  branch_2 = conv2d_bn(branch_2, 48, 3)
127  branch_2 = conv2d_bn(branch_2, 64, 3)
128  branches = [branch_0, branch_1, branch_2]
129  elif block_type == 'block17':
130  branch_0 = conv2d_bn(x, 192, 1)
131  branch_1 = conv2d_bn(x, 128, 1)
132  branch_1 = conv2d_bn(branch_1, 160, [1, 7])
133  branch_1 = conv2d_bn(branch_1, 192, [7, 1])
134  branches = [branch_0, branch_1]
135  elif block_type == 'block8':
136  branch_0 = conv2d_bn(x, 192, 1)
137  branch_1 = conv2d_bn(x, 192, 1)
138  branch_1 = conv2d_bn(branch_1, 224, [1, 3])
139  branch_1 = conv2d_bn(branch_1, 256, [3, 1])
140  branches = [branch_0, branch_1]
141  else:
142  raise ValueError('Unknown Inception-ResNet block type. '
143  'Expects "block35", "block17" or "block8", '
144  'but got: ' + str(block_type))
145 
146  block_name = block_type + '_' + str(block_idx)
147  channel_axis = 1 if K.image_data_format() == 'channels_first' else 3
148  mixed = Concatenate(axis=channel_axis, name=block_name + '_mixed')(branches)
149  up = conv2d_bn(mixed,
150  K.int_shape(x)[channel_axis],
151  1,
152  activation=None,
153  use_bias=True,
154  name=block_name + '_conv')
155 
156  x = Lambda(lambda inputs, scale: inputs[0] + inputs[1] * scale,
157  output_shape=K.int_shape(x)[1:],
158  arguments={'scale': scale},
159  name=block_name)([x, up])
160  if activation is not None:
161  x = Activation(activation, name=block_name + '_ac')(x)
162 
163  # squeeze and excite block
164  x = squeeze_excite_block(x)
165  return x
166 
167 
def squeeze_excite_block(input, ratio=16)
Definition: se.py:5
def inception_resnet_block(x, scale, block_type, block_idx, activation='relu')
def conv2d_bn(x, filters, kernel_size, strides=1, padding='same', activation='relu', use_bias=False, name=None)
static QCString str
def se_inception_resnet_v2.preprocess_input (   x)
Preprocesses a numpy array encoding a batch of images.
# Arguments
    x: a 4D numpy array consists of RGB values within [0, 255].
# Returns
    Preprocessed array.

Definition at line 43 of file se_inception_resnet_v2.py.

44  """Preprocesses a numpy array encoding a batch of images.
45  # Arguments
46  x: a 4D numpy array consists of RGB values within [0, 255].
47  # Returns
48  Preprocessed array.
49  """
50  return imagenet_utils.preprocess_input(x, mode='tf')
51 
52 
def preprocess_input(x, data_format=None, mode='caffe')
def se_inception_resnet_v2.SEInceptionResNetV2 (   include_top = True,
  weights = None,
  input_tensor = None,
  input_shape = None,
  pooling = None,
  classes = 1000 
)
Instantiates the SE-Inception-ResNet v2 architecture.
Optionally loads weights pre-trained on ImageNet.
Note that when using TensorFlow, for best performance you should
set `"image_data_format": "channels_last"` in your Keras config
at `~/.keras/keras.json`.
The model and the weights are compatible with both TensorFlow and Theano
backends (but not CNTK). The data format convention used by the model is
the one specified in your Keras config file.
Note that the default input image size for this model is 299x299, instead
of 224x224 as in the VGG16 and ResNet models. Also, the input preprocessing
function is different (i.e., do not use `imagenet_utils.preprocess_input()`
with this model. Use `preprocess_input()` defined in this module instead).
# 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.
    RuntimeError: If attempting to run this model with an unsupported backend.

Definition at line 173 of file se_inception_resnet_v2.py.

173  classes=1000):
174  """Instantiates the SE-Inception-ResNet v2 architecture.
175  Optionally loads weights pre-trained on ImageNet.
176  Note that when using TensorFlow, for best performance you should
177  set `"image_data_format": "channels_last"` in your Keras config
178  at `~/.keras/keras.json`.
179  The model and the weights are compatible with both TensorFlow and Theano
180  backends (but not CNTK). The data format convention used by the model is
181  the one specified in your Keras config file.
182  Note that the default input image size for this model is 299x299, instead
183  of 224x224 as in the VGG16 and ResNet models. Also, the input preprocessing
184  function is different (i.e., do not use `imagenet_utils.preprocess_input()`
185  with this model. Use `preprocess_input()` defined in this module instead).
186  # Arguments
187  include_top: whether to include the fully-connected
188  layer at the top of the network.
189  weights: one of `None` (random initialization)
190  or `'imagenet'` (pre-training on ImageNet).
191  input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
192  to use as image input for the model.
193  input_shape: optional shape tuple, only to be specified
194  if `include_top` is `False` (otherwise the input shape
195  has to be `(299, 299, 3)` (with `'channels_last'` data format)
196  or `(3, 299, 299)` (with `'channels_first'` data format).
197  It should have exactly 3 inputs channels,
198  and width and height should be no smaller than 139.
199  E.g. `(150, 150, 3)` would be one valid value.
200  pooling: Optional pooling mode for feature extraction
201  when `include_top` is `False`.
202  - `None` means that the output of the model will be
203  the 4D tensor output of the last convolutional layer.
204  - `'avg'` means that global average pooling
205  will be applied to the output of the
206  last convolutional layer, and thus
207  the output of the model will be a 2D tensor.
208  - `'max'` means that global max pooling will be applied.
209  classes: optional number of classes to classify images
210  into, only to be specified if `include_top` is `True`, and
211  if no `weights` argument is specified.
212  # Returns
213  A Keras `Model` instance.
214  # Raises
215  ValueError: in case of invalid argument for `weights`,
216  or invalid input shape.
217  RuntimeError: If attempting to run this model with an unsupported backend.
218  """
219  if K.backend() in {'cntk'}:
220  raise RuntimeError(K.backend() + ' backend is currently unsupported for this model.')
221 
222  if weights not in {'imagenet', None}:
223  raise ValueError('The `weights` argument should be either '
224  '`None` (random initialization) or `imagenet` '
225  '(pre-training on ImageNet).')
226 
227  if weights == 'imagenet' and include_top and classes != 1000:
228  raise ValueError('If using `weights` as imagenet with `include_top`'
229  ' as true, `classes` should be 1000')
230 
231  # Determine proper input shape
232  input_shape = _obtain_input_shape(
233  input_shape,
234  default_size=299,
235  min_size=139,
236  data_format=K.image_data_format(),
237  require_flatten=False,
238  weights=weights)
239 
240  if input_tensor is None:
241  img_input = Input(shape=input_shape)
242  else:
243  if not K.is_keras_tensor(input_tensor):
244  img_input = Input(tensor=input_tensor, shape=input_shape)
245  else:
246  img_input = input_tensor
247 
248  # Stem block: 35 x 35 x 192
249  x = conv2d_bn(img_input, 32, 3, strides=2, padding='valid')
250  x = conv2d_bn(x, 32, 3, padding='valid')
251  x = conv2d_bn(x, 64, 3)
252  x = MaxPooling2D(3, strides=2)(x)
253  x = conv2d_bn(x, 80, 1, padding='valid')
254  x = conv2d_bn(x, 192, 3, padding='valid')
255  x = MaxPooling2D(3, strides=2)(x)
256 
257  # Mixed 5b (Inception-A block): 35 x 35 x 320
258  branch_0 = conv2d_bn(x, 96, 1)
259  branch_1 = conv2d_bn(x, 48, 1)
260  branch_1 = conv2d_bn(branch_1, 64, 5)
261  branch_2 = conv2d_bn(x, 64, 1)
262  branch_2 = conv2d_bn(branch_2, 96, 3)
263  branch_2 = conv2d_bn(branch_2, 96, 3)
264  branch_pool = AveragePooling2D(3, strides=1, padding='same')(x)
265  branch_pool = conv2d_bn(branch_pool, 64, 1)
266  branches = [branch_0, branch_1, branch_2, branch_pool]
267  channel_axis = 1 if K.image_data_format() == 'channels_first' else 3
268  x = Concatenate(axis=channel_axis, name='mixed_5b')(branches)
269 
270  # squeeze and excite block
271  x = squeeze_excite_block(x)
272 
273  # 10x block35 (Inception-ResNet-A block): 35 x 35 x 320
274  for block_idx in range(1, 11):
276  scale=0.17,
277  block_type='block35',
278  block_idx=block_idx)
279 
280  # Mixed 6a (Reduction-A block): 17 x 17 x 1088
281  branch_0 = conv2d_bn(x, 384, 3, strides=2, padding='valid')
282  branch_1 = conv2d_bn(x, 256, 1)
283  branch_1 = conv2d_bn(branch_1, 256, 3)
284  branch_1 = conv2d_bn(branch_1, 384, 3, strides=2, padding='valid')
285  branch_pool = MaxPooling2D(3, strides=2, padding='valid')(x)
286  branches = [branch_0, branch_1, branch_pool]
287  x = Concatenate(axis=channel_axis, name='mixed_6a')(branches)
288 
289  # squeeze and excite block
290  x = squeeze_excite_block(x)
291 
292  # 20x block17 (Inception-ResNet-B block): 17 x 17 x 1088
293  for block_idx in range(1, 21):
295  scale=0.1,
296  block_type='block17',
297  block_idx=block_idx)
298 
299  # Mixed 7a (Reduction-B block): 8 x 8 x 2080
300  branch_0 = conv2d_bn(x, 256, 1)
301  branch_0 = conv2d_bn(branch_0, 384, 3, strides=2, padding='valid')
302  branch_1 = conv2d_bn(x, 256, 1)
303  branch_1 = conv2d_bn(branch_1, 288, 3, strides=2, padding='valid')
304  branch_2 = conv2d_bn(x, 256, 1)
305  branch_2 = conv2d_bn(branch_2, 288, 3)
306  branch_2 = conv2d_bn(branch_2, 320, 3, strides=2, padding='valid')
307  branch_pool = MaxPooling2D(3, strides=2, padding='valid')(x)
308  branches = [branch_0, branch_1, branch_2, branch_pool]
309  x = Concatenate(axis=channel_axis, name='mixed_7a')(branches)
310 
311  # squeeze and excite block
312  x = squeeze_excite_block(x)
313 
314  # 10x block8 (Inception-ResNet-C block): 8 x 8 x 2080
315  for block_idx in range(1, 10):
317  scale=0.2,
318  block_type='block8',
319  block_idx=block_idx)
321  scale=1.,
322  activation=None,
323  block_type='block8',
324  block_idx=10)
325 
326  # squeeze and excite block
327  x = squeeze_excite_block(x)
328 
329  # Final convolution block: 8 x 8 x 1536
330  x = conv2d_bn(x, 1536, 1, name='conv_7b')
331 
332  if include_top:
333  # Classification block
334  x = GlobalAveragePooling2D(name='avg_pool')(x)
335  x = Dense(classes, activation='softmax', name='predictions')(x)
336  else:
337  if pooling == 'avg':
338  x = GlobalAveragePooling2D()(x)
339  elif pooling == 'max':
340  x = GlobalMaxPooling2D()(x)
341 
342  # Ensure that the model takes into account
343  # any potential predecessors of `input_tensor`
344  if input_tensor is not None:
345  inputs = get_source_inputs(input_tensor)
346  else:
347  inputs = img_input
348 
349  # Create model
350  model = Model(inputs, x, name='se_inception_resnet_v2')
351 
352  return model
353 
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 inception_resnet_block(x, scale, block_type, block_idx, activation='relu')
def conv2d_bn(x, filters, kernel_size, strides=1, padding='same', activation='relu', use_bias=False, name=None)