def residual_connection(input_tensor, output_dims):
    """
    Reshapes input tensor to be of ouput_dims dimensions but with more layers.
    Ex transforms 51x51x1 image to 24x24x5 and pads the extra with zeros.
    """
    from keras import layers
    #import keras.backend as K #for gather
    input_size = 1
    input_dims = input_tensor.shape[1:3]
    for input_dim in input_dims:
        input_size *= int(input_dim)

    output_size = 1
    for output_dim in output_dims:
        output_size *= int(output_dim)
    nbr_layers_ouput = int(
        int(input_size) / int(output_size)) + 1  # nbr layers we will need
    padding_size = int(nbr_layers_ouput * output_size - input_size)

    flattened = layers.Reshape(
        (input_dims[0] * input_dims[1], 1))(input_tensor)
    # use gather to make it nicer?
    padded = layers.ZeroPadding1D((0, padding_size))(flattened)

    output_shape = (int(output_dims[0]), int(output_dims[1]), nbr_layers_ouput)
    tmp = layers.Reshape(output_shape)(padded)
    return tmp
Exemple #2
0
def Spatial_pyramid_pooling(x,name):

    x_max_2 = layers.MaxPooling1D(2, strides=2, name=name + '_SPP_max2',padding='same')(x)
    _, W, C = K.int_shape(x_max_2)

    x_max_3 = layers.MaxPooling1D(4, strides=4, name=name + '_stride_4',padding='same')(x)
    _, W1, _ = K.int_shape(x_max_3)
    x_max_3_r = layers.ZeroPadding1D(padding=(0, W - W1) )(x_max_3)

    x_max_4 = layers.MaxPooling1D(8, strides=8, name=name + '_stride_8',padding='same')(x)
    _, W1, _ = K.int_shape(x_max_4)
    x_max_4_r = layers.ZeroPadding1D(padding=(0, W - W1) )(x_max_4)

    x = layers.Concatenate(name=name+ '3_4')([x_max_3_r,x_max_4_r])
    x = layers.Concatenate(name=name + '_all')([x_max_2,x])

    x = layers.Conv1D(
        filters=C,
        kernel_size=1,
        strides=1,
        padding="same",
    )(x)

    return x
Exemple #3
0
def create_model(input_shape,
                 classes,
                 pooling=None,
                 include_top=True,
                 **kwargs):
    """Instantiates the ResNet50 architecture.
    Optionally loads weights pre-trained on ImageNet.
    Note that the data format convention used by the model is
    the one specified in your Keras config at `~/.keras/keras.json`.
    # Arguments
        include_top: whether to include the fully-connected
            layer at the top of the network.
        weights: one of `None` (random initialization),
              'imagenet' (pre-training on ImageNet),
              or the path to the weights file to be loaded.
        input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
            to use as image input for the model.
        input_shape: optional shape tuple, only to be specified
            if `include_top` is False (otherwise the input shape
            has to be `(224, 224, 3)` (with `channels_last` data format)
            or `(3, 224, 224)` (with `channels_first` data format).
            It should have exactly 3 inputs channels,
            and width and height should be no smaller than 32.
            E.g. `(200, 200, 3)` would be one valid value.
        pooling: Optional pooling mode for feature extraction
            when `include_top` is `False`.
            - `None` means that the output of the model will be
                the 4D tensor output of the
                last convolutional block.
            - `avg` means that global average pooling
                will be applied to the output of the
                last convolutional block, and thus
                the output of the model will be a 1D tensor.
            - `max` means that global max pooling will
                be applied.
        classes: optional number of classes to classify images
            into, only to be specified if `include_top` is True, and
            if no `weights` argument is specified.
    # Returns
        A Keras model instance.
    # Raises
        ValueError: in case of invalid argument for `weights`,
            or invalid input shape.
    """
    if backend.image_data_format() == 'channels_last':
        bn_axis = 2
    else:
        bn_axis = 1

    img_input = layers.Input(shape=input_shape)

    x = layers.ZeroPadding1D(padding=3, name='conv1_pad')(img_input)
    x = layers.Conv1D(64,
                      7,
                      strides=2,
                      padding='valid',
                      kernel_initializer='he_normal',
                      name='conv1')(x)
    x = layers.BatchNormalization(axis=bn_axis, name='bn_conv1')(x)
    x = layers.Activation('relu')(x)
    x = layers.ZeroPadding1D(padding=1, name='pool1_pad')(x)
    x = layers.MaxPooling1D(3, strides=2)(x)

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

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

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

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

    if include_top:
        x = layers.GlobalAveragePooling1D(name='avg_pool')(x)
        x = layers.Dense(classes, activation='softmax', name='fc1000')(x)
    else:
        if pooling == 'avg':
            x = layers.GlobalAveragePooling1D()(x)
        elif pooling == 'max':
            x = layers.GlobalMaxPooling1D()(x)
        else:
            warnings.warn('The output shape of `ResNet50(include_top=False)` '
                          'has been changed since Keras 2.2.0.')

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

    return model
X_train_values.shape
#X_train_values  = np.expand_dims(X_train_values, axis=2) #autre méthode de reshape
X_test_values = X_test.values
y_test_values = y_test.values
X_test_values = X_test_values.reshape((X_test_values.shape[0],X_test_values.shape[1],1))
#X_test_values  = np.expand_dims(X_test_values, axis=2)  #autre méthode de reshape



unitsNumber = 40  #(~2/3*( 61+1))

#Reseau simple avec 1 couche de convolution, 1 zero padding, 1 couche de convolution, 1 couche de pooling
#une couche standard FC et une couche de sortie sigmoide car on cherche top10 ou non.
model = models.Sequential()
model.add(layers.Conv1D(filters=unitsNumber, kernel_size=3, activation='relu', input_shape=(61,1)))
model.add(layers.ZeroPadding1D(padding=1))
model.add(layers.Conv1D(filters=unitsNumber, kernel_size=3, activation='relu'))
model.add(layers.MaxPooling1D(pool_size=2))
model.add(layers.Flatten())  # now output shape == (None, 1160)
model.add(layers.Dense(unitsNumber, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))  #sortie binaire

#my CNN
model.summary()

#Compile and train the model
model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])
#you could check
#model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['acc'])
#model.compile(optimizer='sgd',loss='binary_crossentropy',metrics=['acc'])
#model.compile(optimizer='Nadam',loss='binary_crossentropy',metrics=['acc'])
Exemple #5
0
def build_full_model(frames, freq_bins, mod_options):
    '''
    Build a keras model based on the Dielemann model for music recommandation.
    This model is trained on time-frequency representation of music. The input 
    data fed to the model must be shaped like (batch_size, time, frequency).
    
    Parameters
    ----------
    frames: int
        Number of time frames in a single input
        
    freq_bin: int
        Number of frequency bands in a single input
        
    mod_option: dictionnary
        Specific options for the model. This dictionnary must contain:
            
        'activation': string
            name of the activation function for keras layers
            
        'batchNormConv': bool
            Choose if the model should apply babtch normalization after each 
            convnet
            
        'FC number': int
            Number of cells for each FC layer
            
        'batchNormDense': bool
            Choose if the model should apply babtch normalization after each 
            FC layer
        
        'Alphabet size': int
            Size of the training alphabet (last layer, size of the output)
        
    Returns
    -------
    model: keras model
        The model built. Expects inputs of shape (batch_size, frames, 
        freq_bins) and outputs tensor of shape (batch_size, alphabet_size).
    '''

    inputs = layers.Input(shape=(frames, freq_bins))

    #%%=========== First layer ===============
    #zero-padding the input
    padding_1 = layers.ZeroPadding1D(padding=2)(inputs)
    #Convnet 256 neurons with 4 sample window. Activation defined in mod_option dictionnary
    conv1 = layers.Conv1D(256,
                          4,
                          padding='same',
                          activation=mod_options['activation'])(padding_1)
    #Normalise batch if defined in mod_options
    if mod_options['batchNormConv']:
        conv1 = layers.BatchNormalization()(conv1)
    #Reduce data by max pooling between 2 values
    pool_1 = layers.MaxPooling1D(pool_size=2)(conv1)

    #%%============ Second layer ==============
    #Same layer as the previous one
    padding_2 = layers.ZeroPadding1D(padding=2)(pool_1)
    conv_2 = layers.Conv1D(256,
                           4,
                           padding='same',
                           activation=mod_options['activation'])(padding_2)
    if mod_options['batchNormConv']:
        conv_2 = layers.BatchNormalization()(conv_2)
    pool_2 = layers.MaxPooling1D(pool_size=2)(conv_2)
    '''
    #%%=========== Third layer ???================
    #zero-padding the input
    model.Add(layers.ZeroPadding1D(padding = 2))
    #Convnet 512 neurons with 4 sample window.
    model.add(layers.Conv1D(512, 4, padding = 'same', activation = mod_options['activation']))
    #Normalize batch if defined
    if mod_options['batchNormConv']:
        model.add(layers.BatchNormalization())
    
    '''

    #%%=========== Fourth layer =================
    #zero-padding the input
    padding_3 = layers.ZeroPadding1D(padding=2)(pool_2)
    #Convnet 512 neurons with 4 sample window.
    conv_3 = layers.Conv1D(512,
                           4,
                           padding='same',
                           activation=mod_options['activation'])(padding_3)
    #Normalize batch if defined
    if mod_options['batchNormConv']:
        conv_3 = layers.BatchNormalization()(conv_3)

    #%%========== Global temporal pooling layer =========
    pool_max = layers.GlobalMaxPooling1D()(conv_3)
    pool_average = layers.GlobalAveragePooling1D()(conv_3)
    pool_LP = layers.Lambda(lambda x: GlobalLPPooling1D(x))(conv_3)

    pool_time = layers.Concatenate()([pool_max, pool_average, pool_LP])

    #%%========== FC Layers =========================
    FC_1 = layers.Dense(mod_options['FC number'],
                        activation=mod_options['activation'])(pool_time)
    if mod_options['batchNormDense']:
        FC_1 = layers.BatchNormalization()(FC_1)

    FC_2 = layers.Dense(mod_options['FC number'],
                        activation=mod_options['activation'])(FC_1)
    if mod_options['batchNormDense']:
        FC_2 = layers.BatchNormalization()(FC_2)

    FC_3 = layers.Dense(mod_options['Alphabet size'],
                        activation='softmax')(FC_2)

    model = Model(inputs=inputs, outputs=FC_3)
    model.compile(optimizer='adam',
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    return model
Exemple #6
0
def build_conv_layers(frames, freq_bins, mod_options):
    '''
    Build the convolutionnal base of the model based on the Dielemann model 
    for music recommandation (useful for transfer learning).
    This model is trained on time-frequency representation of music. The input 
    data fed to the model must be shaped like (batch_size, time, frequency).
    Once FC layers are added via the add_fc_layers function, the model can be 
    trained and saved for re-use in transfer learning applications.
    
    Parameters
    ----------
    frames: int
        Number of time frames in a single input
        
    freq_bin: int
        Number of frequency bands in a single input
        
    mod_option: dictionnary
        Specific options for the model. This dictionnary must contain:
            
        'activation': string
            name of the activation function for keras layers
            
        'batchNormConv': bool
            Choose if the model should apply babtch normalization after each 
            convnet
    Returns
    -------
    model: keras model
        The model is not compiled since it requires FC layers on top. Expects 
        inputs of shape (batch size, frames, freq_bins) and outputs tensor of 
        shape: (batch size, frames_pool, freq_bins) where frames_pool has been 
        through 3 poolings of size 2 (divise by 2) and with 4 zeros added 
        before each pooling (add 4).
    
    '''

    inputs = layers.Input(shape=(frames, freq_bins))

    #%%=========== First layer ===============
    #zero-padding the input
    padding_1 = layers.ZeroPadding1D(padding=2)(inputs)
    #Convnet 256 neurons with 4 sample window. Activation defined in mod_option dictionnary
    conv1 = layers.Conv1D(256,
                          4,
                          padding='same',
                          activation=mod_options['activation'])(padding_1)
    #Normalise batch if defined in mod_options
    if mod_options['batchNormConv']:
        conv1 = layers.BatchNormalization()(conv1)
    #Reduce data by max pooling between 2 values
    pool_1 = layers.MaxPooling1D(pool_size=2)(conv1)

    #%%============ Second layer ==============
    #Same layer as the previous one
    padding_2 = layers.ZeroPadding1D(padding=2)(pool_1)
    conv_2 = layers.Conv1D(256,
                           4,
                           padding='same',
                           activation=mod_options['activation'])(padding_2)
    if mod_options['batchNormConv']:
        conv_2 = layers.BatchNormalization()(conv_2)
    pool_2 = layers.MaxPooling1D(pool_size=2)(conv_2)
    '''
    #%%=========== Third layer ???================
    #zero-padding the input
    model.Add(layers.ZeroPadding1D(padding = 2))
    #Convnet 512 neurons with 4 sample window.
    model.add(layers.Conv1D(512, 4, padding = 'same', activation = mod_options['activation']))
    #Normalize batch if defined
    if mod_options['batchNormConv']:
        model.add(layers.BatchNormalization())
    
    '''

    #%%=========== Fourth layer =================
    #zero-padding the input
    padding_3 = layers.ZeroPadding1D(padding=2)(pool_2)
    #Convnet 512 neurons with 4 sample window.
    conv_3 = layers.Conv1D(512,
                           4,
                           padding='same',
                           activation=mod_options['activation'])(padding_3)
    #Normalize batch if defined
    if mod_options['batchNormConv']:
        conv_3 = layers.BatchNormalization()(conv_3)

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

    return model
Exemple #7
0
def cnn_2x_lstm_siamese(voc_size, max_len, dropout=0.2):
    """Two siamese branches, each embedding a statement.

    Binary classifier on top.

    Args:
      voc_size: size of the vocabulary for the input statements.
      max_len: maximum length for the input statements.
      dropout: Fraction of units to drop.
    Returns:
      A Keras model instance.
    """
    pivot_input = layers.Input(shape=(max_len, ), dtype='int32')
    statement_input = layers.Input(shape=(max_len, ), dtype='int32')

    x = layers.Embedding(output_dim=256,
                         input_dim=voc_size,
                         input_length=max_len)(pivot_input)

    x = layers.Conv1D(32,
                      7,
                      activation='relu',
                      kernel_regularizer=regularizers.l2(0.01))(x)
    x = layers.ZeroPadding1D(padding=1)(x)
    x = layers.Conv1D(32,
                      7,
                      activation='relu',
                      kernel_regularizer=regularizers.l2(0.01))(x)
    x = layers.MaxPooling1D(pool_size=2, strides=2)(x)

    x = layers.Conv1D(64,
                      5,
                      activation='relu',
                      kernel_regularizer=regularizers.l2(0.01))(x)
    x = layers.ZeroPadding1D(padding=1)(x)
    x = layers.Conv1D(64,
                      5,
                      activation='relu',
                      kernel_regularizer=regularizers.l2(0.01))(x)
    x = layers.MaxPooling1D(pool_size=2, strides=2)(x)

    x = layers.Conv1D(128,
                      3,
                      activation='relu',
                      kernel_regularizer=regularizers.l2(0.01))(x)
    x = layers.ZeroPadding1D(padding=1)(x)
    x = layers.Conv1D(128,
                      3,
                      activation='relu',
                      kernel_regularizer=regularizers.l2(0.01))(x)
    x = layers.MaxPooling1D(pool_size=2, strides=2)(x)

    x = layers.Conv1D(256,
                      3,
                      activation='relu',
                      kernel_regularizer=regularizers.l2(0.01))(x)
    x = layers.ZeroPadding1D(padding=1)(x)
    x = layers.Conv1D(256,
                      3,
                      activation='relu',
                      kernel_regularizer=regularizers.l2(0.01))(x)
    x = layers.MaxPooling1D(pool_size=2, strides=2)(x)

    embedded_pivot = layers.LSTM(256)(x)

    encoder_model = Model(pivot_input, embedded_pivot)
    embedded_statement = encoder_model(statement_input)

    concat = layers.merge([embedded_pivot, embedded_statement], mode='concat')
    x = layers.Dense(256, activation='relu')(concat)
    x = layers.Dropout(dropout)(x)
    prediction = layers.Dense(1, activation='relu')(x)

    model = Model([pivot_input, statement_input], prediction)
    return model
Exemple #8
0
def build_model():
    model_weights = np.load('sound8.npy', encoding='latin1').item()
    print(type(model_weights))
    model = models.Sequential()
    model.add(layers.InputLayer(batch_input_shape=(1, None, 1)))

    filter_parameters = [
        {
            'name': 'conv1',
            'num_filters': 16,
            'padding': 32,
            'kernel_size': 64,
            'conv_strides': 2,
            'pool_size': 8,
            'pool_strides': 8
        },
        {
            'name': 'conv2',
            'num_filters': 32,
            'padding': 16,
            'kernel_size': 32,
            'conv_strides': 2,
            'pool_size': 8,
            'pool_strides': 8
        },
        {
            'name': 'conv3',
            'num_filters': 64,
            'padding': 8,
            'kernel_size': 16,
            'conv_strides': 2
        },
        {
            'name': 'conv4',
            'num_filters': 128,
            'padding': 4,
            'kernel_size': 8,
            'conv_strides': 2
        },
        {
            'name': 'conv5',
            'num_filters': 256,
            'padding': 2,
            'kernel_size': 4,
            'conv_strides': 2,
            'pool_size': 4,
            'pool_strides': 4
        },
        {
            'name': 'conv6',
            'num_filters': 512,
            'padding': 2,
            'kernel_size': 4,
            'conv_strides': 2
        },
        {
            'name': 'conv7',
            'num_filters': 1024,
            'padding': 2,
            'kernel_size': 4,
            'conv_strides': 2
        },
        {
            'name': 'conv8_2',
            'num_filters': 401,
            'padding': 1,
            'kernel_size': 8,
            'conv_strides': 2
        },
    ]

    for x in filter_parameters:
        model.add(layers.ZeroPadding1D(padding=x['padding']))
        model.add(
            layers.Conv1D(x['num_filters'],
                          kernel_size=x['kernel_size'],
                          strides=x['conv_strides'],
                          padding='valid'))
        weights = model_weights[x['name']]['weights'].reshape(
            model.layers[-1].get_weights()[0].shape)
        biases = model_weights[x['name']]['biases']

        model.layers[-1].set_weights([weights, biases])

        if 'conv8' not in x['name']:
            gamma = model_weights[x['name']]['gamma']
            beta = model_weights[x['name']]['beta']
            mean = model_weights[x['name']]['mean']
            var = model_weights[x['name']]['var']

            model.add(layers.BatchNormalization())
            model.layers[-1].set_weights([gamma, beta, mean, var])
            model.add(layers.Activation('relu'))

        if 'pool_size' in x:
            model.add(
                layers.MaxPooling1D(pool_size=x['pool_size'],
                                    strides=x['pool_strides'],
                                    padding='valid'))
    return model