def test_elu():
    from keras.layers.advanced_activations import ELU
    np.random.seed(1337)
    inp = get_standard_values()
    for alpha in [0.1, .5, -1., 1.]:
        layer = ELU(alpha=alpha)
        layer.input = K.variable(inp)
        for train in [True, False]:
            outp = K.eval(layer.get_output(train))
            assert_allclose(outp, inp, rtol=1e-3)

        layer.input = K.variable(-inp)
        for train in [True, False]:
            outp = K.eval(layer.get_output(train))
            assert_allclose(outp, alpha*(np.exp(-inp)-1.), rtol=1e-3)

        config = layer.get_config()
        assert config['alpha'] == alpha
Exemple #2
0
def CapsNet(input_shape, n_class, routings):
    x = layers.Input(shape=input_shape)
    conv1 = layers.Conv2D(filters=64,
                          kernel_size=(1, 12),
                          strides=(1, 1),
                          padding='same',
                          dilation_rate=5)(x)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    conv1 = layers.Conv2D(filters=64,
                          kernel_size=(1, 12),
                          strides=(1, 2),
                          padding='same',
                          dilation_rate=1)(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    conv1 = layers.MaxPooling2D((1, 2), strides=(1, 2))(conv1)

    conv1 = layers.Conv2D(filters=96,
                          kernel_size=(1, 9),
                          strides=1,
                          padding='same',
                          dilation_rate=4)(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    conv1 = layers.Conv2D(filters=96,
                          kernel_size=(1, 9),
                          strides=1,
                          padding='same',
                          dilation_rate=4)(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    conv1 = layers.MaxPooling2D((1, 2), strides=(1, 2))(conv1)

    conv1 = layers.Conv2D(filters=128,
                          kernel_size=(1, 6),
                          strides=1,
                          padding='same',
                          dilation_rate=3)(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    conv1 = layers.Conv2D(filters=128,
                          kernel_size=(1, 6),
                          strides=1,
                          padding='same',
                          dilation_rate=3)(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    conv1 = layers.MaxPooling2D((1, 2), strides=(1, 2))(conv1)

    conv1 = layers.Conv2D(filters=192,
                          kernel_size=(1, 3),
                          strides=1,
                          padding='same',
                          dilation_rate=2)(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    conv1 = layers.Conv2D(filters=192,
                          kernel_size=(1, 3),
                          strides=1,
                          padding='same',
                          dilation_rate=2)(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    #conv1 = layers.Conv2D(filters=192, kernel_size=(1,3), strides=1, padding='same',dilation_rate = 2)(conv1)
    #conv1 = ELU(alpha=0.5)(conv1)
    #conv1 = BN()(conv1)
    conv1 = layers.MaxPooling2D((1, 2), strides=(1, 2))(conv1)

    conv1 = layers.Conv2D(filters=256,
                          kernel_size=(1, 3),
                          strides=1,
                          padding='same',
                          dilation_rate=2)(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    conv1 = layers.Conv2D(filters=256,
                          kernel_size=(1, 3),
                          strides=1,
                          padding='same',
                          dilation_rate=2)(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)

    conv1 = layers.GlobalAveragePooling2D(data_format='channels_first')(conv1)
    output = layers.Dense(8, activation='sigmoid')(conv1)

    model = models.Model(x, output)
    return model
def AudioConvRNN(weights='msd', input_tensor=None):
    '''Instantiate the AudioConvRNN architecture,
    optionally loading weights pre-trained
    on Million Song Dataset. Note that when using TensorFlow,
    for best performance you should set
    `image_dim_ordering="tf"` in your Keras config
    at ~/.keras/keras.json.

    The model and the weights are compatible with both
    TensorFlow and Theano. The dimension ordering
    convention used by the model is the one
    specified in your Keras config file.

    # Arguments
        weights: one of `None` (random initialization)
            or "imagenet" (pre-training on ImageNet).
        input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
            to use as image input for the model.


    # Returns
        A Keras model instance.
    '''
    if weights not in {'msd', None}:
        raise ValueError('The `weights` argument should be either '
                         '`None` (random initialization) or `msd` '
                         '(pre-training on Million Song Dataset).')

    # Determine proper input shape
    if K.image_dim_ordering() == 'th':
        input_shape = (1, 96, 1366)
    else:
        input_shape = (96, 1366, 1)

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

    # Determine input axis
    if K.image_dim_ordering() == 'th':
        channel_axis = 1
        freq_axis = 2
        time_axis = 3
    else:
        channel_axis = 3
        freq_axis = 1
        time_axis = 2

    # Input block
    x = ZeroPadding2D(padding=(0, 37))(melgram_input)
    x = BatchNormalization(axis=time_axis, name='bn_0_freq')(x)

    # Conv block 1
    x = Convolution2D(64, 3, 3, border_mode='same', name='conv1')(x)
    x = BatchNormalization(axis=channel_axis, mode=2, name='bn1')(x)
    x = ELU()(x)
    x = MaxPooling2D((2, 2), strides=(2, 2), name='pool1')(x)
    x = Dropout(0.5, name='dropout1')(x)

    # Conv block 2
    x = Convolution2D(128, 3, 3, border_mode='same', name='conv2')(x)
    x = BatchNormalization(axis=channel_axis, mode=2, name='bn2')(x)
    x = ELU()(x)
    x = MaxPooling2D((3, 3), strides=(3, 3), name='pool2')(x)
    x = Dropout(0.5, name='dropout2')(x)

    # Conv block 3
    x = Convolution2D(128, 3, 3, border_mode='same', name='conv3')(x)
    x = BatchNormalization(axis=channel_axis, mode=2, name='bn3')(x)
    x = ELU()(x)
    x = MaxPooling2D((4, 4), strides=(4, 4), name='pool3')(x)
    x = Dropout(0.5, name='dropout3')(x)

    # Conv block 4
    x = Convolution2D(128, 3, 3, border_mode='same', name='conv4')(x)
    x = BatchNormalization(axis=channel_axis, mode=2, name='bn4')(x)
    x = ELU()(x)
    x = MaxPooling2D((4, 4), strides=(4, 4), name='pool4')(x)
    x = Dropout(0.5, name='dropout4')(x)

    # reshaping
    if K.image_dim_ordering() == 'th':
        x = Permute((3, 1, 2))(x)
    x = Reshape((15, 128))(x)

    # GRU block 1, 2, output
    x = GRU(32, return_sequences=True, name='gru1')(x)
    x = GRU(32, return_sequences=False, name='gru2')(x)
    x = Dropout(0.3)(x)
    x = Dense(50, activation='sigmoid', name='output')(x)

    # Create model
    model = Model(melgram_input, x)
    if True:
        model.load_weights('data/%s_weights_%s.h5' % ('rnn', K._BACKEND))
        return model

    else:  # This is for keras-application
        # Load input
        if K._BACKEND == 'theano':
            weights_path = get_file('rnn_weights_theano.h5',
                                    TH_WEIGHTS_PATH,
                                    cache_subdir='models')
        else:
            weights_path = get_file('rnn_weights_tensorflow.h5',
                                    TF_WEIGHTS_PATH,
                                    cache_subdir='models')

        model.load_weights(weights_path)
        return model
Exemple #4
0
def MusicTaggerCRNN(weights='msd', input_tensor=None):
    '''Instantiate the MusicTaggerCRNN architecture,
    optionally loading weights pre-trained
    on Million Song Dataset. Note that when using TensorFlow,
    for best performance you should set
    `image_dim_ordering="tf"` in your Keras config
    at ~/.keras/keras.json.

    For preparing mel-spectrogram input, see
    `audio_conv_utils.py` in [applications](https://github.com/fchollet/keras/tree/master/keras/applications).
    You will need to install [Librosa](http://librosa.github.io/librosa/) to use it.

    # Arguments
        weights: one of `None` (random initialization)
            or "msd" (pre-training on ImageNet).
        input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
            to use as image input for the model.
    # Returns
        A Keras model instance.
    '''

    if weights not in {'msd', None}:
        raise ValueError('The `weights` argument should be either '
                         '`None` (random initialization) or `msd` '
                         '(pre-training on Million Song Dataset).')

    # Determine proper input shape
    if K.image_dim_ordering() == 'th':
        input_shape = (96, 1366, 1)
    else:
        input_shape = (1, 96, 1366)

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

    # Determine input axis
    if K.image_dim_ordering() == 'th':
        channel_axis = 1
        freq_axis = 2
        time_axis = 3
    else:
        channel_axis = 3
        freq_axis = 1
        time_axis = 2

    # Input block
    x = ZeroPadding2D(padding=(0, 37))(melgram_input)
    x = BatchNormalization(axis=time_axis, name='bn_0_freq')(x)

    # Conv block 1
    x = Convolution2D(64,
                      3,
                      3,
                      border_mode='same',
                      name='conv1',
                      trainable=False)(x)
    x = BatchNormalization(axis=channel_axis,
                           mode=0,
                           name='bn1',
                           trainable=False)(x)
    x = ELU()(x)
    x = MaxPooling2D(pool_size=(2, 2),
                     strides=(2, 2),
                     name='pool1',
                     trainable=False)(x)
    x = Dropout(0.1, name='dropout1', trainable=False)(x)

    # Conv block 2
    x = Convolution2D(128,
                      3,
                      3,
                      border_mode='same',
                      name='conv2',
                      trainable=False)(x)
    x = BatchNormalization(axis=channel_axis,
                           mode=0,
                           name='bn2',
                           trainable=False)(x)
    x = ELU()(x)
    x = MaxPooling2D(pool_size=(3, 3),
                     strides=(3, 3),
                     name='pool2',
                     trainable=False)(x)
    x = Dropout(0.1, name='dropout2', trainable=False)(x)

    # Conv block 3
    x = Convolution2D(128,
                      3,
                      3,
                      border_mode='same',
                      name='conv3',
                      trainable=False)(x)
    x = BatchNormalization(axis=channel_axis,
                           mode=0,
                           name='bn3',
                           trainable=False)(x)
    x = ELU()(x)
    x = MaxPooling2D(pool_size=(4, 4),
                     strides=(4, 4),
                     name='pool3',
                     trainable=False)(x)
    x = Dropout(0.1, name='dropout3', trainable=False)(x)

    # Conv block 4
    x = Convolution2D(128,
                      3,
                      3,
                      border_mode='same',
                      name='conv4',
                      trainable=False)(x)
    x = BatchNormalization(axis=channel_axis,
                           mode=0,
                           name='bn4',
                           trainable=False)(x)
    x = ELU()(x)
    x = MaxPooling2D(pool_size=(4, 4),
                     strides=(4, 4),
                     name='pool4',
                     trainable=False)(x)
    x = Dropout(0.1, name='dropout4', trainable=False)(x)

    # reshaping
    if K.image_dim_ordering() == 'th':
        x = Permute((3, 1, 2))(x)
    x = Reshape((15, 128))(x)

    # GRU block 1, 2, output
    x = GRU(32, return_sequences=True, name='gru1')(x)
    x = GRU(32, return_sequences=False, name='gru2')(x)
    x = Dropout(0.3, name='final_drop')(x)

    if weights is None:
        # Create model
        x = Dense(10, activation='sigmoid', name='output')(x)
        model = Model(melgram_input, x)
        return model
    else:
        # Load input
        x = Dense(50, activation='sigmoid', name='output')(x)
        if K.image_dim_ordering() == 'tf':
            raise RuntimeError("Please set image_dim_ordering == 'th'."
                               "You can set it at ~/.keras/keras.json")
        # Create model
        initial_model = Model(melgram_input, x)
        initial_model.load_weights('weights/music_tagger_crnn_weights_%s.h5' %
                                   K._BACKEND,
                                   by_name=True)

        # Eliminate last layer
        pop_layer(initial_model)

        # Add new Dense layer
        last = initial_model.get_layer('final_drop')
        preds = (Dense(10, activation='sigmoid', name='preds'))(last.output)
        model = Model(initial_model.input, preds)

        return model
Exemple #5
0
def MusicTaggerCNN(weights='msd', input_tensor=None, include_top=True):
    '''Instantiate the MusicTaggerCNN architecture,
    optionally loading weights pre-trained
    on Million Song Dataset. Note that when using TensorFlow,
    for best performance you should set
    `image_dim_ordering="tf"` in your Keras config
    at ~/.keras/keras.json.

    The model and the weights are compatible with both
    TensorFlow and Theano. The dimension ordering
    convention used by the model is the one
    specified in your Keras config file.

    For preparing mel-spectrogram input, see
    `audio_conv_utils.py` in [applications](https://github.com/fchollet/keras/tree/master/keras/applications).
    You will need to install [Librosa](http://librosa.github.io/librosa/)
    to use it.

    # Arguments
        weights: one of `None` (random initialization)
            or "msd" (pre-training on ImageNet).
        input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
            to use as image input for the model.
        include_top: whether to include the 1 fully-connected
            layer (output layer) at the top of the network.
            If False, the network outputs 256-dim features.


    # Returns
        A Keras model instance.
    '''
    if weights not in {'msd', None}:
        raise ValueError('The `weights` argument should be either '
                         '`None` (random initialization) or `msd` '
                         '(pre-training on Million Song Dataset).')

    K.set_image_dim_ordering('th')

    # Determine proper input shape
    if K.image_dim_ordering() == 'th':
        input_shape = (1, 96, 1366)
        # raise RuntimeError("th")
    else:
        input_shape = (96, 1366, 1)
        # raise RuntimeError("tf")

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

    # Determine input axis
    if K.image_dim_ordering() == 'th':
        channel_axis = 1
        freq_axis = 2
        time_axis = 3
    else:
        channel_axis = 3
        freq_axis = 1
        time_axis = 2

    # Input block
    x = BatchNormalization(axis=freq_axis, name='bn_0_freq')(melgram_input)

    # Conv block 1
    x = Convolution2D(64, 3, 3, border_mode='same', name='conv1')(x)
    x = BatchNormalization(axis=channel_axis, mode=0, name='bn1')(x)
    x = ELU()(x)
    x = MaxPooling2D(pool_size=(2, 4), name='pool1')(x)

    # Conv block 2
    x = Convolution2D(128, 3, 3, border_mode='same', name='conv2')(x)
    x = BatchNormalization(axis=channel_axis, mode=0, name='bn2')(x)
    x = ELU()(x)
    x = MaxPooling2D(pool_size=(2, 4), name='pool2')(x)

    # Conv block 3
    x = Convolution2D(128, 3, 3, border_mode='same', name='conv3')(x)
    x = BatchNormalization(axis=channel_axis, mode=0, name='bn3')(x)
    x = ELU()(x)
    x = MaxPooling2D(pool_size=(2, 4), name='pool3')(x)

    # Conv block 4
    x = Convolution2D(128, 3, 3, border_mode='same', name='conv4')(x)
    x = BatchNormalization(axis=channel_axis, mode=0, name='bn4')(x)
    x = ELU()(x)
    x = MaxPooling2D(pool_size=(3, 5), name='pool4')(x)

    # Conv block 5
    x = Convolution2D(64, 3, 3, border_mode='same', name='conv5')(x)
    x = BatchNormalization(axis=channel_axis, mode=0, name='bn5')(x)
    x = ELU()(x)
    x = MaxPooling2D(pool_size=(4, 4), name='pool5')(x)

    # Output
    x = Flatten()(x)
    if include_top:
        x = Dense(50, activation='sigmoid', name='output')(x)

    # Create model
    model = Model(melgram_input, x)
    if weights is None:
        return model
    else:
        # weights used by MSD
        if K.image_dim_ordering() == 'tf':
            raise RuntimeError("Please set image_dim_ordering == 'th'."
                               "You can set it at ~/.keras/keras.json")
        model.load_weights('data/music_tagger_cnn_weights_%s.h5' % K._BACKEND,
                           by_name=True)
        return model
    def build(width, height, depth, classes, reg=0.0005):
        # initialize the model along with the input shape to be
        # "channels last" and the channels dimension itself
        model = Sequential()
        inputShape = (height, width, depth)
        chanDim = -1

        # if we use "channels first", update the input shape and channels dimensions
        if K.image_data_format() == "channels_first":
            inputShape = (depth, height, width)
            chanDim = 1

        # Block #1: first CONV => ELU => CONV => ELU => POOL layer set
        model.add(
            Conv2D(32, (3, 3),
                   padding="same",
                   kernel_initializer="he_normal",
                   kernel_regularizer=l2(reg),
                   input_shape=inputShape))
        model.add(ELU())
        model.add(BatchNormalization(axis=chanDim))
        model.add(
            Conv2D(32, (3, 3),
                   padding="same",
                   kernel_regularizer=l2(reg),
                   kernel_initializer="he_normal"))
        model.add(ELU())
        model.add(BatchNormalization(axis=chanDim))
        model.add(MaxPooling2D(pool_size=(2, 2)))
        model.add(Dropout(0.25))

        # Block #2: second CONV => ELU => CONV => ELU => POOL layer set
        model.add(
            Conv2D(64, (3, 3),
                   padding="same",
                   kernel_regularizer=l2(reg),
                   kernel_initializer="he_normal"))
        model.add(ELU())
        model.add(BatchNormalization(axis=chanDim))
        model.add(
            Conv2D(64, (3, 3),
                   padding="same",
                   kernel_regularizer=l2(reg),
                   kernel_initializer="he_normal"))
        model.add(ELU())
        model.add(BatchNormalization(axis=chanDim))
        model.add(MaxPooling2D(pool_size=(2, 2)))
        model.add(Dropout(0.25))

        # Block #3: third CONV => ELU => CONV => ELU => POOL
        model.add(
            Conv2D(128, (3, 3),
                   padding="same",
                   kernel_regularizer=l2(reg),
                   kernel_initializer="he_normal"))
        model.add(ELU())
        model.add(BatchNormalization(axis=chanDim))
        model.add(
            Conv2D(128, (3, 3),
                   padding="same",
                   kernel_regularizer=l2(reg),
                   kernel_initializer="he_normal"))
        model.add(ELU())
        model.add(BatchNormalization(axis=chanDim))
        model.add(MaxPooling2D(pool_size=(2, 2)))
        model.add(Dropout(0.25))

        # Block #3*: fourth CONV => ELU => CONV => ELU => POOL
        # uncomment this block for experiment #5 or later
        model.add(
            Conv2D(256, (3, 3),
                   padding="same",
                   kernel_regularizer=l2(reg),
                   kernel_initializer="he_normal"))
        model.add(ELU())
        model.add(BatchNormalization(axis=chanDim))
        model.add(
            Conv2D(256, (3, 3),
                   padding="same",
                   kernel_regularizer=l2(reg),
                   kernel_initializer="he_normal"))
        model.add(ELU())
        model.add(BatchNormalization(axis=chanDim))
        model.add(MaxPooling2D(pool_size=(2, 2)))
        model.add(Dropout(0.25))

        # Block #4: first set of FC => ELU layer set
        model.add(Flatten())
        model.add(
            Dense(64,
                  kernel_initializer="he_normal",
                  kernel_regularizer=l2(reg)))
        model.add(ELU())
        model.add(BatchNormalization(axis=chanDim))
        model.add(Dropout(0.5))

        # Block #5: second set of FC => ELU layer set
        model.add(
            Dense(64,
                  kernel_initializer="he_normal",
                  kernel_regularizer=l2(reg)))
        model.add(ELU())
        model.add(BatchNormalization(axis=chanDim))
        model.add(Dropout(0.5))

        # Block #6: softmax classifier
        model.add(
            Dense(classes,
                  kernel_initializer="he_normal",
                  kernel_regularizer=l2(reg)))
        model.add(Activation("softmax"))

        # return the constructed network architecture
        return model
Exemple #7
0
def main():
    # ========================================================================
    # VGG-16 ARCHITECTURE
    # ========================================================================
    model = Sequential()

    model.add(ZeroPadding2D((1, 1), input_shape=(224, 224, 20)))
    model.add(Conv2D(64, (3, 3), activation='relu', name='conv1_1'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Conv2D(64, (3, 3), activation='relu', name='conv1_2'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Conv2D(128, (3, 3), activation='relu', name='conv2_1'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Conv2D(128, (3, 3), activation='relu', name='conv2_2'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Conv2D(256, (3, 3), activation='relu', name='conv3_1'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Conv2D(256, (3, 3), activation='relu', name='conv3_2'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Conv2D(256, (3, 3), activation='relu', name='conv3_3'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Conv2D(512, (3, 3), activation='relu', name='conv4_1'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Conv2D(512, (3, 3), activation='relu', name='conv4_2'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Conv2D(512, (3, 3), activation='relu', name='conv4_3'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Conv2D(512, (3, 3), activation='relu', name='conv5_1'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Conv2D(512, (3, 3), activation='relu', name='conv5_2'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Conv2D(512, (3, 3), activation='relu', name='conv5_3'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(Flatten())
    model.add(
        Dense(num_features, name='fc6', kernel_initializer='glorot_uniform'))

    # ========================================================================
    # WEIGHT INITIALIZATION
    # ========================================================================
    layerscaffe = [
        'conv1_1', 'conv1_2', 'conv2_1', 'conv2_2', 'conv3_1', 'conv3_2',
        'conv3_3', 'conv4_1', 'conv4_2', 'conv4_3', 'conv5_1', 'conv5_2',
        'conv5_3', 'fc6', 'fc7', 'fc8'
    ]
    h5 = h5py.File(vgg_16_weights, 'r')

    layer_dict = dict([(layer.name, layer) for layer in model.layers])

    # Copy the weights stored in the 'vgg_16_weights' file to the
    # feature extractor part of the VGG16
    for layer in layerscaffe[:-3]:
        w2, b2 = h5['data'][layer]['0'], h5['data'][layer]['1']
        #w2 = np.transpose(np.asarray(w2), (0,1,2,3))
        #w2 = w2[:, :, ::-1, ::-1]
        w2 = np.transpose(np.asarray(w2), (2, 3, 1, 0))
        w2 = w2[::-1, ::-1, :, :]
        b2 = np.asarray(b2)
        #layer_dict[layer].W.set_value(w2)
        #layer_dict[layer].b.set_value(b2)
        layer_dict[layer].set_weights((w2, b2))

    # Copy the weights of the first fully-connected layer (fc6)
    layer = layerscaffe[-3]
    w2, b2 = h5['data'][layer]['0'], h5['data'][layer]['1']
    w2 = np.transpose(np.asarray(w2), (1, 0))
    b2 = np.asarray(b2)
    #layer_dict[layer].W.set_value(w2)
    #layer_dict[layer].b.set_value(b2)
    layer_dict[layer].set_weights((w2, b2))

    # ========================================================================
    # FEATURE EXTRACTION
    # ========================================================================
    if save_features:
        saveFeatures(model, features_file, labels_file, features_key,
                     labels_key)

    # ========================================================================
    # TRAINING
    # ========================================================================
    adam = Adam(lr=learning_rate,
                beta_1=0.9,
                beta_2=0.999,
                epsilon=1e-08,
                decay=0.0005)
    model.compile(optimizer=adam,
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    do_training = True
    compute_metrics = True
    threshold = 0.5

    if do_training:
        h5features = h5py.File(features_file, 'r')
        h5labels = h5py.File(labels_file, 'r')

        # X_full will contain all the feature vectors extracted from optical
        # flow images
        X_full = h5features[features_key]
        _y_full = np.asarray(h5labels[labels_key])

        zeroes_full = np.asarray(np.where(_y_full == 0)[0])
        ones_full = np.asarray(np.where(_y_full == 1)[0])
        zeroes_full.sort()
        ones_full.sort()
        print(len(zeroes_full), len(ones_full))
        print('=====')
        # Use a 5 fold cross-validation
        kf_falls = KFold(n_splits=5, shuffle=True)
        kf_falls.get_n_splits(X_full[zeroes_full, ...])

        kf_nofalls = KFold(n_splits=5, shuffle=True)
        kf_nofalls.get_n_splits(X_full[ones_full, ...])

        sensitivities = []
        specificities = []
        accuracies = []

        fold_number = 1
        # CROSS-VALIDATION: Stratified partition of the dataset into
        # train/test sets
        for ((train_index_falls, test_index_falls),
             (train_index_nofalls, test_index_nofalls)) in zip(
                 kf_falls.split(X_full[zeroes_full, ...]),
                 kf_nofalls.split(X_full[ones_full, ...])):

            train_index_falls = np.asarray(train_index_falls)
            test_index_falls = np.asarray(test_index_falls)
            train_index_nofalls = np.asarray(train_index_nofalls)
            test_index_nofalls = np.asarray(test_index_nofalls)

            X = np.concatenate(
                (X_full[zeroes_full,
                        ...][train_index_falls,
                             ...], X_full[ones_full, ...][train_index_nofalls,
                                                          ...]))
            _y = np.concatenate(
                (_y_full[zeroes_full,
                         ...][train_index_falls,
                              ...], _y_full[ones_full,
                                            ...][train_index_nofalls, ...]))
            X2 = np.concatenate(
                (X_full[zeroes_full, ...][test_index_falls,
                                          ...], X_full[ones_full,
                                                       ...][test_index_nofalls,
                                                            ...]))
            _y2 = np.concatenate(
                (_y_full[zeroes_full,
                         ...][test_index_falls,
                              ...], _y_full[ones_full, ...][test_index_nofalls,
                                                            ...]))

            # Create a validation subset from the training set
            val_size = 100
            zeroes = np.asarray(np.where(_y == 0)[0])
            ones = np.asarray(np.where(_y == 1)[0])

            zeroes.sort()
            ones.sort()

            trainval_split_0 = StratifiedShuffleSplit(n_splits=1,
                                                      test_size=val_size / 2,
                                                      random_state=7)
            indices_0 = trainval_split_0.split(X[zeroes, ...],
                                               np.argmax(_y[zeroes, ...], 1))
            trainval_split_1 = StratifiedShuffleSplit(n_splits=1,
                                                      test_size=val_size / 2,
                                                      random_state=7)
            indices_1 = trainval_split_1.split(X[ones, ...],
                                               np.argmax(_y[ones, ...], 1))
            train_indices_0, val_indices_0 = indices_0.next()
            train_indices_1, val_indices_1 = indices_1.next()

            X_train = np.concatenate([
                X[zeroes, ...][train_indices_0, ...],
                X[ones, ...][train_indices_1, ...]
            ],
                                     axis=0)
            y_train = np.concatenate([
                _y[zeroes, ...][train_indices_0, ...],
                _y[ones, ...][train_indices_1, ...]
            ],
                                     axis=0)
            X_val = np.concatenate([
                X[zeroes, ...][val_indices_0, ...], X[ones, ...][val_indices_1,
                                                                 ...]
            ],
                                   axis=0)
            y_val = np.concatenate([
                _y[zeroes, ...][val_indices_0, ...],
                _y[ones, ...][val_indices_1, ...]
            ],
                                   axis=0)

            # Balance the number of positive and negative samples so that
            # there is the same amount of each of them
            all0 = np.asarray(np.where(y_train == 0)[0])
            all1 = np.asarray(np.where(y_train == 1)[0])

            if len(all0) < len(all1):
                all1 = np.random.choice(all1, len(all0), replace=False)
            else:
                all0 = np.random.choice(all0, len(all1), replace=False)
            allin = np.concatenate((all0.flatten(), all1.flatten()))
            allin.sort()
            X_train = X_train[allin, ...]
            y_train = y_train[allin]

            all0 = np.asarray(np.where(y_train == 0)[0])
            all1 = np.asarray(np.where(y_train == 1)[0])

            # ==================== CLASSIFIER ========================
            extracted_features = Input(shape=(num_features, ),
                                       dtype='float32',
                                       name='input')
            if batch_norm:
                x = BatchNormalization(axis=-1, momentum=0.99,
                                       epsilon=0.001)(extracted_features)
                x = Activation('relu')(x)
            else:
                x = ELU(alpha=1.0)(extracted_features)

            x = Dropout(0.9)(x)
            x = Dense(4096, name='fc2', init='glorot_uniform')(x)
            if batch_norm:
                x = BatchNormalization(axis=-1, momentum=0.99,
                                       epsilon=0.001)(x)
                x = Activation('relu')(x)
            else:
                x = ELU(alpha=1.0)(x)
            x = Dropout(0.8)(x)
            x = Dense(1, name='predictions', init='glorot_uniform')(x)
            x = Activation('sigmoid')(x)

            classifier = Model(input=extracted_features,
                               output=x,
                               name='classifier')
            fold_best_model_path = best_model_path + 'fdd_fold_{}.h5'.format(
                fold_number)
            classifier.compile(optimizer=adam,
                               loss='binary_crossentropy',
                               metrics=['accuracy'])

            if not use_checkpoint:
                # ==================== TRAINING ========================
                # weighting of each class: only the fall class gets
                # a different weight
                class_weight = {0: weight_0, 1: 1}

                # callback definition
                metric = 'val_loss'
                e = EarlyStopping(monitor=metric,
                                  min_delta=0,
                                  patience=100,
                                  mode='auto')
                c = ModelCheckpoint(fold_best_model_path,
                                    monitor=metric,
                                    save_best_only=True,
                                    save_weights_only=False,
                                    mode='auto')
                callbacks = [e, c]

                # Batch training
                if mini_batch_size == 0:
                    history = classifier.fit(X_train,
                                             y_train,
                                             validation_data=(X_val, y_val),
                                             batch_size=X.shape[0],
                                             nb_epoch=epochs,
                                             shuffle='batch',
                                             class_weight=class_weight,
                                             callbacks=callbacks)
                else:
                    history = classifier.fit(X_train,
                                             y_train,
                                             validation_data=(X_val, y_val),
                                             batch_size=mini_batch_size,
                                             nb_epoch=epochs,
                                             shuffle='batch',
                                             class_weight=class_weight,
                                             callbacks=callbacks)

                plot_training_info(plots_folder + exp, ['accuracy', 'loss'],
                                   save_plots, history.history)

                classifier = load_model(fold_best_model_path)

                # Use full training set (training+validation)
                X_train = np.concatenate((X_train, X_val), axis=0)
                y_train = np.concatenate((y_train, y_val), axis=0)

                if mini_batch_size == 0:
                    history = classifier.fit(X_train,
                                             y_train,
                                             batch_size=X_train.shape[0],
                                             nb_epoch=1,
                                             shuffle='batch',
                                             class_weight=class_weight)
                else:
                    history = classifier.fit(X_train,
                                             y_train,
                                             batch_size=mini_batch_size,
                                             nb_epoch=1,
                                             shuffle='batch',
                                             class_weight=class_weight)

                classifier.save(fold_best_model_path)

            # ==================== EVALUATION ========================
            if compute_metrics:
                predicted = classifier.predict(np.asarray(X2))
                for i in range(len(predicted)):
                    if predicted[i] < threshold:
                        predicted[i] = 0
                    else:
                        predicted[i] = 1
                # Array of predictions 0/1
                predicted = np.asarray(predicted)
                # Compute metrics and print them
                cm = confusion_matrix(_y2, predicted, labels=[0, 1])
                tp = cm[0][0]
                fn = cm[0][1]
                fp = cm[1][0]
                tn = cm[1][1]
                tpr = tp / float(tp + fn)
                fpr = fp / float(fp + tn)
                fnr = fn / float(fn + tp)
                tnr = tn / float(tn + fp)
                precision = tp / float(tp + fp)
                recall = tp / float(tp + fn)
                specificity = tn / float(tn + fp)
                f1 = 2 * float(precision * recall) / float(precision + recall)
                accuracy = accuracy_score(_y2, predicted)

                print('TP: {}, TN: {}, FP: {}, FN: {}'.format(tp, tn, fp, fn))
                print('TPR: {}, TNR: {}, FPR: {}, FNR: {}'.format(
                    tpr, tnr, fpr, fnr))
                print('Sensitivity/Recall: {}'.format(recall))
                print('Specificity: {}'.format(specificity))
                print('Precision: {}'.format(precision))
                print('F1-measure: {}'.format(f1))
                print('Accuracy: {}'.format(accuracy))
                fold_number += 1

                # Store the metrics for this epoch
                sensitivities.append(tp / float(tp + fn))
                specificities.append(tn / float(tn + fp))
                accuracies.append(accuracy)

        print('5-FOLD CROSS-VALIDATION RESULTS ===================')
        print("Sensitivity: %.2f%% (+/- %.2f%%)" %
              (np.mean(sensitivities), np.std(sensitivities)))
        print("Specificity: %.2f%% (+/- %.2f%%)" %
              (np.mean(specificities), np.std(specificities)))
        print("Accuracy: %.2f%% (+/- %.2f%%)" %
              (np.mean(accuracies), np.std(accuracies)))
def advanced_autoencoder(x_in,x, epochs, batch_size, activations, depth, neurons):
    sess = tf.Session(graph=tf.get_default_graph(), config=session_conf)
    K.set_session(sess)
    num_stock=len(x_in.columns)
    
    # activation functions    
    if activations == 'elu':
        function = ELU(alpha=1.0)
    elif activations == 'lrelu':
        function = LeakyReLU(alpha=0.1)
    else:
        function = ReLU(max_value=None, negative_slope=0.0, threshold=0.0)
        
    autoencoder = Sequential()
    # encoding layers of desired depth
    for n in range(1, depth+1):
        # input layer
        if n==1:
            #autoencoder.add(GaussianNoise(stddev=0.01, input_shape=(num_stock,)))
            autoencoder.add(Dense(int(neurons/n), input_shape=(num_stock,)))
            autoencoder.add(function)
        else:            
            autoencoder.add(Dense(int(neurons/n)))
            autoencoder.add(function)
    # decoding layers of desired depth
    for n in range(depth, 1, -1):
        autoencoder.add(Dense(int(neurons/(n-1))))
        autoencoder.add(function)
    # output layer
    autoencoder.add(Dense(num_stock, activation='linear'))
    
    #autoencoder.compile(optimizer='sgd', loss='mean_absolute_error', metrics=['accuracy'])
    
    autoencoder.compile(optimizer='adam', loss='mean_squared_error', metrics=['accuracy'])
    
    #checkpointer = ModelCheckpoint(filepath='weights.{epoch:02d}-{val_loss:.2f}.txt', verbose=0, save_best_only=True)
    earlystopper=EarlyStopping(monitor='val_loss',min_delta=0,patience=10,verbose=0,mode='auto',baseline=None,restore_best_weights=True)
    history=autoencoder.fit(x_in, x_in, epochs=epochs, batch_size=batch_size, \
                              shuffle=False, validation_split=0.15, verbose=0,callbacks=[earlystopper])
    #errors = np.add(autoencoder.predict(x_in),-x_in)
    y=autoencoder.predict(x)
    # saving results of error distribution tests
    #A=np.zeros((5))
    #A[0]=chi2test(errors)
    #A[1]=pesarantest(errors)
    #A[2]=portmanteau(errors,1)
    #A[3]=portmanteau(errors,3)
    #A[4]=portmanteau(errors,5)
        
    #autoencoder.summary()
    
    # plot accuracy and loss of autoencoder
    # plot_accuracy(history)
    # plot_loss(history)
    
    # plot original, encoded and decoded data for some stock
    # plot_two_series(x_in, 'Original data', auto_data, 'Reconstructed data')
    
    # the histogram of the data
    # make_histogram(x_in, 'Original data', auto_data, 'Reconstructed data')
    
    #CLOSE TF SESSION
    K.clear_session()
    return y
                  W_constraint=maxnorm(3))(x)
x = BatchNormalization(epsilon=0.001, mode=0, axis=2, momentum=0.99)(x)
x = Dropout(0.4)(x)
x = Convolution2D(64,
                  3,
                  3,
                  activation='elu',
                  border_mode='valid',
                  dim_ordering='tf',
                  W_constraint=maxnorm(3))(x)
x = BatchNormalization(epsilon=0.001, mode=0, axis=2, momentum=0.99)(x)

x = (Flatten())(x)
print(np.shape(x))
x = (Dense(100))(x)
x = (ELU(alpha=1.0))(x)
x = (Dropout(0.4))(x)
x = (Dense(50))(x)
x = (ELU(alpha=1.0))(x)
out = (Dense(3))(x)

model = Model(input=x_input, output=out)

model.compile(loss='mse', optimizer='adam')
model.fit_generator(train_generator,
                    samples_per_epoch=len(train_data),
                    validation_data=validation_generator,
                    nb_val_samples=len(val_data),
                    nb_epoch=4)

print('Saving model')
Exemple #10
0
    def __init__(self, num_features, classes):
        input_shape = (num_features, 1)
        model = Sequential()
        #Block1
        filter_num = ['None',32,64,128,256]
        kernel_size = ['None',8,8,8,8]
        conv_stride_size = ['None',1,1,1,1]
        pool_stride_size = ['None',4,4,4,4]
        pool_size = ['None',8,8,8,8]

        model.add(Conv1D(filters=filter_num[1], kernel_size=kernel_size[1], input_shape=input_shape,
                         strides=conv_stride_size[1], padding='same',
                         name='block1_conv1'))
        model.add(BatchNormalization(axis=-1))
        model.add(ELU(alpha=1.0, name='block1_adv_act1'))
        model.add(Conv1D(filters=filter_num[1], kernel_size=kernel_size[1],
                         strides=conv_stride_size[1], padding='same',
                         name='block1_conv2'))
        model.add(BatchNormalization(axis=-1))
        model.add(ELU(alpha=1.0, name='block1_adv_act2'))
        model.add(MaxPooling1D(pool_size=pool_size[1], strides=pool_stride_size[1],
                               padding='same', name='block1_pool'))
        model.add(Dropout(0.1, name='block1_dropout'))

        model.add(Conv1D(filters=filter_num[2], kernel_size=kernel_size[2],
                         strides=conv_stride_size[2], padding='same',
                         name='block2_conv1'))
        model.add(BatchNormalization())
        model.add(Activation('relu', name='block2_act1'))

        model.add(Conv1D(filters=filter_num[2], kernel_size=kernel_size[2],
                         strides=conv_stride_size[2], padding='same',
                         name='block2_conv2'))
        model.add(BatchNormalization())
        model.add(Activation('relu', name='block2_act2'))
        model.add(MaxPooling1D(pool_size=pool_size[2], strides=pool_stride_size[3],
                               padding='same', name='block2_pool'))
        model.add(Dropout(0.1, name='block2_dropout'))

        model.add(Conv1D(filters=filter_num[3], kernel_size=kernel_size[3],
                         strides=conv_stride_size[3], padding='same',
                         name='block3_conv1'))
        model.add(BatchNormalization())
        model.add(Activation('relu', name='block3_act1'))
        model.add(Conv1D(filters=filter_num[3], kernel_size=kernel_size[3],
                         strides=conv_stride_size[3], padding='same',
                         name='block3_conv2'))
        model.add(BatchNormalization())
        model.add(Activation('relu', name='block3_act2'))
        model.add(MaxPooling1D(pool_size=pool_size[3], strides=pool_stride_size[3],
                               padding='same', name='block3_pool'))
        model.add(Dropout(0.1, name='block3_dropout'))

        model.add(Conv1D(filters=filter_num[4], kernel_size=kernel_size[4],
                         strides=conv_stride_size[4], padding='same',
                         name='block4_conv1'))
        model.add(BatchNormalization())
        model.add(Activation('relu', name='block4_act1'))
        model.add(Conv1D(filters=filter_num[4], kernel_size=kernel_size[4],
                         strides=conv_stride_size[4], padding='same',
                         name='block4_conv2'))
        model.add(BatchNormalization())
        model.add(Activation('relu', name='block4_act2'))
        model.add(MaxPooling1D(pool_size=pool_size[4], strides=pool_stride_size[4],
                               padding='same', name='block4_pool'))
        model.add(Dropout(0.1, name='block4_dropout'))

        model.add(Flatten(name='flatten'))
        model.add(Dense(512, kernel_initializer=glorot_uniform(seed=0), name='fc1'))
        model.add(BatchNormalization())
        model.add(Activation('relu', name='fc1_act'))

        model.add(Dropout(0.7, name='fc1_dropout'))

        model.add(Dense(512, kernel_initializer=glorot_uniform(seed=0), name='fc2'))
        model.add(BatchNormalization())
        model.add(Activation('relu', name='fc2_act'))

        model.add(Dropout(0.5, name='fc2_dropout'))

        model.add(Dense(classes, kernel_initializer=glorot_uniform(seed=0), name='fc3'))
        model.add(Activation('softmax', name="softmax"))
        optimizer = Adamax(lr=0.002, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
        model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=[
            'accuracy'])
        self.model = model
Exemple #11
0
    hidden_input_dim = DAE_INPUT[h]
    hidden_output_dim = DAE_INPUT[h]

    print "Pre-trainig for Denoising Layer : %d" % (h + 1)
    print "Number of Hidden Units : %d" % (hidden_unit)
    print "--- Input Dimension  : %d" % (hidden_input_dim)
    print "--- Output Dimension : %d" % (hidden_output_dim)

    DAE_layer = Sequential()
    DAE_layer.add(
        Dense(hidden_unit,
              input_dim=hidden_input_dim,
              name='Pretrain_hidden_encode_%s' % str(h + 1)))
    if h == 0:  # For first pre-trainin layer
        DAE_layer.add(
            ELU(alpha=1.0, name='Pretrain_activation_encode_%s' % str(h + 1)))
    else:  # For the next following layers
        DAE_layer.add(
            Activation('relu',
                       name='Pretrain_activation_encode_%s' % str(h + 1)))
    DAE_layer.add(
        Dense(hidden_output_dim,
              name='Pretrain_hidden_decode_%s' % str(h + 1)))

    # Reconstruction to from corrupted input to the original input
    # This process allow each layers to learn the representation of the input in each layer.
    DAE_layer.compile(optimizer=OPTIMIZER_PRE, loss='mse')
    DAE_layer.fit(X_noised_train,
                  X_input,
                  epochs=PRE_EPOCH,
                  batch_size=BATCH_SIZE_PRE,
Exemple #12
0
def CapsNet(input_shape, n_class, routings):
    x = layers.Input(shape=input_shape)
    conv1 = layers.Conv2D(filters=64,
                          kernel_size=(1, 3),
                          strides=(1, 2),
                          padding='same')(x)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    conv1 = layers.Conv2D(filters=64,
                          kernel_size=(1, 3),
                          strides=(1, 1),
                          padding='same')(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    conv1 = layers.MaxPooling2D((1, 2), strides=(1, 2))(conv1)

    conv1 = layers.Conv2D(filters=96,
                          kernel_size=(1, 3),
                          strides=1,
                          padding='same')(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    conv1 = layers.Conv2D(filters=96,
                          kernel_size=(1, 3),
                          strides=1,
                          padding='same')(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    conv1 = layers.MaxPooling2D((1, 2), strides=(1, 2))(conv1)

    conv1 = layers.Conv2D(filters=128,
                          kernel_size=(1, 3),
                          strides=1,
                          padding='same')(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    conv1 = layers.Conv2D(filters=128,
                          kernel_size=(1, 3),
                          strides=1,
                          padding='same')(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    conv1 = layers.MaxPooling2D((1, 2), strides=(1, 2))(conv1)

    conv1 = layers.Conv2D(filters=192,
                          kernel_size=(1, 3),
                          strides=1,
                          padding='same')(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    conv1 = layers.Conv2D(filters=192,
                          kernel_size=(1, 3),
                          strides=1,
                          padding='same')(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    conv1 = layers.Conv2D(filters=192,
                          kernel_size=(1, 3),
                          strides=1,
                          padding='same')(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    conv1 = layers.MaxPooling2D((1, 2), strides=(1, 2))(conv1)

    conv1 = layers.Conv2D(filters=256,
                          kernel_size=(1, 3),
                          strides=1,
                          padding='same')(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    conv1 = layers.Conv2D(filters=256,
                          kernel_size=(1, 3),
                          strides=1,
                          padding='same')(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    conv1 = layers.Conv2D(filters=256,
                          kernel_size=(1, 3),
                          strides=1,
                          padding='same')(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)

    primarycaps = PrimaryCap(conv1,
                             dim_capsule=8,
                             n_channels=32,
                             kernel_size=(1, 3),
                             strides=1,
                             padding='same')
    digitcaps = CapsuleLayer(num_capsule=n_class,
                             dim_capsule=args.dim_capsule,
                             routings=routings,
                             name='digitcaps')(primarycaps)
    out_caps = Length(name='capsnet')(digitcaps)

    model = models.Model(x, out_caps)
    return model
Exemple #13
0
# Activation - 하이퍼볼릭 탄젠트
model = Sequential()
model.add(Dense(128, activation='tanh', input_shape=(28 * 28, )))
model.add(Dense(10, activation='softmax'))
# test_acc : 0.9739999771118164

# Activation - 리키렐루
from keras.layers.advanced_activations import LeakyReLU
model = Sequential()
activation = LeakyReLU(.001)
# test_acc : 0.9801999926567078

# Activation - 엘루
from keras.layers.advanced_activations import ELU
model = Sequential()
activation = ELU(.001)
# test_acc : 0.982200026512146

model.add(Dense(512, activation=activation, input_shape=(28 * 28, )))
model.add(Dense(10, activation='softmax'))

# Optimizer - 아담
from tensorflow.keras.optimizers import Adam
optimizer = Adam(.001)

model.compile(optimizer=optimizer,
              loss='categorical_crossentropy',
              metrics='accuracy')

model.summary()
Exemple #14
0
def BNA(_input):
    inputs_norm = BatchNormalization(axis=3)(_input)
    return ELU()(inputs_norm)
Exemple #15
0
 def f(_input):
     conv = Conv2D(nb_filter, nb_row, strides=2,
                           padding=padding)(_input)
     norm = BatchNormalization(axis=3)(conv)
     return ELU()(norm)
def inception_block(inputs,
                    depth,
                    batch_mode=0,
                    splitted=False,
                    activation='relu'):
    assert depth % 16 == 0
    actv = activation == 'relu' and (lambda: LeakyReLU(
        0.0)) or activation == 'elu' and (lambda: ELU(1.0)) or None
    print(inputs)
    # c1_1 = Convolution2D(depth//4, 1, 1, init='he_normal', border_mode='same')(inputs)
    c1_1 = Conv2D(depth // 4, (1, 1),
                  padding="same",
                  kernel_initializer="he_normal",
                  data_format='channels_first')(inputs)
    print(c1_1.shape)
    # c2_1 = Convolution2D(depth//8*3, 1, 1, init='he_normal', border_mode='same')(inputs)
    c2_1 = Conv2D(depth // 8 * 3, (1, 1),
                  padding="same",
                  kernel_initializer="he_normal",
                  data_format='channels_first')(inputs)
    print(c2_1.shape)

    c2_1 = actv()(c2_1)
    if splitted:
        # c2_2 = Convolution2D(depth//2, 1, 3, init='he_normal', border_mode='same')(c2_1)
        c2_2 = Conv2D(depth // 2, (1, 3),
                      padding="same",
                      kernel_initializer="he_normal",
                      data_format='channels_first')(c2_1)

        # c2_2 = BatchNormalization(mode=batch_mode, axis=1)(c2_2)
        c2_2 = BatchNormalization(axis=1)(c2_2)
        c2_2 = actv()(c2_2)
        # c2_3 = Convolution2D(depth//2, 3, 1, init='he_normal', border_mode='same')(c2_2)
        c2_3 = Conv2D(depth // 2, (3, 1),
                      padding="same",
                      kernel_initializer="he_normal",
                      data_format='channels_first')(c2_2)

    else:
        # c2_3 = Convolution2D(depth//2, 3, 3, init='he_normal', border_mode='same')(c2_1)
        c2_3 = Conv2D(depth // 2, (3, 3),
                      padding="same",
                      kernel_initializer="he_normal",
                      data_format='channels_first')(c2_1)

    # c3_1 = Convolution2D(depth//16, 1, 1, init='he_normal', border_mode='same')(inputs)
    c3_1 = Conv2D(depth // 16, (1, 1),
                  padding="same",
                  kernel_initializer="he_normal",
                  data_format='channels_first')(inputs)

    #missed batch norm
    c3_1 = actv()(c3_1)
    if splitted:
        # c3_2 = Convolution2D(depth//8, 1, 5, init='he_normal', border_mode='same')(c3_1)
        c3_2 = Conv2D(depth // 8, (1, 5),
                      padding="same",
                      kernel_initializer="he_normal",
                      data_format='channels_first')(c3_1)

        # c3_2 = BatchNormalization(mode=batch_mode, axis=1)(c3_2)
        c3_2 = BatchNormalization(axis=1)(c3_2)
        c3_2 = actv()(c3_2)
        # c3_3 = Convolution2D(depth//8, 5, 1, init='he_normal', border_mode='same')(c3_2)
        c3_3 = Conv2D(depth // 8, (5, 1),
                      padding="same",
                      kernel_initializer="he_normal",
                      data_format='channels_first')(c3_2)

    else:
        # c3_3 = Convolution2D(depth//8, 5, 5, init='he_normal', border_mode='same')(c3_1)
        c3_3 = Conv2D(depth // 8, (5, 5),
                      padding="same",
                      kernel_initializer="he_normal",
                      data_format='channels_first')(c3_1)

    p4_1 = MaxPooling2D(pool_size=(3, 3), strides=(1, 1),
                        border_mode='same')(inputs)
    # c4_2 = Convolution2D(depth//8, 1, 1, init='he_normal', border_mode='same')(p4_1)
    c4_2 = Conv2D(depth // 8, (1, 1),
                  padding="same",
                  kernel_initializer="he_normal",
                  data_format='channels_first')(p4_1)

    # res = merge([c1_1, c2_3, c3_3, c4_2], mode='concat', concat_axis=1)
    res = concatenate([c1_1, c2_3, c3_3, c4_2], axis=1)

    # res = BatchNormalization(mode=batch_mode, axis=1)(res)
    res = BatchNormalization(axis=1)(res)
    res = actv()(res)
    return res
Exemple #17
0
def getModels():
    input_img = Input(shape=(imageSize, imageSize, 1))
    x = Convolution2D(32, 3, 3, border_mode='same')(input_img)
    x = ELU()(x)
    x = MaxPooling2D((2, 2), border_mode='same')(x)

    x = Convolution2D(64, 3, 3, border_mode='same')(x)
    x = ELU()(x)
    x = MaxPooling2D((2, 2), border_mode='same')(x)

    # Latent space // bottleneck layer
    x = Flatten()(x)
    x = Dense(latent_dim)(x)
    z = ELU()(x)

    ##### MODEL 1: ENCODER #####
    encoder = Model(input_img, z)
   
    # We instantiate these layers separately so as to reuse them for the decoder
    # Dense from latent space to image dimension
    x_decoded_dense1 = Dense(7 * 7 * 64)

    # Reshape for image
    x_decoded_reshape0 = Reshape((7, 7, 64))
    x_decoded_upsample0 = UpSampling2D((2, 2))
    x_decoded_conv0  = Convolution2D(32, 3, 3, border_mode='same')

    x_decoded_upsample3 = UpSampling2D((2, 2))
    x_decoded_conv3 = Convolution2D(1, 3, 3, activation='sigmoid', border_mode='same')

    # Create second part of autoencoder
    x_decoded = x_decoded_dense1(z)
    x_decoded = ELU()(x_decoded)

    x_decoded = x_decoded_reshape0(x_decoded)
    x_decoded = x_decoded_upsample0(x_decoded)
    x_decoded = x_decoded_conv0(x_decoded)
    x_decoded = ELU()(x_decoded)

    # Tanh layer
    x_decoded = x_decoded_upsample3(x_decoded)
    decoded_img = x_decoded_conv3(x_decoded)

    ##### MODEL 2: AUTO-ENCODER #####
    autoencoder = Model(input_img, decoded_img)

    # Create decoder
    input_z = Input(shape=(latent_dim,))
    x_decoded_decoder = x_decoded_dense1(input_z)
    x_decoded_decoder = ELU()(x_decoded_decoder)

    x_decoded_decoder = x_decoded_reshape0(x_decoded_decoder)
    x_decoded_decoder = x_decoded_upsample0(x_decoded_decoder)
    x_decoded_decoder = x_decoded_conv0(x_decoded_decoder)
    x_decoded_decoder = ELU()(x_decoded_decoder)

    # Tanh layer
    x_decoded_decoder = x_decoded_upsample3(x_decoded_decoder)
    decoded_decoder_img = x_decoded_conv3(x_decoded_decoder)

    ##### MODEL 3: DECODER #####
    decoder = Model(input_z, decoded_decoder_img)
    return autoencoder, encoder, decoder
Exemple #18
0
def nn_model(xtrain=xtrain):
    model = Sequential()
    model.add(
        Dense(
            200,
            input_dim=xtrain.shape[1],
            W_regularizer=keras.regularizers.WeightRegularizer(
                l1=w_reg_weight_l1, l2=w_reg_weight_l2),
            #                    activity_regularizer = keras.regularizers.ActivityRegularizer(l1=0.0002, l2=0.0),
            init=init_string))

    #    model.add(PReLU())

    model.add(ELU())
    #    model.add(LeakyReLU())
    #    model.add(PReLU())
    model.add(BatchNormalization())
    model.add(Dropout(0.5))

    model.add(
        Dense(100,
              W_regularizer=keras.regularizers.WeightRegularizer(
                  l1=w_reg_weight_l1, l2=w_reg_weight_l2),
              init=init_string))

    model.add(ELU())
    #    model.add(LeakyReLU())
    #    model.add(PReLU())
    model.add(BatchNormalization())
    model.add(Dropout(0.25))

    #    model.add(Dense(50,
    ##                    W_regularizer=l2(w_reg_weight_l2),
    #                    init = init_string))
    #    model.add(ELU())
    #    model.add(LeakyReLU())
    #    model.add(BatchNormalization())
    #    model.add(Dropout(0.1))

    model.add(
        Dense(
            1,
            #                    W_regularizer=l2(w_reg_weight_l2),
            init=init_string))
    #    model.add(PReLU())
    #    model.add(BatchNormalization())
    #    model.add(LeakyReLU())

    #    model.add(Dense(1, W_regularizer=l2(w_reg_weight), init = 'he_normal'))

    #    model.compile(loss = 'mae', optimizer = 'RMSprop')
    #    model.compile(loss = 'mae', optimizer = 'adadelta')
    #    model.compile(loss = 'mae', optimizer = 'rmsprop')
    sgd = keras.optimizers.SGD(lr=0.005,
                               decay=1e-3,
                               momentum=0.2,
                               nesterov=False)
    model.compile(loss='mae', optimizer=sgd)
    #    rms_prop = keras.optimizers.RMSprop(lr=0.0005, rho=0.9, epsilon=1e-08, decay=0.0)
    #    model.compile(loss='mae', optimizer=rms_prop)
    return (model)
Exemple #19
0
def ResNet50_3D(weights=None,
                input_tensor=None,
                input_shape=None,
                classes=None):
    """Instantiates the ResNet50_3D architecture.


    # Arguments
        weights: one of `None` (random initialization),
              'imagenet' (pre-training on ImageNet),
              or the path to the weights file to be loaded.
        input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
            to use as image input for the model.
        input_shape: optional shape tuple, only to be specified
            if `include_top` is False (otherwise the input shape
            has to be `(224, 224, 3)` (with `channels_last` data format)
            or `(3, 224, 224)` (with `channels_first` data format).
            It should have exactly 3 inputs channels,
            and width and height should be no smaller than 197.
            E.g. `(200, 200, 3)` would be one valid value.
        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.

    """

    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
    if K.image_data_format() == 'channels_last':
        bn_axis = 4
    else:
        bn_axis = 1

#    x = ZeroPadding2D(padding=(3, 3), name='conv1_pad')(img_input)
    x = Conv3D(64, (1, 1, 3),
               strides=(1, 1, 2),
               kernel_initializer=kernel_Initializer,
               kernel_regularizer=kernel_Regularizer,
               padding='valid',
               name='conv1')(img_input)
    x = BatchNormalization(axis=bn_axis, name='bn_conv1')(x)
    #    x = Activation('relu')(x)
    x = ELU()(x)
    x = MaxPooling3D((1, 1, 3), strides=(1, 1, 2))(x)

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

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

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

    x = AveragePooling3D((7, 7, 1), name='avg_pool')(x)

    x = Flatten()(x)
    x = Dropout(0.5055)(x)
    #
    #
    x = Dense(512,
              kernel_initializer=kernel_Initializer,
              kernel_regularizer=kernel_Regularizer,
              name='dense')(x)
    x = ELU()(x)
    #
    #
    x = Dense(classes,
              activation='softmax',
              kernel_initializer=kernel_Initializer,
              kernel_regularizer=kernel_Regularizer,
              name='softmax')(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
    # Create model.
    model = Model(inputs, x, name='resnet50_3D')

    if weights is not None:
        model.load_weights(weights)

    return model
Exemple #20
0
def conv_block(input_tensor,
               kernel_size,
               filters,
               stage,
               block,
               strides=(1, 1, 1)):  #strides=(2, 2, 2)
    """A block that has a conv layer at shortcut.

    # Arguments
        input_tensor: input tensornb_
        kernel_size: default 3, the kernel size of middle conv layer at main path
        filters: list of integers, the filters of 3 conv layer at main path
        stage: integer, current stage label, used for generating layer names
        block: 'a','b'..., current block label, used for generating layer names
        strides: Strides for the first conv layer in the block.

    # Returns
        Output tensor for the block.

    Note that from stage 3,2
    the first conv layer at main path is with strides=(2, 2)
    And the shortcut should have strides=(2, 2) as well
    """
    filters1, filters2, filters3 = filters
    if K.image_data_format() == 'channels_last':
        bn_axis = 4
    else:
        bn_axis = 1
    conv_name_base = 'res' + str(stage) + block + '_branch'
    bn_name_base = 'bn' + str(stage) + block + '_branch'

    x = Conv3D(filters1, (1, 1, 1),
               strides=strides,
               kernel_initializer=kernel_Initializer,
               kernel_regularizer=kernel_Regularizer,
               name=conv_name_base + '2a')(input_tensor)
    x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2a')(x)
    #    x = Activation('relu')(x)
    x = ELU()(x)

    x = Conv3D(filters2,
               kernel_size,
               padding='same',
               kernel_initializer=kernel_Initializer,
               kernel_regularizer=kernel_Regularizer,
               name=conv_name_base + '2b')(x)
    x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2b')(x)
    #    x = Activation('relu')(x)
    x = ELU()(x)

    x = Conv3D(filters3, (1, 1, 1),
               kernel_initializer=kernel_Initializer,
               kernel_regularizer=kernel_Regularizer,
               name=conv_name_base + '2c')(x)
    x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2c')(x)

    shortcut = Conv3D(filters3, (1, 1, 1),
                      strides=strides,
                      kernel_initializer=kernel_Initializer,
                      kernel_regularizer=kernel_Regularizer,
                      name=conv_name_base + '1')(input_tensor)
    shortcut = BatchNormalization(axis=bn_axis,
                                  name=bn_name_base + '1')(shortcut)

    x = layers.add([x, shortcut])
    #    x = Activation('relu')(x)
    x = ELU()(x)
    return x
Exemple #21
0
x, y = utils.shuffle(img_arr, Y)

X_train, X_test, y_train, y_test = model_selection.train_test_split(
    x, y, test_size=0.2, random_state=2)

input_shape = img_arr[0].shape

model = Sequential()

model.add(
    Conv2D(32,
           kernel_size=(3, 3),
           padding='same',
           input_shape=input_shape,
           activation='linear'))
model.add(ELU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64, kernel_size=(3, 3), activation='linear', padding='same'))
model.add(ELU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Dropout(0.5))

model.add(Conv2D(128, kernel_size=(3, 3), activation='linear', padding='same'))
model.add(ELU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Dropout(0.5))

model.add(Flatten())
    def get_model(self, with_compile=True):
        # input layer
        audio_input = Input(shape=(128, 43, 1), name='input')
        x = audio_input

        # Layer 1
        x = Conv2D(16, (5, 5),
                   padding='same',
                   kernel_regularizer=self.regularizer,
                   name='conv_1')(x)
        x = ELU()(x)
        x = MaxPooling2D(pool_size=(2, 1), strides=(2, 1), name='MP_1')(x)

        # Layer 2
        x = Conv2D(32, (3, 3),
                   padding='same',
                   kernel_regularizer=self.regularizer,
                   name='conv_2')(x)
        x = BatchNormalization(axis=3)(x)
        x = ELU()(x)
        x = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name='MP_2')(x)
        x = Dropout(0.1)(x)

        # Layer 3
        x = Conv2D(64, (3, 3),
                   padding='same',
                   kernel_regularizer=self.regularizer,
                   name='conv_3')(x)
        x = ELU()(x)
        x = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name='MP_3')(x)

        # Layer 4
        x = Conv2D(64, (3, 3),
                   padding='same',
                   kernel_regularizer=self.regularizer,
                   name='conv_4')(x)
        x = BatchNormalization(axis=3)(x)
        x = ELU()(x)
        x = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name='MP_4')(x)
        x = Dropout(0.1)(x)

        # Layer 5
        x = Conv2D(128, (3, 3),
                   padding='same',
                   kernel_regularizer=self.regularizer,
                   name='conv_5')(x)
        x = ELU()(x)
        x = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name='MP_5')(x)

        # Layer 6
        x = Conv2D(256, (3, 3),
                   padding='same',
                   kernel_regularizer=self.regularizer,
                   name='conv_6')(x)
        x = ELU()(x)

        # layer 7
        x = Conv2D(256, (1, 1),
                   padding='same',
                   kernel_regularizer=self.regularizer,
                   name='conv_7')(x)
        x = BatchNormalization(axis=3)(x)
        x = ELU()(x)

        # GAP
        x = GlobalAveragePooling2D()(x)
        x = BatchNormalization()(x)

        # Dense
        x = Dense(256, kernel_regularizer=self.regularizer, name='dense')(x)
        x = BatchNormalization()(x)
        x = ELU()(x)
        x = Dropout(0.5)(x)

        # output
        x = Dense(self.num_tags,
                  kernel_regularizer=self.regularizer,
                  name='output')(x)
        x = Activation('softmax')(x)

        # model
        model = Model(audio_input, x)

        if with_compile:
            optimizer = Adam(lr=0.001)
            model.compile(optimizer=optimizer,
                          loss='categorical_crossentropy',
                          metrics=['accuracy'])
        return model
Exemple #23
0
def get_unet():
    inputs = Input((img_rows, img_cols, 1))
    conv1 = Conv2D(32, (3, 3), kernel_initializer='he_normal', padding='same')(inputs)
    conv1 = ELU()(conv1)
    conv1 = BatchNormalization()(conv1)
    conv1 = Conv2D(32, (3, 3), kernel_initializer='he_normal', padding='same')(conv1)
    conv1 = ELU()(conv1)
    conv1 = BatchNormalization()(conv1)
    pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
    #pool1 = Dropout(0.2)(pool1)

    conv2 = Conv2D(64, (3, 3), kernel_initializer='he_normal', padding='same')(pool1)
    conv2 = ELU()(conv2)
    conv2 = BatchNormalization()(conv2)
    conv2 = Conv2D(64, (3, 3), kernel_initializer='he_normal', padding='same')(conv2)
    conv2 = ELU()(conv2)
    conv2 = BatchNormalization()(conv2)
    pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
    #pool2 = Dropout(0.2)(pool2)

    conv3 = Conv2D(128, (3, 3), kernel_initializer='he_normal', padding='same')(pool2)
    conv3 = ELU()(conv3)
    conv3 = BatchNormalization()(conv3)
    conv3 = Conv2D(128, (3, 3), kernel_initializer='he_normal', padding='same')(conv3)
    conv3 = ELU()(conv3)
    conv3 = BatchNormalization()(conv3)
    pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)
    #pool3 = Dropout(0.2)(pool3)

    conv4 = Conv2D(256, (3, 3), kernel_initializer='he_normal', padding='same')(pool3)
    conv4 = ELU()(conv4)
    conv4 = BatchNormalization()(conv4)
    conv4 = Conv2D(256, (3, 3), kernel_initializer='he_normal', padding='same')(conv4)
    conv4 = ELU()(conv4)
    conv4 = BatchNormalization()(conv4)
    pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)
    #pool4 = Dropout(0.2)(pool4)

    conv5 = Conv2D(512, (3, 3), kernel_initializer='he_normal', padding='same')(pool4)
    conv5 = ELU()(conv5)
    conv5 = BatchNormalization()(conv5)
    conv5 = Conv2D(512, (3, 3), kernel_initializer='he_normal', padding='same')(conv5)
    conv5 = ELU()(conv5)
    conv5 = BatchNormalization()(conv5)
    #conv5 = Dropout(0.2)(conv5)

    up6 = concatenate([Conv2DTranspose(256, (2, 2), strides=(2, 2), padding='same')(conv5), conv4], axis=3)
    conv6 = Conv2D(256, (3, 3), padding='same')(up6)
    conv6 = ELU()(conv6)
    #conv6 = BatchNormalization()(conv6)
    conv6 = Conv2D(256, (3, 3), padding='same')(conv6)
    conv6 = ELU()(conv6)
    #conv6 = BatchNormalization()(conv6)
    conv6 = Dropout(0.2)(conv6)

    up7 = concatenate([Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same')(conv6), conv3], axis=3)
    conv7 = Conv2D(128, (3, 3), padding='same')(up7)
    conv7 = ELU()(conv7)
    #conv7 = BatchNormalization()(conv7)
    conv7 = Conv2D(128, (3, 3), padding='same')(conv7)
    conv7 = ELU()(conv7)
    #conv7 = BatchNormalization()(conv7)
    conv7 = Dropout(0.2)(conv7)

    up8 = concatenate([Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same')(conv7), conv2], axis=3)
    conv8 = Conv2D(64, (3, 3), padding='same')(up8)
    conv8 = ELU()(conv8)
    #conv8 = BatchNormalization()(conv8)
    conv8 = Conv2D(64, (3, 3), padding='same')(conv8)
    conv8 = ELU()(conv8)
    #conv8 = BatchNormalization()(conv8)
    conv8 = Dropout(0.2)(conv8)

    up9 = concatenate([Conv2DTranspose(32, (2, 2), strides=(2, 2), padding='same')(conv8), conv1], axis=3)
    conv9 = Conv2D(32, (3, 3), padding='same')(up9)
    conv9 = ELU()(conv9)
    #conv9 = BatchNormalization()(conv9)
    conv9 = Conv2D(32, (3, 3), padding='same')(conv9)
    conv9 = ELU()(conv9)
    #conv9 = BatchNormalization()(conv9)
    conv9 = Dropout(0.2)(conv9)

    conv10 = Conv2D(1, (1, 1), activation='sigmoid')(conv9)

    model = Model(inputs=[inputs], outputs=[conv10])

    model.compile(optimizer=Adam(lr=1e-5), loss=dice_coef_loss, metrics=[dice_coef])

    return model
    n_hidden2 = n_neurons
    n_hidden3 = n_neurons
    n_hidden4 = n_neurons
    n_hidden5 = n_neurons
    n_hidden6 = n_neurons
    n_hidden7 = n_neurons
    n_hidden8 = n_neurons
    n_hidden9 = n_neurons
    n_hidden10 = n_neurons

    # model functions
    init = 'he_uniform'
    optimizer = 'adam'
    loss = 'sparse_categorical_crossentropy'
    output_activation = 'softmax'
    activation = ELU(alpha=ELU_alpha)
    '''
    keranl initializer: 'he_uniform', 'lecun_normal', 'lecun_uniform'
    optimizer function: 'adam', 'adamax', 'nadam', 'sgd'
    loss function: 'categorical_crossentropy'
    activation function: LeakyReLU(alpha=alpha)
    '''

    # data and results path
    project_dir = r'\\10.39.42.102\temp\Prostate_Cancer_ex_vivo\Deep_Learning'
    result_dir = r'\\10.39.42.102\temp\Prostate_Cancer_ex_vivo\Deep_Learning\ROC\result'
    log_dir = r'\\10.39.42.102\temp\Prostate_Cancer_ex_vivo\Deep_Learning\ROC\log'
    train_file = 'PCa.csv'

    # ----------------------------------------------------------------------------------
    # run the model
Exemple #25
0
batch_normalization = partial(
                              keras.layers.BatchNormalization,
                              axis=-1,
                              momentum=batch_momentum,
                              epsilon=0.001,
                              beta_initializer='zeros',
                              gamma_initializer='ones',
                              beta_regularizer=None,
                              gamma_regularizer=None                             
)     
                                    
                                    
# input layer                              
model.add(dense_layer(18, input_dim=n_inputs))
model.add(batch_normalization())
model.add(ELU(alpha=1.0))
model.add(Dropout(dropout_rate))

# hidden layer 1
model.add(dense_layer(n_hidden1))
model.add(batch_normalization())
model.add(ELU(alpha=1.0))
#model.add(Dropout(dropout_rate))

# hidden layer 2
model.add(dense_layer(n_hidden2))
model.add(batch_normalization())
model.add(ELU(alpha=1.0))
model.add(Dropout(dropout_rate))

# hidden layer 3
def Keras_model(init, loss):
    model = Sequential()

    dense_layer = partial(
        keras.layers.Dense,
        init=init,
        use_bias=False,
        activation=None,
    )

    batch_normalization = partial(keras.layers.BatchNormalization,
                                  axis=-1,
                                  momentum=batch_momentum,
                                  epsilon=0.001,
                                  beta_initializer='zeros',
                                  gamma_initializer='ones',
                                  beta_regularizer=None,
                                  gamma_regularizer=None)

    # input layer
    model.add(dense_layer(18, input_dim=n_inputs))
    model.add(batch_normalization())
    model.add(ELU(alpha=1.0))
    #model.add(LeakyReLU(alpha=alpha))
    model.add(Dropout(dropout_rate))

    # hidden layer 1
    model.add(dense_layer(n_hidden1))
    model.add(batch_normalization())
    model.add(ELU(alpha=1.0))
    #model.add(LeakyReLU(alpha=alpha))
    model.add(Dropout(dropout_rate))

    # hidden layer 2
    model.add(dense_layer(n_hidden2))
    model.add(batch_normalization())
    model.add(ELU(alpha=1.0))
    #model.add(LeakyReLU(alpha=alpha))
    model.add(Dropout(dropout_rate))

    # hidden layer 3
    model.add(dense_layer(n_hidden3))
    model.add(batch_normalization())
    model.add(ELU(alpha=1.0))
    #model.add(LeakyReLU(alpha=alpha))
    model.add(Dropout(dropout_rate))

    # hidden layer 4
    model.add(dense_layer(n_hidden4))
    model.add(batch_normalization())
    #model.add(ELU(alpha=1.0))
    model.add(LeakyReLU(alpha=alpha))
    model.add(Dropout(dropout_rate))

    # hidden layer 5
    model.add(dense_layer(n_hidden5))
    model.add(batch_normalization())
    model.add(ELU(alpha=1.0))
    #model.add(LeakyReLU(alpha=alpha))
    model.add(Dropout(dropout_rate))

    # hidden layer 6
    model.add(dense_layer(n_hidden6))
    model.add(batch_normalization())
    model.add(ELU(alpha=1.0))
    #model.add(LeakyReLU(alpha=alpha))
    model.add(Dropout(dropout_rate))

    # hidden layer 7
    model.add(dense_layer(n_hidden7))
    model.add(batch_normalization())
    model.add(ELU(alpha=1.0))
    #model.add(LeakyReLU(alpha=alpha))
    model.add(Dropout(dropout_rate))

    # hidden layer 8
    model.add(dense_layer(n_hidden8))
    model.add(batch_normalization())
    model.add(ELU(alpha=1.0))
    #model.add(LeakyReLU(alpha=alpha))
    model.add(Dropout(dropout_rate))

    # hidden layer 9
    model.add(dense_layer(n_hidden9))
    model.add(batch_normalization())
    #model.add(ELU(alpha=1.0))
    model.add(LeakyReLU(alpha=alpha))
    model.add(Dropout(dropout_rate))

    # hidden layer 10
    model.add(dense_layer(n_hidden10))
    model.add(batch_normalization())
    model.add(ELU(alpha=1.0))
    #model.add(LeakyReLU(alpha=alpha))
    model.add(Dropout(dropout_rate))

    # output layer
    model.add(dense_layer(n_outputs))
    model.add(batch_normalization())
    model.add(Activation('softmax'))

    model.summary()

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

    return model
Exemple #27
0
def get_unet(input_shape, n_classes=1, n_filters=32):
    """
    UNet without crop. May be exposed to some uncertaineties near the boundaries, but works good on our tasks
    input shape should be with "channels_last"

    :param input_shape: input shape of the images
    :param n_classes: the number of classes for prediction mask
    :param n_filters: parameter of the network, channels multiplier
    """

    inputs = Input(shape=(None, None, input_shape[-1]))

    conv1 = Conv2D(n_filters, (3, 3),
                   padding='same',
                   kernel_initializer='he_uniform')(inputs)
    conv1 = BatchNormalization()(conv1)
    conv1 = ELU()(conv1)
    conv1 = Conv2D(n_filters, (3, 3),
                   padding='same',
                   kernel_initializer='he_uniform')(conv1)
    conv1 = BatchNormalization()(conv1)
    conv1 = ELU()(conv1)
    pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)

    conv2 = Conv2D(n_filters * 2, (3, 3),
                   padding='same',
                   kernel_initializer='he_uniform')(pool1)
    conv2 = BatchNormalization()(conv2)
    conv2 = ELU()(conv2)
    conv2 = Conv2D(n_filters * 2, (3, 3),
                   padding='same',
                   kernel_initializer='he_uniform')(conv2)
    conv2 = BatchNormalization()(conv2)
    conv2 = ELU()(conv2)
    pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)

    conv3 = Conv2D(n_filters * 4, (3, 3),
                   padding='same',
                   kernel_initializer='he_uniform')(pool2)
    conv3 = BatchNormalization()(conv3)
    conv3 = ELU()(conv3)
    conv3 = Conv2D(n_filters * 4, (3, 3),
                   padding='same',
                   kernel_initializer='he_uniform')(conv3)
    conv3 = BatchNormalization()(conv3)
    conv3 = ELU()(conv3)
    pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)

    conv4 = Conv2D(n_filters * 8, (3, 3),
                   padding='same',
                   kernel_initializer='he_uniform')(pool3)
    conv4 = BatchNormalization()(conv4)
    conv4 = ELU()(conv4)
    conv4 = Conv2D(n_filters * 8, (3, 3),
                   padding='same',
                   kernel_initializer='he_uniform')(conv4)
    conv4 = BatchNormalization()(conv4)
    conv4 = ELU()(conv4)
    pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)

    conv5 = Conv2D(n_filters * 16, (3, 3),
                   padding='same',
                   kernel_initializer='he_uniform')(pool4)
    conv5 = BatchNormalization()(conv5)
    conv5 = ELU()(conv5)
    conv5 = Conv2D(n_filters * 16, (3, 3),
                   padding='same',
                   kernel_initializer='he_uniform')(conv5)
    conv5 = BatchNormalization()(conv5)
    conv5 = ELU()(conv5)

    up6 = concatenate([UpSampling2D(size=(2, 2))(conv5), conv4], axis=3)
    conv6 = Conv2D(n_filters * 8, (3, 3),
                   padding='same',
                   kernel_initializer='he_uniform')(up6)
    conv6 = BatchNormalization()(conv6)
    conv6 = ELU()(conv6)
    conv6 = Conv2D(n_filters * 8, (3, 3),
                   padding='same',
                   kernel_initializer='he_uniform')(conv6)
    conv6 = BatchNormalization()(conv6)
    conv6 = ELU()(conv6)

    up7 = concatenate([UpSampling2D(size=(2, 2))(conv6), conv3], axis=3)
    conv7 = Conv2D(n_filters * 4, (3, 3),
                   padding='same',
                   kernel_initializer='he_uniform')(up7)
    conv7 = BatchNormalization()(conv7)
    conv7 = ELU()(conv7)
    conv7 = Conv2D(n_filters * 4, (3, 3),
                   padding='same',
                   kernel_initializer='he_uniform')(conv7)
    conv7 = BatchNormalization()(conv7)
    conv7 = ELU()(conv7)

    up8 = concatenate([UpSampling2D(size=(2, 2))(conv7), conv2], axis=3)
    conv8 = Conv2D(n_filters * 2, (3, 3),
                   padding='same',
                   kernel_initializer='he_uniform')(up8)
    conv8 = BatchNormalization()(conv8)
    conv8 = ELU()(conv8)
    conv8 = Conv2D(n_filters * 2, (3, 3),
                   padding='same',
                   kernel_initializer='he_uniform')(conv8)
    conv8 = BatchNormalization()(conv8)
    conv8 = ELU()(conv8)

    up9 = concatenate([UpSampling2D(size=(2, 2))(conv8), conv1], axis=3)
    conv9 = Conv2D(n_filters, (3, 3),
                   padding='same',
                   kernel_initializer='he_uniform')(up9)
    conv9 = BatchNormalization()(conv9)
    conv9 = ELU()(conv9)
    conv9 = Conv2D(n_filters, (3, 3),
                   padding='same',
                   kernel_initializer='he_uniform')(conv9)
    conv9 = BatchNormalization()(conv9)
    conv9 = ELU()(conv9)
    conv10 = Conv2D(n_classes, (1, 1), activation='sigmoid')(conv9)

    model = Model(inputs=inputs, outputs=conv10)

    return model
Exemple #28
0
def define_network(input_shape, fine_tuning=False, learning_rate=0.001):
    """ Here we define the Nvidia network.

        Further reading at:
        https://images.nvidia.com/content/tegra/automotive/images/2016/solutions/pdf/end-to-end-dl-using-px.pdf
    """
    weight_init = 'glorot_uniform'
    padding = 'valid'
    dropout_prob = 0.25

    # Define model
    model = Sequential()

    # Normalize the input without changing shape
    model.add(
        Lambda(normalize, input_shape=input_shape, output_shape=input_shape))

    # Convolution layers
    model.add(
        Convolution2D(24,
                      5,
                      5,
                      border_mode=padding,
                      init=weight_init,
                      subsample=(2, 2)))
    model.add(ELU())
    model.add(
        Convolution2D(36,
                      5,
                      5,
                      border_mode=padding,
                      init=weight_init,
                      subsample=(2, 2)))
    model.add(ELU())
    model.add(
        Convolution2D(48,
                      5,
                      5,
                      border_mode=padding,
                      init=weight_init,
                      subsample=(2, 2)))
    model.add(ELU())
    model.add(
        Convolution2D(64,
                      3,
                      3,
                      border_mode=padding,
                      init=weight_init,
                      subsample=(1, 1)))
    model.add(ELU())
    model.add(
        Convolution2D(64,
                      3,
                      3,
                      border_mode=padding,
                      init=weight_init,
                      subsample=(1, 1)))

    # Fully connected classifier
    model.add(Flatten())
    model.add(Dropout(dropout_prob))
    model.add(ELU())

    model.add(Dense(100, init=weight_init))
    model.add(Dropout(dropout_prob))
    model.add(ELU())

    model.add(Dense(50, init=weight_init))
    model.add(Dropout(dropout_prob))
    model.add(ELU())

    model.add(Dense(10, init=weight_init))
    model.add(Dropout(dropout_prob))
    model.add(ELU())

    model.add(Dense(1, init=weight_init, name='output'))

    # Display the model summary
    model.summary()

    # Compile it
    model.compile(loss='mse', optimizer=Adam(lr=learning_rate))
    return model
def BNA(_input):
    # inputs_norm = BatchNormalization(mode=2, axis=1)(_input)
    inputs_norm = BatchNormalization(axis=1)(_input)
    return ELU()(inputs_norm)

#####################################
# CNN HERE
#####################################
# NVIDIA CNN
# Based on:
# http://images.nvidia.com/content/tegra/automotive/images/2016/solutions/pdf/end-to-end-dl-using-px.pdf

model = Sequential()
# Normalisation
model.add(Lambda(lambda x: x /127.5 - 1.5, input_shape=(66,200,3)))

# three 5x5 Convolutional layers (output depths 24, 36, 48 and 2x2 strides)
model.add(Convolution2D(24,5,5, subsample=(2,2), border_mode='valid', W_regularizer=l2(0.001)))
model.add(ELU())
model.add(Convolution2D(36,5,5, subsample=(2,2), border_mode='valid', W_regularizer=l2(0.001)))
model.add(ELU())
model.add(Convolution2D(48,5,5, subsample=(2,2), border_mode='valid', W_regularizer=l2(0.001)))
model.add(ELU())

# two 3x3 Convolutional layers (output depths 64 and 64)
model.add(Convolution2D(64,3,3, border_mode='valid', W_regularizer=l2(0.001)))
model.add(ELU())
model.add(Convolution2D(64,3,3, border_mode='valid', W_regularizer=l2(0.001)))
model.add(ELU())

# Flatten layer
model.add(Flatten())

# three fully connected layers (depths 100, 50, 10)
Exemple #31
0
def rblock(inputs, num, depth, scale=0.1):    
    residual = Conv2D(depth, num, strides=1, padding='same')(inputs)
    residual = BatchNormalization(axis=3)(residual)
    residual = Lambda(lambda x: x*scale)(residual)
    res = _shortcut(inputs, residual)
    return ELU()(res)