예제 #1
0
def get_seq_model():
  """Define three channel input shape depending on image data format."""
  if K.image_data_format() == 'channels_first':
    input_shape = (3, img_width, img_height)
  else:
    input_shape = (img_width, img_height, 3)

  # Initialize CNN by creating a sequential model.
  model = Sequential()
  model.add(Conv2D(32, (3, 3), input_shape=input_shape))
  model.add(Activation('relu'))
  model.add(MaxPooling2D(pool_size=(2, 2)))

  model.add(Conv2D(32, (3, 3)))
  model.add(Activation('relu'))
  model.add(MaxPooling2D(pool_size=(2, 2)))

  model.add(Conv2D(64, (3, 3)))
  model.add(Activation('relu'))
  model.add(MaxPooling2D(pool_size=(2, 2)))

  model.add(Flatten())
  model.add(Dense(64))
  model.add(Activation('relu'))
  model.add(Dropout(0.5))
  model.add(Dense(2))
  model.add(Activation('sigmoid'))

  model.compile(
      loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])

  return model
예제 #2
0
def squeeze_excite_block(tensor, ratio=16):
    init = tensor
    channel_axis = 1 if K.image_data_format() == "channels_first" else -1
    filters = init.shape[channel_axis]
    se_shape = (1, 1, filters)

    se = GlobalAveragePooling2D()(init)
    se = Reshape(se_shape)(se)
    se = Dense(filters // ratio,
               kernel_initializer='he_normal',
               use_bias=False)(se)
    se = LeakyReLU(alpha=0.1)(se)
    se = Dense(filters,
               activation='sigmoid',
               kernel_initializer='he_normal',
               use_bias=False)(se)

    if K.image_data_format() == 'channels_first':
        se = Permute((3, 1, 2))(se)

    x = multiply([init, se])
    return x
예제 #3
0
def _handle_data_format():
    global DIM1_AXIS
    global DIM2_AXIS
    global DIM3_AXIS
    global CHANNEL_AXIS
    if K.image_data_format() == 'channels_last':
        DIM1_AXIS = 1
        DIM2_AXIS = 2
        DIM3_AXIS = 3
        CHANNEL_AXIS = 4
    else:
        CHANNEL_AXIS = 1
        DIM1_AXIS = 2
        DIM2_AXIS = 3
        DIM3_AXIS = 4
예제 #4
0
def identity_block_base(input_tensor,
                        kernel_size,
                        filters,
                        stage,
                        block,
                        num_updates,
                        dropout_rate=0.,
                        use_variational_layers=False):
    """The identity block is the block that has no conv layer at shortcut.

  Arguments:
      input_tensor: input tensor
      kernel_size: default 3, the kernel size of
          middle conv layer at main path
      filters: list of integers, the filters of 3 conv layer at main path
      stage: integer, current stage label, used for generating layer names
      block: 'a','b'..., current block label, used for generating layer names
      num_updates: integer, total steps in an epoch (for weighting the loss)
      dropout_rate: float, always-on dropout rate.
      use_variational_layers: boolean, if true train a variational model

  Returns:
      x: Output tensor for the block.
  """
    filters1, filters2, filters3 = filters
    divergence_fn = lambda q, p, ignore: (tfd.kl_divergence(q, p) / num_updates
                                          )
    if backend.image_data_format() == 'channels_last':
        bn_axis = 3
    else:
        bn_axis = 1
    conv_name_base = 'res' + str(stage) + block + '_branch'
    bn_name_base = 'bn' + str(stage) + block + '_branch'
    if not use_variational_layers:
        first_conv_2d = layers.Conv2D(
            filters1, (1, 1),
            use_bias=False,
            kernel_initializer='he_normal',
            kernel_regularizer=regularizers.l2(L2_WEIGHT_DECAY),
            name=conv_name_base + '2a')
        if dropout_rate > 0.:
            x = layers.Dropout(dropout_rate)(input_tensor, training=True)
            x = first_conv_2d(x)
        else:
            x = first_conv_2d(input_tensor)
        x = layers.BatchNormalization(axis=bn_axis,
                                      momentum=BATCH_NORM_DECAY,
                                      epsilon=BATCH_NORM_EPSILON,
                                      name=bn_name_base + '2a')(x)
        x = layers.Activation('relu')(x)
        if dropout_rate > 0.:
            x = layers.Dropout(dropout_rate)(x, training=True)
        x = layers.Conv2D(filters2,
                          kernel_size,
                          use_bias=False,
                          padding='same',
                          kernel_initializer='he_normal',
                          kernel_regularizer=regularizers.l2(L2_WEIGHT_DECAY),
                          name=conv_name_base + '2b')(x)
        x = layers.BatchNormalization(axis=bn_axis,
                                      momentum=BATCH_NORM_DECAY,
                                      epsilon=BATCH_NORM_EPSILON,
                                      name=bn_name_base + '2b')(x)
        x = layers.Activation('relu')(x)
        if dropout_rate > 0.:
            x = layers.Dropout(dropout_rate)(x, training=True)
        x = layers.Conv2D(filters3, (1, 1),
                          use_bias=False,
                          kernel_initializer='he_normal',
                          kernel_regularizer=regularizers.l2(L2_WEIGHT_DECAY),
                          name=conv_name_base + '2c')(x)
        x = layers.BatchNormalization(axis=bn_axis,
                                      momentum=BATCH_NORM_DECAY,
                                      epsilon=BATCH_NORM_EPSILON,
                                      name=bn_name_base + '2c')(x)
    else:
        x = tfpl.Convolution2DFlipout(
            filters1,
            kernel_size=(1, 1),
            padding='SAME',
            name=conv_name_base + '2a',
            kernel_divergence_fn=divergence_fn,
        )(input_tensor)
        x = layers.BatchNormalization(axis=bn_axis,
                                      momentum=BATCH_NORM_DECAY,
                                      epsilon=BATCH_NORM_EPSILON,
                                      name=bn_name_base + '2a')(x)
        x = layers.Activation('relu')(x)
        x = tfpl.Convolution2DFlipout(
            filters2,
            kernel_size=kernel_size,
            padding='SAME',
            activation=None,
            name=conv_name_base + '2b',
            kernel_divergence_fn=divergence_fn,
        )(x)
        x = layers.BatchNormalization(axis=bn_axis,
                                      momentum=BATCH_NORM_DECAY,
                                      epsilon=BATCH_NORM_EPSILON,
                                      name=bn_name_base + '2b')(x)
        x = layers.Activation('relu')(x)

        x = tfpl.Convolution2DFlipout(
            filters3,
            kernel_size=(1, 1),
            padding='SAME',
            activation=None,
            name=conv_name_base + '2c',
            kernel_divergence_fn=divergence_fn,
        )(x)
        x = layers.BatchNormalization(axis=bn_axis,
                                      momentum=BATCH_NORM_DECAY,
                                      epsilon=BATCH_NORM_EPSILON,
                                      name=bn_name_base + '2c')(x)
    x = layers.add([x, input_tensor])
    x = layers.Activation('relu')(x)
    return x
예제 #5
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')
예제 #6
0
    class_mapping['bg'] = len(class_mapping)

class_mapping = {v: k for k, v in class_mapping.items()}
print(class_mapping)
class_to_color = {
    class_mapping[v]: np.random.randint(0, 255, 3)
    for v in class_mapping
}
C.num_rois = int(options.num_rois)

if C.network == 'resnet50':
    num_features = 1024
elif C.network == 'vgg':
    num_features = 512

if K.image_data_format() == 'channels_first':
    input_shape_img = (3, None, None)
    input_shape_features = (num_features, None, None)
else:
    input_shape_img = (None, None, 3)
    input_shape_features = (None, None, num_features)

img_input = Input(shape=input_shape_img)
roi_input = Input(shape=(C.num_rois, 4))
feature_map_input = Input(shape=input_shape_features)

# define the base network (resnet here, can be VGG, Inception, etc)
shared_layers = nn.nn_base(img_input, trainable=True)

# define the RPN, built on the base layers
num_anchors = len(C.anchor_box_scales) * len(C.anchor_box_ratios)