Functions | Variables
resnext Namespace Reference

Functions

def ResNext (input_shape=None, depth=29, cardinality=8, width=64, weight_decay=5e-4, include_top=True, weights=None, input_tensor=None, pooling=None, classes=10)
 
def ResNextImageNet (input_shape=None, depth=[3, cardinality=32, width=4, weight_decay=5e-4, include_top=True, weights=None, input_tensor=None, pooling=None, classes=1000)
 
def __initial_conv_block (input, weight_decay=5e-4)
 
def __initial_conv_block_imagenet (input, weight_decay=5e-4)
 
def __grouped_convolution_block (input, grouped_channels, cardinality, strides, weight_decay=5e-4)
 
def __bottleneck_block (input, filters=64, cardinality=8, strides=1, weight_decay=5e-4)
 
def __create_res_next (nb_classes, img_input, include_top, depth=29, cardinality=8, width=4, weight_decay=5e-4, pooling=None)
 
def __create_res_next_imagenet (nb_classes, img_input, include_top, depth, cardinality=32, width=4, weight_decay=5e-4, pooling=None)
 

Variables

string CIFAR_TH_WEIGHTS_PATH = ''
 
string CIFAR_TF_WEIGHTS_PATH = ''
 
string CIFAR_TH_WEIGHTS_PATH_NO_TOP = ''
 
string CIFAR_TF_WEIGHTS_PATH_NO_TOP = ''
 
string IMAGENET_TH_WEIGHTS_PATH = ''
 
string IMAGENET_TF_WEIGHTS_PATH = ''
 
string IMAGENET_TH_WEIGHTS_PATH_NO_TOP = ''
 
string IMAGENET_TF_WEIGHTS_PATH_NO_TOP = ''
 

Detailed Description

ResNeXt models for Keras.
# Reference
- [Aggregated Residual Transformations for Deep Neural Networks](https://arxiv.org/pdf/1611.05431.pdf))

Function Documentation

def resnext.__bottleneck_block (   input,
  filters = 64,
  cardinality = 8,
  strides = 1,
  weight_decay = 5e-4 
)
private
Adds a bottleneck block
Args:
    input: input tensor
    filters: number of output filters
    cardinality: cardinality factor described number of
        grouped convolutions
    strides: performs strided convolution for downsampling if > 1
    weight_decay: weight decay factor
Returns: a keras tensor

Definition at line 380 of file resnext.py.

380 def __bottleneck_block(input, filters=64, cardinality=8, strides=1, weight_decay=5e-4):
381  ''' Adds a bottleneck block
382  Args:
383  input: input tensor
384  filters: number of output filters
385  cardinality: cardinality factor described number of
386  grouped convolutions
387  strides: performs strided convolution for downsampling if > 1
388  weight_decay: weight decay factor
389  Returns: a keras tensor
390  '''
391  init = input
392 
393  grouped_channels = int(filters / cardinality)
394  channel_axis = 1 if K.image_data_format() == 'channels_first' else -1
395 
396  # Check if input number of filters is same as 16 * k, else create convolution2d for this input
397  if K.image_data_format() == 'channels_first':
398  if init._keras_shape[1] != 2 * filters:
399  init = Conv2D(filters * 2, (1, 1), padding='same', strides=(strides, strides),
400  use_bias=False, kernel_initializer='he_normal', kernel_regularizer=l2(weight_decay))(init)
401  init = BatchNormalization(axis=channel_axis)(init)
402  else:
403  if init._keras_shape[-1] != 2 * filters:
404  init = Conv2D(filters * 2, (1, 1), padding='same', strides=(strides, strides),
405  use_bias=False, kernel_initializer='he_normal', kernel_regularizer=l2(weight_decay))(init)
406  init = BatchNormalization(axis=channel_axis)(init)
407 
408  x = Conv2D(filters, (1, 1), padding='same', use_bias=False,
409  kernel_initializer='he_normal', kernel_regularizer=l2(weight_decay))(input)
410  x = BatchNormalization(axis=channel_axis)(x)
411  x = Activation('relu')(x)
412 
413  x = __grouped_convolution_block(x, grouped_channels, cardinality, strides, weight_decay)
414 
415  x = Conv2D(filters * 2, (1, 1), padding='same', use_bias=False, kernel_initializer='he_normal',
416  kernel_regularizer=l2(weight_decay))(x)
417  x = BatchNormalization(axis=channel_axis)(x)
418 
419  x = add([init, x])
420  x = Activation('relu')(x)
421 
422  return x
423 
424 
Coord add(Coord c1, Coord c2)
Definition: restypedef.cpp:23
def __bottleneck_block(input, filters=64, cardinality=8, strides=1, weight_decay=5e-4)
Definition: resnext.py:380
def __grouped_convolution_block(input, grouped_channels, cardinality, strides, weight_decay=5e-4)
Definition: resnext.py:340
def resnext.__create_res_next (   nb_classes,
  img_input,
  include_top,
  depth = 29,
  cardinality = 8,
  width = 4,
  weight_decay = 5e-4,
  pooling = None 
)
private
Creates a ResNeXt model with specified parameters
Args:
    nb_classes: Number of output classes
    img_input: Input tensor or layer
    include_top: Flag to include the last dense layer
    depth: Depth of the network. Can be an positive integer or a list
           Compute N = (n - 2) / 9.
           For a depth of 56, n = 56, N = (56 - 2) / 9 = 6
           For a depth of 101, n = 101, N = (101 - 2) / 9 = 11
    cardinality: the size of the set of transformations.
           Increasing cardinality improves classification accuracy,
    width: Width of the network.
    weight_decay: weight_decay (l2 norm)
    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.
Returns: a Keras Model

Definition at line 426 of file resnext.py.

426  weight_decay=5e-4, pooling=None):
427  ''' Creates a ResNeXt model with specified parameters
428  Args:
429  nb_classes: Number of output classes
430  img_input: Input tensor or layer
431  include_top: Flag to include the last dense layer
432  depth: Depth of the network. Can be an positive integer or a list
433  Compute N = (n - 2) / 9.
434  For a depth of 56, n = 56, N = (56 - 2) / 9 = 6
435  For a depth of 101, n = 101, N = (101 - 2) / 9 = 11
436  cardinality: the size of the set of transformations.
437  Increasing cardinality improves classification accuracy,
438  width: Width of the network.
439  weight_decay: weight_decay (l2 norm)
440  pooling: Optional pooling mode for feature extraction
441  when `include_top` is `False`.
442  - `None` means that the output of the model will be
443  the 4D tensor output of the
444  last convolutional layer.
445  - `avg` means that global average pooling
446  will be applied to the output of the
447  last convolutional layer, and thus
448  the output of the model will be a 2D tensor.
449  - `max` means that global max pooling will
450  be applied.
451  Returns: a Keras Model
452  '''
453 
454  if type(depth) is list or type(depth) is tuple:
455  # If a list is provided, defer to user how many blocks are present
456  N = list(depth)
457  else:
458  # Otherwise, default to 3 blocks each of default number of group convolution blocks
459  N = [(depth - 2) // 9 for _ in range(3)]
460 
461  filters = cardinality * width
462  filters_list = []
463 
464  for i in range(len(N)):
465  filters_list.append(filters)
466  filters *= 2 # double the size of the filters
467 
468  x = __initial_conv_block(img_input, weight_decay)
469 
470  # block 1 (no pooling)
471  for i in range(N[0]):
472  x = __bottleneck_block(x, filters_list[0], cardinality, strides=1, weight_decay=weight_decay)
473 
474  N = N[1:] # remove the first block from block definition list
475  filters_list = filters_list[1:] # remove the first filter from the filter list
476 
477  # block 2 to N
478  for block_idx, n_i in enumerate(N):
479  for i in range(n_i):
480  if i == 0:
481  x = __bottleneck_block(x, filters_list[block_idx], cardinality, strides=2,
482  weight_decay=weight_decay)
483  else:
484  x = __bottleneck_block(x, filters_list[block_idx], cardinality, strides=1,
485  weight_decay=weight_decay)
486 
487  if include_top:
488  x = GlobalAveragePooling2D()(x)
489  x = Dense(nb_classes, use_bias=False, kernel_regularizer=l2(weight_decay),
490  kernel_initializer='he_normal', activation='softmax')(x)
491  else:
492  if pooling == 'avg':
493  x = GlobalAveragePooling2D()(x)
494  elif pooling == 'max':
495  x = GlobalMaxPooling2D()(x)
496 
497  return x
498 
499 
def __initial_conv_block(input, weight_decay=5e-4)
Definition: resnext.py:304
auto enumerate(Iterables &&...iterables)
Range-for loop helper tracking the number of iteration.
Definition: enumerate.h:69
def __bottleneck_block(input, filters=64, cardinality=8, strides=1, weight_decay=5e-4)
Definition: resnext.py:380
def resnext.__create_res_next_imagenet (   nb_classes,
  img_input,
  include_top,
  depth,
  cardinality = 32,
  width = 4,
  weight_decay = 5e-4,
  pooling = None 
)
private
Creates a ResNeXt model with specified parameters
Args:
    nb_classes: Number of output classes
    img_input: Input tensor or layer
    include_top: Flag to include the last dense layer
    depth: Depth of the network. List of integers.
           Increasing cardinality improves classification accuracy,
    width: Width of the network.
    weight_decay: weight_decay (l2 norm)
    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.
Returns: a Keras Model

Definition at line 501 of file resnext.py.

501  weight_decay=5e-4, pooling=None):
502  ''' Creates a ResNeXt model with specified parameters
503  Args:
504  nb_classes: Number of output classes
505  img_input: Input tensor or layer
506  include_top: Flag to include the last dense layer
507  depth: Depth of the network. List of integers.
508  Increasing cardinality improves classification accuracy,
509  width: Width of the network.
510  weight_decay: weight_decay (l2 norm)
511  pooling: Optional pooling mode for feature extraction
512  when `include_top` is `False`.
513  - `None` means that the output of the model will be
514  the 4D tensor output of the
515  last convolutional layer.
516  - `avg` means that global average pooling
517  will be applied to the output of the
518  last convolutional layer, and thus
519  the output of the model will be a 2D tensor.
520  - `max` means that global max pooling will
521  be applied.
522  Returns: a Keras Model
523  '''
524 
525  if type(depth) is list or type(depth) is tuple:
526  # If a list is provided, defer to user how many blocks are present
527  N = list(depth)
528  else:
529  # Otherwise, default to 3 blocks each of default number of group convolution blocks
530  N = [(depth - 2) // 9 for _ in range(3)]
531 
532  filters = cardinality * width
533  filters_list = []
534 
535  for i in range(len(N)):
536  filters_list.append(filters)
537  filters *= 2 # double the size of the filters
538 
539  x = __initial_conv_block_imagenet(img_input, weight_decay)
540 
541  # block 1 (no pooling)
542  for i in range(N[0]):
543  x = __bottleneck_block(x, filters_list[0], cardinality, strides=1, weight_decay=weight_decay)
544 
545  N = N[1:] # remove the first block from block definition list
546  filters_list = filters_list[1:] # remove the first filter from the filter list
547 
548  # block 2 to N
549  for block_idx, n_i in enumerate(N):
550  for i in range(n_i):
551  if i == 0:
552  x = __bottleneck_block(x, filters_list[block_idx], cardinality, strides=2,
553  weight_decay=weight_decay)
554  else:
555  x = __bottleneck_block(x, filters_list[block_idx], cardinality, strides=1,
556  weight_decay=weight_decay)
557 
558  if include_top:
559  x = GlobalAveragePooling2D()(x)
560  x = Dense(nb_classes, use_bias=False, kernel_regularizer=l2(weight_decay),
561  kernel_initializer='he_normal', activation='softmax')(x)
562  else:
563  if pooling == 'avg':
564  x = GlobalAveragePooling2D()(x)
565  elif pooling == 'max':
566  x = GlobalMaxPooling2D()(x)
567 
568  return x
569 
570 '''
571 if __name__ == '__main__':
572  model = ResNext((32, 32, 3), depth=29, cardinality=8, width=64)
573  model.summary()
574 '''
575 
auto enumerate(Iterables &&...iterables)
Range-for loop helper tracking the number of iteration.
Definition: enumerate.h:69
def __bottleneck_block(input, filters=64, cardinality=8, strides=1, weight_decay=5e-4)
Definition: resnext.py:380
def __initial_conv_block_imagenet(input, weight_decay=5e-4)
Definition: resnext.py:321
def resnext.__grouped_convolution_block (   input,
  grouped_channels,
  cardinality,
  strides,
  weight_decay = 5e-4 
)
private
Adds a grouped convolution block. It is an equivalent block from the paper
Args:
    input: input tensor
    grouped_channels: grouped number of filters
    cardinality: cardinality factor describing the number of groups
    strides: performs strided convolution for downscaling if > 1
    weight_decay: weight decay term
Returns: a keras tensor

Definition at line 340 of file resnext.py.

340 def __grouped_convolution_block(input, grouped_channels, cardinality, strides, weight_decay=5e-4):
341  ''' Adds a grouped convolution block. It is an equivalent block from the paper
342  Args:
343  input: input tensor
344  grouped_channels: grouped number of filters
345  cardinality: cardinality factor describing the number of groups
346  strides: performs strided convolution for downscaling if > 1
347  weight_decay: weight decay term
348  Returns: a keras tensor
349  '''
350  init = input
351  channel_axis = 1 if K.image_data_format() == 'channels_first' else -1
352 
353  group_list = []
354 
355  if cardinality == 1:
356  # with cardinality 1, it is a standard convolution
357  x = Conv2D(grouped_channels, (3, 3), padding='same', use_bias=False, strides=(strides, strides),
358  kernel_initializer='he_normal', kernel_regularizer=l2(weight_decay))(init)
359  x = BatchNormalization(axis=channel_axis)(x)
360  x = Activation('relu')(x)
361  return x
362 
363  for c in range(cardinality):
364  x = Lambda(lambda z: z[:, :, :, c * grouped_channels:(c + 1) * grouped_channels]
365  if K.image_data_format() == 'channels_last' else
366  lambda z: z[:, c * grouped_channels:(c + 1) * grouped_channels, :, :])(input)
367 
368  x = Conv2D(grouped_channels, (3, 3), padding='same', use_bias=False, strides=(strides, strides),
369  kernel_initializer='he_normal', kernel_regularizer=l2(weight_decay))(x)
370 
371  group_list.append(x)
372 
373  group_merge = concatenate(group_list, axis=channel_axis)
374  x = BatchNormalization(axis=channel_axis)(group_merge)
375  x = Activation('relu')(x)
376 
377  return x
378 
379 
std::string concatenate(H const &h, T const &...t)
Definition: select.h:138
def __grouped_convolution_block(input, grouped_channels, cardinality, strides, weight_decay=5e-4)
Definition: resnext.py:340
def resnext.__initial_conv_block (   input,
  weight_decay = 5e-4 
)
private
Adds an initial convolution block, with batch normalization and relu activation
Args:
    input: input tensor
    weight_decay: weight decay factor
Returns: a keras tensor

Definition at line 304 of file resnext.py.

304 def __initial_conv_block(input, weight_decay=5e-4):
305  ''' Adds an initial convolution block, with batch normalization and relu activation
306  Args:
307  input: input tensor
308  weight_decay: weight decay factor
309  Returns: a keras tensor
310  '''
311  channel_axis = 1 if K.image_data_format() == 'channels_first' else -1
312 
313  x = Conv2D(64, (3, 3), padding='same', use_bias=False, kernel_initializer='he_normal',
314  kernel_regularizer=l2(weight_decay))(input)
315  x = BatchNormalization(axis=channel_axis)(x)
316  x = Activation('relu')(x)
317 
318  return x
319 
320 
def __initial_conv_block(input, weight_decay=5e-4)
Definition: resnext.py:304
def resnext.__initial_conv_block_imagenet (   input,
  weight_decay = 5e-4 
)
private
Adds an initial conv block, with batch norm and relu for the inception resnext
Args:
    input: input tensor
    weight_decay: weight decay factor
Returns: a keras tensor

Definition at line 321 of file resnext.py.

321 def __initial_conv_block_imagenet(input, weight_decay=5e-4):
322  ''' Adds an initial conv block, with batch norm and relu for the inception resnext
323  Args:
324  input: input tensor
325  weight_decay: weight decay factor
326  Returns: a keras tensor
327  '''
328  channel_axis = 1 if K.image_data_format() == 'channels_first' else -1
329 
330  x = Conv2D(64, (7, 7), padding='same', use_bias=False, kernel_initializer='he_normal',
331  kernel_regularizer=l2(weight_decay), strides=(2, 2))(input)
332  x = BatchNormalization(axis=channel_axis)(x)
333  x = Activation('relu')(x)
334 
335  x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)
336 
337  return x
338 
339 
def __initial_conv_block_imagenet(input, weight_decay=5e-4)
Definition: resnext.py:321
def resnext.ResNext (   input_shape = None,
  depth = 29,
  cardinality = 8,
  width = 64,
  weight_decay = 5e-4,
  include_top = True,
  weights = None,
  input_tensor = None,
  pooling = None,
  classes = 10 
)
Instantiate the ResNeXt architecture. 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 are compatible with both
    TensorFlow and Theano. The dimension ordering
    convention used by the model is the one
    specified in your Keras config file.
    # Arguments
        depth: number or layers in the ResNeXt model. Can be an
            integer or a list of integers.
        cardinality: the size of the set of transformations
        width: multiplier to the ResNeXt width (number of filters)
        weight_decay: weight decay (l2 norm)
        include_top: whether to include the fully-connected
            layer at the top of the network.
        weights: `None` (random initialization)
        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 `(32, 32, 3)` (with `tf` dim ordering)
            or `(3, 32, 32)` (with `th` dim ordering).
            It should have exactly 3 inputs channels,
            and width and height should be no smaller than 8.
            E.g. `(200, 200, 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.

Definition at line 39 of file resnext.py.

39  pooling=None, classes=10):
40  """Instantiate the ResNeXt architecture. Note that ,
41  when using TensorFlow for best performance you should set
42  `image_data_format="channels_last"` in your Keras config
43  at ~/.keras/keras.json.
44  The model are compatible with both
45  TensorFlow and Theano. The dimension ordering
46  convention used by the model is the one
47  specified in your Keras config file.
48  # Arguments
49  depth: number or layers in the ResNeXt model. Can be an
50  integer or a list of integers.
51  cardinality: the size of the set of transformations
52  width: multiplier to the ResNeXt width (number of filters)
53  weight_decay: weight decay (l2 norm)
54  include_top: whether to include the fully-connected
55  layer at the top of the network.
56  weights: `None` (random initialization)
57  input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
58  to use as image input for the model.
59  input_shape: optional shape tuple, only to be specified
60  if `include_top` is False (otherwise the input shape
61  has to be `(32, 32, 3)` (with `tf` dim ordering)
62  or `(3, 32, 32)` (with `th` dim ordering).
63  It should have exactly 3 inputs channels,
64  and width and height should be no smaller than 8.
65  E.g. `(200, 200, 3)` would be one valid value.
66  pooling: Optional pooling mode for feature extraction
67  when `include_top` is `False`.
68  - `None` means that the output of the model will be
69  the 4D tensor output of the
70  last convolutional layer.
71  - `avg` means that global average pooling
72  will be applied to the output of the
73  last convolutional layer, and thus
74  the output of the model will be a 2D tensor.
75  - `max` means that global max pooling will
76  be applied.
77  classes: optional number of classes to classify images
78  into, only to be specified if `include_top` is True, and
79  if no `weights` argument is specified.
80  # Returns
81  A Keras model instance.
82  """
83 
84  if weights not in {'cifar10', None}:
85  raise ValueError('The `weights` argument should be either '
86  '`None` (random initialization) or `cifar10` '
87  '(pre-training on CIFAR-10).')
88 
89  if weights == 'cifar10' and include_top and classes != 10:
90  raise ValueError('If using `weights` as CIFAR 10 with `include_top`'
91  ' as true, `classes` should be 10')
92 
93  if type(depth) == int:
94  if (depth - 2) % 9 != 0:
95  raise ValueError('Depth of the network must be such that (depth - 2)'
96  'should be divisible by 9.')
97 
98  # Determine proper input shape
99  input_shape = _obtain_input_shape(input_shape,
100  default_size=32,
101  min_size=8,
102  data_format=K.image_data_format(),
103  require_flatten=include_top)
104 
105  if input_tensor is None:
106  img_input = Input(shape=input_shape)
107  else:
108  if not K.is_keras_tensor(input_tensor):
109  img_input = Input(tensor=input_tensor, shape=input_shape)
110  else:
111  img_input = input_tensor
112 
113  x = __create_res_next(classes, img_input, include_top, depth, cardinality, width,
114  weight_decay, pooling)
115 
116  # Ensure that the model takes into account
117  # any potential predecessors of `input_tensor`.
118  if input_tensor is not None:
119  inputs = get_source_inputs(input_tensor)
120  else:
121  inputs = img_input
122  # Create model.
123  model = Model(inputs, x, name='resnext')
124 
125  # load weights
126  if weights == 'cifar10':
127  if (depth == 29) and (cardinality == 8) and (width == 64):
128  # Default parameters match. Weights for this model exist:
129 
130  if K.image_data_format() == 'channels_first':
131  if include_top:
132  weights_path = get_file('resnext_cifar_10_8_64_th_dim_ordering_th_kernels.h5',
133  CIFAR_TH_WEIGHTS_PATH,
134  cache_subdir='models')
135  else:
136  weights_path = get_file('resnext_cifar_10_8_64_th_dim_ordering_th_kernels_no_top.h5',
137  CIFAR_TH_WEIGHTS_PATH_NO_TOP,
138  cache_subdir='models')
139 
140  model.load_weights(weights_path)
141 
142  if K.backend() == 'tensorflow':
143  warnings.warn('You are using the TensorFlow backend, yet you '
144  'are using the Theano '
145  'image dimension ordering convention '
146  '(`image_dim_ordering="th"`). '
147  'For best performance, set '
148  '`image_dim_ordering="tf"` in '
149  'your Keras config '
150  'at ~/.keras/keras.json.')
151  convert_all_kernels_in_model(model)
152  else:
153  if include_top:
154  weights_path = get_file('resnext_cifar_10_8_64_tf_dim_ordering_tf_kernels.h5',
155  CIFAR_TF_WEIGHTS_PATH,
156  cache_subdir='models')
157  else:
158  weights_path = get_file('resnext_cifar_10_8_64_tf_dim_ordering_tf_kernels_no_top.h5',
159  CIFAR_TF_WEIGHTS_PATH_NO_TOP,
160  cache_subdir='models')
161 
162  model.load_weights(weights_path)
163 
164  if K.backend() == 'theano':
165  convert_all_kernels_in_model(model)
166 
167  return model
168 
169 
def _obtain_input_shape(input_shape, default_size, min_size, data_format, require_flatten, weights=None)
def __create_res_next(nb_classes, img_input, include_top, depth=29, cardinality=8, width=4, weight_decay=5e-4, pooling=None)
Definition: resnext.py:426
def resnext.ResNextImageNet (   input_shape = None,
  depth = [3,
  cardinality = 32,
  width = 4,
  weight_decay = 5e-4,
  include_top = True,
  weights = None,
  input_tensor = None,
  pooling = None,
  classes = 1000 
)
Instantiate the ResNeXt architecture for the ImageNet dataset. 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 are compatible with both
    TensorFlow and Theano. The dimension ordering
    convention used by the model is the one
    specified in your Keras config file.
    # Arguments
        depth: number or layers in the each block, defined as a list.
            ResNeXt-50 can be defined as [3, 4, 6, 3].
            ResNeXt-101 can be defined as [3, 4, 23, 3].
            Defaults is ResNeXt-50.
        cardinality: the size of the set of transformations
        width: multiplier to the ResNeXt width (number of filters)
        weight_decay: weight decay (l2 norm)
        include_top: whether to include the fully-connected
            layer at the top of the network.
        weights: `None` (random initialization) or `imagenet` (trained
            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 `(224, 224, 3)` (with `tf` dim ordering)
            or `(3, 224, 224)` (with `th` dim ordering).
            It should have exactly 3 inputs channels,
            and width and height should be no smaller than 8.
            E.g. `(200, 200, 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.

Definition at line 172 of file resnext.py.

172  pooling=None, classes=1000):
173  """ Instantiate the ResNeXt architecture for the ImageNet dataset. Note that ,
174  when using TensorFlow for best performance you should set
175  `image_data_format="channels_last"` in your Keras config
176  at ~/.keras/keras.json.
177  The model are compatible with both
178  TensorFlow and Theano. The dimension ordering
179  convention used by the model is the one
180  specified in your Keras config file.
181  # Arguments
182  depth: number or layers in the each block, defined as a list.
183  ResNeXt-50 can be defined as [3, 4, 6, 3].
184  ResNeXt-101 can be defined as [3, 4, 23, 3].
185  Defaults is ResNeXt-50.
186  cardinality: the size of the set of transformations
187  width: multiplier to the ResNeXt width (number of filters)
188  weight_decay: weight decay (l2 norm)
189  include_top: whether to include the fully-connected
190  layer at the top of the network.
191  weights: `None` (random initialization) or `imagenet` (trained
192  on ImageNet)
193  input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
194  to use as image input for the model.
195  input_shape: optional shape tuple, only to be specified
196  if `include_top` is False (otherwise the input shape
197  has to be `(224, 224, 3)` (with `tf` dim ordering)
198  or `(3, 224, 224)` (with `th` dim ordering).
199  It should have exactly 3 inputs channels,
200  and width and height should be no smaller than 8.
201  E.g. `(200, 200, 3)` would be one valid value.
202  pooling: Optional pooling mode for feature extraction
203  when `include_top` is `False`.
204  - `None` means that the output of the model will be
205  the 4D tensor output of the
206  last convolutional layer.
207  - `avg` means that global average pooling
208  will be applied to the output of the
209  last convolutional layer, and thus
210  the output of the model will be a 2D tensor.
211  - `max` means that global max pooling will
212  be applied.
213  classes: optional number of classes to classify images
214  into, only to be specified if `include_top` is True, and
215  if no `weights` argument is specified.
216  # Returns
217  A Keras model instance.
218  """
219 
220  if weights not in {'imagenet', None}:
221  raise ValueError('The `weights` argument should be either '
222  '`None` (random initialization) or `imagenet` '
223  '(pre-training on ImageNet).')
224 
225  if weights == 'imagenet' and include_top and classes != 1000:
226  raise ValueError('If using `weights` as imagenet with `include_top`'
227  ' as true, `classes` should be 1000')
228 
229  if type(depth) == int and (depth - 2) % 9 != 0:
230  raise ValueError('Depth of the network must be such that (depth - 2)'
231  'should be divisible by 9.')
232  # Determine proper input shape
233  input_shape = _obtain_input_shape(input_shape,
234  default_size=224,
235  min_size=112,
236  data_format=K.image_data_format(),
237  require_flatten=include_top)
238 
239  if input_tensor is None:
240  img_input = Input(shape=input_shape)
241  else:
242  if not K.is_keras_tensor(input_tensor):
243  img_input = Input(tensor=input_tensor, shape=input_shape)
244  else:
245  img_input = input_tensor
246 
247  x = __create_res_next_imagenet(classes, img_input, include_top, depth, cardinality, width,
248  weight_decay, pooling)
249 
250  # Ensure that the model takes into account
251  # any potential predecessors of `input_tensor`.
252  if input_tensor is not None:
253  inputs = get_source_inputs(input_tensor)
254  else:
255  inputs = img_input
256  # Create model.
257  model = Model(inputs, x, name='resnext')
258 
259  # load weights
260  if weights == 'imagenet':
261  if (depth == [3, 4, 6, 3]) and (cardinality == 32) and (width == 4):
262  # Default parameters match. Weights for this model exist:
263 
264  if K.image_data_format() == 'channels_first':
265  if include_top:
266  weights_path = get_file('resnext_imagenet_32_4_th_dim_ordering_th_kernels.h5',
267  IMAGENET_TH_WEIGHTS_PATH,
268  cache_subdir='models')
269  else:
270  weights_path = get_file('resnext_imagenet_32_4_th_dim_ordering_th_kernels_no_top.h5',
271  IMAGENET_TH_WEIGHTS_PATH_NO_TOP,
272  cache_subdir='models')
273 
274  model.load_weights(weights_path)
275 
276  if K.backend() == 'tensorflow':
277  warnings.warn('You are using the TensorFlow backend, yet you '
278  'are using the Theano '
279  'image dimension ordering convention '
280  '(`image_dim_ordering="th"`). '
281  'For best performance, set '
282  '`image_dim_ordering="tf"` in '
283  'your Keras config '
284  'at ~/.keras/keras.json.')
285  convert_all_kernels_in_model(model)
286  else:
287  if include_top:
288  weights_path = get_file('resnext_imagenet_32_4_tf_dim_ordering_tf_kernels.h5',
289  IMAGENET_TF_WEIGHTS_PATH,
290  cache_subdir='models')
291  else:
292  weights_path = get_file('resnext_imagenet_32_4_tf_dim_ordering_tf_kernels_no_top.h5',
293  IMAGENET_TF_WEIGHTS_PATH_NO_TOP,
294  cache_subdir='models')
295 
296  model.load_weights(weights_path)
297 
298  if K.backend() == 'theano':
299  convert_all_kernels_in_model(model)
300 
301  return model
302 
303 
def __create_res_next_imagenet(nb_classes, img_input, include_top, depth, cardinality=32, width=4, weight_decay=5e-4, pooling=None)
Definition: resnext.py:501
def _obtain_input_shape(input_shape, default_size, min_size, data_format, require_flatten, weights=None)

Variable Documentation

string resnext.CIFAR_TF_WEIGHTS_PATH = ''

Definition at line 27 of file resnext.py.

string resnext.CIFAR_TF_WEIGHTS_PATH_NO_TOP = ''

Definition at line 29 of file resnext.py.

string resnext.CIFAR_TH_WEIGHTS_PATH = ''

Definition at line 26 of file resnext.py.

string resnext.CIFAR_TH_WEIGHTS_PATH_NO_TOP = ''

Definition at line 28 of file resnext.py.

string resnext.IMAGENET_TF_WEIGHTS_PATH = ''

Definition at line 32 of file resnext.py.

string resnext.IMAGENET_TF_WEIGHTS_PATH_NO_TOP = ''

Definition at line 34 of file resnext.py.

string resnext.IMAGENET_TH_WEIGHTS_PATH = ''

Definition at line 31 of file resnext.py.

string resnext.IMAGENET_TH_WEIGHTS_PATH_NO_TOP = ''

Definition at line 33 of file resnext.py.