Esempio n. 1
0
def get_train_valid(state):
    datapath = os.path.join(state['data_dir'], state['resolution'])
    train_path = datapath + '_train'
    valid_path = datapath + '_valid'
    x_train, y_train, label_to_idx, _ = load_featurized(train_path)
    x_valid, y_valid, label_to_idx, _ = load_featurized(valid_path)
    return x_train, y_train, x_valid, y_valid, label_to_idx
Esempio n. 2
0
def med_vs_lowres(params, n_art):

    log = get_medres_vs_lowres_outfile(params['logpath'])

    low_xs, low_ys, y_to_idx, ids = load_featurized(params['lowres_path'])
    med_xs, med_ys, y_to_idx, ids = load_featurized(params['medres_path'])
    n_classes = len(y_to_idx)

    assert np.array_equal(low_ys, med_ys)
    ys = low_ys

    low_model = load_model(params['lowres_id'], params['model_dir'])
    med_model = load_model(params['medres_id'], params['model_dir'])

    low_preds = get_pred(low_model, low_xs, n_classes)
    med_preds = get_pred(med_model, med_xs, n_classes)

    low_acc = accuracy_score(ys, low_preds)
    med_acc = accuracy_score(ys, med_preds)

    p_diff = art_test(ys,
                      low_preds,
                      med_preds,
                      absolute=True,
                      n=n_art,
                      scoring=precision_recall_fscore_micro,
                      return_distribution=False)[2]

    r = [
        n_art, med_acc, low_acc, med_acc - low_acc, p_diff,
        p_diff * params['bonferroni']
    ]

    csv.writer(log).writerow(r)
    log.close()
Esempio n. 3
0
def per_vs_cnn(state, n_art):

    log = get_per_vs_cnn_outfile(state['logpath'])

    low_xs, low_ys, y_to_idx, ids = load_featurized(state['lowres_path'])
    med_xs, med_ys, y_to_idx, ids = load_featurized(state['medres_path'])
    n_classes = len(y_to_idx)

    assert np.array_equal(low_ys, med_ys)
    ys = med_ys

    per_medres = load_model(state['medres_id'], state['per_dir'])
    per_lowres = load_model(state['lowres_id'], state['per_dir'])
    cnn_medres = load_model(state['medres_id'], state['cnn_dir'])
    cnn_lowres = load_model(state['lowres_id'], state['cnn_dir'])

    preds_per_medres = get_pred(per_medres, med_xs, n_classes)
    preds_per_lowres = get_pred(per_lowres, low_xs, n_classes)
    preds_cnn_medres = get_pred(cnn_medres, med_xs, n_classes)
    preds_cnn_lowres = get_pred(cnn_lowres, low_xs, n_classes)

    acc_per_medres = accuracy_score(ys, preds_per_medres)
    acc_per_lowres = accuracy_score(ys, preds_per_lowres)
    acc_cnn_medres = accuracy_score(ys, preds_cnn_medres)
    acc_cnn_lowres = accuracy_score(ys, preds_cnn_lowres, n_classes)

    p_diff_ch = art_test(ys,
                         preds_per_lowres,
                         preds_cnn_lowres,
                         absolute=True,
                         n=n_art,
                         scoring=precision_recall_fscore_micro,
                         return_distribution=False)[2]

    p_diff_plain = art_test(ys,
                            preds_per_medres,
                            preds_cnn_medres,
                            absolute=True,
                            n=n_art,
                            scoring=precision_recall_fscore_micro,
                            return_distribution=False)[2]

    r = [
        n_art, 'low_res', acc_cnn_lowres, acc_per_lowres,
        acc_cnn_lowres - acc_per_lowres, p_diff_ch,
        p_diff_ch * state['bonferroni']
    ]
    csv.writer(log).writerow(r)

    r = [
        n_art, 'med_res', acc_cnn_medres, acc_per_medres,
        acc_cnn_medres - acc_per_medres, p_diff_ch,
        p_diff_plain * state['bonferroni']
    ]

    csv.writer(log).writerow(r)
    log.close()
def plot_spectrograms(highres_path, medres_path, lowres_path, plot_dir):

    high_xs, ys, label_to_idx, x_ids = load_featurized(highres_path)
    med_xs, ys, label_to_idx, x_ids = load_featurized(medres_path)
    low_xs, ys, label_to_idx, x_ids = load_featurized(lowres_path)

    for res, xs in [('highres', high_xs), ('medres', med_xs),
                    ('lowres', low_xs)]:
        for i, x, in enumerate(xs[:100]):
            plot_path = os.path.join(plot_dir, '%s_%s' % (i, res))
            plot_spectrogram(x, plot_path)