def _build_model(self):
     backbone_model = self._get_backbone_model()
     assert backbone_model is not None, f'backbone should be one of {list(_FEATURE_LAYERS.keys())}'
     x = layers.concatenate([layers.GlobalAvgPool2D()(backbone_model.output),
                             layers.GlobalMaxPool2D()(backbone_model.output)])
     o = layers.Dense(self.n_classes, activation='sigmoid', name='classification_output')(x)
     self.model = models.Model(inputs=backbone_model.input, outputs=o)
Ejemplo n.º 2
0
def global_max(x, params):
    """
    Global max pooling
    :param x: input tensor
    :param params: {dict} hyperparams (sub-selection)
    :return: output tensor
    """
    return layers.GlobalMaxPool2D()(x)
def objloc():
    Xinp = L.Input(shape=(120, 160, 3))
    X = L.Conv2D(32, 3, padding='same', activation='relu')(Xinp)
    X = L.MaxPooling2D()(X)   #60, 80
    X = L.Conv2D(64, 3, padding='same', activation='relu')(X)
    X = L.MaxPooling2D()(X)   #30, 40
    X = L.Conv2D(128, 3, padding='same', activation='relu')(X)
    X = L.MaxPooling2D()(X)   #15, 20
    X = L.Conv2D(256, 3, padding='same', activation='relu')(X)
    X = L.MaxPooling2D()(X)   #7, 10
    X = L.Conv2D(512, 3, padding='same', activation='relu')(X)
    X = L.MaxPooling2D()(X)   #3, 5
    X = L.Conv2D(1024, 3, padding='same', activation='relu')(X)
    X = L.GlobalMaxPool2D()(X)
    X = L.Dropout(0.2)(X)
    X = L.Dense(4096, activation='relu')(X)
    X = L.Dropout(0.4)(X)
    X = L.Dense(2048, activation='relu')(X)
    X = L.Dense(4, activation='relu')(X)
    model = Model(inputs=Xinp, outputs=X)
    return model
Ejemplo n.º 4
0
        def get_stream(name, shape, stream_type):
            input_ = KL.Input(shape=shape, name=name)
            last_ = input_

            last_ = KL.GaussianNoise(0.025)(last_)

            if stream_type == 'Conv2D':
                for conv in range(self.conv2_num):
                    last_ = KL.Conv2D(self.conv2_num_filters,
                                      kernel_size=self.conv2_size)(input_)
                    last_ = KL.BatchNormalization()(last_)
                    last_ = KL.Activation(self.hidden_activation)(last_)
                last_ = KL.GlobalMaxPool2D()(last_)
            elif stream_type == 'Conv1D':
                for conv in range(self.conv1_num):
                    for _ in range(self.conv1_cons_num):
                        last_ = KL.Conv1D(self.conv1_num_filters,
                                          kernel_size=self.conv1_size)(last_)
                        last_ = KL.Activation(self.hidden_activation)(last_)
                    # last_ = KL.MaxPooling1D()(last_)
                last_ = KL.Flatten()(last_)
            elif stream_type == 'Norm':
                last_ = KL.BatchNormalization()(input_)
                # last_ = KL.Dense(10, activation='linear')(last_)
                last_ = KL.Activation(self.hidden_activation)(last_)
            elif stream_type == 'LSTM':
                last_ = KL.LSTM(self.conv1_num_filters,
                                activation='tanh',
                                use_bias=True,
                                kernel_regularizer=l2(self.kernel_regularizer),
                                recurrent_regularizer=None,
                                dropout=0.0,
                                recurrent_dropout=0.0,
                                return_sequences=False,
                                return_state=False)(last_)
            else:
                raise NotImplementedError

            return input_, last_
             layers.Conv2D(layer['filters'],
                           kernel_size=tuple(layer['kernel_size']),
                           activation=layer['activation'],
                           padding=layer['padding']))
     if layer['layer'] == 'MaxPooling2D':
         model.add(
             layers.MaxPooling2D(pool_size=tuple(layer['pool_size']),
                                 strides=layer['stride']))
     if layer['layer'] == 'AveragePooling2D':
         model.add(
             layers.AveragePooling2D(pool_size=tuple(layer['pool_size']),
                                     strides=layer['stride']))
     if layer['layer'] == 'GlobalAveragePooling2D':
         model.add(layers.GlobalAveragePooling2D())
     if layer['layer'] == 'GlobalMaxPool2D':
         model.add(layers.GlobalMaxPool2D())
     if layer['layer'] == 'Dropout':
         model.add(layers.Dropout(layer['rate'], seed=layer['seed']))
     if layer['layer'] == 'Dense':
         model.add(
             layers.Dense(layer['unit'], activation=layer['activation']))
     if layer['layer'] == 'Flatten':
         model.add(layers.Flatten())
 model.add(layers.Dense(len(labels), activation='softmax'))
 # compile
 compiler = config['compiler']
 model.compile(loss=compiler['loss'],
               optimizer=compiler['optimizer'],
               metrics=compiler['metrics'])
 # print summary
 print(model.summary())
Ejemplo n.º 6
0
def build_model(input_shape, num_classes):
    """Instantiates the ResNet50 architecture.

    Optionally loads weights pre-trained on ImageNet.
    Note that the data format convention used by the model is
    the one specified in your Keras config at `~/.keras/keras.json`.

    # 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 `(224, 224, 3)` (with `channels_last` data format)
            or `(3, 224, 224)` (with `channels_first` data format).
            It should have exactly 3 inputs channels,
            and width and height should be no smaller than 197.
            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.

    # Raises
        ValueError: in case of invalid argument for `weights`,
            or invalid input shape.
    """

    if K.image_data_format() == 'channels_last':
        bn_axis = 3
    else:
        bn_axis = 1

    inputs = Input(input_shape)
    x = layers.ZeroPadding2D(padding=(0, 0), name='conv1_pad')(inputs)
    x = layers.Conv2D(64, (3, 3), strides=(2, 2), padding='valid',
                      kernel_initializer='he_normal',
                      name='conv1')(x)
    x = layers.BatchNormalization(axis=bn_axis, 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)

    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.GlobalMaxPool2D(name='max_pool')(x)
    x = layers.Dense(num_classes, activation='sigmoid', name='fc28')(x)

    sgd = SGD(lr=0.01, momentum=0.9, nesterov=True)

    model = models.Model(inputs, x, name='resnet50')

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

    model.summary()

    return model
Ejemplo n.º 7
0
def get_baseline_convolutional_encoder(embedding_dimension, input_shape,
                                       config):
    """
    input shape: [batch_sz; frame_wnd; band; channel]
    """
    print('DNN input shape', input_shape)

    if K.image_dim_ordering() == 'tf':
        channel_axis = 3
        freq_axis = 2
    else:
        raise NotImplementedError('[ERROR] Only for TensorFlow background.')

    nb_filters = config['feature_maps']
    dropout_rate = config['dropout']
    pool_sz = [4, 2, 2]  # max-pooling across frequency only

    # Input block
    # feat_input = layers.Input(shape=input_shape, name='input')

    encoder = Sequential()
    encoder.add(layers.BatchNormalization(axis=freq_axis, name='bn_0_freq'))
    # CNN block
    for i, sz in enumerate(pool_sz):
        encoder.add(
            layers.Conv2D(filters=(i + 1) * nb_filters,
                          kernel_size=(3, 3),
                          padding='same'))
        encoder.add(layers.BatchNormalization(axis=channel_axis))
        encoder.add(layers.Activation(config['activation']))
        encoder.add(layers.MaxPool2D(pool_size=(sz, sz)))
        encoder.add(layers.Dropout(dropout_rate))
    encoder.add(layers.MaxPool2D())
    encoder.add(layers.GlobalMaxPool2D())
    encoder.add(layers.Dense(embedding_dimension))

    # Unwrap network
    # filters = 128
    # feat_input = layers.Input(shape=(48000, 1), name='input')
    # x = layers.Conv1D(filters, 3, padding='same', activation='relu')(feat_input)
    # x = layers.BatchNormalization()(x)
    # x = layers.SpatialDropout1D(dropout_rate)(x)
    # x = layers.MaxPool1D(4, 4)(x)
    #
    # # Further convs
    # x =layers.Conv1D(2 * filters, 3, padding='same', activation='relu')(x)
    # x =layers.BatchNormalization()(x)
    # x =layers.SpatialDropout1D(dropout_rate)(x)
    # x =layers.MaxPool1D()(x)
    #
    # x =layers.Conv1D(3 * filters, 3, padding='same', activation='relu')(x)
    # x =layers.BatchNormalization()(x)
    # x =layers.SpatialDropout1D(dropout_rate)(x)
    # x =layers.MaxPool1D()(x)
    #
    # x =layers.Conv1D(4 * filters, 3, padding='same', activation='relu')(x)
    # x =layers.BatchNormalization()(x)
    # x =layers.SpatialDropout1D(dropout_rate)(x)
    # x =layers.MaxPool1D()(x)
    #
    # x =layers.GlobalMaxPool1D()(x)
    #
    # encoder =layers.Dense(embedding_dimension)(x)
    return encoder
Ejemplo n.º 8
0
def mk_conv_64(*, channels):

    i = kr.Input((64, 64, channels), name='x0')

    cc_stack = [
        kr.GaussianNoise(0.025),
        kr.Conv2D(10, 1),
        kr.LeakyReLU(alpha=0.5),
        kr.Conv2D(2),
    ]

    h = apply_layers(i, cc_stack)

    conv_stack = [
        kr.GaussianNoise(0.025),
        kr.Conv2D(
            64,
            3,
            strides=1,
            activation='elu',
        ),
        kr.Conv2D(
            128,
            3,
            strides=1,
            activation='elu',
        ),
        kr.Conv2D(
            256,
            3,
            strides=1,
            activation='elu',
        ),
        kr.Conv2D(
            384,
            3,
            strides=2,
            activation='elu',
        ),
    ]

    h = apply_layers(i, conv_stack)
    gp0 = kr.GlobalMaxPool2D()(h)
    ap0 = kr.GlobalAveragePooling2D()(h)

    conv_stack_2 = [
        kr.Conv2D(
            384,
            3,
            strides=1,
            activation='elu',
        ),
        kr.Conv2D(
            384,
            3,
            strides=1,
            activation='elu',
        ),
        kr.Conv2D(
            384,
            3,
            strides=2,
            activation='elu',
        ),
    ]

    conv_stack_3 = [
        kr.Conv2D(
            198,
            3,
            strides=1,
            activation='elu',
        ),
        kr.Conv2D(
            198,
            3,
            strides=1,
            activation='elu',
        ),
        kr.Conv2D(
            198,
            3,
            strides=1,
            activation='elu',
        ),
        kr.Conv2D(
            198,
            3,
            strides=2,
            activation='elu',
        ),
        kr.Flatten(),
    ]

    h = apply_layers(h, conv_stack_2)
    h = apply_layers(h, conv_stack_3)

    head = [
        kr.Dense(512, activation='elu'),
        kr.Dropout(0.5),
        kr.Dense(256, activation='elu'),
        kr.Dropout(0.5),
        kr.Dense(128, activation='elu'),
        kr.Dense(NL, activation='sigmoid', name='labs'),
    ]

    cat = kr.Concatenate()([h, gp0, ap0])
    y = apply_layers(cat, head)

    m = krm.Model(inputs=[i], outputs=[y], name='base_conv')
    m.compile(loss=f2_crossentropy,
              optimizer='adam',
              metrics=['binary_accuracy'])

    return m