gnss_cross_validation / plot_results.py
FelixOtt's picture
Upload folder using huggingface_hub
8711d24 verified
Raw History Blame Contribute Delete
6.73 kB
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from pylab import setp
import argparse
import os
from glob import glob
import pickle as pkl
matplotlib.rcParams['pdf.fonttype'] = 42
matplotlib.rcParams['ps.fonttype'] = 42
parser = argparse.ArgumentParser()
parser.add_argument('--folder', help='Folder of the runs. Either resnet or resnet_augment',
required=False, default="resnet")
parser.add_argument('--repeats', help='The number of times each model was trained', required=False, default="10")
args = parser.parse_args()
cur_folder = str(args.folder)
repeats = int(args.repeats)
colors = {0: "#1f77b4", 1: "#ff7f0e", 2: "#2ca02c", 3: "#d62728", 4: "#9467bd", 5: "#8c564b", 6: "#e377c2",
7: "#7f7f7f", 8: "#bcbd22", 9: "#17becf", 10: "#000000", 11: "#000080"}
# Plot Accuracy and F2-Score overview
dp = "./results_" + cur_folder + "/results/summary.txt"
dp2 = "./results_" + cur_folder + "/results/summary_rev.txt"
sp = "./results_" + cur_folder + "/images/"
if not os.path.exists("./results_" + cur_folder + "/images/"):
os.makedirs("./results_" + cur_folder + "/images/")
with open(dp, "r") as f:
data = f.readlines()
data = np.array([[float(x) for x in y.split("\n")[0].split("\t")] for y in data])
data[np.isnan(data)] = 0.0
data = data.reshape((6, repeats, 6, 7))
data_mean = np.mean(data, axis=1)
with open(dp2, "r") as f:
data2 = f.readlines()
data2 = np.array([[float(x) for x in y.split("\n")[0].split("\t")] for y in data2])
data2[np.isnan(data2)] = 0.0
data2 = data2.reshape((6, repeats, 6, 5))
data_mean2 = np.mean(data2, axis=1)
poses = np.arange(3, 36, 6)
w = 0.5
poses = np.array([[x - 3*w, x - 1.8*w, x - 0.6*w, x + 0.6*w, x + 1.8*w, x + 3*w] for x in poses]).flatten()
data_sum = np.zeros((6, repeats, 6, 2))
data_sum[:, :, :, 0] = data[:, :, :, 0]
data_sum[:, :, :, 1] = (data[:, :, :, 3] + data2[:, :, :, 1]) / 2.
data_sum = data_sum.transpose((1, 0, 2, 3)).reshape((repeats, 36, 2)) * 100
plt.figure(figsize=(10, 4))
plt.rcParams["font.family"] = "serif"
bp = plt.boxplot(data_sum[:, :, 0], positions=poses, widths=w)
for i in range(6):
if i < 3:
cur_col = colors[i]
else:
cur_col = colors[i - 1]
for j in range(6):
setp(bp['boxes'][j*6 + i], color=cur_col)
setp(bp['caps'][(j*6 + i) * 2], color=cur_col)
setp(bp['caps'][(j*6 + i) * 2 + 1], color=cur_col)
setp(bp['whiskers'][(j*6 + i) * 2], color=cur_col)
setp(bp['whiskers'][(j*6 + i) * 2 + 1], color=cur_col)
setp(bp['fliers'][j*6 + i], markeredgecolor=cur_col)
plt.ylabel("Accuracy [%]", fontsize=12)
plt.xticks(np.arange(3, 36, 6), ["Real-world\nHighway 1", "Real-world\nHighway 2",
"Controlled\nlarge-scale 1+2\n[low]", "Controlled\nlarge-scale 2\n[high]",
"Controlled\n[large+small]-scale\n[high]", "Combined"])
for tick_label, tick_color in zip(plt.gca().get_xticklabels(), [colors[x] for x in range(5)] + ["black"]):
tick_label.set_color(tick_color)
plt.tight_layout()
plt.savefig(sp + "/overview_accuracy.pdf")
plt.close()
plt.figure(figsize=(10, 4))
plt.rcParams["font.family"] = "serif"
bp = plt.boxplot(data_sum[:, :, 1], positions=poses, widths=w)
for i in range(6):
if i < 3:
cur_col = colors[i]
else:
cur_col = colors[i - 1]
for j in range(6):
setp(bp['boxes'][j*6 + i], color=cur_col)
setp(bp['caps'][(j*6 + i) * 2], color=cur_col)
setp(bp['caps'][(j*6 + i) * 2 + 1], color=cur_col)
setp(bp['whiskers'][(j*6 + i) * 2], color=cur_col)
setp(bp['whiskers'][(j*6 + i) * 2 + 1], color=cur_col)
setp(bp['fliers'][j*6 + i], markeredgecolor=cur_col)
plt.ylabel("F2-Score", fontsize=12)
plt.xticks(np.arange(3, 36, 6), ["Real-world\nHighway 1", "Real-world\nHighway 2",
"Controlled\nlarge-scale 1+2\n[low]", "Controlled\nlarge-scale 2\n[high]",
"Controlled\n[large+small]-scale\n[high]", "Combined"])
for tick_label, tick_color in zip(plt.gca().get_xticklabels(), [colors[x] for x in range(5)] + ["black"]):
tick_label.set_color(tick_color)
plt.tight_layout()
plt.savefig(sp + "overview_f2.pdf")
plt.close()
# Plot Confusion Matrices and Uncertainty
base_path = "./results_" + cur_folder + "/uncertainties/"
dp = base_path + "*_confusion.pkl"
sp = "./results_" + cur_folder + "/images/%06d_%02d_plot.pdf"
files = glob(dp)
files.sort()
files = [x.replace("\\", "/") for x in files]
nums = [int(x.split("/")[-1].split("_")[0]) for x in files]
classes = ["0", "1", "2", "3", "4", "5", "6", "7", "8"]
font = {'family': 'normal',
'weight': 'bold',
'size': 10}
data_keys = ["T0", "T1", "T2", "T3", "T4", "T5"]
for cur_num in nums:
with open(base_path + "%06d_aleatoric_diag.pkl" % cur_num, "rb") as f:
aleatoric = pkl.load(f)
with open(base_path + "%06d_epistemic_diag.pkl" % cur_num, "rb") as f:
epistemic = pkl.load(f)
with open(base_path + "%06d_confusion.pkl" % cur_num, "rb") as f:
confusion_mat = pkl.load(f)
for ik, key in enumerate(data_keys):
aleatoric[key][np.isnan(aleatoric[key])] = 0.0
epistemic[key][np.isnan(epistemic[key])] = 0.0
plt.figure(figsize=(9, 3))
plt.rcParams["font.family"] = "serif"
num_c = len(confusion_mat[key])
plt.subplot(1, 3, 1)
plt.imshow(confusion_mat[key], cmap="rainbow", norm=matplotlib.colors.LogNorm(vmin=1.0))
for ci, c in enumerate(confusion_mat[key].flatten()):
cii = ci % len(confusion_mat[key])
cij = ci // len(confusion_mat[key])
plt.text(cii, cij, np.minimum(int(c), 9999), ha='center', va='center', size=8)
plt.xlabel("Class [Prediction]", fontsize=12)
plt.ylabel("Class [Ground Truth]", fontsize=12)
plt.xticks(np.arange(num_c), classes, fontsize=12)
plt.yticks(np.arange(num_c), classes, fontsize=12)
plt.colorbar(shrink=0.7)
plt.subplot(1, 3, 2)
plt.imshow(aleatoric[key], cmap="rainbow", vmin=0.0, vmax=0.25)
plt.xlabel("Class [Prediction]", fontsize=12)
plt.xticks(np.arange(num_c), classes, fontsize=12)
plt.yticks(np.arange(num_c), classes, fontsize=12)
plt.colorbar(shrink=0.7)
plt.subplot(1, 3, 3)
plt.imshow(epistemic[key], cmap="rainbow", vmin=0.0, vmax=np.max(epistemic[key]))
plt.xlabel("Class [Prediction]", fontsize=12)
plt.xticks(np.arange(num_c), classes, fontsize=12)
plt.yticks(np.arange(num_c), classes, fontsize=12)
plt.colorbar(shrink=0.7)
plt.tight_layout()
plt.savefig(sp % (cur_num, ik))
plt.close()