示例#1
0
def ResNet50(method, num_classes, num_updates, dropout_rate):
    """Instantiates the ResNet50 architecture.

  Args:
    method: `str`, method for accounting for uncertainty. Must be one of
      ['vanilla', 'll_dropout', 'll_svi', 'dropout', 'svi', 'dropout_nofirst']
    num_classes: `int` number of classes for image classification.
    num_updates: integer, total steps in an epoch (for weighting the loss)
    dropout_rate: Dropout rate for ll_dropout, dropout methods.

  Returns:
      A Keras model instance.
  pylint: disable=invalid-name
  """

    # Determine proper input shape
    if backend.image_data_format() == 'channels_first':
        input_shape = (3, 224, 224)
        bn_axis = 1
    else:
        input_shape = (224, 224, 3)
        bn_axis = 3

    if (method in ['dropout', 'll_dropout', 'dropout_nofirst'
                   ]) != (dropout_rate > 0.):
        raise ValueError(
            'Dropout rate should be nonzero iff a dropout method is used.'
            'Method is {}, dropout is {}.'.format(method, dropout_rate))

    use_variational_layers = method == 'svi'
    hidden_layer_dropout = dropout_rate if method in [
        'dropout', 'dropout_nofirst'
    ] else 0.

    img_input = layers.Input(shape=input_shape)
    x = layers.ZeroPadding2D(padding=(3, 3), name='conv1_pad')(img_input)
    if (dropout_rate > 0.) and (method != 'dropout_nofirst'):
        x = layers.Dropout(hidden_layer_dropout)(x, training=True)
    x = layers.Conv2D(64, (7, 7),
                      use_bias=False,
                      strides=(2, 2),
                      padding='valid',
                      kernel_initializer='he_normal',
                      kernel_regularizer=regularizers.l2(L2_WEIGHT_DECAY),
                      name='conv1')(x)
    x = layers.BatchNormalization(axis=bn_axis,
                                  momentum=BATCH_NORM_DECAY,
                                  epsilon=BATCH_NORM_EPSILON,
                                  name='bn_conv1')(x)
    x = layers.Activation('relu')(x)
    x = layers.ZeroPadding2D(padding=(1, 1), name='pool1_pad')(x)
    x = layers.MaxPooling2D((3, 3), strides=(2, 2))(x)

    conv_block = functools.partial(
        conv_block_base,
        num_updates=num_updates,
        dropout_rate=hidden_layer_dropout,
        use_variational_layers=use_variational_layers)
    identity_block = functools.partial(
        identity_block_base,
        num_updates=num_updates,
        dropout_rate=hidden_layer_dropout,
        use_variational_layers=use_variational_layers)

    x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1))
    x = identity_block(x, 3, [64, 64, 256], stage=2, block='b')
    x = identity_block(x, 3, [64, 64, 256], stage=2, block='c')
    x = conv_block(x, 3, [128, 128, 512], stage=3, block='a')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='b')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='c')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='d')

    x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f')

    x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a')
    x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b')
    x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c')

    x = layers.GlobalAveragePooling2D(name='avg_pool')(x)

    if dropout_rate > 0.:
        x = layers.Dropout(dropout_rate)(x, training=True)

    if method in ['ll_svi', 'svi']:

        x = tfpl.dense_variational_v2.DenseVariational(
            units=num_classes,
            make_posterior_fn=posterior_mean_field,
            make_prior_fn=functools.partial(prior_trainable,
                                            num_updates=num_updates),
            use_bias=True,
            kl_weight=1. / num_updates,
            kl_use_exact=True,
            name='fc1000')(x)
    else:
        x = layers.Dense(num_classes,
                         kernel_regularizer=regularizers.l2(L2_WEIGHT_DECAY),
                         bias_regularizer=regularizers.l2(L2_WEIGHT_DECAY),
                         name='fc1000')(x)

    # Create model.
    return models.Model(img_input, x, name='resnet50')
示例#2
0
def stack_layers(inputs, net_layers, kernel_initializer='glorot_uniform'):
  """Builds the architecture of the network by applying each layer specified in net_layers to inputs.

  Args:
    inputs: a dict containing input_types and input_placeholders for each key
      and value pair, respecively.
    net_layers:  a list of dicts containing all layers to be used in the
      network, where each dict describes one such layer. each dict requires the
      key 'type'. all other keys are dependent on the layer type.
    kernel_initializer: initialization configuration passed to keras (see keras
      initializers).

  Returns:
    outputs: a dict formatted in much the same way as inputs. it
      contains input_types and output_tensors for each key and value pair,
      respectively, where output_tensors are the outputs of the
      input_placeholders in inputs after each layer in net_layers is applied.
  """
  outputs = dict()

  for key in inputs:
    outputs[key] = inputs[key]

  for layer in net_layers:
    # check for l2_reg argument
    l2_reg = layer.get('l2_reg')
    if l2_reg:
      l2_reg = l2(layer['l2_reg'])

    # create the layer
    if layer['type'] in [
        'softplus', 'softsign', 'softmax', 'tanh', 'sigmoid', 'relu', 'selu'
    ]:
      l = layers.Dense(
          layer['size'],
          activation=layer['type'],
          kernel_initializer=kernel_initializer,
          kernel_regularizer=l2_reg,
          name=layer.get('name'))
    elif layer['type'] == 'None':
      l = layers.Dense(
          layer['size'],
          kernel_initializer=kernel_initializer,
          kernel_regularizer=l2_reg,
          name=layer.get('name'))
    elif layer['type'] == 'Conv2D':
      l = layers.Conv2D(
          layer['channels'],
          kernel_size=layer['kernel'],
          activation='relu',
          data_format='channels_last',
          kernel_regularizer=l2_reg,
          name=layer.get('name'))
    elif layer['type'] == 'BatchNormalization':
      l = layers.BatchNormalization(name=layer.get('name'))
    elif layer['type'] == 'MaxPooling2D':
      l = layers.MaxPooling2D(
          pool_size=layer['pool_size'],
          data_format='channels_first',
          name=layer.get('name'))
    elif layer['type'] == 'Dropout':
      l = layers.Dropout(layer['rate'], name=layer.get('name'))
    elif layer['type'] == 'Flatten':
      l = layers.Flatten(name=layer.get('name'))
    else:
      raise ValueError("Invalid layer type '{}'".format(layer['type']))

    # apply the layer to each input in inputs
    for k in outputs:
      outputs[k] = l(outputs[k])

  return outputs
示例#3
0
def get_components_layer_for_general_diversity_and_depth_model(
        layer_description, num_filters, group_norm_num_groups, layer_strides):
    """Create a single routed layer.

  Args:
    layer_description: (list of string) description of the layer. Each element
      of the list described one component as a pipe-separated sequence of
      operations. Each operation should be one of the following: `convAxB`,
      `maxpoolAxB`, `avgpoolAxB`, `identity` where `A` and `B` are integers.
    num_filters: (int) number of filters for each convolution.
    group_norm_num_groups: (int) number of groups to use for group
      normalization; has to divide `num_filters`.
    layer_strides: (int) strides for this routed layer. In order to stride
      a routed layer, we stride every component within a layer. Since some
      components contain several operations (e.g. two convolutions), we apply
      the stride to the last operation in each component. To these operations
      we pass the value of `layer_strides` as the `strides` argument.

  Returns:
    A list of `keras.layer.Layer`s, corresponding to components in the routed
    layer.
  """
    components = []

    for component_description in layer_description:
        element_descriptions = component_description.split("|")

        component_elements = []

        for i, element_description in enumerate(element_descriptions):
            if i == len(element_descriptions) - 1:
                strides = layer_strides
            else:
                strides = 1

            if element_description == "identity":
                if strides == 1:
                    component_elements.append(layers.Lambda(lambda x: x))
                else:
                    # In case of strides, apply an additional 1x1 convolution.
                    component_elements.append(
                        layers.Conv2D(filters=num_filters,
                                      kernel_size=1,
                                      padding="same",
                                      strides=strides))
            elif element_description.startswith("conv"):
                kernel_size = parse_kernel_size(
                    element_description[len("conv"):])

                component_elements.append(
                    layers.Conv2D(filters=num_filters,
                                  kernel_size=kernel_size,
                                  padding="same",
                                  strides=strides))

                component_elements.append(
                    GroupNorm(num_groups=group_norm_num_groups))
                component_elements.append(layers.ReLU())
            elif element_description.startswith("maxpool"):
                pool_size = parse_kernel_size(
                    element_description[len("maxpool"):])

                component_elements.append(
                    layers.MaxPooling2D(pool_size=pool_size,
                                        padding="same",
                                        strides=strides))
            elif element_description.startswith("avgpool"):
                pool_size = parse_kernel_size(
                    element_description[len("avgpool"):])

                component_elements.append(
                    layers.AveragePooling2D(pool_size=pool_size,
                                            padding="same",
                                            strides=strides))
            else:
                assert False

        components.append(models.Sequential(component_elements))

    return components
示例#4
0
        test_data = dataset[int(dataset.shape[0] * 0.75):]
        test_data = test_data.reshape(85794, 20, 7)
        test_data = np.expand_dims(test_data, axis=3)
        print(test_data.shape)
        test_data_labels = keras.utils.to_categorical(
            dataset_labels[int(dataset.shape[0] * 0.75):])

        # Init statemnts
        print(tf.__version__)

        # Define model

        model = models.Sequential()
        model.add(layers.InputLayer(input_shape=(20, 7, 1)))
        model.add(layers.Conv2D(32, (3, 2), activation='relu'))
        model.add(layers.MaxPooling2D((2, 1)))
        model.add(layers.Conv2D(64, (2, 1), activation='relu'))
        model.add(layers.MaxPooling2D((2, 1)))
        model.add(layers.Conv2D(128, (2, 1), activation='relu'))
        model.add(layers.Flatten())
        # model.add(layers.Dense(128, activation='relu'))
        # model.add(layers.Dense(32, activation='relu'))
        model.add(layers.Dense(12, activation='softmax'))

        # Init TensorBoard callback for data visualization in real time
        tensorboard = TensorBoard(log_dir="logs/test".format(time()))

        model.compile(optimizer='adam',
                      loss='categorical_crossentropy',
                      metrics=[
                          "accuracy",