Beispiel #1
0
def main(filepath,
         curr_epoch,
         params,
         data=None,
         no_threads=10,
         verbose=True,
         cp_interval=100,
         test=False):
    """
    Resumes training for the specified Keras model

    Requires a directory called 'logs' in the same folder for the TensorBoard visualisation

    Arguments:
        filepath - the path to the specified HDF5 file containing the Keras model (must be in the model store)
        curr_epoch - the number of the last completed epoch (1-indexed)
        data - the Datasets object to run on, if None then loads data (default = None)
        params - a dictionary object containing the following parameters
            lr - the learning rate of the Adam optimiser (default = 0.001)
            conv_dr - the dropout rate after the convolutional layers (default = 0.7)
            fc_dr - the dropout rate after the fully-connected layers (default = 0.5)
            no_epochs - the number of epochs to run for
            steps_per_epoch - the number of batches in each epoch
            dp_prob - the proportion of double pulse waveforms shown at train time (default = 0.5)
            batch_norm - if true, use batch norm after each layer
            regularise - sets the amount of L2 regularisation for each layer (default = 0.0)
            decay - sets the decay rate for the proportion of double-pulse waveforms used for 
                    training and validation (default = 0.0)
        no_threads - number of threads to use (default is 10, use -1 to set no limit)
        verbose - dictates the amount of output that keras gives
        cp_interval - the number of epochs between saving model checkpoints (default = 100)
        test - suppresses saving of model and output of logs (for testing new features; default = False)
    
    No returns

    """
    model_name = filepath.split('/')[-1]

    # Set up CPU, GPU options
    config = None
    if no_threads == -1:
        config = tf.ConfigProto(allow_soft_placement=True,
                                device_count={
                                    'CPU': 1,
                                    'GPU': 1
                                },
                                gpu_options=tf.GPUOptions(allow_growth=True))
    else:
        config = tf.ConfigProto(intra_op_parallelism_threads=no_threads,
                                inter_op_parallelism_threads=no_threads,
                                allow_soft_placement=True,
                                device_count={
                                    'CPU': 1,
                                    'GPU': 1
                                },
                                gpu_options=tf.GPUOptions(allow_growth=True))
    sess = tf.Session(config=config)
    K.set_session(sess)

    # Get model
    if params['lr'] != None:
        # Load model
        model = load_uncompiled_model(filepath)

        optimiser = Adam(params['lr'])

        # Create model
        model.compile(
            optimizer=optimiser,
            loss='categorical_crossentropy',
            metrics=['accuracy', precision, recall, f1, class_balance])
    else:
        model = load_model(filepath)

    # Read initial parameters
    # Code adapted from https://docs.python.org/2/library/csv.html
    with open(MODEL_DIR + MODEL_SUMMARY) as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
            if row['model_name'] == model_name:
                old_params = row
                break

    # Compare parameters
    for key in old_params:
        if (key == 'model_name') or (key == 'comments'):
            continue
        try:
            # If key doesn't exist, a KeyError will be thrown
            if params[key] == None:
                params[key] = literal_eval(old_params[key])
        except KeyError:
            # Key doesn't exist - populate with value from old params index
            params[key] = literal_eval(old_params[key])

    # Read in data
    if data == None:
        data = load_data(verbose=verbose)

    # Create generators for training, validation
    train_gen = WaveformGenerator(data.train,
                                  batch_size=params['batch_size'],
                                  balanced=True,
                                  dp_prob=params['dp_prob'],
                                  decay=params['decay'])

    val_gen = WaveformGenerator(data.val,
                                batch_size=params['batch_size'],
                                balanced=True,
                                dp_prob=params['dp_prob'],
                                decay=params['decay'])

    # Prepare callbacks
    callbacks = [train_gen, val_gen]

    if test == False:
        tb = TensorBoard(log_dir='logs', histogram_freq=0, write_graph=True)
        model_saver = ModelSaver(
            model,
            'retrain',
            params,
            comment="Retrained from {}".format(model_name),
            verbose=verbose,
            period=cp_interval)
        callbacks += [tb, model_saver]

    # Train model
    model.fit_generator(train_gen,
                        steps_per_epoch=params['steps_per_epoch'],
                        epochs=params['no_epochs'],
                        verbose=int(verbose),
                        validation_data=val_gen,
                        validation_steps=params['steps_per_epoch'],
                        callbacks=callbacks,
                        initial_epoch=curr_epoch)

    # Evaluate model
    test_preds = model.predict(data.val.waveforms, verbose=int(verbose))
    print()
    print_metric_results(data.val.labels,
                         test_preds,
                         data.val.weights,
                         data.val.ids,
                         th=0.5)
    print_metric_results(data.val.labels,
                         test_preds,
                         data.val.weights,
                         data.val.ids,
                         th=0.9)
Beispiel #2
0
def main(
        data=None,
        params={
            'lr': 0.001,
            'conv_dr': 0.2,
            'fc_dr': 0.2,
            'batch_size': 128,
            'no_epochs': 1000,
            'steps_per_epoch': 100,
            'dp_prob': 0.5,
            'batch_norm': False,
            'regularise': 0.0,
            'decay': 0.0
        },
        no_threads=10,
        implementation=0,
        verbose=True,
        cp_interval=100,
        test=False):
    """
    Runs a LSTM recurrent neural network on the waveform data, saving the model to the filestore
    Requires a directory called 'logs' in the same folder for the TensorBoard visualisation
    
    The current neural network structure is:
        lstm > lstm > lstm > softmax

    Arguments:
        data - the Datasets object to run on, if None then loads data (default = None)
        params - a dictionary object containing the following parameters
            lr - the learning rate of the Adam optimiser (default = 0.001)
            conv_dr - the dropout rate for the recurrent state (default = 0.2)
                      (misleadingly named to maintain compatibility with other tools)
            fc_dr - the dropout rate for the alpha dropout layers (default = 0.2)
            no_epochs - the number of epochs to run for
            steps_per_epoch - the number of batches in each epoch
            dp_prob - the proportion of double pulse waveforms shown at train time (default = 0.5)
            batch_norm - unused
            regularise - sets the amount of L2 regularisation for each layer (default = 0.0)
            decay - sets the decay rate for the proportion of double-pulse waveforms used for 
                    training and validation (default = 0.0)
        no_threads - number of threads to use (default is 10, use -1 to set no limit)
        implementation - sets the implementation used by Keras for the LSTM layers (default = 0)
            0 - RNN uses fewer, larger, matrix products (good for CPU but uses more memory)
            1 - Uses fewer, smaller, matrix products (slow on CPU, may be faster than 0 on GPU, uses less memory)
            2 - Combines different gates in LSTM into one matrix (more efficient on GPU)
        verbose - dictates the amount of output that keras gives
        cp_interval - the number of epochs between saving model checkpoints (default = 100)
        test - suppresses saving of model and output of logs (for testing new features; default = False)
    
    No returns

    """
    # Read in data
    if data == None:
        data = load_data(verbose=verbose)

    # Set up CPU, GPU options
    config = None
    if no_threads == -1:
        config = tf.ConfigProto(allow_soft_placement=True,
                                device_count={
                                    'CPU': 1,
                                    'GPU': 1
                                },
                                gpu_options=tf.GPUOptions(allow_growth=True))
    else:
        config = tf.ConfigProto(intra_op_parallelism_threads=no_threads,
                                inter_op_parallelism_threads=no_threads,
                                allow_soft_placement=True,
                                device_count={
                                    'CPU': 1,
                                    'GPU': 1
                                },
                                gpu_options=tf.GPUOptions(allow_growth=True))
    sess = tf.Session(config=config)
    K.set_session(sess)

    # Define model
    model = Sequential()

    # Set up regulariser
    regulariser = l2(params['regularise'])

    # Reshape input to fit with LSTM
    model.add(Reshape((128, 1), input_shape=(128, )))

    # Define model
    model.add(
        LSTM(128,
             dropout=params['fc_dr'],
             recurrent_dropout=params['conv_dr'],
             kernel_regularizer=regulariser,
             unroll=True,
             return_sequences=True,
             implementation=implementation))
    model.add(
        LSTM(128,
             dropout=params['fc_dr'],
             recurrent_dropout=params['conv_dr'],
             kernel_regularizer=regulariser,
             unroll=True,
             return_sequences=True,
             implementation=implementation))
    model.add(
        LSTM(128,
             dropout=params['fc_dr'],
             recurrent_dropout=params['conv_dr'],
             kernel_regularizer=regulariser,
             unroll=True,
             implementation=implementation))
    model.add(Dense(2, activation='softmax'))

    # Set-up optimiser
    optimiser = Adam(lr=params['lr'])

    # Create model
    model.compile(optimizer=optimiser,
                  loss='categorical_crossentropy',
                  metrics=['accuracy', precision, recall, f1, class_balance])

    # Create generators for training, validation
    train_gen = WaveformGenerator(data.train,
                                  batch_size=params['batch_size'],
                                  balanced=True,
                                  dp_prob=params['dp_prob'],
                                  decay=params['decay'])

    val_gen = WaveformGenerator(data.val,
                                batch_size=params['batch_size'],
                                balanced=True,
                                dp_prob=params['dp_prob'],
                                decay=params['decay'])

    # Prepare callbacks
    callbacks = [train_gen, val_gen]

    if test == False:
        tb = TensorBoard(log_dir='logs', histogram_freq=0, write_graph=True)
        model_saver = ModelSaver(model,
                                 'lstm',
                                 params,
                                 verbose=verbose,
                                 period=cp_interval)
        callbacks += [tb, model_saver]

    # Train model
    model.fit_generator(train_gen,
                        steps_per_epoch=params['steps_per_epoch'],
                        epochs=params['no_epochs'],
                        verbose=int(verbose),
                        validation_data=val_gen,
                        validation_steps=params['steps_per_epoch'],
                        callbacks=callbacks)

    # Evaluate model
    test_preds = model.predict(data.val.waveforms, verbose=int(verbose))
    print()
    print_metric_results(data.val.labels,
                         test_preds,
                         data.val.weights,
                         data.val.ids,
                         th=0.5)
    print_metric_results(data.val.labels,
                         test_preds,
                         data.val.weights,
                         data.val.ids,
                         th=0.9)
Beispiel #3
0
def main(
        data=None,
        params={
            'lr': 0.001,
            'conv_dr': 0.7,
            'fc_dr': 0.5,
            'batch_size': 128,
            'no_epochs': 1000,
            'steps_per_epoch': 100,
            'dp_prob': 0.5,
            'batch_norm': False,
            'regularise': 0.0,
            'decay': 0.0
        },
        no_threads=10,
        verbose=True,
        cp_interval=100,
        test=False):
    """
    Runs a convolutional neural network on the waveform data, saving the model to the filestore
    Requires a directory called 'logs' in the same folder for the TensorBoard visualisation
    
    The current neural network structure is:
        conv > dropout > conv > dropout > conv > dropout > fc > dropout > fc > dropout > softmax

    Arguments:
        data - the Datasets object to run on, if None then loads data (default = None)
        params - a dictionary object containing the following parameters
            lr - the learning rate of the Adam optimiser (default = 0.001)
            conv_dr - the dropout rate after the convolutional layers (default = 0.7)
            fc_dr - the dropout rate after the fully-connected layers (default = 0.5)
            no_epochs - the number of epochs to run for
            steps_per_epoch - the number of batches in each epoch
            dp_prob - the proportion of double pulse waveforms shown at train time (default = 0.5)
            batch_norm - if true, use batch norm after each layer
            regularise - sets the amount of L2 regularisation for each layer (default = 0.0)
            decay - sets the decay rate for the proportion of double-pulse waveforms used for 
                    training and validation (default = 0.0)
        no_threads - number of threads to use (default is 10, use -1 to set no limit)
        verbose - dictates the amount of output that keras gives
        cp_interval - the number of epochs between saving model checkpoints (default = 100)
        test - suppresses saving of model and output of logs (for testing new features; default = False)
    
    No returns

    """
    # Read in data
    if data == None:
        data = load_data(verbose=verbose)

    # Set up CPU, GPU options
    config = None
    if no_threads == -1:
        config = tf.ConfigProto(allow_soft_placement=True,
                                device_count={
                                    'CPU': 1,
                                    'GPU': 1
                                },
                                gpu_options=tf.GPUOptions(allow_growth=True))
    else:
        config = tf.ConfigProto(intra_op_parallelism_threads=no_threads,
                                inter_op_parallelism_threads=no_threads,
                                allow_soft_placement=True,
                                device_count={
                                    'CPU': 1,
                                    'GPU': 1
                                },
                                gpu_options=tf.GPUOptions(allow_growth=True))
    sess = tf.Session(config=config)
    K.set_session(sess)

    # Define model
    model = Sequential()

    # Set up regulariser
    regulariser = l2(params['regularise'])

    # Reshape input to fit with Conv1D
    model.add(Reshape((128, 1), input_shape=(128, )))

    # Start with convolutional layers
    model.add(
        Conv1D(filters=64,
               kernel_size=5,
               strides=1,
               padding='same',
               kernel_regularizer=regulariser))
    if params['batch_norm']:
        model.add(BatchNormalization(axis=2))
    model.add(Activation('relu'))
    model.add(Dropout(params['conv_dr']))
    model.add(
        Conv1D(filters=128,
               kernel_size=3,
               strides=1,
               padding='same',
               kernel_regularizer=regulariser))
    if params['batch_norm']:
        model.add(BatchNormalization(axis=2))
    model.add(Activation('relu'))
    model.add(Dropout(params['conv_dr']))
    model.add(
        Conv1D(filters=64,
               kernel_size=3,
               strides=1,
               padding='same',
               kernel_regularizer=regulariser))
    if params['batch_norm']:
        model.add(BatchNormalization(axis=2))
    model.add(Activation('relu'))
    model.add(Dropout(params['conv_dr']))

    # Flatten before fully connected layer
    model.add(Flatten())

    # Fully connected layers
    model.add(Dense(1024, kernel_regularizer=regulariser))
    if params['batch_norm']:
        model.add(BatchNormalization(axis=1))
    model.add(Activation('relu'))
    model.add(Dropout(params['fc_dr']))
    model.add(Dense(1024, kernel_regularizer=regulariser))
    if params['batch_norm']:
        model.add(BatchNormalization(axis=1))
    model.add(Activation('relu'))
    model.add(Dropout(params['fc_dr']))
    model.add(Dense(2, activation='softmax'))

    # Set-up optimiser
    optimiser = Adam(lr=params['lr'])

    # Create model
    model.compile(optimizer=optimiser,
                  loss='categorical_crossentropy',
                  metrics=['accuracy', precision, recall, f1, class_balance])

    # Create generators for training, validation
    train_gen = WaveformGenerator(data.train,
                                  batch_size=params['batch_size'],
                                  balanced=True,
                                  dp_prob=params['dp_prob'],
                                  decay=params['decay'])

    val_gen = WaveformGenerator(data.val,
                                batch_size=params['batch_size'],
                                balanced=True,
                                dp_prob=params['dp_prob'],
                                decay=params['decay'])

    # Prepare callbacks
    callbacks = [train_gen, val_gen]

    if test == False:
        tb = TensorBoard(log_dir='logs', histogram_freq=0, write_graph=True)
        model_saver = ModelSaver(model,
                                 'cnn',
                                 params,
                                 verbose=verbose,
                                 period=cp_interval)
        callbacks += [tb, model_saver]

    # Train model
    model.fit_generator(train_gen,
                        steps_per_epoch=params['steps_per_epoch'],
                        epochs=params['no_epochs'],
                        verbose=int(verbose),
                        validation_data=val_gen,
                        validation_steps=params['steps_per_epoch'],
                        callbacks=callbacks)

    # Evaluate model
    test_preds = model.predict(data.val.waveforms, verbose=int(verbose))
    print()
    print_metric_results(data.val.labels,
                         test_preds,
                         data.val.weights,
                         data.val.ids,
                         th=0.5)
    print_metric_results(data.val.labels,
                         test_preds,
                         data.val.weights,
                         data.val.ids,
                         th=0.9)
def main(model_list, data=None, no_threads=10, verbose=True, val=True):
    # Set up CPU, GPU options
    config = None
    if no_threads == -1:
        config = tf.ConfigProto(allow_soft_placement=True,
                                device_count={
                                    'CPU': 1,
                                    'GPU': 1
                                },
                                gpu_options=tf.GPUOptions(allow_growth=True))
    else:
        config = tf.ConfigProto(intra_op_parallelism_threads=no_threads,
                                inter_op_parallelism_threads=no_threads,
                                allow_soft_placement=True,
                                device_count={
                                    'CPU': 1,
                                    'GPU': 1
                                },
                                gpu_options=tf.GPUOptions(allow_growth=True))
    sess = tf.Session(config=config)
    K.set_session(sess)

    # Read in data
    if data == None:
        data = load_eval_data(verbose=verbose)

    for model_path in model_list:
        # Load model
        model = load_model(model_path)

        # Get the name of the model
        model_name = model_path.split('/')[-1]
        model_name = model_name[:-3]  # Strip off extension

        secs_per_year = 86400 * 365.25

        # Evaluate model
        if val:
            dataset = data.val
            data_ratio = 0.07
        else:
            dataset = data.test
            data_ratio = 0.13

        # Rescale weights
        weights = dataset.weights * secs_per_year / data_ratio

        test_preds = model.predict(dataset.waveforms, verbose=int(verbose))

        training_mask = np.logical_or(dataset.labels[:, 0] == 1,
                                      dataset.labels[:, 1] == 1)

        print()
        print_metric_results(dataset.labels[training_mask, 0:2],
                             test_preds[training_mask, 0:2],
                             weights[training_mask],
                             dataset.ids[training_mask],
                             th=0.5)
        print_metric_results(dataset.labels[training_mask, 0:2],
                             test_preds[training_mask, 0:2],
                             weights[training_mask],
                             dataset.ids[training_mask],
                             th=0.9)

        purity_efficiency_plot(dataset.labels[training_mask, 0:2],
                               test_preds[training_mask, 0:2],
                               savepath="plots/" + model_name + "_pe_plot.pdf")
        rate_plot(dataset.labels[training_mask, 0:2],
                  test_preds[training_mask, 0:2],
                  weights[training_mask],
                  savepath="plots/" + model_name + "_train_rate_plot.pdf")
        rate_plot(dataset.labels,
                  test_preds,
                  weights,
                  combine_nu_tau_cc=True,
                  savepath="plots/" + model_name + "_rate_plot.pdf")