Functions | Variables
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 InceptionResNetV2 (include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000)
 

Variables

string BASE_WEIGHT_URL = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.7/'
 

Function Documentation

def 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`.
    strides: strides in `Conv2D`.
    padding: padding mode in `Conv2D`.
    activation: activation in `Conv2D`.
    use_bias: whether to use a bias 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 69 of file inception_resnet_v2.py.

69  name=None):
70  """Utility function to apply conv + BN.
71 
72  # Arguments
73  x: input tensor.
74  filters: filters in `Conv2D`.
75  kernel_size: kernel size as in `Conv2D`.
76  strides: strides in `Conv2D`.
77  padding: padding mode in `Conv2D`.
78  activation: activation in `Conv2D`.
79  use_bias: whether to use a bias in `Conv2D`.
80  name: name of the ops; will become `name + '_ac'` for the activation
81  and `name + '_bn'` for the batch norm layer.
82 
83  # Returns
84  Output tensor after applying `Conv2D` and `BatchNormalization`.
85  """
86  x = Conv2D(filters,
87  kernel_size,
88  strides=strides,
89  padding=padding,
90  use_bias=use_bias,
91  name=name)(x)
92  if not use_bias:
93  bn_axis = 1 if K.image_data_format() == 'channels_first' else 3
94  bn_name = None if name is None else name + '_bn'
95  x = BatchNormalization(axis=bn_axis, scale=False, name=bn_name)(x)
96  if activation is not None:
97  ac_name = None if name is None else name + '_ac'
98  x = Activation(activation, name=ac_name)(x)
99  return x
100 
101 
def inception_resnet_v2.inception_resnet_block (   x,
  scale,
  block_type,
  block_idx,
  activation = 'relu' 
)
Adds a Inception-ResNet block.

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 102 of file inception_resnet_v2.py.

102 def inception_resnet_block(x, scale, block_type, block_idx, activation='relu'):
103  """Adds a Inception-ResNet block.
104 
105  This function builds 3 types of Inception-ResNet blocks mentioned
106  in the paper, controlled by the `block_type` argument (which is the
107  block name used in the official TF-slim implementation):
108  - Inception-ResNet-A: `block_type='block35'`
109  - Inception-ResNet-B: `block_type='block17'`
110  - Inception-ResNet-C: `block_type='block8'`
111 
112  # Arguments
113  x: input tensor.
114  scale: scaling factor to scale the residuals (i.e., the output of
115  passing `x` through an inception module) before adding them
116  to the shortcut branch. Let `r` be the output from the residual branch,
117  the output of this block will be `x + scale * r`.
118  block_type: `'block35'`, `'block17'` or `'block8'`, determines
119  the network structure in the residual branch.
120  block_idx: an `int` used for generating layer names. The Inception-ResNet blocks
121  are repeated many times in this network. We use `block_idx` to identify
122  each of the repetitions. For example, the first Inception-ResNet-A block
123  will have `block_type='block35', block_idx=0`, ane the layer names will have
124  a common prefix `'block35_0'`.
125  activation: activation function to use at the end of the block
126  (see [activations](../activations.md)).
127  When `activation=None`, no activation is applied
128  (i.e., "linear" activation: `a(x) = x`).
129 
130  # Returns
131  Output tensor for the block.
132 
133  # Raises
134  ValueError: if `block_type` is not one of `'block35'`,
135  `'block17'` or `'block8'`.
136  """
137  if block_type == 'block35':
138  branch_0 = conv2d_bn(x, 32, 1)
139  branch_1 = conv2d_bn(x, 32, 1)
140  branch_1 = conv2d_bn(branch_1, 32, 3)
141  branch_2 = conv2d_bn(x, 32, 1)
142  branch_2 = conv2d_bn(branch_2, 48, 3)
143  branch_2 = conv2d_bn(branch_2, 64, 3)
144  branches = [branch_0, branch_1, branch_2]
145  elif block_type == 'block17':
146  branch_0 = conv2d_bn(x, 192, 1)
147  branch_1 = conv2d_bn(x, 128, 1)
148  branch_1 = conv2d_bn(branch_1, 160, [1, 7])
149  branch_1 = conv2d_bn(branch_1, 192, [7, 1])
150  branches = [branch_0, branch_1]
151  elif block_type == 'block8':
152  branch_0 = conv2d_bn(x, 192, 1)
153  branch_1 = conv2d_bn(x, 192, 1)
154  branch_1 = conv2d_bn(branch_1, 224, [1, 3])
155  branch_1 = conv2d_bn(branch_1, 256, [3, 1])
156  branches = [branch_0, branch_1]
157  else:
158  raise ValueError('Unknown Inception-ResNet block type. '
159  'Expects "block35", "block17" or "block8", '
160  'but got: ' + str(block_type))
161 
162  block_name = block_type + '_' + str(block_idx)
163  channel_axis = 1 if K.image_data_format() == 'channels_first' else 3
164  mixed = Concatenate(axis=channel_axis, name=block_name + '_mixed')(branches)
165  up = conv2d_bn(mixed,
166  K.int_shape(x)[channel_axis],
167  1,
168  activation=None,
169  use_bias=True,
170  name=block_name + '_conv')
171 
172  x = Lambda(lambda inputs, scale: inputs[0] + inputs[1] * scale,
173  output_shape=K.int_shape(x)[1:],
174  arguments={'scale': scale},
175  name=block_name)([x, up])
176  if activation is not None:
177  x = Activation(activation, name=block_name + '_ac')(x)
178  return x
179 
180 
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')
static QCString str
def inception_resnet_v2.InceptionResNetV2 (   include_top = True,
  weights = 'imagenet',
  input_tensor = None,
  input_shape = None,
  pooling = None,
  classes = 1000 
)
Instantiates the 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 TensorFlow, Theano and
CNTK backends. 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),
          'imagenet' (pre-training on ImageNet),
          or the path to the weights file to be loaded.
    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 186 of file inception_resnet_v2.py.

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

51  """Preprocesses a numpy array encoding a batch of images.
52 
53  # Arguments
54  x: a 4D numpy array consists of RGB values within [0, 255].
55 
56  # Returns
57  Preprocessed array.
58  """
59  return imagenet_utils.preprocess_input(x, mode='tf')
60 
61 
def preprocess_input(x, data_format=None, mode='caffe')

Variable Documentation

string inception_resnet_v2.BASE_WEIGHT_URL = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.7/'

Definition at line 47 of file inception_resnet_v2.py.