Пример #1
0
def analyze_metrics():

    n_av = metaseg.get("NUM_LASSO_AVERAGES")
    n_steps = metaseg.get("NUM_LASSO_LAMBDAS")
    num_cores = min(n_av, metaseg.get("NUM_CORES"))

    metrics = concatenate_metrics(save=False)
    nclasses = np.max(metrics["class"]) + 1

    Xa, classes, ya, y0a, X_names, class_names = metrics_to_dataset(
        metrics, nclasses)

    Xa = np.concatenate((Xa, classes), axis=-1)
    X_names += class_names

    alphas = get_alphas(n_steps, min_pow=-4.2, max_pow=0.8)
    stats = init_stats(n_av, alphas, X_names)
    single_run_stats = init_stats(n_av, alphas, X_names)

    p = Pool(num_cores)
    p_args = [(Xa, ya, y0a, alphas, X_names, single_run_stats, run)
              for run in range(n_av)]
    single_run_stats = p.starmap(fit_model_run, p_args)

    stats = merge_stats(stats, single_run_stats, n_av)

    mean_stats, _ = dump_stats(stats, metrics)

    generate_lasso_plots(stats, mean_stats, X_names, class_names)
Пример #2
0
def plot_regression(X2_val, y2_val, y2_pred, ya_val, ypred, X_names):

    os.environ['PATH'] = os.environ[
        'PATH'] + ':/Library/TeX/texbin'  # for tex in matplotlib
    plt.rc('font', size=10, family='serif')
    plt.rc('axes', titlesize=10)
    plt.rc('figure', titlesize=10)
    plt.rc('text', usetex=True)

    cmap = plt.get_cmap('tab20')

    figsize = (3.0, 13.0 / 5.0)
    plt.figure(figsize=figsize, dpi=300)
    plt.clf()
    S_ind = 0
    for S_ind in range(len(X_names)):
        if X_names[S_ind] == "S":
            break

    sizes = np.squeeze(X2_val[:, S_ind] * np.std(X2_val[:, S_ind]))
    sizes = sizes - np.min(sizes)
    sizes = sizes / np.max(sizes) * 50  #+ 1.5
    x = np.arange(0., 1, .01)
    plt.plot(x, x, color='black', alpha=0.5, linestyle='dashed')
    plt.scatter(y2_val,
                np.clip(y2_pred, 0, 1),
                s=sizes,
                linewidth=.5,
                c=cmap(0),
                edgecolors=cmap(1),
                alpha=0.25)
    plt.xlabel('$\mathit{IoU}_\mathrm{adj}$')
    plt.ylabel('predicted $\mathit{IoU}_\mathrm{adj}$')
    plt.savefig(metaseg.get("RESULTS_DIR") + 'regression1.png',
                bbox_inches='tight')

    figsize = (8.75, 5.25)
    plt.clf()

    density1 = kde.gaussian_kde(ya_val[ypred == 1])
    density2 = kde.gaussian_kde(ya_val[ypred == 0])

    density1.set_bandwidth(bw_method=density1.factor / 2.)
    density2.set_bandwidth(bw_method=density2.factor / 2.)

    x = np.arange(0., 1, .01)

    plt.clf()
    plt.figure(figsize=figsize)
    plt.plot(x, density1(x), color='red', alpha=0.66, label="pred. $IoU = 0$")
    plt.plot(x, density2(x), color='blue', alpha=0.66, label="pred. $IoU > 0$")
    plt.hist(ya_val[ypred == 1], bins=20, color='red', alpha=0.1, normed=True)
    plt.hist(ya_val[ypred == 0], bins=20, color='blue', alpha=0.1, normed=True)
    legend = plt.legend(loc='upper right')
    plt.xlabel('$\mathit{IoU}_\mathrm{adj}$')
    plt.savefig(metaseg.get("RESULTS_DIR") + 'classif_hist.pdf',
                bbox_inches='tight')

    plt.clf()
Пример #3
0
def compute_metrics_per_image():

    num_cores = metaseg.get("NUM_CORES")

    print("calculating statistics")

    p = Pool(num_cores)

    p_args = [(k, ) for k in range(metaseg.get("NUM_IMAGES"))]

    p.starmap(compute_metrics_i, p_args)

    concatenate_metrics(save=True)
Пример #4
0
def main():

    metaseg.set_from_argv(sys.argv)

    metaseg.print_attr()

    if metaseg.get("COMPUTE_METRICS"):
        compute_metrics_per_image()

    if metaseg.get("VISUALIZE_METRICS"):
        visualize_metrics()

    if metaseg.get("ANALYZE_METRICS"):
        analyze_metrics()
Пример #5
0
def concatenate_metrics(save=False):

    metrics = metrics_load(0)

    for i in range(1, metaseg.get("NUM_IMAGES")):
        sys.stdout.write("\t concatenated file number {} / {}\r".format(
            i + 1, metaseg.get("NUM_IMAGES")))

        m = metrics_load(i)

        for j in metrics:
            metrics[j] += m[j]

    print(" ")
    print("connected components:", len(metrics['iou']))
    print("non-empty connected components:",
          np.sum(np.asarray(metrics['S_in']) != 0))

    if (save == True):
        metrics_dump(metrics, "_all")

    return metrics
Пример #6
0
def visualize_metrics():

    num_cores = metaseg.get("NUM_CORES")

    print("visualization running")

    metrics = metrics_load(0)

    start = list([0, len(metrics["S"])])

    for i in range(1, metaseg.get("NUM_IMAGES")):
        m = metrics_load(i)
        start += [start[-1] + len(m["S"])]

        for j in metrics:
            metrics[j] += m[j]

    nclasses = np.max(metrics["class"]) + 1

    Xa, classes, ya, _, X_names, class_names = metrics_to_dataset(
        metrics, nclasses, non_empty=False)
    Xa = np.concatenate((Xa, classes), axis=-1)
    X_names += class_names

    lmr = linear_model.LinearRegression()
    lmr.fit(Xa, ya)

    ya_pred = np.clip(lmr.predict(Xa), 0, 1)

    print("model r2 score:", r2_score(ya, ya_pred))
    print(" ")

    p = Pool(num_cores)

    p_args = [(ya[start[i]:start[i + 1]], ya_pred[start[i]:start[i + 1]], i)
              for i in range(metaseg.get("NUM_IMAGES"))]

    p.starmap(visualize_metrics_i, p_args)
Пример #7
0
def visualize_metrics_i(iou, iou_pred, i):

    if os.path.isfile(get_save_path_probs_i(i)):

        probs, gt, filename = probs_gt_load(i)

        path = get_img_path_fname(filename)

        input_image = np.asarray(Image.open(path))
        components = components_load(i)

        pred = np.asarray(np.argmax(probs, axis=-1), dtype='int')

        gt[gt == 255] = 0
        predc = np.asarray([
            cs_labels.trainId2label[pred[p, q]].color
            for p in range(pred.shape[0]) for q in range(pred.shape[1])
        ])
        gtc = np.asarray([
            cs_labels.trainId2label[gt[p, q]].color for p in range(gt.shape[0])
            for q in range(gt.shape[1])
        ])
        predc = predc.reshape(input_image.shape)
        gtc = gtc.reshape(input_image.shape)

        img_iou = visualize_segments(components, iou)

        I4 = predc / 2.0 + input_image / 2.0
        I3 = gtc / 2.0 + input_image / 2.0

        img_pred = visualize_segments(components, iou_pred)

        img = np.concatenate((img_iou, img_pred), axis=1)
        img2 = np.concatenate((I3, I4), axis=1)
        img = np.concatenate((img, img2), axis=0)
        image = Image.fromarray(img.astype('uint8'), 'RGB')

        seg_dir = metaseg.get("IOU_SEG_VIS_DIR")
        if not os.path.exists(seg_dir):
            os.makedirs(seg_dir)
        image.save(seg_dir + "img" + str(i) + ".png")

        print("stored:", seg_dir + "img" + str(i) + ".png")
Пример #8
0
def metrics_to_dataset(metrics, nclasses, non_empty=True, all_metrics=[]):

    class_names = []
    X_names = sorted([
        m for m in metrics
        if m not in ["class", "iou", "iou0"] and "cprob" not in m
    ])

    if metaseg.get("CLASS_DTYPE") == metaseg.get("CLASS_DTYPES")[1]:
        class_names = [
            "cprob" + str(i) for i in range(nclasses)
            if "cprob" + str(i) in metrics
        ]
    elif metaseg.get("CLASS_DTYPE") == metaseg.get("CLASS_DTYPES")[0]:
        class_names = ["class"]

    Xa = metrics_to_nparray(metrics,
                            X_names,
                            normalize=True,
                            non_empty=non_empty,
                            all_metrics=all_metrics)
    classes = metrics_to_nparray(metrics,
                                 class_names,
                                 normalize=True,
                                 non_empty=non_empty,
                                 all_metrics=all_metrics)
    ya = metrics_to_nparray(metrics, ["iou"],
                            normalize=False,
                            non_empty=non_empty)
    y0a = metrics_to_nparray(metrics, ["iou0"],
                             normalize=False,
                             non_empty=non_empty)

    if metaseg.get("CLASS_DTYPE") == metaseg.get("CLASS_DTYPES")[0]:
        classes, class_names = classes_to_categorical(classes, nclasses)

    return Xa, classes, ya, y0a, X_names, class_names
Пример #9
0
def dump_stats(stats, metrics):

    iou_corrs = compute_correlations(metrics)
    y0a = metrics_to_nparray(metrics, ["iou0"],
                             normalize=False,
                             non_empty=True)

    mean_stats = dict({})
    std_stats = dict({})

    for s in stats:
        if s not in ["alphas", "n_av", "n_metrics", "metric_names"]:
            mean_stats[s] = np.mean(stats[s], axis=0)
            std_stats[s] = np.std(stats[s], axis=0)

    best_pen_ind = np.argmax(mean_stats['penalized_val_acc'])
    best_plain_ind = np.argmax(mean_stats['plain_val_acc'])

    # dump stats latex ready
    with open(metaseg.get("RESULTS_DIR") + 'av_results.txt', 'wt') as f:

        print(iou_corrs, file=f)
        print(" ", file=f)

        print("classification", file=f)
        print(
            "                             & train                &  val                 &    \\\\ ",
            file=f)
        M = sorted([s for s in mean_stats if 'penalized' in s and 'acc' in s])
        print("ACC penalized               ", end=" & ", file=f)
        for s in M:
            print("${:.2f}\%".format(100 * mean_stats[s][best_pen_ind]) +
                  "(\pm{:.2f}\%)$".format(100 * std_stats[s][best_pen_ind]),
                  end=" & ",
                  file=f)
        print("   \\\\ ", file=f)
        M = sorted([s for s in mean_stats if 'plain' in s and 'acc' in s])
        print("ACC unpenalized             ", end=" & ", file=f)
        for s in M:
            print("${:.2f}\%".format(100 * mean_stats[s][best_pen_ind]) +
                  "(\pm{:.2f}\%)$".format(100 * std_stats[s][best_pen_ind]),
                  end=" & ",
                  file=f)
        print("   \\\\ ", file=f)
        M = sorted([s for s in mean_stats if 'entropy' in s and 'acc' in s])
        print("ACC entropy baseline        ", end=" & ", file=f)
        for s in M:
            print("${:.2f}\%".format(100 * mean_stats[s]) +
                  "(\pm{:.2f}\%)$".format(100 * std_stats[s]),
                  end=" & ",
                  file=f)
        print("   \\\\ ", file=f)

        M = sorted(
            [s for s in mean_stats if 'penalized' in s and 'auroc' in s])
        print("AUROC penalized             ", end=" & ", file=f)
        for s in M:
            print("${:.2f}\%".format(100 * mean_stats[s][best_pen_ind]) +
                  "(\pm{:.2f}\%)$".format(100 * std_stats[s][best_pen_ind]),
                  end=" & ",
                  file=f)
        print("   \\\\ ", file=f)
        M = sorted([s for s in mean_stats if 'plain' in s and 'auroc' in s])
        print("AUROC unpenalized           ", end=" & ", file=f)
        for s in M:
            print("${:.2f}\%".format(100 * mean_stats[s][best_pen_ind]) +
                  "(\pm{:.2f}\%)$".format(100 * std_stats[s][best_pen_ind]),
                  end=" & ",
                  file=f)
        print("   \\\\ ", file=f)
        M = sorted([s for s in mean_stats if 'entropy' in s and 'auroc' in s])
        print("AUROC entropy baseline      ", end=" & ", file=f)
        for s in M:
            print("${:.2f}\%".format(100 * mean_stats[s]) +
                  "(\pm{:.2f}\%)$".format(100 * std_stats[s]),
                  end=" & ",
                  file=f)
        print("   \\\\ ", file=f)

        print(" ", file=f)
        print("regression", file=f)

        M = sorted([
            s for s in mean_stats
            if 'regr' in s and 'mse' in s and 'entropy' not in s
        ])
        print("$\sigma$, all metrics       ", end=" & ", file=f)
        for s in M:
            print("${:.3f}".format(mean_stats[s]) +
                  "(\pm{:.3f})$".format(std_stats[s]),
                  end="    & ",
                  file=f)
        print("   \\\\ ", file=f)
        M = sorted([
            s for s in mean_stats
            if 'regr' in s and 'mse' in s and 'entropy' in s
        ])
        print("$\sigma$, entropy baseline  ", end=" & ", file=f)
        for s in M:
            print("${:.3f}".format(mean_stats[s]) +
                  "(\pm{:.3f})$".format(std_stats[s]),
                  end="    & ",
                  file=f)
        print("   \\\\ ", file=f)

        M = sorted([
            s for s in mean_stats
            if 'regr' in s and 'r2' in s and 'entropy' not in s
        ])
        print("$R^2$, all metrics          ", end=" & ", file=f)
        for s in M:
            print("${:.2f}\%".format(100 * mean_stats[s]) +
                  "(\pm{:.2f}\%)$".format(100 * std_stats[s]),
                  end=" & ",
                  file=f)
        print("   \\\\ ", file=f)
        M = sorted([
            s for s in mean_stats
            if 'regr' in s and 'r2' in s and 'entropy' in s
        ])
        print("$R^2$, entropy baseline     ", end=" & ", file=f)
        for s in M:
            print("${:.2f}\%".format(100 * mean_stats[s]) +
                  "(\pm{:.2f}\%)$".format(100 * std_stats[s]),
                  end=" & ",
                  file=f)
        print("   \\\\ ", file=f)

        print(" ", file=f)
        M = sorted([s for s in mean_stats if 'iou' in s])
        for s in M:
            print(s,
                  ": {:.0f}".format(mean_stats[s]) +
                  "($\pm${:.0f})".format(std_stats[s]),
                  file=f)
        print("IoU=0:",
              np.sum(y0a == 1),
              "of",
              y0a.shape[0],
              "non-empty components",
              file=f)
        print("IoU>0:",
              np.sum(y0a == 0),
              "of",
              y0a.shape[0],
              "non-empty components",
              file=f)
        print("total number of components: ", len(metrics['S']), file=f)
        print(" ", file=f)

        dump_path = get_save_path_stats()
        dump_dir = os.path.dirname(dump_path)

        if not os.path.exists(dump_dir):
            os.makedirs(dump_dir)

        pickle.dump(stats, open(dump_path, "wb"))

    return mean_stats, std_stats
Пример #10
0
def fit_model_run(Xa, ya, y0a, alphas, X_names, stats, run):

    print("run", run)

    np.random.seed(run)

    val_mask = np.random.rand(len(ya)) < 3.0 / 6.0

    Xa_val = Xa[val_mask]
    ya_val = ya[val_mask]
    y0a_val = y0a[val_mask]

    Xa_train = Xa[np.logical_not(val_mask)]
    ya_train = ya[np.logical_not(val_mask)]
    y0a_train = y0a[np.logical_not(val_mask)]

    coefs = np.zeros((len(alphas), Xa.shape[1]))

    max_acc = 0
    best_lm = []

    for i in range(len(alphas)):
        lm = linear_model.LogisticRegression(
            C=alphas[i], penalty='l1', solver='saga', max_iter=1000,
            tol=1e-3)  #, class_weight='balanced')
        lm.fit(Xa_train, y0a_train)

        stats['penalized_val_acc'][run, i] = lm.score(Xa_val, y0a_val)
        stats['penalized_train_acc'][run, i] = lm.score(Xa_train, y0a_train)

        if stats['penalized_val_acc'][run, i] > max_acc:
            max_acc = stats['penalized_val_acc'][run, i]
            best_lm = lm

        print("step" + str(i) + ", alpha={:.2E}".format(alphas[i]) +
              ", val. acc.: {:.2f}%".format(
                  100 * stats['penalized_val_acc'][run, i]),
              end=", ")
        print("coefs non-zero:", end=" ")

        metapr = lm.predict_proba(Xa_val)
        fpr, tpr, _ = roc_curve(y0a_val, metapr[:, 1])
        stats['penalized_val_auroc'][run, i] = auc(fpr, tpr)

        metapr_t = lm.predict_proba(Xa_train)
        fpr, tpr, _ = roc_curve(y0a_train, metapr_t[:, 1])
        stats['penalized_train_auroc'][run, i] = auc(fpr, tpr)

        coefs[i] = np.asarray(lm.coef_[0])
        print([j for j in range(len(coefs[i])) if np.abs(coefs[i, j]) > 1e-6])

        if np.sum(np.abs(coefs[i]) > 1e-6) > 0:
            lm2 = linear_model.LogisticRegression(
                penalty=None, solver='saga', max_iter=1000,
                tol=1e-3)  #, class_weight='balanced')
            lm2.fit(Xa_train[:, np.abs(coefs[i]) > 1e-6], y0a_train)

            stats['plain_val_acc'][run, i] = lm2.score(
                Xa_val[:, np.abs(coefs[i]) > 1e-6], y0a_val)
            stats['plain_train_acc'][run, i] = lm2.score(
                Xa_train[:, np.abs(coefs[i]) > 1e-6], y0a_train)

            metapr = lm2.predict_proba(Xa_val[:, np.abs(coefs[i]) > 1e-6])
            fpr, tpr, _ = roc_curve(y0a_val, metapr[:, 1])
            stats['plain_val_auroc'][run, i] = auc(fpr, tpr)

            metapr_t = lm2.predict_proba(Xa_train[:, np.abs(coefs[i]) > 1e-6])
            fpr, tpr, _ = roc_curve(y0a_train, metapr_t[:, 1])
            stats['plain_train_auroc'][run, i] = auc(fpr, tpr)
        else:
            stats['plain_val_acc'][run, i] = stats['penalized_val_acc'][run, i]
            stats['plain_train_acc'][run,
                                     i] = stats['penalized_train_acc'][run, i]

            stats['plain_val_auroc'][run,
                                     i] = stats['penalized_val_auroc'][run, i]
            stats['plain_train_auroc'][run,
                                       i] = stats['penalized_train_auroc'][run,
                                                                           i]

    max_acc = np.argmax(stats['penalized_val_acc'][run])

    ypred = best_lm.predict(Xa_val)
    ypred_t = best_lm.predict(Xa_train)

    E_ind = 0
    for E_ind in range(len(X_names)):
        if X_names[E_ind] == "E":
            break

    lme = linear_model.LogisticRegression(penalty=None, solver='saga')
    lme.fit(Xa_train[:, E_ind].reshape((Xa_train.shape[0], 1)), y0a_train)

    stats['entropy_val_acc'][run] = lme.score(
        Xa_val[:, E_ind].reshape((Xa_val.shape[0], 1)), y0a_val)
    stats['entropy_train_acc'][run] = lme.score(
        Xa_train[:, E_ind].reshape((Xa_train.shape[0], 1)), y0a_train)

    metapr = lme.predict_proba(Xa_val[:, E_ind].reshape((Xa_val.shape[0], 1)))
    fpr, tpr, _ = roc_curve(y0a_val, metapr[:, 1])
    stats['entropy_val_auroc'][run] = auc(fpr, tpr)

    metapr = lme.predict_proba(Xa_train[:, E_ind].reshape(
        (Xa_train.shape[0], 1)))
    fpr, tpr, _ = roc_curve(y0a_train, metapr[:, 1])
    stats['entropy_train_auroc'][run] = auc(fpr, tpr)

    if run == 0:
        metapr = best_lm.predict_proba(Xa_val)
        plot_roc_curve(y0a_val, metapr[:, 1],
                       metaseg.get("RESULTS_DIR") + 'roccurve.pdf')

    stats['iou0_found'][run] = np.sum(np.logical_and(
        ypred == 1, y0a_val == 1)) + np.sum(
            np.logical_and(ypred_t == 1, y0a_train == 1))
    stats['iou0_not_found'][run] = np.sum(
        np.logical_and(ypred == 0, y0a_val == 1)) + np.sum(
            np.logical_and(ypred_t == 0, y0a_train == 1))
    stats['not_iou0_found'][run] = np.sum(
        np.logical_and(ypred == 0, y0a_val == 0)) + np.sum(
            np.logical_and(ypred_t == 0, y0a_train == 0))
    stats['not_iou0_not_found'][run] = np.sum(
        np.logical_and(ypred == 1, y0a_val == 0)) + np.sum(
            np.logical_and(ypred_t == 1, y0a_train == 0))

    X2_train = Xa_val.copy()
    y2_train = ya_val.copy()
    X2_val = Xa_train.copy()
    y2_val = ya_train.copy()

    lmr = linear_model.LinearRegression()
    lmr.fit(X2_train, y2_train)
    y2_pred = lmr.predict(X2_val)
    y2_pred_t = lmr.predict(X2_train)

    stats['regr_val_mse'][run] = np.sqrt(mean_squared_error(y2_val, y2_pred))
    stats['regr_val_r2'][run] = r2_score(y2_val, y2_pred)
    stats['regr_train_mse'][run] = np.sqrt(
        mean_squared_error(y2_train, y2_pred_t))
    stats['regr_train_r2'][run] = r2_score(y2_train, y2_pred_t)

    lmer = linear_model.LinearRegression()
    lmer.fit(X2_train[:, E_ind].reshape((X2_train.shape[0], 1)), y2_train)
    y2e_pred = lmer.predict(X2_val[:, E_ind].reshape((X2_val.shape[0], 1)))
    y2e_pred_t = lmer.predict(X2_train[:, E_ind].reshape(
        (X2_train.shape[0], 1)))

    stats['entropy_regr_val_mse'][run] = np.sqrt(
        mean_squared_error(y2_val, y2e_pred))
    stats['entropy_regr_val_r2'][run] = r2_score(y2_val, y2e_pred)
    stats['entropy_regr_train_mse'][run] = np.sqrt(
        mean_squared_error(y2_train, y2e_pred_t))
    stats['entropy_regr_train_r2'][run] = r2_score(y2_train, y2e_pred_t)

    stats['coefs'][run] = np.asarray(coefs)

    if run == 0:

        plot_regression(X2_val, y2_val, y2_pred, ya_val, ypred, X_names)

    return stats
Пример #11
0
                       metrics_dump, metrics_load, \
                       components_dump, components_load, \
                       get_save_path_probs_i, \
                       get_save_path_metrics_i, get_save_path_components_i, \
                       get_iou_seg_vis_path_i, get_save_path_stats, \
                       get_img_path_fname, metaseg

from metaseg_plot import add_scatterplot_vs_iou, make_scatterplots, \
                         plot_roc_curve, name_to_latex, generate_lasso_plots, \
                         plot_regression

# NOTE:
# "cs_labels" is included for the segmentations color code, this is only required for visualization.
# Replace this if necessary and modify the lines in "visualize_metrics_i()" that contain "cs_labels"
# accordingly.
sys.path.append(metaseg.get("DEEPLAB_PARENT_DIR"))
from deeplab import cs_labels

np.random.seed(0)


def main():

    metaseg.set_from_argv(sys.argv)

    metaseg.print_attr()

    if metaseg.get("COMPUTE_METRICS"):
        compute_metrics_per_image()

    if metaseg.get("VISUALIZE_METRICS"):
Пример #12
0
def make_scatterplots(save_dir,
                      df_full,
                      df_full_nei,
                      filename='iou_vs_ucm_allcls.png'):
    # nei = only cc with non-empty interior

    print("")
    print("making iou scatterplots ...")
    scale = .75
    size_fac = 50 * scale

    os.environ['PATH'] = os.environ[
        'PATH'] + ':/Library/TeX/texbin'  # for tex in matplotlib
    plt.rc('font', size=10, family='serif')
    plt.rc('axes', titlesize=10)
    plt.rc('figure', titlesize=10 * scale)
    plt.rc('text', usetex=True)
    plt.figure(figsize=(9 * scale, 13 * scale), dpi=300)

    plt.subplot(5, 3, 1, aspect='equal')
    add_scatterplot_vs_iou(df_full['iou'], df_full['S'], df_full['E'],
                           "$\\bar E$", size_fac, scale)
    plt.subplot(5, 3, 2, aspect='equal')
    add_scatterplot_vs_iou(df_full['iou'], df_full['S'], df_full['D'],
                           "$\\bar D$", size_fac, scale)
    plt.subplot(5, 3, 3, aspect='equal')
    add_scatterplot_vs_iou(df_full['iou'], 1,
                           df_full['S'] / df_full['S'].max(), "$S/S_{max}$",
                           .5, scale)
    plt.subplot(5, 3, 4, aspect='equal')
    add_scatterplot_vs_iou(df_full_nei['iou'], df_full_nei['S'],
                           df_full_nei['E_in'], "$\\bar E_{in}$", size_fac,
                           scale)
    plt.subplot(5, 3, 5, aspect='equal')
    add_scatterplot_vs_iou(df_full_nei['iou'], df_full_nei['S'],
                           df_full_nei['D_in'], "$\\bar D_{in}$", size_fac,
                           scale)
    plt.subplot(5, 3, 6, aspect='equal')
    add_scatterplot_vs_iou(df_full_nei['iou'], 1,
                           df_full_nei['S_in'] / df_full_nei['S_in'].max(),
                           "$S_{in}/S_{in,max}$", .5, scale)
    plt.subplot(5, 3, 7, aspect='equal')
    add_scatterplot_vs_iou(df_full['iou'], df_full['S'], df_full['E_bd'],
                           "$\\bar E_{bd}$", size_fac, scale)
    plt.subplot(5, 3, 8, aspect='equal')
    add_scatterplot_vs_iou(df_full['iou'], df_full['S'], df_full['D_bd'],
                           "$\\bar D_{bd}$", size_fac, scale)
    plt.subplot(5, 3, 9, aspect='equal')
    add_scatterplot_vs_iou(df_full['iou'], 1,
                           df_full['S_bd'] / df_full['S_bd'].max(),
                           "$S_{bd}/S_{bd,max}$", .5, scale)
    plt.subplot(5, 3, 10, aspect='equal')
    add_scatterplot_vs_iou(df_full['iou'], df_full['S'],
                           df_full['E_rel'] / df_full['E_rel'].max(),
                           "$\\tilde{\\bar E}/\\tilde{\\bar E}_{max}$",
                           size_fac, scale)
    plt.subplot(5, 3, 11, aspect='equal')
    add_scatterplot_vs_iou(df_full['iou'], df_full['S'],
                           df_full['D_rel'] / df_full['D_rel'].max(),
                           "$\\tilde{\\bar D}/\\tilde{\\bar D}_{max}$",
                           size_fac, scale)
    plt.subplot(5, 3, 12, aspect='equal')
    add_scatterplot_vs_iou(df_full['iou'], 1,
                           df_full['S_rel'] / df_full['S_rel'].max(),
                           "$\\tilde{S}/\\tilde{S}_{max}$", .5, scale)
    plt.subplot(5, 3, 13, aspect='equal')
    add_scatterplot_vs_iou(
        df_full_nei['iou'], df_full_nei['S'],
        df_full_nei['E_rel_in'] / df_full_nei['E_rel_in'].max(),
        "$\\tilde{\\bar E}_{in}/\\tilde{\\bar E}_{in,max}$", size_fac, scale)
    plt.subplot(5, 3, 14, aspect='equal')
    add_scatterplot_vs_iou(
        df_full_nei['iou'], df_full_nei['S'],
        df_full_nei['D_rel_in'] / df_full_nei['D_rel_in'].max(),
        "$\\tilde{\\bar D}_{in}/\\tilde{\\bar D}_{in,max}$", size_fac, scale)
    plt.subplot(5, 3, 15, aspect='equal')
    add_scatterplot_vs_iou(
        df_full_nei['iou'], 1,
        df_full_nei['S_rel_in'] / df_full_nei['S_rel_in'].max(),
        "$\\tilde{S}_{in}/\\tilde{S}_{in,max}$", .5, scale)

    plt.tight_layout(pad=1.0 * scale, w_pad=0.5 * scale, h_pad=1.5 * scale)
    save_path = os.path.join(metaseg.get("RESULTS_DIR"), filename)
    plt.savefig(save_path)
    print("scatterplots saved to " + save_path)
Пример #13
0
def generate_lasso_plots(stats, mean_stats, X_names, class_names):

    nc = len(X_names) - len(class_names)
    coefs = np.squeeze(stats['coefs'][0, :, :])
    classcoefs = np.squeeze(stats['coefs'][0, :, nc:])
    coefs = np.concatenate([
        coefs[:, 0:nc],
        np.max(np.abs(coefs[:, nc:]), axis=1).reshape((coefs.shape[0], 1))
    ],
                           axis=1)
    max_acc = np.argmax(stats['penalized_val_acc'][0], axis=-1)
    alphas = stats["alphas"]

    cmap = plt.get_cmap('tab20')
    figsize = (8.75, 5.25)

    os.environ['PATH'] = os.environ[
        'PATH'] + ':/Library/TeX/texbin'  # for tex in matplotlib
    plt.rc('font', size=10, family='serif')
    plt.rc('axes', titlesize=10)
    plt.rc('figure', titlesize=10)
    plt.rc('text', usetex=True)

    plot_names = X_names[0:nc] + ["$C_p$"]
    plt.figure(figsize=figsize)
    plt.clf()
    for i in range(coefs.shape[1]):
        plt.semilogx(alphas,
                     coefs[:, i],
                     label=name_to_latex(plot_names[i]),
                     color=cmap(i / 20))
    ymin, ymax = plt.ylim()
    plt.vlines(alphas[max_acc],
               ymin,
               ymax,
               linestyle='dashed',
               linewidth=0.5,
               color='grey')
    legend = plt.legend(loc='upper right')
    plt.xlabel('$\lambda^{-1}$')
    plt.ylabel('coefficients $c_i$')
    plt.axis('tight')
    plt.savefig(metaseg.get("RESULTS_DIR") + 'lasso1.pdf', bbox_inches='tight')

    plt.clf()
    for i in range(classcoefs.shape[1]):
        plt.semilogx(alphas,
                     classcoefs[:, i],
                     label="$C_{" + str(i) + "}$",
                     color=cmap(i / 20))
    plt.vlines(alphas[max_acc],
               ymin,
               ymax,
               linestyle='dashed',
               linewidth=0.5,
               color='grey')
    legend = plt.legend(loc='upper right')
    plt.xlabel('$\lambda^{-1}$')
    plt.ylabel('coefficients $c_i$')
    plt.axis('tight')
    plt.savefig(metaseg.get("RESULTS_DIR") + 'lasso2.pdf', bbox_inches='tight')

    plt.clf()
    plt.semilogx(alphas,
                 stats['plain_val_acc'][0],
                 label="unpenalized model",
                 color=cmap(2))
    plt.semilogx(alphas,
                 stats['penalized_val_acc'][0],
                 label="penalized model",
                 color=cmap(0))
    plt.semilogx(alphas,
                 mean_stats['entropy_val_acc'] * np.ones((len(alphas), )),
                 label="entropy baseline",
                 color='black',
                 linestyle='dashed')
    ymin, ymax = plt.ylim()
    plt.vlines(alphas[max_acc],
               ymin,
               ymax,
               linestyle='dashed',
               linewidth=0.5,
               color='grey')
    legend = plt.legend(loc='lower right')
    plt.xlabel('$\lambda^{-1}$')
    plt.ylabel('classification accuracy')
    plt.axis('tight')
    plt.savefig(metaseg.get("RESULTS_DIR") + 'classif_perf.pdf',
                bbox_inches='tight')

    plt.clf()
    plt.semilogx(alphas,
                 stats['plain_val_auroc'][0],
                 label="unpenalized model",
                 color=cmap(2))
    plt.semilogx(alphas,
                 stats['penalized_val_auroc'][0],
                 label="penalized model",
                 color=cmap(0))
    plt.semilogx(alphas,
                 mean_stats['entropy_val_auroc'] * np.ones((len(alphas), )),
                 label="entropy baseline",
                 color='black',
                 linestyle='dashed')
    ymin, ymax = plt.ylim()
    plt.vlines(alphas[max_acc],
               ymin,
               ymax,
               linestyle='dashed',
               linewidth=0.5,
               color='grey')
    legend = plt.legend(loc='lower right')
    plt.xlabel('$\lambda^{-1}$')
    plt.ylabel('AUROC')
    plt.axis('tight')
    plt.savefig(metaseg.get("RESULTS_DIR") + 'classif_auroc.pdf',
                bbox_inches='tight')