Exemple #1
0
    def __init__(self, weights_path, opts):
        # Create the layers of the neural network, with the same options we used in training
        self.opts = opts.__dict__

        net_options = {xx: opts[xx] for xx in train_helpers.net_params}
        net = train_helpers.create_net(SPEC_HEIGHT=N_MELS, **net_options)

        # Create theano functions
        test_output = lasagne.layers.get_output(net['prob'],
                                                deterministic=True)
        x_in = net['input'].input_var
        self.pred_fn = theano.function([x_in], test_output)

        # Load params
        weights = pickle.load(open(weights_path))
        lasagne.layers.set_all_param_values(net['prob'], weights)

        # Create an object which will iterate over the test spectrograms appropriately
        self.test_sampler = train_helpers.SpecSampler(256,
                                                      opts.HWW_X,
                                                      opts.HWW_Y,
                                                      False,
                                                      opts.LEARN_LOG,
                                                      randomise=False,
                                                      seed=10,
                                                      balanced=False)
Exemple #2
0
    def __init__(self, opts, weights_path):
        """Create the layers of the neural network, with the same options we used in training"""
        self.opts = namedtuple("opts", opts.keys())(*opts.values())
        tf.reset_default_graph()

        net_options = {xx: opts[xx] for xx in train_helpers.net_params}
        self.net = train_helpers.create_net(SPEC_HEIGHT=N_MELS, **net_options)

        self.sess = tf.Session()
        self.sess.run(tf.global_variables_initializer())

        train_saver = tf.train.Saver()
        print("Loading from {}".format(weights_path))
        train_saver.restore(self.sess, weights_path)

        # Create an object which will iterate over the test spectrograms appropriately
        self.test_sampler = train_helpers.SpecSampler(
            256, self.opts.HWW_X, self.opts.HWW_Y, False, self.opts.LEARN_LOG, randomise=False, seed=10, balanced=False)
Exemple #3
0
def train_and_test(train_X,
                   test_X,
                   train_y,
                   test_y,
                   test_files,
                   logging_dir,
                   opts,
                   TEST_FOLD=99,
                   val_X=None,
                   val_y=None):
    '''
    Doesn't do any data loading - assumes the train and test data are passed
    in as parameters!
    '''
    if val_X is None:
        val_X = test_X
        val_y = test_y

    # # creaging samplers and batch iterators
    train_sampler = SpecSampler(64,
                                opts.HWW_X,
                                opts.HWW_Y,
                                opts.DO_AUGMENTATION,
                                opts.LEARN_LOG,
                                randomise=True,
                                balanced=True)
    test_sampler = SpecSampler(64,
                               opts.HWW_X,
                               opts.HWW_Y,
                               False,
                               opts.LEARN_LOG,
                               randomise=False,
                               seed=10,
                               balanced=True)

    height = train_X[0].shape[0]
    net = train_helpers.create_net(height, opts.HWW_X, opts.LEARN_LOG,
                                   opts.NUM_FILTERS, opts.WIGGLE_ROOM,
                                   opts.CONV_FILTER_WIDTH,
                                   opts.NUM_DENSE_UNITS, opts.DO_BATCH_NORM)

    y_in = T.ivector()
    x_in = net['input'].input_var

    # print lasagne.layers.get_output_shape_for(net['prob'])
    trn_output = lasagne.layers.get_output(net['prob'], deterministic=False)
    test_output = lasagne.layers.get_output(net['prob'], deterministic=True)
    params = lasagne.layers.get_all_params(net['prob'], trainable=True)

    _trn_loss = lasagne.objectives.categorical_crossentropy(trn_output,
                                                            y_in).mean()
    _test_loss = lasagne.objectives.categorical_crossentropy(
        test_output, y_in).mean()
    _trn_acc = T.mean(T.eq(T.argmax(trn_output, axis=1), y_in))
    _test_acc = T.mean(T.eq(T.argmax(test_output, axis=1), y_in))

    updates = lasagne.updates.adam(_trn_loss,
                                   params,
                                   learning_rate=opts.LEARNING_RATE)

    print "Compiling...",
    train_fn = theano.function([x_in, y_in], [_trn_loss, _trn_acc],
                               updates=updates)
    val_fn = theano.function([x_in, y_in], [_test_loss, _test_acc])
    pred_fn = theano.function([x_in], test_output)
    print "DONE"

    for epoch in range(opts.MAX_EPOCHS):

        ######################
        # TRAINING
        trn_losses = []
        trn_accs = []

        for xx, yy in tqdm(train_sampler(train_X, train_y)):
            trn_ls, trn_acc = train_fn(xx, yy)
            trn_losses.append(trn_ls)
            trn_accs.append(trn_acc)

        ######################
        # VALIDATION
        val_losses = []
        val_accs = []

        for xx, yy in test_sampler(test_X, test_y):
            val_ls, val_acc = val_fn(xx, yy)
            val_losses.append(val_ls)
            val_accs.append(val_acc)

        print " %03d :: %02f  -  %02f  -  %02f  -  %02f" % (
            epoch, np.mean(trn_losses), np.mean(trn_accs), np.mean(val_losses),
            np.mean(val_accs))

    #######################
    # TESTING
    if small:
        results_savedir = force_make_dir(logging_dir + 'results/')
        predictions_savedir = force_make_dir(logging_dir +
                                             'per_file_predictions/')
    else:
        results_savedir = force_make_dir(logging_dir + 'results_SMALL_TEST/')
        predictions_savedir = force_make_dir(
            logging_dir + 'per_file_predictions_SMALL_TEST/')

    test_sampler = SpecSampler(64,
                               opts.HWW_X,
                               opts.HWW_Y,
                               False,
                               opts.LEARN_LOG,
                               randomise=False,
                               seed=10,
                               balanced=False)

    for fname, spec, y in zip(test_files, test_X, test_y):
        probas = []
        y_true = []
        for Xb, yb in test_sampler([spec], [y]):
            probas.append(pred_fn(Xb))
            y_true.append(yb)

        y_pred_prob = np.vstack(probas)
        y_true = np.hstack(y_true)
        y_pred = np.argmax(y_pred_prob, axis=1)

        with open(predictions_savedir + fname, 'w') as f:
            pickle.dump([y_true, y_pred_prob], f, -1)

    # save weights from network
    param_vals = lasagne.layers.get_all_param_values(net['prob'])
    with open(results_savedir + "weights_%d.pkl" % TEST_FOLD, 'w') as f:
        pickle.dump(param_vals, f, -1)
Exemple #4
0
def predict(A, B, ENSEMBLE_MEMBERS, SPEC_TYPE, CLASSNAME, HWW_X, HWW_Y,
            DO_AUGMENTATION, LEARN_LOG, NUM_FILTERS, WIGGLE_ROOM,
            CONV_FILTER_WIDTH, NUM_DENSE_UNITS, DO_BATCH_NORM, MAX_EPOCHS,
            LEARNING_RATE):

    # Loading data
    golden_1, golden_2 = data_io.load_splits(test_fold=0)
    test_files = golden_1 + golden_2
    test_X, test_y = data_io.load_data(test_files, SPEC_TYPE, LEARN_LOG,
                                       CLASSNAME, A, B)

    # # creaging samplers and batch iterators
    test_sampler = SpecSampler(64,
                               HWW_X,
                               HWW_Y,
                               False,
                               LEARN_LOG,
                               randomise=False,
                               seed=10,
                               balanced=True)

    height = test_X[0].shape[0]
    net = train_helpers.create_net(height, HWW_X, LEARN_LOG, NUM_FILTERS,
                                   WIGGLE_ROOM, CONV_FILTER_WIDTH,
                                   NUM_DENSE_UNITS, DO_BATCH_NORM)

    x_in = net['input'].input_var

    test_output = lasagne.layers.get_output(net['prob'], deterministic=True)
    pred_fn = theano.function([x_in], test_output)

    test_sampler = SpecSampler(64,
                               HWW_X,
                               HWW_Y,
                               False,
                               LEARN_LOG,
                               randomise=False,
                               seed=10,
                               balanced=False)

    y_preds_proba = collections.defaultdict(list)
    y_gts = {}

    def bal_acc(y_true, y_pred_class):
        total = {}
        total['tm'] = y_true.shape[0]
        total['tp'] = np.logical_and(y_true == y_pred_class, y_true == 1).sum()
        total['tn'] = np.logical_and(y_true == y_pred_class, y_true == 0).sum()
        total['fp'] = np.logical_and(y_true != y_pred_class, y_true == 0).sum()
        total['fn'] = np.logical_and(y_true != y_pred_class, y_true == 1).sum()

        A = float(total['tp']) / sum(total[key] for key in ['tp', 'fn'])
        B = float(total['tn']) / sum(total[key] for key in ['fp', 'tn'])
        return (A + B) / 2.0

    # Load network weights
    for idx in range(ENSEMBLE_MEMBERS):

        print "Predicting with ensemble member: %d" % idx

        weights = pickle.load(open(load_path % (train_name, idx, classname)))
        lasagne.layers.set_all_param_values(net['prob'], weights)

        #######################
        # TESTING
        for fname, spec, y in zip(test_files, test_X, test_y):

            probas = []
            y_true = []

            for Xb, yb in test_sampler([spec], [y]):
                probas.append(pred_fn(Xb))
                y_true.append(yb)

            y_preds_proba[fname].append(np.vstack(probas))
            y_gts[fname] = np.hstack(y_true)

        all_pred_prob = np.vstack(y_preds_proba[fname][-1]
                                  for fname in test_files)
        all_pred = np.argmax(all_pred_prob, axis=1)
        all_gt = np.hstack(y_gts[fname] for fname in test_files)
        print bal_acc(all_gt, all_pred)

    # aggregating ensemble members
    all_probs = []
    all_gt = []

    for fname in test_files:

        combined_preds_prob = np.stack(y_preds_proba[fname]).mean(0)
        combined_preds = np.argmax(combined_preds_prob, axis=1)
        y_gt = y_gts[fname]

        with open(predictions_savedir + fname, 'w') as f:
            pickle.dump([y_gt, combined_preds_prob], f, -1)

        all_probs.append(combined_preds)
        all_gt.append(y_gt)

    y_pred_class = np.hstack(all_probs)
    y_true = np.hstack(all_gt)
    print "Combined:", bal_acc(y_true, y_pred_class)
Exemple #5
0
def train_and_test(train_X, test_X, train_y, test_y, test_files, logging_dir, opts, TEST_FOLD=99,
        val_X=None, val_y=None):
    '''
    Doesn't do any data loading - assumes the train and test data are passed
    in as parameters!
    '''
    if val_X is None:
        val_X = test_X
        val_y = test_y

    # # creaging samplers and batch iterators
    train_sampler = SpecSampler(64, opts.HWW_X, opts.HWW_Y, opts.DO_AUGMENTATION, opts.LEARN_LOG, randomise=True, balanced=True)
    test_sampler = SpecSampler(64, opts.HWW_X, opts.HWW_Y, False, opts.LEARN_LOG, randomise=False, seed=10, balanced=True)

    height = train_X[0].shape[0]
    net = create_net(height, opts.HWW_X, opts.LEARN_LOG, opts.NUM_FILTERS,
        opts.WIGGLE_ROOM, opts.CONV_FILTER_WIDTH, opts.NUM_DENSE_UNITS, opts.DO_BATCH_NORM)

    y_in = tf.placeholder(tf.int32, (None))
    x_in = net['input']

    print("todo - fix this up...")
    trn_output = net['fc8']
    test_output = net['fc8']

    _trn_loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(
        logits=trn_output, labels=y_in))
    _test_loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(
        logits=test_output, labels=y_in))
    print(y_in, trn_output, tf.argmax(trn_output, axis=1))

    pred = tf.cast(tf.argmax(trn_output, axis=1), tf.int32)
    _trn_acc = tf.reduce_mean(tf.cast(tf.equal(y_in, pred), tf.float32))

    pred = tf.cast(tf.argmax(test_output, axis=1), tf.int32)
    _test_acc = tf.reduce_mean(tf.cast(tf.equal(y_in, pred), tf.float32))

    optimizer = tf.train.AdamOptimizer(learning_rate=opts.LEARNING_RATE, beta1=0.5, beta2=0.9)

    train_op = slim.learning.create_train_op(_trn_loss, optimizer)

    saver = tf.train.Saver(max_to_keep=5)

    with tf.Session() as sess:

        sess.run(tf.global_variables_initializer())

        for epoch in range(opts.MAX_EPOCHS):

            ######################
            # TRAINING
            trn_losses = []
            trn_accs = []

            for xx, yy in tqdm(train_sampler(train_X, train_y)):
                trn_ls, trn_acc, _ = sess.run(
                    [_trn_loss, _trn_acc, train_op], feed_dict={x_in: xx, y_in: yy})
                trn_losses.append(trn_ls)
                trn_accs.append(trn_acc)

            ######################
            # VALIDATION
            val_losses = []
            val_accs = []

            for xx, yy in test_sampler(test_X, test_y):
                val_ls, val_acc = sess.run([_test_loss, _test_acc], feed_dict={x_in: xx, y_in: yy})
                val_losses.append(val_ls)
                val_accs.append(val_acc)

            print(" %03d :: %02f  -  %02f  -  %02f  -  %02f" % (epoch, np.mean(trn_losses),
                np.mean(trn_accs), np.mean(val_losses), np.mean(val_accs)))

        #######################
        # TESTING
        if small:
            results_savedir = force_make_dir(logging_dir + 'results_SMALL_TEST/')
            predictions_savedir = force_make_dir(logging_dir + 'per_file_predictions_SMALL_TEST/')
        else:
            results_savedir = force_make_dir(logging_dir + 'results/')
            predictions_savedir = force_make_dir(logging_dir + 'per_file_predictions/')

        test_sampler = SpecSampler(64, opts.HWW_X, opts.HWW_Y, False, opts.LEARN_LOG, randomise=False, seed=10, balanced=False)

        for fname, spec, y in zip(test_files, test_X, test_y):
            probas = []
            y_true = []
            for Xb, yb in test_sampler([spec], [y]):
                preds = sess.run(test_output, feed_dict={x_in: Xb})
                probas.append(preds)
                y_true.append(yb)

            y_pred_prob = np.vstack(probas)
            y_true = np.hstack(y_true)
            y_pred = np.argmax(y_pred_prob, axis=1)

            print("Saving to {}".format(predictions_savedir))
            with open(predictions_savedir + fname, 'wb') as f:
                pickle.dump([y_true, y_pred_prob], f, -1)

        # save weights from network
        saver.save(sess, results_savedir + "weights_%d.pkl" % TEST_FOLD, global_step=1)