Beispiel #1
0
def bn_feature_net_2D(receptive_field=61,
                      input_shape=(256, 256, 1),
                      n_features=3,
                      n_channels=1,
                      reg=1e-5,
                      n_conv_filters=64,
                      n_dense_filters=200,
                      VGG_mode=False,
                      init='he_normal',
                      norm_method='std',
                      location=False,
                      dilated=False,
                      padding=False,
                      padding_mode='reflect',
                      multires=False,
                      include_top=True):
    # Create layers list (x) to store all of the layers.
    # We need to use the functional API to enable the multiresolution mode
    x = []

    win = (receptive_field - 1) // 2

    if dilated:
        padding = True

    if K.image_data_format() == 'channels_first':
        channel_axis = 1
        row_axis = 2
        col_axis = 3

        if not dilated:
            input_shape = (n_channels, receptive_field, receptive_field)

    else:
        row_axis = 1
        col_axis = 2
        channel_axis = -1
        if not dilated:
            input_shape = (receptive_field, receptive_field, n_channels)

    x.append(Input(shape=input_shape))
    x.append(ImageNormalization2D(norm_method=norm_method, filter_size=receptive_field)(x[-1]))

    if padding:
        if padding_mode == 'reflect':
            x.append(ReflectionPadding2D(padding=(win, win))(x[-1]))
        elif padding_mode == 'zero':
            x.append(ZeroPadding2D(padding=(win, win))(x[-1]))

    if location:
        x.append(Location2D(in_shape=tuple(x[-1].shape.as_list()[1:]))(x[-1]))
        x.append(Concatenate(axis=channel_axis)([x[-2], x[-1]]))

    if multires:
        layers_to_concat = []

    rf_counter = receptive_field
    block_counter = 0
    d = 1

    while rf_counter > 4:
        filter_size = 3 if rf_counter % 2 == 0 else 4
        x.append(Conv2D(n_conv_filters, (filter_size, filter_size), dilation_rate=d, kernel_initializer=init, padding='valid', kernel_regularizer=l2(reg))(x[-1]))
        x.append(BatchNormalization(axis=channel_axis)(x[-1]))
        x.append(Activation('relu')(x[-1]))

        block_counter += 1
        rf_counter -= filter_size - 1

        if block_counter % 2 == 0:
            if dilated:
                x.append(DilatedMaxPool2D(dilation_rate=d, pool_size=(2, 2))(x[-1]))
                d *= 2
            else:
                x.append(MaxPool2D(pool_size=(2, 2))(x[-1]))

            if VGG_mode:
                n_conv_filters *= 2

            rf_counter = rf_counter // 2

            if multires:
                layers_to_concat.append(len(x) - 1)

    if multires:
        c = []
        for l in layers_to_concat:
            output_shape = x[l].get_shape().as_list()
            target_shape = x[-1].get_shape().as_list()

            row_crop = int(output_shape[row_axis] - target_shape[row_axis])
            if row_crop % 2 == 0:
                row_crop = (row_crop // 2, row_crop // 2)
            else:
                row_crop = (row_crop // 2, row_crop // 2 + 1)

            col_crop = int(output_shape[col_axis] - target_shape[col_axis])
            if col_crop % 2 == 0:
                col_crop = (col_crop // 2, col_crop // 2)
            else:
                col_crop = (col_crop // 2, col_crop // 2 + 1)

            cropping = (row_crop, col_crop)

            c.append(Cropping2D(cropping=cropping)(x[l]))
        x.append(Concatenate(axis=channel_axis)(c))

    x.append(Conv2D(n_dense_filters, (rf_counter, rf_counter), dilation_rate=d, kernel_initializer=init, padding='valid', kernel_regularizer=l2(reg))(x[-1]))
    x.append(BatchNormalization(axis=channel_axis)(x[-1]))
    x.append(Activation('relu')(x[-1]))

    x.append(TensorProduct(n_dense_filters, kernel_initializer=init, kernel_regularizer=l2(reg))(x[-1]))
    x.append(BatchNormalization(axis=channel_axis)(x[-1]))
    x.append(Activation('relu')(x[-1]))

    x.append(TensorProduct(n_features, kernel_initializer=init, kernel_regularizer=l2(reg))(x[-1]))

    if not dilated:
        x.append(Flatten()(x[-1]))

    if include_top:
        x.append(Softmax(axis=channel_axis)(x[-1]))

    model = Model(inputs=x[0], outputs=x[-1])

    return model
Beispiel #2
0
            x_batch = []
            y_batch = []
            end = min(start + val_batch_size, len(ids))
            i_val_batch = ids[start:end]
            for i in i_val_batch:
                x_batch.append(process_wav_file(valid_df.wav_file.values[i]))
                y_batch.append(valid_df.label_id.values[i])
            x_batch = np.array(x_batch)
            y_batch = to_categorical(y_batch, num_classes = len(POSSIBLE_LABELS))
            yield x_batch, y_batch

x_in = Input(shape = (257,98,2))
x = BatchNormalization()(x_in)
for i in range(4):
    x = Conv2D(16*(2 ** i), (3,3))(x)
    x = Activation('elu')(x)
    x = BatchNormalization()(x)
    x = MaxPooling2D((2,2))(x)
x = Conv2D(128, (1,1))(x)
x_branch_1 = GlobalAveragePooling2D()(x)
x_branch_2 = GlobalMaxPool2D()(x)
x = concatenate([x_branch_1, x_branch_2])
x = Dense(256, activation = 'relu')(x)
x = Dropout(0.5)(x)
x = Dense(len(POSSIBLE_LABELS), activation = 'softmax')(x)
model = Model(inputs = x_in, outputs = x)
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()


callbacks = [LoggingCallback(logging.info),
Beispiel #3
0
def Deeplabv3(weights='pascal_voc',
              input_tensor=None,
              input_shape=(512, 512, 3),
              classes=21,
              backbone='mobilenetv2',
              OS=16,
              alpha=1.,
              activation=None):
    """ Instantiates the Deeplabv3+ architecture
    Optionally loads weights pre-trained
    on PASCAL VOC or Cityscapes. This model is available for TensorFlow only.
    # Arguments
        weights: one of 'pascal_voc' (pre-trained on pascal voc),
            'cityscapes' (pre-trained on cityscape) or None (random initialization)
        input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
            to use as image input for the model.
        input_shape: shape of input image. format HxWxC
            PASCAL VOC model was trained on (512,512,3) images. None is allowed as shape/width
        classes: number of desired classes. PASCAL VOC has 21 classes, Cityscapes has 19 classes.
            If number of classes not aligned with the weights used, last layer is initialized randomly
        backbone: backbone to use. one of {'xception','mobilenetv2'}
        activation: optional activation to add to the top of the network.
            One of 'softmax', 'sigmoid' or None
        OS: determines input_shape/feature_extractor_output ratio. One of {8,16}.
            Used only for xception backbone.
        alpha: controls the width of the MobileNetV2 network. This is known as the
            width multiplier in the MobileNetV2 paper.
                - If `alpha` < 1.0, proportionally decreases the number
                    of filters in each layer.
                - If `alpha` > 1.0, proportionally increases the number
                    of filters in each layer.
                - If `alpha` = 1, default number of filters from the paper
                    are used at each layer.
            Used only for mobilenetv2 backbone. Pretrained is only available for alpha=1.
    # Returns
        A Keras model instance.
    # Raises
        RuntimeError: If attempting to run this model with a
            backend that does not support separable convolutions.
        ValueError: in case of invalid argument for `weights` or `backbone`
    """

    if not (weights in {'pascal_voc', 'cityscapes', None}):
        raise ValueError(
            'The `weights` argument should be either '
            '`None` (random initialization), `pascal_voc`, or `cityscapes` '
            '(pre-trained on PASCAL VOC)')

    if not (backbone in {'xception', 'mobilenetv2'}):
        raise ValueError('The `backbone` argument should be either '
                         '`xception`  or `mobilenetv2` ')

    if input_tensor is None:
        img_input = Input(shape=input_shape)
    else:
        img_input = input_tensor

    if backbone == 'xception':
        if OS == 8:
            entry_block3_stride = 1
            middle_block_rate = 2  # ! Not mentioned in paper, but required
            exit_block_rates = (2, 4)
            atrous_rates = (12, 24, 36)
        else:
            entry_block3_stride = 2
            middle_block_rate = 1
            exit_block_rates = (1, 2)
            atrous_rates = (6, 12, 18)

        x = Conv2D(32, (3, 3),
                   strides=(2, 2),
                   name='entry_flow_conv1_1',
                   use_bias=False,
                   padding='same')(img_input)
        x = BatchNormalization(name='entry_flow_conv1_1_BN')(x)
        x = Activation(tf.nn.relu)(x)

        x = _conv2d_same(x, 64, 'entry_flow_conv1_2', kernel_size=3, stride=1)
        x = BatchNormalization(name='entry_flow_conv1_2_BN')(x)
        x = Activation(tf.nn.relu)(x)

        x = _xception_block(x, [128, 128, 128],
                            'entry_flow_block1',
                            skip_connection_type='conv',
                            stride=2,
                            depth_activation=False)
        x, skip1 = _xception_block(x, [256, 256, 256],
                                   'entry_flow_block2',
                                   skip_connection_type='conv',
                                   stride=2,
                                   depth_activation=False,
                                   return_skip=True)

        x = _xception_block(x, [728, 728, 728],
                            'entry_flow_block3',
                            skip_connection_type='conv',
                            stride=entry_block3_stride,
                            depth_activation=False)
        for i in range(16):
            x = _xception_block(x, [728, 728, 728],
                                'middle_flow_unit_{}'.format(i + 1),
                                skip_connection_type='sum',
                                stride=1,
                                rate=middle_block_rate,
                                depth_activation=False)

        x = _xception_block(x, [728, 1024, 1024],
                            'exit_flow_block1',
                            skip_connection_type='conv',
                            stride=1,
                            rate=exit_block_rates[0],
                            depth_activation=False)
        x = _xception_block(x, [1536, 1536, 2048],
                            'exit_flow_block2',
                            skip_connection_type='none',
                            stride=1,
                            rate=exit_block_rates[1],
                            depth_activation=True)

    else:
        OS = 8
        first_block_filters = _make_divisible(32 * alpha, 8)
        x = Conv2D(first_block_filters,
                   kernel_size=3,
                   strides=(2, 2),
                   padding='same',
                   use_bias=False,
                   name='Conv')(img_input)
        x = BatchNormalization(epsilon=1e-3, momentum=0.999, name='Conv_BN')(x)
        x = Activation(tf.nn.relu6, name='Conv_Relu6')(x)

        x = _inverted_res_block(x,
                                filters=16,
                                alpha=alpha,
                                stride=1,
                                expansion=1,
                                block_id=0,
                                skip_connection=False)

        x = _inverted_res_block(x,
                                filters=24,
                                alpha=alpha,
                                stride=2,
                                expansion=6,
                                block_id=1,
                                skip_connection=False)
        x = _inverted_res_block(x,
                                filters=24,
                                alpha=alpha,
                                stride=1,
                                expansion=6,
                                block_id=2,
                                skip_connection=True)

        x = _inverted_res_block(x,
                                filters=32,
                                alpha=alpha,
                                stride=2,
                                expansion=6,
                                block_id=3,
                                skip_connection=False)
        x = _inverted_res_block(x,
                                filters=32,
                                alpha=alpha,
                                stride=1,
                                expansion=6,
                                block_id=4,
                                skip_connection=True)
        x = _inverted_res_block(x,
                                filters=32,
                                alpha=alpha,
                                stride=1,
                                expansion=6,
                                block_id=5,
                                skip_connection=True)

        # stride in block 6 changed from 2 -> 1, so we need to use rate = 2
        x = _inverted_res_block(
            x,
            filters=64,
            alpha=alpha,
            stride=1,  # 1!
            expansion=6,
            block_id=6,
            skip_connection=False)
        x = _inverted_res_block(x,
                                filters=64,
                                alpha=alpha,
                                stride=1,
                                rate=2,
                                expansion=6,
                                block_id=7,
                                skip_connection=True)
        x = _inverted_res_block(x,
                                filters=64,
                                alpha=alpha,
                                stride=1,
                                rate=2,
                                expansion=6,
                                block_id=8,
                                skip_connection=True)
        x = _inverted_res_block(x,
                                filters=64,
                                alpha=alpha,
                                stride=1,
                                rate=2,
                                expansion=6,
                                block_id=9,
                                skip_connection=True)

        x = _inverted_res_block(x,
                                filters=96,
                                alpha=alpha,
                                stride=1,
                                rate=2,
                                expansion=6,
                                block_id=10,
                                skip_connection=False)
        x = _inverted_res_block(x,
                                filters=96,
                                alpha=alpha,
                                stride=1,
                                rate=2,
                                expansion=6,
                                block_id=11,
                                skip_connection=True)
        x = _inverted_res_block(x,
                                filters=96,
                                alpha=alpha,
                                stride=1,
                                rate=2,
                                expansion=6,
                                block_id=12,
                                skip_connection=True)

        x = _inverted_res_block(
            x,
            filters=160,
            alpha=alpha,
            stride=1,
            rate=2,  # 1!
            expansion=6,
            block_id=13,
            skip_connection=False)
        x = _inverted_res_block(x,
                                filters=160,
                                alpha=alpha,
                                stride=1,
                                rate=4,
                                expansion=6,
                                block_id=14,
                                skip_connection=True)
        x = _inverted_res_block(x,
                                filters=160,
                                alpha=alpha,
                                stride=1,
                                rate=4,
                                expansion=6,
                                block_id=15,
                                skip_connection=True)

        x = _inverted_res_block(x,
                                filters=320,
                                alpha=alpha,
                                stride=1,
                                rate=4,
                                expansion=6,
                                block_id=16,
                                skip_connection=False)

    # end of feature extractor

    # branching for Atrous Spatial Pyramid Pooling

    # Image Feature branch
    shape_before = tf.shape(x)
    b4 = GlobalAveragePooling2D()(x)
    # from (b_size, channels)->(b_size, 1, 1, channels)
    b4 = Lambda(lambda x: K.expand_dims(x, 1))(b4)
    b4 = Lambda(lambda x: K.expand_dims(x, 1))(b4)
    b4 = Conv2D(256, (1, 1),
                padding='same',
                use_bias=False,
                name='image_pooling')(b4)
    b4 = BatchNormalization(name='image_pooling_BN', epsilon=1e-5)(b4)
    b4 = Activation(tf.nn.relu)(b4)
    # upsample. have to use compat because of the option align_corners
    size_before = tf.keras.backend.int_shape(x)
    b4 = Lambda(lambda x: tf.compat.v1.image.resize(
        x, size_before[1:3], method='bilinear', align_corners=True))(b4)
    # simple 1x1
    b0 = Conv2D(256, (1, 1), padding='same', use_bias=False, name='aspp0')(x)
    b0 = BatchNormalization(name='aspp0_BN', epsilon=1e-5)(b0)
    b0 = Activation(tf.nn.relu, name='aspp0_activation')(b0)

    # there are only 2 branches in mobilenetV2. not sure why
    if backbone == 'xception':
        # rate = 6 (12)
        b1 = SepConv_BN(x,
                        256,
                        'aspp1',
                        rate=atrous_rates[0],
                        depth_activation=True,
                        epsilon=1e-5)
        # rate = 12 (24)
        b2 = SepConv_BN(x,
                        256,
                        'aspp2',
                        rate=atrous_rates[1],
                        depth_activation=True,
                        epsilon=1e-5)
        # rate = 18 (36)
        b3 = SepConv_BN(x,
                        256,
                        'aspp3',
                        rate=atrous_rates[2],
                        depth_activation=True,
                        epsilon=1e-5)

        # concatenate ASPP branches & project
        x = Concatenate()([b4, b0, b1, b2, b3])
    else:
        x = Concatenate()([b4, b0])

    x = Conv2D(256, (1, 1),
               padding='same',
               use_bias=False,
               name='concat_projection')(x)
    x = BatchNormalization(name='concat_projection_BN', epsilon=1e-5)(x)
    x = Activation(tf.nn.relu)(x)
    x = Dropout(0.1)(x)
    # DeepLab v.3+ decoder

    if backbone == 'xception':
        # Feature projection
        # x4 (x2) block
        skip_size = tf.keras.backend.int_shape(skip1)
        x = Lambda(lambda xx: tf.compat.v1.image.resize(
            xx, skip_size[1:3], method='bilinear', align_corners=True))(x)

        dec_skip1 = Conv2D(48, (1, 1),
                           padding='same',
                           use_bias=False,
                           name='feature_projection0')(skip1)
        dec_skip1 = BatchNormalization(name='feature_projection0_BN',
                                       epsilon=1e-5)(dec_skip1)
        dec_skip1 = Activation(tf.nn.relu)(dec_skip1)
        x = Concatenate()([x, dec_skip1])
        x = SepConv_BN(x,
                       256,
                       'decoder_conv0',
                       depth_activation=True,
                       epsilon=1e-5)
        x = SepConv_BN(x,
                       256,
                       'decoder_conv1',
                       depth_activation=True,
                       epsilon=1e-5)

    # you can use it with arbitary number of classes
    if (weights == 'pascal_voc' and classes == 21) or (weights == 'cityscapes'
                                                       and classes == 19):
        last_layer_name = 'logits_semantic'
    else:
        last_layer_name = 'custom_logits_semantic'

    x = Conv2D(classes, (1, 1), padding='same', name=last_layer_name)(x)
    size_before3 = tf.keras.backend.int_shape(img_input)
    x = Lambda(lambda xx: tf.compat.v1.image.resize(
        xx, size_before3[1:3], method='bilinear', align_corners=True))(x)

    # Ensure that the model takes into account
    # any potential predecessors of `input_tensor`.
    if input_tensor is not None:
        inputs = get_source_inputs(input_tensor)
    else:
        inputs = img_input

    if activation in {'softmax', 'sigmoid'}:
        x = tf.keras.layers.Activation(activation)(x)

    model = Model(inputs, x, name='deeplabv3plus')

    # load weights

    if weights == 'pascal_voc':
        if backbone == 'xception':
            weights_path = get_file(
                'deeplabv3_xception_tf_dim_ordering_tf_kernels.h5',
                WEIGHTS_PATH_X,
                cache_subdir='models')
        else:
            weights_path = get_file(
                'deeplabv3_mobilenetv2_tf_dim_ordering_tf_kernels.h5',
                WEIGHTS_PATH_MOBILE,
                cache_subdir='models')
        model.load_weights(weights_path, by_name=True)
    elif weights == 'cityscapes':
        if backbone == 'xception':
            weights_path = get_file(
                'deeplabv3_xception_tf_dim_ordering_tf_kernels_cityscapes.h5',
                WEIGHTS_PATH_X_CS,
                cache_subdir='models')
        else:
            weights_path = get_file(
                'deeplabv3_mobilenetv2_tf_dim_ordering_tf_kernels_cityscapes.h5',
                WEIGHTS_PATH_MOBILE_CS,
                cache_subdir='models')
        model.load_weights(weights_path, by_name=True)
    return model
Beispiel #4
0
def create_vgg_like_model(input_shape=(40, 256, 1),
                          output_dim=256,
                          filters=64,
                          pool_shape=(1, 2),
                          max_pool=True,
                          filter_shapes=[(1, 5), (1, 3)],
                          dropout=0.3):

    model_name = 'vgg_like_in={}_out={}_filters={}_pool_shape={}_max={}_filter_shapes={}_dropout={}'\
        .format(input_shape, output_dim, filters, pool_shape, max_pool, filter_shapes, dropout)\
        .replace(',', '_').replace(' ', '_')

    visible = Input(shape=(input_shape[0], None, input_shape[2]))
    x = visible
    x = Conv2D(filters * 2**0,
               filter_shapes[0],
               padding='same',
               activation='relu',
               name='Conv0')(x)
    x = BatchNormalization(name='BN0')(x)
    x = Conv2D(filters * 2**0,
               filter_shapes[1],
               padding='same',
               activation='relu',
               name='Conv1')(x)
    x = BatchNormalization(name='BN1')(x)
    x = possible_pool(1, input_shape, pool_shape, max_pool, x)
    x = Dropout(dropout)(x)

    x = Conv2D(filters * 2**1,
               filter_shapes[0],
               padding='same',
               activation='relu',
               name='Conv2')(x)
    x = BatchNormalization(name='BN2')(x)
    x = Conv2D(filters * 2**1,
               filter_shapes[1],
               padding='same',
               activation='relu',
               name='Conv3')(x)
    x = BatchNormalization(name='BN3')(x)
    x = possible_pool(2, input_shape, pool_shape, max_pool, x)

    x = Dropout(dropout)(x)

    x = Conv2D(filters * 2**2,
               filter_shapes[0],
               padding='same',
               activation='relu',
               name='Conv4')(x)
    x = BatchNormalization(name='BN4')(x)
    x = Conv2D(filters * 2**2,
               filter_shapes[1],
               padding='same',
               activation='relu',
               name='Conv5')(x)
    x = BatchNormalization(name='BN5')(x)
    x = possible_pool(3, input_shape, pool_shape, max_pool, x)
    x = Dropout(dropout)(x)

    x = Conv2D(filters * 2**2,
               filter_shapes[0],
               padding='same',
               activation='relu',
               name='Conv6')(x)
    x = BatchNormalization(name='BN6')(x)
    x = Conv2D(filters * 2**2,
               filter_shapes[1],
               padding='same',
               activation='relu',
               name='Conv7')(x)
    x = BatchNormalization(name='BN7')(x)
    x = possible_pool(4, input_shape, pool_shape, max_pool, x)

    x = Dropout(dropout)(x)

    x = Conv2D(filters * 2**3,
               filter_shapes[0],
               padding='same',
               activation='relu',
               name='Conv8')(x)
    x = BatchNormalization(name='BN8')(x)
    x = Conv2D(filters * 2**3,
               filter_shapes[1],
               padding='same',
               activation='relu',
               name='Conv9')(x)
    x = BatchNormalization(name='BN9')(x)
    x = possible_pool(5, input_shape, pool_shape, max_pool, x)
    x = Dropout(dropout)(x)

    x = Conv2D(filters * 2**3,
               filter_shapes[0],
               padding='same',
               activation='relu',
               name='Conv10')(x)
    x = BatchNormalization(name='BN10')(x)
    x = Conv2D(filters * 2**3,
               filter_shapes[1],
               padding='same',
               activation='relu',
               name='Conv11')(x)
    x = BatchNormalization(name='BN11')(x)
    x = possible_pool(6, input_shape, pool_shape, max_pool, x)
    x = Dropout(dropout)(x)

    x = Conv2D(output_dim, (1, 1),
               padding='same',
               activation='relu',
               name='1x1')(x)
    x = GlobalAveragePooling2D()(x)

    x = Activation('softmax')(x)
    return Model(inputs=visible, outputs=x, name=model_name)
Beispiel #5
0
def test_tf_keras_mnist_cnn():
    """ This is the basic mnist cnn example from keras.
    """

    try:
        import tensorflow as tf
        from tensorflow.python import keras
        from tensorflow.python.keras.models import Sequential
        from tensorflow.python.keras.layers import Dense, Dropout, Flatten, Activation
        from tensorflow.python.keras.layers import Conv2D, MaxPooling2D
        from tensorflow.python.keras import backend as K
    except Exception as e:
        print("Skipping test_tf_keras_mnist_cnn!")
        return
    import shap

    batch_size = 128
    num_classes = 10
    epochs = 1

    # input image dimensions
    img_rows, img_cols = 28, 28

    # the data, split between train and test sets
    (x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()

    if K.image_data_format() == 'channels_first':
        x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
        x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
        input_shape = (1, img_rows, img_cols)
    else:
        x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
        x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
        input_shape = (img_rows, img_cols, 1)

    x_train = x_train.astype('float32')
    x_test = x_test.astype('float32')
    x_train /= 255
    x_test /= 255

    # convert class vectors to binary class matrices
    y_train = keras.utils.to_categorical(y_train, num_classes)
    y_test = keras.utils.to_categorical(y_test, num_classes)

    model = Sequential()
    model.add(
        Conv2D(32,
               kernel_size=(3, 3),
               activation='relu',
               input_shape=input_shape))
    model.add(Conv2D(64, (3, 3), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))
    model.add(Flatten())
    model.add(Dense(32, activation='relu'))  # 128
    model.add(Dropout(0.5))
    model.add(Dense(num_classes))
    model.add(Activation('softmax'))

    model.compile(loss=keras.losses.categorical_crossentropy,
                  optimizer=keras.optimizers.Adadelta(),
                  metrics=['accuracy'])

    model.fit(x_train[:1000, :],
              y_train[:1000, :],
              batch_size=batch_size,
              epochs=epochs,
              verbose=1,
              validation_data=(x_test[:1000, :], y_test[:1000, :]))

    # explain by passing the tensorflow inputs and outputs
    np.random.seed(0)
    inds = np.random.choice(x_train.shape[0], 10, replace=False)
    e = shap.DeepExplainer((model.layers[0].input, model.layers[-1].input),
                           x_train[inds, :, :])
    shap_values = e.shap_values(x_test[:1])

    sess = tf.keras.backend.get_session()
    diff = sess.run(model.layers[-1].input, feed_dict={model.layers[0].input: x_test[:1]}) - \
    sess.run(model.layers[-1].input, feed_dict={model.layers[0].input: x_train[inds,:,:]}).mean(0)

    sums = np.array([shap_values[i].sum() for i in range(len(shap_values))])
    d = np.abs(sums - diff).sum()
    assert d / np.abs(diff).sum(
    ) < 0.001, "Sum of SHAP values does not match difference! %f" % d
Beispiel #6
0
    y = Input(shape=(None, ), dtype='int32')
    y_in = Lambda(lambda x: x[:, :-1])(y)
    y_out = Lambda(lambda x: x[:, 1:])(y)

    # 誤差関数のマスク
    mask = Lambda(lambda x: K.cast(K.not_equal(x, w2i['<pad>']), 'float32'))(
        y_out)

    # 層の定義
    embedding = Embedding(vocab_size, emb_dim)
    lstm = LSTM(hid_dim,
                activation='tanh',
                return_sequences=True,
                return_state=True)
    dense = Dense(vocab_size)
    softmax = Activation('softmax')

    # 順伝播
    y_emb = embedding(y_in)
    h, _, _ = lstm(y_emb, initial_state=[h_0, c_0])  # 第2,3戻り値(最終ステップのh, c)は無視
    h = dense(h)
    y_pred = softmax(h)

    def masked_cross_entropy(y_true, y_pred):
        return -K.mean(
            K.sum(K.sum(y_true * K.log(K.clip(y_pred, 1e-10, 1)), axis=-1) *
                  mask,
                  axis=1))

    model = Model([x, y], y_pred)
    model.compile(loss=masked_cross_entropy, optimizer='rmsprop')
Beispiel #7
0
def __create_pyramid_features(backbone_dict, ndim=2, feature_size=256,
                              include_final_layers=True):
    """Creates the FPN layers on top of the backbone features.

    Args:
        backbone_dict (dictionary): A dictionary of the backbone layers, with
            the names as keys, e.g. {'C0': C0, 'C1': C1, 'C2': C2, ...}
        feature_size (int, optional): Defaults to 256. The feature size to use
            for the resulting feature levels.
        include_final_layers: Defaults to True. Option to add two coarser
            pyramid levels
        ndim: The spatial dimensions of the input data. Default is 2, but it
            also works with 3
    Returns:
        A dictionary with the feature pyramid names and levels,
            e.g. {'P3': P3, 'P4': P4, ...}
        Each backbone layer gets a pyramid level, and two additional levels are
            added, e.g. [C3, C4, C5] --> [P3, P4, P5, P6, P7]
    """

    acceptable_ndims = [2, 3]
    if ndim not in acceptable_ndims:
        raise ValueError('Only 2 and 3 dimensional networks are supported')

    # Get names of the backbone levels and place in ascending order
    backbone_names = get_sorted_keys(backbone_dict)
    backbone_features = [backbone_dict[name] for name in backbone_names]

    pyramid_names = []
    pyramid_finals = []
    pyramid_upsamples = []

    # Reverse lists
    backbone_names.reverse()
    backbone_features.reverse()

    for i in range(len(backbone_names)):

        N = backbone_names[i]
        level = int(re.findall(r'\d+', N)[0])
        p_name = 'P' + str(level)
        pyramid_names.append(p_name)

        backbone_input = backbone_features[i]

        # Don't add for the bottom of the pyramid
        if i == 0:
            if len(backbone_features) > 1:
                upsamplelike_input = backbone_features[i + 1]
            else:
                upsamplelike_input = None
            addition_input = None

        # Don't upsample for the top of the pyramid
        elif i == len(backbone_names) - 1:
            upsamplelike_input = None
            addition_input = pyramid_upsamples[-1]

        # Otherwise, add and upsample
        else:
            upsamplelike_input = backbone_features[i + 1]
            addition_input = pyramid_upsamples[-1]

        pf, pu = create_pyramid_level(backbone_input,
                                      upsamplelike_input=upsamplelike_input,
                                      addition_input=addition_input,
                                      level=level)
        pyramid_finals.append(pf)
        pyramid_upsamples.append(pu)

    # Add the final two pyramid layers
    if include_final_layers:
        # "Second to last pyramid layer is obtained via a
        # 3x3 stride-2 conv on the coarsest backbone"
        N = backbone_names[0]
        F = backbone_features[0]
        level = int(re.findall(r'\d+', N)[0]) + 1
        P_minus_2_name = 'P%s' % level

        if ndim == 2:
            P_minus_2 = Conv2D(feature_size, kernel_size=(3, 3), strides=(2, 2),
                               padding='same', name=P_minus_2_name)(F)
        else:
            P_minus_2 = Conv3D(feature_size, kernel_size=(3, 3, 3),
                               strides=(2, 2, 2), padding='same',
                               name=P_minus_2_name)(F)

        pyramid_names.insert(0, P_minus_2_name)
        pyramid_finals.insert(0, P_minus_2)

        # "Last pyramid layer is computed by applying ReLU
        # followed by a 3x3 stride-2 conv on second to last layer"
        level = int(re.findall(r'\d+', N)[0]) + 2
        P_minus_1_name = 'P' + str(level)
        P_minus_1 = Activation('relu', name=N + '_relu')(P_minus_2)

        if ndim == 2:
            P_minus_1 = Conv2D(feature_size, kernel_size=(3, 3), strides=(2, 2),
                               padding='same', name=P_minus_1_name)(P_minus_1)
        else:
            P_minus_1 = Conv3D(feature_size, kernel_size=(3, 3, 3),
                               strides=(2, 2, 2), padding='same',
                               name=P_minus_1_name)(P_minus_1)

        pyramid_names.insert(0, P_minus_1_name)
        pyramid_finals.insert(0, P_minus_1)

    pyramid_names.reverse()
    pyramid_finals.reverse()

    # Reverse lists
    backbone_names.reverse()
    backbone_features.reverse()

    pyramid_dict = {}
    for name, feature in zip(pyramid_names, pyramid_finals):
        pyramid_dict[name] = feature

    return pyramid_dict
Beispiel #8
0
def MobileNetV2(classes=1000,
                input_tensor=None,
                input_shape=(512, 512, 3),
                weights_info=None,
                OS=16,
                alpha=1.,
                include_top=True):
    """ Instantiates the Deeplabv3+ architecture

    Optionally loads weights pre-trained
    on PASCAL VOC or Cityscapes. This model is available for TensorFlow only.
    # Arguments
        classes: Integer, optional number of classes to classify images
            into, only to be specified if `include_top` is True, and
            if no `weights` argument is specified.
        input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
            to use as image input for the model.
        input_shape: shape of input image. format HxWxC
            PASCAL VOC model was trained on (512,512,3) images. None is allowed as shape/width
        weights_info: this dict is consisted of `classes` and `weghts`.
            `classes` is number of `weights` output units.
            `weights` is one of 'imagenet' (pre-training on ImageNet), 'pascal_voc', 'cityscapes',
            original weights path (pre-training on original data) or None (random initialization)
        OS: determines input_shape/feature_extractor_output ratio. One of {8,16}.
            Used only for xception backbone.
        alpha: controls the width of the MobileNetV2 network. This is known as the
            width multiplier in the MobileNetV2 paper.
                - If `alpha` < 1.0, proportionally decreases the number
                    of filters in each layer.
                - If `alpha` > 1.0, proportionally increases the number
                    of filters in each layer.
                - If `alpha` = 1, default number of filters from the paper
                    are used at each layer.
            Used only for mobilenetv2 backbone. Pretrained is only available for alpha=1.
        include_top: Boolean, whether to include the fully-connected
            layer at the top of the network. Defaults to `True`.

    # Returns
        A Keras model instance.

    """

    if weights_info is not None:
        if weights_info.get("weights") is None:
            weights = None

        elif weights_info["weights"] in {
                'imagenet', 'pascal_voc', 'cityscapes', None
        }:
            weights = weights_info["weights"]

        elif os.path.exists(weights_info["weights"]) and weights_info.get(
                "classes") is not None:
            classes = int(weights_info["classes"])
            weights = weights_info["weights"]

        else:
            raise ValueError(
                'The `weights` should be either '
                '`None` (random initialization), `imagenet`, `pascal_voc`, `cityscapes`, '
                'original weights path (pre-training on original data), '
                'or the path to the weights file to be loaded and'
                '`classes` should be number of original weights output units')

    else:
        weights = None
        if classes is None:
            raise ValueError('`classes` should be any number')

    if input_tensor is None:
        img_input = Input(shape=input_shape)
    else:
        img_input = input_tensor

    # If input_shape is None, infer shape from input_tensor
    if backend.image_data_format() == 'channels_first':
        rows = input_shape[1]
        cols = input_shape[2]
    else:
        rows = input_shape[0]
        cols = input_shape[1]

    if rows == cols and rows in [96, 128, 160, 192, 224]:
        default_size = rows
    else:
        default_size = 224

    if weights == 'imagenet':
        if alpha not in [0.35, 0.50, 0.75, 1.0, 1.3, 1.4]:
            raise ValueError('If imagenet weights are being loaded, '
                             'alpha can be one of `0.35`, `0.50`, `0.75`, '
                             '`1.0`, `1.3` or `1.4` only.')

        if rows != cols or rows not in [96, 128, 160, 192, 224]:
            rows = 224

    OS = 8
    first_block_filters = _make_divisible(32 * alpha, 8)
    x = Conv2D(first_block_filters,
               kernel_size=3,
               strides=(2, 2),
               padding='same',
               use_bias=False,
               name='Conv')(img_input)
    x = BatchNormalization(epsilon=1e-3, momentum=0.999, name='Conv_BN')(x)
    x = Activation(relu6, name='Conv_Relu6')(x)

    x = _inverted_res_block(x,
                            filters=16,
                            alpha=alpha,
                            stride=1,
                            expansion=1,
                            block_id=0,
                            skip_connection=False)

    x = _inverted_res_block(x,
                            filters=24,
                            alpha=alpha,
                            stride=2,
                            expansion=6,
                            block_id=1,
                            skip_connection=False)
    x = _inverted_res_block(x,
                            filters=24,
                            alpha=alpha,
                            stride=1,
                            expansion=6,
                            block_id=2,
                            skip_connection=True)

    x = _inverted_res_block(x,
                            filters=32,
                            alpha=alpha,
                            stride=2,
                            expansion=6,
                            block_id=3,
                            skip_connection=False)
    x = _inverted_res_block(x,
                            filters=32,
                            alpha=alpha,
                            stride=1,
                            expansion=6,
                            block_id=4,
                            skip_connection=True)
    x = _inverted_res_block(x,
                            filters=32,
                            alpha=alpha,
                            stride=1,
                            expansion=6,
                            block_id=5,
                            skip_connection=True)

    # stride in block 6 changed from 2 -> 1, so we need to use rate = 2
    x = _inverted_res_block(
        x,
        filters=64,
        alpha=alpha,
        stride=1,  # 1!
        expansion=6,
        block_id=6,
        skip_connection=False)
    x = _inverted_res_block(x,
                            filters=64,
                            alpha=alpha,
                            stride=1,
                            rate=2,
                            expansion=6,
                            block_id=7,
                            skip_connection=True)
    x = _inverted_res_block(x,
                            filters=64,
                            alpha=alpha,
                            stride=1,
                            rate=2,
                            expansion=6,
                            block_id=8,
                            skip_connection=True)
    x = _inverted_res_block(x,
                            filters=64,
                            alpha=alpha,
                            stride=1,
                            rate=2,
                            expansion=6,
                            block_id=9,
                            skip_connection=True)

    x = _inverted_res_block(x,
                            filters=96,
                            alpha=alpha,
                            stride=1,
                            rate=2,
                            expansion=6,
                            block_id=10,
                            skip_connection=False)
    x = _inverted_res_block(x,
                            filters=96,
                            alpha=alpha,
                            stride=1,
                            rate=2,
                            expansion=6,
                            block_id=11,
                            skip_connection=True)
    x = _inverted_res_block(x,
                            filters=96,
                            alpha=alpha,
                            stride=1,
                            rate=2,
                            expansion=6,
                            block_id=12,
                            skip_connection=True)

    x = _inverted_res_block(
        x,
        filters=160,
        alpha=alpha,
        stride=1,
        rate=2,  # 1!
        expansion=6,
        block_id=13,
        skip_connection=False)
    x = _inverted_res_block(x,
                            filters=160,
                            alpha=alpha,
                            stride=1,
                            rate=4,
                            expansion=6,
                            block_id=14,
                            skip_connection=True)
    x = _inverted_res_block(x,
                            filters=160,
                            alpha=alpha,
                            stride=1,
                            rate=4,
                            expansion=6,
                            block_id=15,
                            skip_connection=True)

    x = _inverted_res_block(x,
                            filters=320,
                            alpha=alpha,
                            stride=1,
                            rate=4,
                            expansion=6,
                            block_id=16,
                            skip_connection=False)

    if alpha > 1.0:
        last_block_filters = _make_divisible(1280 * alpha, 8)
    else:
        last_block_filters = 1280

    x = Conv2D(last_block_filters,
               kernel_size=1,
               use_bias=False,
               name='Conv_1')(x)
    x = BatchNormalization(epsilon=1e-3, momentum=0.999, name='Conv_1_bn')(x)
    x = Activation(relu6, name='out_relu')(x)

    x = GlobalAveragePooling2D()(x)
    x = Dense(classes, activation='softmax')(x)

    # Ensure that the model takes into account
    # any potential predecessors of `input_tensor`.
    if input_tensor is not None:
        inputs = layer_utils.get_source_inputs(input_tensor)
    else:
        inputs = img_input

    # Create model.
    model = Model(inputs, x, name='mobilenetv2')

    # Load weights.
    if weights == 'imagenet':
        print("movilenetv2 load model imagenet")
        model_name = ('mobilenet_v2_weights_tf_dim_ordering_tf_kernels_' +
                      str(alpha) + '_' + str(rows) + '.h5')
        weight_path = BASE_WEIGHT_PATH + model_name
        weights_path = data_utils.get_file(model_name,
                                           weight_path,
                                           cache_subdir='models')
        model.load_weights(weights_path)
    elif not (weights in {'pascal_voc', 'cityscapes', None}):
        model.load_weights(weights)

    if include_top:
        return model
    else:
        # get last _inverted_res_block layer
        no_top_model = Model(inputs=model.input,
                             outputs=model.get_layer(index=-6).output)
        return no_top_model
def 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'`.
    """
    if block_type == 'block35':
        branch_0 = conv2d_bn(x, 32, 1)
        branch_1 = conv2d_bn(x, 32, 1)
        branch_1 = conv2d_bn(branch_1, 32, 3)
        branch_2 = conv2d_bn(x, 32, 1)
        branch_2 = conv2d_bn(branch_2, 48, 3)
        branch_2 = conv2d_bn(branch_2, 64, 3)
        branches = [branch_0, branch_1, branch_2]
    elif block_type == 'block17':
        branch_0 = conv2d_bn(x, 192, 1)
        branch_1 = conv2d_bn(x, 128, 1)
        branch_1 = conv2d_bn(branch_1, 160, [1, 7])
        branch_1 = conv2d_bn(branch_1, 192, [7, 1])
        branches = [branch_0, branch_1]
    elif block_type == 'block8':
        branch_0 = conv2d_bn(x, 192, 1)
        branch_1 = conv2d_bn(x, 192, 1)
        branch_1 = conv2d_bn(branch_1, 224, [1, 3])
        branch_1 = conv2d_bn(branch_1, 256, [3, 1])
        branches = [branch_0, branch_1]
    else:
        raise ValueError('Unknown Inception-ResNet block type. '
                         'Expects "block35", "block17" or "block8", '
                         'but got: ' + str(block_type))

    block_name = block_type + '_' + str(block_idx)
    channel_axis = 1 if K.image_data_format() == 'channels_first' else 3
    mixed = Concatenate(axis=channel_axis,
                        name=block_name + '_mixed')(branches)
    up = conv2d_bn(mixed,
                   K.int_shape(x)[channel_axis],
                   1,
                   activation=None,
                   use_bias=True,
                   name=block_name + '_conv')

    x = Lambda(
        lambda inputs, scale: inputs[0] + inputs[1] * scale,
        # output_shape=K.int_shape(x)[1:],
        arguments={'scale': scale},
        name=block_name)([x, up])
    if activation is not None:
        x = Activation(activation, name=block_name + '_ac')(x)
    return x
Beispiel #10
0
                                        batch_size=16,
                                        class_mode='categorical')
test_gen = datagen.flow_from_directory(test_dir,
                                       target_size=(sz, sz),
                                       batch_size=16,
                                       class_mode='categorical')

label_map = train_gen.class_indices
print(label_map)

label_map1 = test_gen.class_indices
print(label_map1)

model = Sequential()
model.add(Conv2D(64, (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(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())
# model.add(Dense(64))
# model.add(Activation('softmax'))
# model.add(Dropout(0.5))
model.add(Dense(46))
def get_test_model_full():
    """Returns a maximally complex test model,
    using all supported layer types with different parameter combination.
    """
    input_shapes = [
        (26, 28, 3),
        (4, 4, 3),
        (4, 4, 3),
        (4, ),
        (2, 3),
        (27, 29, 1),
        (17, 1),
        (17, 4),
    ]
    inputs = [Input(shape=s) for s in input_shapes]

    outputs = []

    for inp in inputs[6:8]:
        for padding in ['valid', 'same']:
            for s in range(1, 6):
                for out_channels in [1, 2]:
                    for d in range(1, 4):
                        outputs.append(
                            Conv1D(out_channels,
                                   s,
                                   padding=padding,
                                   dilation_rate=d)(inp))
        for padding_size in range(0, 5):
            outputs.append(ZeroPadding1D(padding_size)(inp))
        for crop_left in range(0, 2):
            for crop_right in range(0, 2):
                outputs.append(Cropping1D((crop_left, crop_right))(inp))
        for upsampling_factor in range(1, 5):
            outputs.append(UpSampling1D(upsampling_factor)(inp))
        for padding in ['valid', 'same']:
            for pool_factor in range(1, 6):
                for s in range(1, 4):
                    outputs.append(
                        MaxPooling1D(pool_factor, strides=s,
                                     padding=padding)(inp))
                    outputs.append(
                        AveragePooling1D(pool_factor,
                                         strides=s,
                                         padding=padding)(inp))
        outputs.append(GlobalMaxPooling1D()(inp))
        outputs.append(GlobalAveragePooling1D()(inp))

    for inp in [inputs[0], inputs[5]]:
        for padding in ['valid', 'same']:
            for h in range(1, 6):
                for out_channels in [1, 2]:
                    for d in range(1, 4):
                        outputs.append(
                            Conv2D(out_channels, (h, 1),
                                   padding=padding,
                                   dilation_rate=(d, 1))(inp))
                        outputs.append(
                            SeparableConv2D(out_channels, (h, 1),
                                            padding=padding,
                                            dilation_rate=(d, 1))(inp))
                    for sy in range(1, 4):
                        outputs.append(
                            Conv2D(out_channels, (h, 1),
                                   strides=(1, sy),
                                   padding=padding)(inp))
                        outputs.append(
                            SeparableConv2D(out_channels, (h, 1),
                                            strides=(sy, sy),
                                            padding=padding)(inp))
                for sy in range(1, 4):
                    outputs.append(
                        MaxPooling2D((h, 1), strides=(1, sy),
                                     padding=padding)(inp))
            for w in range(1, 6):
                for out_channels in [1, 2]:
                    for d in range(1, 4) if sy == 1 else [1]:
                        outputs.append(
                            Conv2D(out_channels, (1, w),
                                   padding=padding,
                                   dilation_rate=(1, d))(inp))
                        outputs.append(
                            SeparableConv2D(out_channels, (1, w),
                                            padding=padding,
                                            dilation_rate=(1, d))(inp))
                    for sx in range(1, 4):
                        outputs.append(
                            Conv2D(out_channels, (1, w),
                                   strides=(sx, 1),
                                   padding=padding)(inp))
                        outputs.append(
                            SeparableConv2D(out_channels, (1, w),
                                            strides=(sx, sx),
                                            padding=padding)(inp))
                for sx in range(1, 4):
                    outputs.append(
                        MaxPooling2D((1, w), strides=(1, sx),
                                     padding=padding)(inp))
    outputs.append(ZeroPadding2D(2)(inputs[0]))
    outputs.append(ZeroPadding2D((2, 3))(inputs[0]))
    outputs.append(ZeroPadding2D(((1, 2), (3, 4)))(inputs[0]))
    outputs.append(Cropping2D(2)(inputs[0]))
    outputs.append(Cropping2D((2, 3))(inputs[0]))
    outputs.append(Cropping2D(((1, 2), (3, 4)))(inputs[0]))
    for y in range(1, 3):
        for x in range(1, 3):
            outputs.append(UpSampling2D(size=(y, x))(inputs[0]))
    outputs.append(GlobalAveragePooling2D()(inputs[0]))
    outputs.append(GlobalMaxPooling2D()(inputs[0]))
    outputs.append(AveragePooling2D((2, 2))(inputs[0]))
    outputs.append(MaxPooling2D((2, 2))(inputs[0]))
    outputs.append(UpSampling2D((2, 2))(inputs[0]))
    outputs.append(keras.layers.concatenate([inputs[0], inputs[0]]))
    outputs.append(Dropout(0.5)(inputs[0]))

    outputs.append(BatchNormalization()(inputs[0]))
    outputs.append(BatchNormalization(center=False)(inputs[0]))
    outputs.append(BatchNormalization(scale=False)(inputs[0]))

    outputs.append(Conv2D(2, (3, 3), use_bias=True)(inputs[0]))
    outputs.append(Conv2D(2, (3, 3), use_bias=False)(inputs[0]))
    outputs.append(SeparableConv2D(2, (3, 3), use_bias=True)(inputs[0]))
    outputs.append(SeparableConv2D(2, (3, 3), use_bias=False)(inputs[0]))

    outputs.append(Dense(2, use_bias=True)(inputs[3]))
    outputs.append(Dense(2, use_bias=False)(inputs[3]))

    shared_conv = Conv2D(1, (1, 1),
                         padding='valid',
                         name='shared_conv',
                         activation='relu')

    up_scale_2 = UpSampling2D((2, 2))
    x1 = shared_conv(up_scale_2(inputs[1]))  # (1, 8, 8)
    x2 = shared_conv(up_scale_2(inputs[2]))  # (1, 8, 8)
    x3 = Conv2D(1, (1, 1), padding='valid')(up_scale_2(inputs[2]))  # (1, 8, 8)
    x = keras.layers.concatenate([x1, x2, x3])  # (3, 8, 8)
    outputs.append(x)

    x = Conv2D(3, (1, 1), padding='same', use_bias=False)(x)  # (3, 8, 8)
    outputs.append(x)
    x = Dropout(0.5)(x)
    outputs.append(x)
    x = keras.layers.concatenate(
        [MaxPooling2D((2, 2))(x),
         AveragePooling2D((2, 2))(x)])  # (6, 4, 4)
    outputs.append(x)

    x = Flatten()(x)  # (1, 1, 96)
    x = Dense(4, use_bias=False)(x)
    outputs.append(x)
    x = Dense(3)(x)  # (1, 1, 3)
    outputs.append(x)

    intermediate_input_shape = (3, )
    intermediate_in = Input(intermediate_input_shape)
    intermediate_x = intermediate_in
    intermediate_x = Dense(8)(intermediate_x)
    intermediate_x = Dense(5)(intermediate_x)
    intermediate_model = Model(inputs=[intermediate_in],
                               outputs=[intermediate_x],
                               name='intermediate_model')
    intermediate_model.compile(loss='mse', optimizer='nadam')

    x = intermediate_model(x)  # (1, 1, 5)

    intermediate_model_2 = Sequential()
    intermediate_model_2.add(Dense(7, input_shape=(5, )))
    intermediate_model_2.add(Dense(5))
    intermediate_model_2.compile(optimizer='rmsprop',
                                 loss='categorical_crossentropy')

    x = intermediate_model_2(x)  # (1, 1, 5)

    x = Dense(3)(x)  # (1, 1, 3)

    shared_activation = Activation('tanh')

    outputs = outputs + [
        Activation('tanh')(inputs[3]),
        Activation('hard_sigmoid')(inputs[3]),
        Activation('selu')(inputs[3]),
        Activation('sigmoid')(inputs[3]),
        Activation('softplus')(inputs[3]),
        Activation('softmax')(inputs[3]),
        Activation('relu')(inputs[3]),
        LeakyReLU()(inputs[3]),
        ELU()(inputs[3]),
        shared_activation(inputs[3]),
        inputs[4],
        inputs[1],
        x,
        shared_activation(x),
    ]

    print('Model has {} outputs.'.format(len(outputs)))

    model = Model(inputs=inputs, outputs=outputs, name='test_model_full')
    model.compile(loss='mse', optimizer='nadam')

    # fit to dummy data
    training_data_size = 1
    batch_size = 1
    epochs = 10
    data_in = generate_input_data(training_data_size, input_shapes)
    data_out = generate_output_data(training_data_size, outputs)
    model.fit(data_in, data_out, epochs=epochs, batch_size=batch_size)
    return model
Beispiel #12
0
def cifar100_student_strong(n_classes: int,
                            input_shape=None,
                            input_tensor=None,
                            weights_path: Union[None, str] = None) -> Model:
    """
    Defines a cifar100 strong student network.

    :param n_classes: the number of classes.
    :param input_shape: the input shape of the network. Can be omitted if input_tensor is used.
    :param input_tensor: the input tensor of the network. Can be omitted if input_shape is used.
    :param weights_path: a path to a trained cifar10 tiny network's weights.
    :return: Keras functional Model.
    """
    inputs = create_inputs(input_shape, input_tensor)

    # Define a weight decay for the regularisation.
    weight_decay = 1e-4

    # Block1.
    x = Conv2D(32, (3, 3),
               padding='same',
               activation='elu',
               name='block1_conv1',
               kernel_regularizer=l2(weight_decay))(inputs)

    x = BatchNormalization(name='block1_batch-norm1')(x)
    x = Conv2D(64, (3, 3),
               padding='same',
               activation='elu',
               name='block1_conv2',
               kernel_regularizer=l2(weight_decay))(x)
    x = BatchNormalization(name='block1_batch-norm2')(x)
    x = MaxPooling2D(pool_size=(2, 2), name='block1_pool')(x)
    x = Dropout(0.2, name='block1_dropout', seed=0)(x)

    # Block2.
    x = Conv2D(128, (3, 3),
               padding='same',
               activation='elu',
               name='block2_conv1',
               kernel_regularizer=l2(weight_decay))(x)
    x = BatchNormalization(name='block2_batch-norm1')(x)
    x = Conv2D(128, (3, 3),
               padding='same',
               activation='elu',
               name='block2_conv2',
               kernel_regularizer=l2(weight_decay))(x)
    x = BatchNormalization(name='block2_batch-norm2')(x)
    x = MaxPooling2D(pool_size=(2, 2), name='block2_pool')(x)
    x = Dropout(0.3, name='block2_dropout', seed=0)(x)

    # Block3.
    x = Conv2D(256, (3, 3),
               padding='same',
               activation='elu',
               name='block3_conv1',
               kernel_regularizer=l2(weight_decay))(x)
    x = BatchNormalization(name='block3_batch-norm1')(x)
    x = Conv2D(256, (3, 3),
               padding='same',
               activation='elu',
               name='block3_conv2',
               kernel_regularizer=l2(weight_decay))(x)
    x = BatchNormalization(name='block3_batch-norm2')(x)
    x = MaxPooling2D(pool_size=(2, 2), name='block3_pool')(x)
    x = Dropout(0.4, name='block3_dropout', seed=0)(x)

    # Add top layers.
    x = Flatten()(x)
    x = Dense(n_classes)(x)
    outputs = Activation('softmax', name='softmax')(x)

    # Create model.
    model = Model(inputs, outputs, name='cifar100_student_strong')

    # Load weights, if they exist.
    load_weights(weights_path, model)

    return model
Beispiel #13
0
 def build_model():
     inputs = Input(shape=(NORM_H, NORM_W, 3))
     # Block 1__
     x = Conv2D(
         64,
         (3, 3),
         padding="same",
         kernel_initializer="he_normal",
         kernel_regularizer=l2(1e-4),
         name="block1_conv1",
     )(inputs)
     x = Activation("relu")(x)
     x = Conv2D(
         64,
         (3, 3),
         padding="same",
         kernel_initializer="he_normal",
         kernel_regularizer=l2(1e-4),
         name="block1_conv2",
     )(x)
     x = Activation("relu")(x)
     x = MaxPooling2D(strides=(2, 2), name="block1_pool")(x)
     # Block 2
     x = Conv2D(
         128,
         (3, 3),
         padding="same",
         kernel_initializer="he_normal",
         kernel_regularizer=l2(1e-4),
         name="block2_conv1",
     )(x)
     x = Activation("relu")(x)
     x = Conv2D(
         128,
         (3, 3),
         padding="same",
         kernel_initializer="he_normal",
         kernel_regularizer=l2(1e-4),
         name="block2_conv2",
     )(x)
     x = Activation("relu")(x)
     x = MaxPooling2D(strides=(2, 2), name="block2_pool")(x)
     # Block 3
     x = Conv2D(
         256,
         (3, 3),
         padding="same",
         kernel_initializer="he_normal",
         kernel_regularizer=l2(1e-4),
         name="block3_conv1",
     )(x)
     x = Activation("relu")(x)
     x = Conv2D(
         256,
         (3, 3),
         padding="same",
         kernel_initializer="he_normal",
         kernel_regularizer=l2(1e-4),
         name="block3_conv2",
     )(x)
     x = Activation("relu")(x)
     x = Conv2D(
         256,
         (3, 3),
         padding="same",
         kernel_initializer="he_normal",
         kernel_regularizer=l2(1e-4),
         name="block3_conv3",
     )(x)
     x = Activation("relu")(x)
     x = MaxPooling2D(strides=(2, 2), name="block3_pool")(x)
     # Block 4
     x = Conv2D(
         512,
         (3, 3),
         padding="same",
         kernel_initializer="he_normal",
         kernel_regularizer=l2(1e-4),
         name="block4_conv1",
     )(x)
     x = Activation("relu")(x)
     x = Conv2D(
         512,
         (3, 3),
         padding="same",
         kernel_initializer="he_normal",
         kernel_regularizer=l2(1e-4),
         name="block4_conv2",
     )(x)
     x = Activation("relu")(x)
     x = Conv2D(
         512,
         (3, 3),
         padding="same",
         kernel_initializer="he_normal",
         kernel_regularizer=l2(1e-4),
         name="block4_conv3",
     )(x)
     x = Activation("relu")(x)
     x = MaxPooling2D(strides=(2, 2), name="block4_pool")(x)
     # Block 5
     x = Conv2D(
         512,
         (3, 3),
         padding="same",
         kernel_initializer="he_normal",
         kernel_regularizer=l2(1e-4),
         name="block5_conv1",
     )(x)
     x = Activation("relu")(x)
     x = Conv2D(
         512,
         (3, 3),
         padding="same",
         kernel_initializer="he_normal",
         kernel_regularizer=l2(1e-4),
         name="block5_conv2",
     )(x)
     x = Activation("relu")(x)
     x = Conv2D(
         512,
         (3, 3),
         padding="same",
         kernel_initializer="he_normal",
         kernel_regularizer=l2(1e-4),
         name="block5_conv3",
     )(x)
     x = Activation("relu")(x)
     x = MaxPooling2D(strides=(2, 2), name="block5_pool")(x)
     # Flatten
     x = Flatten(name="Flatten")(x)
     # Dimensions branch
     dimensions = Dense(512, name="d_fc_1")(x)
     dimensions = LeakyReLU(alpha=0.1)(dimensions)
     dimensions = Dropout(0.5)(dimensions)
     dimensions = Dense(3, name="d_fc_2")(dimensions)
     dimensions = LeakyReLU(alpha=0.1, name="dimensions")(dimensions)
     # Orientation branch
     orientation = Dense(256, name="o_fc_1")(x)
     orientation = LeakyReLU(alpha=0.1)(orientation)
     orientation = Dropout(0.5)(orientation)
     orientation = Dense(BIN * 2, name="o_fc_2")(orientation)
     orientation = LeakyReLU(alpha=0.1)(orientation)
     orientation = Reshape((BIN, -1))(orientation)
     orientation = Lambda(VGG16.l2_normalize,
                          name="orientation")(orientation)
     # Confidence branch
     confidence = Dense(256, name="c_fc_1")(x)
     confidence = LeakyReLU(alpha=0.1)(confidence)
     confidence = Dropout(0.5)(confidence)
     confidence = Dense(BIN, activation="softmax",
                        name="confidence")(confidence)
     # Build model
     return tf.keras.Model(inputs, [dimensions, orientation, confidence])
Beispiel #14
0
y_test = to_categorical(y_test, 6)

# input = Input(shape=(128, 128, 3))
# model = VGG16(weights=None, include_top=False, input_tensor=input, pooling='None')
# x = model.output
# x = Flatten()(x)
# x = Dense(4096, activation='relu')(x)
# x = Dense(4096, activation='relu')(x)
# predictions = Dense(6, activation='softmax')(x)
# model = tf.keras.Model(inputs=model.input, outputs=predictions)

k_size = (3,3)
input = Input(shape=(128, 128, 3))
x = Conv2D(32, k_size, padding='same', strides=2)(input)
x = BatchNormalization()(x)
x = Activation('relu')(x)

x = Conv2D(64, k_size, padding='same', strides=1)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)

x = MaxPool2D((2,2))(x)

x = Conv2D(128, k_size, padding='same', strides=2)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)

x = Conv2D(256, k_size, padding='same', strides=2)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
Beispiel #15
0
def Xception(include_top=True,
             weights='imagenet',
             input_tensor=None,
             input_shape=None,
             pooling=None,
             classes=1000):
    """Instantiates the Xception architecture.

  Optionally loads weights pre-trained
  on ImageNet. This model is available for TensorFlow only,
  and can only be used with inputs following the TensorFlow
  data format `(width, height, channels)`.
  You should set `image_data_format='channels_last'` in your Keras config
  located at ~/.keras/keras.json.

  Note that the default input image size for this model is 299x299.

  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)`.
          It should have exactly 3 inputs channels,
          and width and height should be no smaller than 71.
          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.
      RuntimeError: If attempting to run this model with a
          backend that does not support separable convolutions.
  """
    if not (weights in {'imagenet', None} or os.path.exists(weights)):
        raise ValueError('The `weights` argument should be either '
                         '`None` (random initialization), `imagenet` '
                         '(pre-training on ImageNet), '
                         'or the path to the weights file to be loaded.')

    if weights == 'imagenet' and include_top and classes != 1000:
        raise ValueError('If using `weights` as imagenet with `include_top`'
                         ' as true, `classes` should be 1000')

    if K.image_data_format() != 'channels_last':
        logging.warning(
            'The Xception model is only available for the '
            'input data format "channels_last" '
            '(width, height, channels). '
            'However your settings specify the default '
            'data format "channels_first" (channels, width, height). '
            'You should set `image_data_format="channels_last"` in your Keras '
            'config located at ~/.keras/keras.json. '
            'The model being returned right now will expect inputs '
            'to follow the "channels_last" data format.')
        K.set_image_data_format('channels_last')
        old_data_format = 'channels_first'
    else:
        old_data_format = None

    # Determine proper input shape
    input_shape = _obtain_input_shape(input_shape,
                                      default_size=299,
                                      min_size=71,
                                      data_format=K.image_data_format(),
                                      require_flatten=False,
                                      weights=weights)

    if input_tensor is None:
        img_input = Input(shape=input_shape)
    else:
        if not K.is_keras_tensor(input_tensor):
            img_input = Input(tensor=input_tensor, shape=input_shape)
        else:
            img_input = input_tensor

    x = Conv2D(32, (3, 3), strides=(2, 2), use_bias=False,
               name='block1_conv1')(img_input)
    x = BatchNormalization(name='block1_conv1_bn')(x)
    x = Activation('relu', name='block1_conv1_act')(x)
    x = Conv2D(64, (3, 3), use_bias=False, name='block1_conv2')(x)
    x = BatchNormalization(name='block1_conv2_bn')(x)
    x = Activation('relu', name='block1_conv2_act')(x)

    residual = Conv2D(128, (1, 1),
                      strides=(2, 2),
                      padding='same',
                      use_bias=False)(x)
    residual = BatchNormalization()(residual)

    x = SeparableConv2D(128, (3, 3),
                        padding='same',
                        use_bias=False,
                        name='block2_sepconv1')(x)
    x = BatchNormalization(name='block2_sepconv1_bn')(x)
    x = Activation('relu', name='block2_sepconv2_act')(x)
    x = SeparableConv2D(128, (3, 3),
                        padding='same',
                        use_bias=False,
                        name='block2_sepconv2')(x)
    x = BatchNormalization(name='block2_sepconv2_bn')(x)

    x = MaxPooling2D((3, 3),
                     strides=(2, 2),
                     padding='same',
                     name='block2_pool')(x)
    x = layers.add([x, residual])

    residual = Conv2D(256, (1, 1),
                      strides=(2, 2),
                      padding='same',
                      use_bias=False)(x)
    residual = BatchNormalization()(residual)

    x = Activation('relu', name='block3_sepconv1_act')(x)
    x = SeparableConv2D(256, (3, 3),
                        padding='same',
                        use_bias=False,
                        name='block3_sepconv1')(x)
    x = BatchNormalization(name='block3_sepconv1_bn')(x)
    x = Activation('relu', name='block3_sepconv2_act')(x)
    x = SeparableConv2D(256, (3, 3),
                        padding='same',
                        use_bias=False,
                        name='block3_sepconv2')(x)
    x = BatchNormalization(name='block3_sepconv2_bn')(x)

    x = MaxPooling2D((3, 3),
                     strides=(2, 2),
                     padding='same',
                     name='block3_pool')(x)
    x = layers.add([x, residual])

    residual = Conv2D(728, (1, 1),
                      strides=(2, 2),
                      padding='same',
                      use_bias=False)(x)
    residual = BatchNormalization()(residual)

    x = Activation('relu', name='block4_sepconv1_act')(x)
    x = SeparableConv2D(728, (3, 3),
                        padding='same',
                        use_bias=False,
                        name='block4_sepconv1')(x)
    x = BatchNormalization(name='block4_sepconv1_bn')(x)
    x = Activation('relu', name='block4_sepconv2_act')(x)
    x = SeparableConv2D(728, (3, 3),
                        padding='same',
                        use_bias=False,
                        name='block4_sepconv2')(x)
    x = BatchNormalization(name='block4_sepconv2_bn')(x)

    x = MaxPooling2D((3, 3),
                     strides=(2, 2),
                     padding='same',
                     name='block4_pool')(x)
    x = layers.add([x, residual])

    for i in range(8):
        residual = x
        prefix = 'block' + str(i + 5)

        x = Activation('relu', name=prefix + '_sepconv1_act')(x)
        x = SeparableConv2D(728, (3, 3),
                            padding='same',
                            use_bias=False,
                            name=prefix + '_sepconv1')(x)
        x = BatchNormalization(name=prefix + '_sepconv1_bn')(x)
        x = Activation('relu', name=prefix + '_sepconv2_act')(x)
        x = SeparableConv2D(728, (3, 3),
                            padding='same',
                            use_bias=False,
                            name=prefix + '_sepconv2')(x)
        x = BatchNormalization(name=prefix + '_sepconv2_bn')(x)
        x = Activation('relu', name=prefix + '_sepconv3_act')(x)
        x = SeparableConv2D(728, (3, 3),
                            padding='same',
                            use_bias=False,
                            name=prefix + '_sepconv3')(x)
        x = BatchNormalization(name=prefix + '_sepconv3_bn')(x)

        x = layers.add([x, residual])

    residual = Conv2D(1024, (1, 1),
                      strides=(2, 2),
                      padding='same',
                      use_bias=False)(x)
    residual = BatchNormalization()(residual)

    x = Activation('relu', name='block13_sepconv1_act')(x)
    x = SeparableConv2D(728, (3, 3),
                        padding='same',
                        use_bias=False,
                        name='block13_sepconv1')(x)
    x = BatchNormalization(name='block13_sepconv1_bn')(x)
    x = Activation('relu', name='block13_sepconv2_act')(x)
    x = SeparableConv2D(1024, (3, 3),
                        padding='same',
                        use_bias=False,
                        name='block13_sepconv2')(x)
    x = BatchNormalization(name='block13_sepconv2_bn')(x)

    x = MaxPooling2D((3, 3),
                     strides=(2, 2),
                     padding='same',
                     name='block13_pool')(x)
    x = layers.add([x, residual])

    x = SeparableConv2D(1536, (3, 3),
                        padding='same',
                        use_bias=False,
                        name='block14_sepconv1')(x)
    x = BatchNormalization(name='block14_sepconv1_bn')(x)
    x = Activation('relu', name='block14_sepconv1_act')(x)

    x = SeparableConv2D(2048, (3, 3),
                        padding='same',
                        use_bias=False,
                        name='block14_sepconv2')(x)
    x = BatchNormalization(name='block14_sepconv2_bn')(x)
    x = Activation('relu', name='block14_sepconv2_act')(x)

    if include_top:
        x = GlobalAveragePooling2D(name='avg_pool')(x)
        x = Dense(classes, activation='softmax', name='predictions')(x)
    else:
        if pooling == 'avg':
            x = GlobalAveragePooling2D()(x)
        elif pooling == 'max':
            x = GlobalMaxPooling2D()(x)
        x = Flatten(name='custom')(x)  ##DB

    # Ensure that the model takes into account
    # any potential predecessors of `input_tensor`.
    if input_tensor is not None:
        inputs = layer_utils.get_source_inputs(input_tensor)
    else:
        inputs = img_input
    # Create model.
    model = Model(inputs, x, name='xception')

    # load weights
    if weights == 'imagenet':
        if include_top:
            weights_path = get_file(
                'xception_weights_tf_dim_ordering_tf_kernels.h5',
                TF_WEIGHTS_PATH,
                cache_subdir='models',
                file_hash='0a58e3b7378bc2990ea3b43d5981f1f6')
        else:
            weights_path = get_file(
                'xception_weights_tf_dim_ordering_tf_kernels_notop.h5',
                TF_WEIGHTS_PATH_NO_TOP,
                cache_subdir='models',
                file_hash='b0042744bf5b25fce3cb969f33bebb97')
        model.load_weights(weights_path)
    elif weights is not None:
        model.load_weights(weights)

    if old_data_format:
        K.set_image_data_format(old_data_format)
    return model
Beispiel #16
0
 def act_blend(self, linear_input):
     full_conv_relu = Activation('relu')(linear_input)
     return full_conv_relu
     full_conv_sigmoid = Activation('sigmoid')(linear_input)
     full_conv = concatenate([full_conv_relu, full_conv_sigmoid], axis=1)
     return full_conv
model.add(
    LSTM(128,
         return_sequences=True,
         dropout=0.2,
         recurrent_dropout=0.2,
         activation='relu',
         input_shape=(99 * 3, 52)))
model.add(
    LSTM(128,
         name='dense_3',
         return_sequences=True,
         dropout=0.2,
         recurrent_dropout=0.2,
         activation='relu'))
model.add(TimeDistributed(Dense(2)))
model.add(Activation('sigmoid'))

time_distributed_merge_layer = tf.keras.layers.Lambda(
    function=lambda x: tf.keras.backend.mean(x, axis=1))
model.add(time_distributed_merge_layer)

model.compile(optimizer="Adam",
              loss="categorical_crossentropy",
              metrics=['accuracy'])

print(y_train)
print(y_test)

model.fit(x=X_train,
          y=y_train,
          epochs=5,
Beispiel #18
0
def __merge_temporal_features(feature,
                              mode='conv',
                              feature_size=256,
                              frames_per_batch=1):
    """ Merges feature with its temporal residual through addition.
    Input feature (x) --> Temporal convolution* --> Residual feature (x')
    *Type of temporal convolution specified by "mode" argument
    Output: y = x + x'

    Args:
        feature (tensorflow.keras.layers.Layer): Input layer
        mode (str, optional): Mode of temporal convolution. Choose from
            {'conv','lstm','gru', None}. Defaults to 'conv'.
        feature_size (int, optional): Defaults to 256.
        frames_per_batch (int, optional): Defaults to 1.

    Raises:
        ValueError: Mode not 'conv', 'lstm', 'gru' or None

    Returns:
        tensorflow.keras.layers.Layer: Input feature merged with its residual
            from a temporal convolution. If mode is None,
                the output is exactly the input.
    """
    # Check inputs to mode
    acceptable_modes = {'conv', 'lstm', 'gru', None}
    if mode is not None:
        mode = str(mode).lower()
        if mode not in acceptable_modes:
            raise ValueError('Mode {} not supported. Please choose '
                             'from {}.'.format(mode, str(acceptable_modes)))

    f_name = str(feature.name)[:2]

    if mode == 'conv':
        x = Conv3D(
            feature_size,
            (frames_per_batch, 3, 3),
            strides=(1, 1, 1),
            padding='same',
            name='conv3D_mtf_{}'.format(f_name),
        )(feature)
        x = BatchNormalization(axis=-1, name='bnorm_mtf_{}'.format(f_name))(x)
        x = Activation('relu', name='acti_mtf_{}'.format(f_name))(x)
    elif mode == 'lstm':
        x = ConvLSTM2D(feature_size, (3, 3),
                       padding='same',
                       activation='relu',
                       return_sequences=True,
                       name='convLSTM_mtf_{}'.format(f_name))(feature)
    elif mode == 'gru':
        x = ConvGRU2D(feature_size, (3, 3),
                      padding='same',
                      activation='relu',
                      return_sequences=True,
                      name='convGRU_mtf_{}'.format(f_name))(feature)
    else:
        x = feature

    temporal_feature = x

    return temporal_feature
Beispiel #19
0
name = 'SKIP-GRAM_example'
layers = [{
    'Size': 100,
    'Activation': 'relu'
}, {
    'Size': 75,
    'Activation': 'relu'
}, {
    'Size': len(word_bank),
    'Activation': 'softmax'
}]
#Build model --------------------------------->
model = Sequential()
model.add(Flatten())  #Must flatten layer if going from 2D to 1D (dense)
model.add(Dense(layers[0]['Size'], input_shape=data['context'].shape[1:]))
model.add(Activation(layers[0]['Activation']))
for l in layers[1:]:
    model.add(Dropout(0.5))
    model.add(Dense(l['Size']))
    model.add(Activation(l['Activation']))
#Train model --------------------------------->
model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])
model.fit(X, y, epochs=100, validation_split=0.3, callbacks=[tensorboard])

#Save & log new model ------------------------>
if os.path.exists('{}/logs/models.csv'.format(locations['main'])):
    data = load.readData('{}/logs/models.csv'.format(locations['main']))
else:
    data = pd.DataFrame()
Beispiel #20
0
def __create_pyramid_features(C3, C4, C5, feature_size=256):
    """Creates the FPN layers on top of the backbone features.

    Args:
        C3: Feature stage C3 from the backbone.
        C4: Feature stage C4 from the backbone.
        C5: Feature stage C5 from the backbone.
        feature_size: The feature size to use for the resulting feature levels.

    Returns:
        A list of feature levels [P3, P4, P5, P6, P7].
    """

    # upsample C5 to get P5 from the FPN paper
    P5 = Conv3D(feature_size,
                kernel_size=1,
                strides=1,
                padding='same',
                name='C5_reduced')(C5)

    P5_upsampled = UpsampleLike3D(name='P5_upsampled')([P5, C4])
    P5 = Conv3D(feature_size,
                kernel_size=3,
                strides=1,
                padding='same',
                name='P5')(P5)

    # add P5 elementwise to C4
    P4 = Conv3D(feature_size,
                kernel_size=1,
                strides=1,
                padding='same',
                name='C4_reduced')(C4)
    P4 = Add(name='P4_merged')([P5_upsampled, P4])
    P4_upsampled = UpsampleLike3D(name='P4_upsampled')([P4, C3])
    P4 = Conv3D(feature_size,
                kernel_size=3,
                strides=1,
                padding='same',
                name='P4')(P4)

    # add P4 elementwise to C3
    P3 = Conv3D(feature_size,
                kernel_size=1,
                strides=1,
                padding='same',
                name='C3_reduced')(C3)
    P3 = Add(name='P3_merged')([P4_upsampled, P3])
    P3 = Conv3D(feature_size,
                kernel_size=3,
                strides=1,
                padding='same',
                name='P3')(P3)

    # "P6 is obtained via a 3x3 stride-2 conv on C5"
    P6 = Conv3D(feature_size,
                kernel_size=3,
                strides=2,
                padding='same',
                name='P6')(C5)

    # "P7 is computed by applying ReLU followed by a 3x3 stride-2 conv on P6"
    P7 = Activation('relu', name='C6_relu')(P6)
    P7 = Conv3D(feature_size,
                kernel_size=3,
                strides=2,
                padding='same',
                name='P7')(P7)

    return [P3, P4, P5, P6, P7]
Beispiel #21
0
print(np.shape(train))
print(np.shape(test))
# In[3]: fitur ektraksi
train_input = train[:, :-10]
train_labels = train[:, -10:]
# In[3]: fitur ektraksi
test_input = test[:, :-10]
test_labels = test[:, -10:]
# In[3]: fitur ektraksi
print(np.shape(train_input))
print(np.shape(train_labels))

# In[3]: membuat seq NN, layer pertama dense dari 100 neurons, no 7
model = Sequential([
    Dense(100, input_dim=np.shape(train_input)[1]),
    Activation('relu'),
    Dense(10),
    Activation('softmax'),
])
# In[3]: fitur ektraksi no 8
model.compile(optimizer='adam',
              loss='categorical_crossentropy',
              metrics=['accuracy'])
print(model.summary())
# In[3]: fitur ektraksi, no 9
model.fit(train_input,
          train_labels,
          epochs=10,
          batch_size=32,
          validation_split=0.2)
# In[3]: fitur ektraksi no 10
gen.add(BatchNormalization(momentum=0.9))
gen.add(LeakyReLU(alpha=0.2))

gen.add(UpSampling2D())
gen.add(Conv2DTranspose(int(channel_depth / 8), 5, padding='same'))
gen.add(BatchNormalization(momentum=0.9))
gen.add(LeakyReLU(alpha=0.2))
'''
gen.add(Conv2DTranspose(int(channel_depth / 8), 5, padding='same'))
gen.add(BatchNormalization(momentum=0.9))
gen.add(Activation('relu'))
'''

# Out: 28 x 28 x 1
gen.add(Conv2DTranspose(1, 5, padding='same'))
gen.add(Activation('tanh'))

# now the discriminator
channel_depth = 8
dropout = 0.4

D = Sequential()
D.add(InputLayer(input_shape=(img_size_flat, )))
D.add(Reshape(img_size_full))

D.add(Conv2D(channel_depth * 1, 5, strides=2, padding='same'))
D.add(BatchNormalization(momentum=0.9))
D.add(LeakyReLU(alpha=0.2))
D.add(Dropout(dropout))

D.add(Conv2D(channel_depth * 2, 5, strides=2, padding='same'))
Beispiel #23
0
def create_model_v1():
    """
        description: Defining the Model with keras backend
            
        output: 1. The Model
    """
    model = Sequential()

    # 1st Convolutional Layer
    model.add(Conv2D(filters=96, input_shape=(img_rows, img_cols, color_type), kernel_size=(11,11),strides=(4,4), padding='valid'))
    model.add(Activation('relu'))
    # Pooling 
    model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='valid'))
    # Batch Normalisation before passing it to the next layer
    model.add(BatchNormalization())

    # 2nd Convolutional Layer
    model.add(Conv2D(filters=256, kernel_size=(11,11), strides=(1,1), padding='valid'))
    model.add(Activation('relu'))
    # Pooling
    model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='valid'))
    # Batch Normalisation
    model.add(BatchNormalization())

    # 3rd Convolutional Layer
    model.add(Conv2D(filters=384, kernel_size=(3,3), strides=(1,1), padding='valid'))
    model.add(Activation('relu'))
    # Batch Normalisation
    model.add(BatchNormalization())

    # 4th Convolutional Layer
    model.add(Conv2D(filters=384, kernel_size=(3,3), strides=(1,1), padding='valid'))
    model.add(Activation('relu'))
    # Batch Normalisation
    model.add(BatchNormalization())

    # 5th Convolutional Layer
    model.add(Conv2D(filters=256, kernel_size=(3,3), strides=(1,1), padding='valid'))
    model.add(Activation('relu'))
    # Pooling
    model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='valid'))
    # Batch Normalisation
    model.add(BatchNormalization())

    # Passing it to a dense layer
    model.add(Flatten())
    # 1st Dense Layer
    model.add(Dense(4096, input_shape=(224*224*3,)))
    model.add(Activation('relu'))
    # Add Dropout to prevent overfitting
    model.add(Dropout(0.4))
    # Batch Normalisation
    model.add(BatchNormalization())

    # 2nd Dense Layer
    model.add(Dense(4096))
    model.add(Activation('relu'))
    # Add Dropout
    model.add(Dropout(0.4))
    # Batch Normalisation
    model.add(BatchNormalization())

    # 3rd Dense Layer
    model.add(Dense(1000))
    model.add(Activation('relu'))
    # Add Dropout
    model.add(Dropout(0.4))
    # Batch Normalisation
    model.add(BatchNormalization())

    # Output Layer
    model.add(Dense(10))
    model.add(Activation('softmax'))
    
    return model
Beispiel #24
0
def generator(batch_size, gf_dim, ch, rows, cols):
    """
    Generator model.

    Generates image from compressed image representation (vector Z).

    Input: Z (compressed image representation)
    Output: generated image
    """
    model = Sequential()

    # Input layer.
    model.add(
        Dense(gf_dim * 8 * rows[0] * cols[0],
              name="g_h0_lin",
              batch_input_shape=(batch_size, z_dim),
              kernel_initializer=initializers.random_normal(stddev=.02)))
    model.add(Reshape((rows[0], cols[0], gf_dim * 8)))
    model.add(
        BN(axis=3,
           name="g_bn0",
           epsilon=1e-5,
           gamma_initializer=initializers.random_normal(mean=1., stddev=.02)))
    model.add(Activation("relu"))

    # Layer 1.
    model.add(
        Conv2DTranspose(
            gf_dim * 4,
            kernel_size=(5, 5),
            strides=(2, 2),
            padding="same",
            name="g_h1",
            kernel_initializer=initializers.random_normal(stddev=.02)))
    model.add(
        BN(axis=3,
           name="g_bn1",
           epsilon=1e-5,
           gamma_initializer=initializers.random_normal(mean=1., stddev=.02)))
    model.add(Activation("relu"))

    # Layer 2.
    model.add(
        Conv2DTranspose(
            gf_dim * 2,
            kernel_size=(5, 5),
            strides=(2, 2),
            padding="same",
            name="g_h2",
            kernel_initializer=initializers.random_normal(stddev=.02)))
    model.add(
        BN(axis=3,
           name="g_bn2",
           epsilon=1e-5,
           gamma_initializer=initializers.random_normal(mean=1., stddev=.02)))
    model.add(Activation("relu"))

    # Layer 3.
    model.add(
        Conv2DTranspose(
            gf_dim,
            kernel_size=(5, 5),
            strides=(2, 2),
            padding="same",
            name="g_h3",
            kernel_initializer=initializers.random_normal(stddev=.02)))
    model.add(
        BN(axis=3,
           name="g_bn3",
           epsilon=1e-5,
           gamma_initializer=initializers.random_normal(mean=1., stddev=.02)))
    model.add(Activation("relu"))

    # Output layer.
    model.add(
        Conv2DTranspose(
            ch,
            kernel_size=(5, 5),
            strides=(2, 2),
            padding="same",
            name="g_h4",
            kernel_initializer=initializers.random_normal(stddev=.02)))
    model.add(Activation("tanh"))

    return model
    def build(input_shape=None, classes=3):

        img_input = Input(shape=input_shape)

        channel_axis = 1 if image_data_format() == 'channels_first' else -1

        x = Conv2D(32, (3, 3),
                   strides=(2, 2),
                   use_bias=False,
                   name='block1_conv1')(img_input)
        x = BatchNormalization(axis=channel_axis, name='block1_conv1_bn')(x)
        x = Activation('relu', name='block1_conv1_act')(x)
        x = Conv2D(64, (3, 3), use_bias=False, name='block1_conv2')(x)
        x = BatchNormalization(axis=channel_axis, name='block1_conv2_bn')(x)
        x = Activation('relu', name='block1_conv2_act')(x)

        residual = Conv2D(128, (1, 1),
                          strides=(2, 2),
                          padding='same',
                          use_bias=False)(x)
        residual = BatchNormalization(axis=channel_axis)(residual)

        x = SeparableConv2D(128, (3, 3),
                            padding='same',
                            use_bias=False,
                            name='block2_sepconv1')(x)
        x = BatchNormalization(axis=channel_axis, name='block2_sepconv1_bn')(x)
        x = Activation('relu', name='block2_sepconv2_act')(x)
        x = SeparableConv2D(128, (3, 3),
                            padding='same',
                            use_bias=False,
                            name='block2_sepconv2')(x)
        x = BatchNormalization(axis=channel_axis, name='block2_sepconv2_bn')(x)

        x = MaxPooling2D((3, 3),
                         strides=(2, 2),
                         padding='same',
                         name='block2_pool')(x)
        x = add([x, residual])

        residual = Conv2D(256, (1, 1),
                          strides=(2, 2),
                          padding='same',
                          use_bias=False)(x)
        residual = BatchNormalization(axis=channel_axis)(residual)

        x = Activation('relu', name='block3_sepconv1_act')(x)
        x = SeparableConv2D(256, (3, 3),
                            padding='same',
                            use_bias=False,
                            name='block3_sepconv1')(x)
        x = BatchNormalization(axis=channel_axis, name='block3_sepconv1_bn')(x)
        x = Activation('relu', name='block3_sepconv2_act')(x)
        x = SeparableConv2D(256, (3, 3),
                            padding='same',
                            use_bias=False,
                            name='block3_sepconv2')(x)
        x = BatchNormalization(axis=channel_axis, name='block3_sepconv2_bn')(x)

        x = MaxPooling2D((3, 3),
                         strides=(2, 2),
                         padding='same',
                         name='block3_pool')(x)
        x = add([x, residual])

        residual = Conv2D(728, (1, 1),
                          strides=(2, 2),
                          padding='same',
                          use_bias=False)(x)
        residual = BatchNormalization(axis=channel_axis)(residual)

        x = Activation('relu', name='block4_sepconv1_act')(x)
        x = SeparableConv2D(728, (3, 3),
                            padding='same',
                            use_bias=False,
                            name='block4_sepconv1')(x)
        x = BatchNormalization(axis=channel_axis, name='block4_sepconv1_bn')(x)
        x = Activation('relu', name='block4_sepconv2_act')(x)
        x = SeparableConv2D(728, (3, 3),
                            padding='same',
                            use_bias=False,
                            name='block4_sepconv2')(x)
        x = BatchNormalization(axis=channel_axis, name='block4_sepconv2_bn')(x)

        x = MaxPooling2D((3, 3),
                         strides=(2, 2),
                         padding='same',
                         name='block4_pool')(x)
        x = add([x, residual])

        for i in range(8):
            residual = x
            prefix = 'block' + str(i + 5)

            x = Activation('relu', name=prefix + '_sepconv1_act')(x)
            x = SeparableConv2D(728, (3, 3),
                                padding='same',
                                use_bias=False,
                                name=prefix + '_sepconv1')(x)
            x = BatchNormalization(axis=channel_axis,
                                   name=prefix + '_sepconv1_bn')(x)
            x = Activation('relu', name=prefix + '_sepconv2_act')(x)
            x = SeparableConv2D(728, (3, 3),
                                padding='same',
                                use_bias=False,
                                name=prefix + '_sepconv2')(x)
            x = BatchNormalization(axis=channel_axis,
                                   name=prefix + '_sepconv2_bn')(x)
            x = Activation('relu', name=prefix + '_sepconv3_act')(x)
            x = SeparableConv2D(728, (3, 3),
                                padding='same',
                                use_bias=False,
                                name=prefix + '_sepconv3')(x)
            x = BatchNormalization(axis=channel_axis,
                                   name=prefix + '_sepconv3_bn')(x)

            x = add([x, residual])

        residual = Conv2D(1024, (1, 1),
                          strides=(2, 2),
                          padding='same',
                          use_bias=False)(x)
        residual = BatchNormalization(axis=channel_axis)(residual)

        x = Activation('relu', name='block13_sepconv1_act')(x)
        x = SeparableConv2D(728, (3, 3),
                            padding='same',
                            use_bias=False,
                            name='block13_sepconv1')(x)
        x = BatchNormalization(axis=channel_axis,
                               name='block13_sepconv1_bn')(x)
        x = Activation('relu', name='block13_sepconv2_act')(x)
        x = SeparableConv2D(1024, (3, 3),
                            padding='same',
                            use_bias=False,
                            name='block13_sepconv2')(x)
        x = BatchNormalization(axis=channel_axis,
                               name='block13_sepconv2_bn')(x)

        x = MaxPooling2D((3, 3),
                         strides=(2, 2),
                         padding='same',
                         name='block13_pool')(x)
        x = add([x, residual])

        x = SeparableConv2D(1536, (3, 3),
                            padding='same',
                            use_bias=False,
                            name='block14_sepconv1')(x)
        x = BatchNormalization(axis=channel_axis,
                               name='block14_sepconv1_bn')(x)
        x = Activation('relu', name='block14_sepconv1_act')(x)

        x = SeparableConv2D(2048, (3, 3),
                            padding='same',
                            use_bias=False,
                            name='block14_sepconv2')(x)
        x = BatchNormalization(axis=channel_axis,
                               name='block14_sepconv2_bn')(x)
        x = Activation('relu', name='block14_sepconv2_act')(x)
        x = GlobalAveragePooling2D(name='avg_pool')(x)
        x = Dropout(0.25)(x)
        # softmax classifier
        x = Flatten()(x)
        x = Dense(classes)(x)
        x = Activation("softmax")(x)

        inputs = img_input
        # Create model.
        return Model(inputs, x, name='xception')
def build_3d_cnn(w, h, d, s, num_outputs):
    #Credit: https://github.com/jessecha/DNRacing/blob/master/3D_CNN_Model/model.py
    '''
        w : width
        h : height
        d : depth
        s : n_stacked
    '''
    input_shape=(s, h, w, d)

    model = Sequential()
    #First layer
    #model.add(Cropping3D(cropping=((0,0), (50,10), (0,0)), input_shape=input_shape) ) #trim pixels off top
    
    # Second layer
    model.add(Conv3D(
        filters=16, kernel_size=(3,3,3), strides=(1,3,3),
        data_format='channels_last', padding='same', input_shape=input_shape)
    )
    model.add(Activation('relu'))
    model.add(MaxPooling3D(
        pool_size=(1,2,2), strides=(1,2,2), padding='valid', data_format=None)
    )
    # Third layer
    model.add(Conv3D(
        filters=32, kernel_size=(3,3,3), strides=(1,1,1),
        data_format='channels_last', padding='same')
    )
    model.add(Activation('relu'))
    model.add(MaxPooling3D(
        pool_size=(1, 2, 2), strides=(1,2,2), padding='valid', data_format=None)
    )
    # Fourth layer
    model.add(Conv3D(
        filters=64, kernel_size=(3,3,3), strides=(1,1,1),
        data_format='channels_last', padding='same')
    )
    model.add(Activation('relu'))
    model.add(MaxPooling3D(
        pool_size=(1,2,2), strides=(1,2,2), padding='valid', data_format=None)
    )
    # Fifth layer
    model.add(Conv3D(
        filters=128, kernel_size=(3,3,3), strides=(1,1,1),
        data_format='channels_last', padding='same')
    )
    model.add(Activation('relu'))
    model.add(MaxPooling3D(
        pool_size=(1,2,2), strides=(1,2,2), padding='valid', data_format=None)
    )
    # Fully connected layer
    model.add(Flatten())

    model.add(Dense(256))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(Dropout(0.5))

    model.add(Dense(256))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(Dropout(0.5))

    model.add(Dense(num_outputs))
    #model.add(Activation('tanh'))

    return model
Beispiel #27
0
def _inverted_res_block(inputs,
                        expansion,
                        stride,
                        alpha,
                        filters,
                        block_id,
                        skip_connection,
                        rate=1):
    # in_channels = inputs.shape[-1].value  # inputs._keras_shape[-1]
    in_channels = inputs.shape[-1]
    pointwise_conv_filters = int(filters * alpha)
    pointwise_filters = _make_divisible(pointwise_conv_filters, 8)
    x = inputs
    prefix = 'expanded_conv_{}_'.format(block_id)
    if block_id:
        # Expand

        x = Conv2D(expansion * in_channels,
                   kernel_size=1,
                   padding='same',
                   use_bias=False,
                   activation=None,
                   name=prefix + 'expand')(x)
        x = BatchNormalization(epsilon=1e-3,
                               momentum=0.999,
                               name=prefix + 'expand_BN')(x)
        x = Activation(tf.nn.relu6, name=prefix + 'expand_relu')(x)
    else:
        prefix = 'expanded_conv_'
    # Depthwise
    x = DepthwiseConv2D(kernel_size=3,
                        strides=stride,
                        activation=None,
                        use_bias=False,
                        padding='same',
                        dilation_rate=(rate, rate),
                        name=prefix + 'depthwise')(x)
    x = BatchNormalization(epsilon=1e-3,
                           momentum=0.999,
                           name=prefix + 'depthwise_BN')(x)

    x = Activation(tf.nn.relu6, name=prefix + 'depthwise_relu')(x)

    # Project
    x = Conv2D(pointwise_filters,
               kernel_size=1,
               padding='same',
               use_bias=False,
               activation=None,
               name=prefix + 'project')(x)
    x = BatchNormalization(epsilon=1e-3,
                           momentum=0.999,
                           name=prefix + 'project_BN')(x)

    if skip_connection:
        return Add(name=prefix + 'add')([inputs, x])

    # if in_channels == pointwise_filters and stride == 1:
    #    return Add(name='res_connect_' + str(block_id))([inputs, x])

    return x
def SqueezeNet(include_top=True,
               weights="imagenet",
               model_input=None,
               non_top_pooling=None,
               num_classes=1000,
               model_path="",
               initial_num_classes=None,
               transfer_with_full_training=True):

    if (weights == "imagenet" and num_classes != 1000):
        raise ValueError(
            "You must parse in SqueezeNet model trained on the 1000 class ImageNet"
        )

    image_input = model_input

    network = Conv2D(64, (3, 3), strides=(2, 2), padding="valid")(image_input)
    network = Activation("relu")(network)
    network = MaxPool2D(pool_size=(3, 3), strides=(2, 2))(network)

    network = squeezenet_fire_module(input=network,
                                     input_channel_small=16,
                                     input_channel_large=64)
    network = squeezenet_fire_module(input=network,
                                     input_channel_small=16,
                                     input_channel_large=64)
    network = MaxPool2D(pool_size=(3, 3), strides=(2, 2))(network)

    network = squeezenet_fire_module(input=network,
                                     input_channel_small=32,
                                     input_channel_large=128)
    network = squeezenet_fire_module(input=network,
                                     input_channel_small=32,
                                     input_channel_large=128)
    network = MaxPool2D(pool_size=(3, 3), strides=(2, 2))(network)

    network = squeezenet_fire_module(input=network,
                                     input_channel_small=48,
                                     input_channel_large=192)
    network = squeezenet_fire_module(input=network,
                                     input_channel_small=48,
                                     input_channel_large=192)
    network = squeezenet_fire_module(input=network,
                                     input_channel_small=64,
                                     input_channel_large=256)
    network = squeezenet_fire_module(input=network,
                                     input_channel_small=64,
                                     input_channel_large=256)

    if (include_top):
        network = Dropout(0.5)(network)

        if (initial_num_classes != None):
            network = Conv2D(initial_num_classes,
                             kernel_size=(1, 1),
                             padding="valid",
                             name="last_conv")(network)
        else:
            network = Conv2D(num_classes,
                             kernel_size=(1, 1),
                             padding="valid",
                             name="last_conv")(network)
        network = Activation("relu")(network)

        network = GlobalAvgPool2D()(network)
        network = Activation("softmax")(network)

    else:
        if (non_top_pooling == "Average"):
            network = GlobalAvgPool2D()(network)
        elif (non_top_pooling == "Maximum"):
            network = GlobalMaxPool2D()(network)
        elif (non_top_pooling == None):
            pass

    input_image = image_input
    model = Model(inputs=input_image, outputs=network)

    if (weights == "imagenet"):
        weights_path = model_path
        model.load_weights(weights_path)
        return model
    elif (weights == "trained"):
        weights_path = model_path
        model.load_weights(weights_path)
        return model
    elif (weights == "continued"):
        weights_path = model_path
        model.load_weights(weights_path)
        return model
    elif (weights == "transfer"):
        weights_path = model_path
        model.load_weights(weights_path)

        if (transfer_with_full_training == False):
            for eachlayer in model.layers:
                eachlayer.trainable = False
            print("Training with top layers of the Model")
        else:
            print("Training with all layers of the Model")

        network2 = model.layers[-5].output

        network2 = Conv2D(num_classes,
                          kernel_size=(1, 1),
                          padding="valid",
                          name="last_conv")(network2)
        network2 = Activation("relu")(network2)

        network2 = GlobalAvgPool2D()(network2)
        network2 = Activation("softmax")(network2)

        new_model = Model(inputs=model.input, outputs=network2)

        return new_model
    elif (weights == "custom"):
        return model
Beispiel #29
0
    def __init__(self, data_format='channels_first', domain_a_image_channels=3, domain_b_image_channels=1):
        # Encoding layers
        self.g_en_conv0_a = tf.make_template('conv0_a',
                                             LeakyReLUBNNSConv2d(n_filters=64, kernel_size=(5, 5), stride=(2, 2),
                                                                 padding='same', data_format=data_format))
        self.g_en_conv0_b = tf.make_template('conv0_b',
                                             LeakyReLUBNNSConv2d(n_filters=64, kernel_size=(5, 5), stride=(2, 2),
                                                                 padding='same', data_format=data_format))
        self.g_en_conv1 = tf.make_template('conv1',
                                           LeakyReLUBNNSConv2d(n_filters=128, kernel_size=(5, 5), stride=(2, 2),
                                                               padding='same', data_format=data_format))
        self.g_en_conv2 = tf.make_template('conv2',
                                           LeakyReLUBNNSConv2d(n_filters=256, kernel_size=(8, 8), stride=(1, 1),
                                                               padding='valid', data_format=data_format))
        self.g_en_conv3 = tf.make_template('conv3',
                                           LeakyReLUBNNSConv2d(n_filters=512, kernel_size=(1, 1), stride=(1, 1),
                                                               padding='valid', data_format=data_format))

        # Layer to generate the latent variables
        self.g_vae = tf.make_template('vae',
                                      GaussianVAE2D(n_filters=512, kernel_size=(1, 1), stride=(1, 1), padding='valid',
                                                    data_format=data_format))

        # Decoding layers
        # We will reconstruct the images using transposed convolutional layers followed by a tanh activation function
        # because the input image has been normalized such that the value of any pixel is between -1 and 1.
        self.g_de_conv0 = tf.make_template('de_conv0',
                                           LeakyReLUBNNSConvTranspose2d(n_filters=512, kernel_size=(4, 4),
                                                                        stride=(2, 2),
                                                                        padding='valid', data_format=data_format))

        self.g_de_conv1 = tf.make_template('de_conv1',
                                           LeakyReLUBNNSConvTranspose2d(n_filters=256, kernel_size=(4, 4),
                                                                        stride=(2, 2),
                                                                        padding='same', data_format=data_format))

        self.g_de_conv2 = tf.make_template('de_conv2',
                                           LeakyReLUBNNSConvTranspose2d(n_filters=128, kernel_size=(4, 4),
                                                                        stride=(2, 2),
                                                                        padding='same', data_format=data_format))

        self.g_de_conv3_a = tf.make_template('de_conv3_a',
                                             LeakyReLUBNNSConvTranspose2d(n_filters=64, kernel_size=(4, 4),
                                                                          stride=(2, 2),
                                                                          padding='same', data_format=data_format))

        self.g_de_conv3_b = tf.make_template('de_conv3_b',
                                             LeakyReLUBNNSConvTranspose2d(n_filters=64, kernel_size=(4, 4),
                                                                          stride=(2, 2),
                                                                          padding='same', data_format=data_format))

        self.de_conv4_a = tf.make_template('de_conv4_a',
                                           Conv2DTranspose(filters=domain_a_image_channels, kernel_size=(1, 1),
                                                           strides=(1, 1), padding='valid', data_format=data_format))

        self.de_conv4_b = tf.make_template('de_conv4_b',
                                           Conv2DTranspose(filters=domain_b_image_channels, kernel_size=(1, 1),
                                                           strides=(1, 1), padding='valid', data_format=data_format))

        self.tanh_a = tf.make_template('tanh_a', Activation('tanh'))
        self.tanh_b = tf.make_template('tanh_b', Activation('tanh'))
Beispiel #30
0
def siamese_model(input_shape=None,
                  track_length=1,
                  features=None,
                  neighborhood_scale_size=10,
                  reg=1e-5,
                  init='he_normal',
                  softmax=True,
                  norm_method='std',
                  filter_size=61):
    def compute_input_shape(feature):
        if feature == 'appearance':
            return input_shape
        elif feature == 'distance':
            return (None, 2)
        elif feature == 'neighborhood':
            return (None, 2 * neighborhood_scale_size + 1, 2 * neighborhood_scale_size + 1, 1)
        elif feature == 'regionprop':
            return (None, 3)
        else:
            raise ValueError('siamese_model.compute_input_shape: '
                             'Unknown feature `{}`'.format(feature))

    def compute_reshape(feature):
        if feature == 'appearance':
            return (64,)
        elif feature == 'distance':
            return (2,)
        elif feature == 'neighborhood':
            return (64,)
        elif feature == 'regionprop':
            return (3,)
        else:
            raise ValueError('siamese_model.compute_output_shape: '
                             'Unknown feature `{}`'.format(feature))

    def compute_feature_extractor(feature, shape):
        if feature == 'appearance':
            # This should not stay: channels_first/last should be used to
            # dictate size (1 works for either right now)
            N_layers = np.int(np.floor(np.log2(input_shape[1])))
            feature_extractor = Sequential()
            feature_extractor.add(InputLayer(input_shape=shape))
            # feature_extractor.add(ImageNormalization2D(norm_method='std', filter_size=32))
            for layer in range(N_layers):
                feature_extractor.add(Conv3D(64, (1, 3, 3),
                                             kernel_initializer=init,
                                             padding='same',
                                             kernel_regularizer=l2(reg)))
                feature_extractor.add(BatchNormalization(axis=channel_axis))
                feature_extractor.add(Activation('relu'))
                feature_extractor.add(MaxPool3D(pool_size=(1, 2, 2)))

            feature_extractor.add(Reshape((-1, 64)))
            return feature_extractor

        elif feature == 'distance':
            return None
        elif feature == 'neighborhood':
            N_layers_og = np.int(np.floor(np.log2(2 * neighborhood_scale_size + 1)))
            feature_extractor_neighborhood = Sequential()
            feature_extractor_neighborhood.add(
                InputLayer(input_shape=(None,
                                        2 * neighborhood_scale_size + 1,
                                        2 * neighborhood_scale_size + 1,
                                        1))
            )
            for layer in range(N_layers_og):
                feature_extractor_neighborhood.add(Conv3D(64, (1, 3, 3),
                                                          kernel_initializer=init,
                                                          padding='same',
                                                          kernel_regularizer=l2(reg)))
                feature_extractor_neighborhood.add(BatchNormalization(axis=channel_axis))
                feature_extractor_neighborhood.add(Activation('relu'))
                feature_extractor_neighborhood.add(MaxPool3D(pool_size=(1, 2, 2)))

            feature_extractor_neighborhood.add(Reshape((-1, 64)))

            return feature_extractor_neighborhood
        elif feature == 'regionprop':
            return None
        else:
            raise ValueError('siamese_model.compute_feature_extractor: '
                             'Unknown feature `{}`'.format(feature))

    if features is None:
        raise ValueError('siamese_model: No features specified.')

    if K.image_data_format() == 'channels_first':
        channel_axis = 1
        input_shape = (input_shape[0], None, *input_shape[1:])
    else:
        channel_axis = -1
        input_shape = (None, *input_shape)

    features = sorted(features)

    inputs = []
    outputs = []
    for feature in features:
        in_shape = compute_input_shape(feature)
        re_shape = compute_reshape(feature)
        feature_extractor = compute_feature_extractor(feature, in_shape)

        layer_1 = Input(shape=in_shape)
        layer_2 = Input(shape=in_shape)

        inputs.extend([layer_1, layer_2])

        # apply feature_extractor if it exists
        if feature_extractor is not None:
            layer_1 = feature_extractor(layer_1)
            layer_2 = feature_extractor(layer_2)

        # LSTM on 'left' side of network since that side takes in stacks of features
        layer_1 = LSTM(64)(layer_1)
        layer_2 = Reshape(re_shape)(layer_2)

        outputs.append([layer_1, layer_2])

    dense_merged = []
    for layer_1, layer_2 in outputs:
        merge = Concatenate(axis=channel_axis)([layer_1, layer_2])
        dense_merge = Dense(128)(merge)
        bn_merge = BatchNormalization(axis=channel_axis)(dense_merge)
        dense_relu = Activation('relu')(bn_merge)
        dense_merged.append(dense_relu)

    # Concatenate outputs from both instances
    merged_outputs = Concatenate(axis=channel_axis)(dense_merged)

    # Add dense layers
    dense1 = Dense(128)(merged_outputs)
    bn1 = BatchNormalization(axis=channel_axis)(dense1)
    relu1 = Activation('relu')(bn1)
    dense2 = Dense(128)(relu1)
    bn2 = BatchNormalization(axis=channel_axis)(dense2)
    relu2 = Activation('relu')(bn2)
    dense3 = Dense(3, activation='softmax')(relu2)

    # Instantiate model
    final_layer = dense3
    model = Model(inputs=inputs, outputs=final_layer)

    return model