2025-04-19 16:52:03 -04:00
|
|
|
|
import glob
|
|
|
|
|
|
import json
|
|
|
|
|
|
import pandas as pd
|
|
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
2025-04-24 17:45:54 -04:00
|
|
|
|
# BASE_DIR = "./on_GPU"
|
2025-04-21 22:34:51 -04:00
|
|
|
|
|
2025-04-19 16:52:03 -04:00
|
|
|
|
# training diagnostics
|
|
|
|
|
|
histories = []
|
2025-04-24 17:45:54 -04:00
|
|
|
|
for BASE_DIR in ['./on_CPU', './on_GPU']:
|
|
|
|
|
|
for csv in glob.glob(f"{BASE_DIR}/analytics/history_*.csv"):
|
|
|
|
|
|
df = pd.read_csv(csv) # epochs x metrics
|
|
|
|
|
|
tag = "_".join(csv.split("_")[1:4]) # like sgd_none_42.csv
|
|
|
|
|
|
df['condition'] = tag.replace(".csv", "")
|
|
|
|
|
|
histories.append(df)
|
2025-04-21 22:34:51 -04:00
|
|
|
|
|
2025-04-19 16:52:03 -04:00
|
|
|
|
logs = pd.concat(histories, ignore_index=True)
|
|
|
|
|
|
|
|
|
|
|
|
# average across seeds, optimiser, augmentation for clarity
|
|
|
|
|
|
mean_log = logs.groupby('epoch').mean(numeric_only=True)
|
|
|
|
|
|
|
|
|
|
|
|
plt.figure()
|
2025-04-21 22:34:51 -04:00
|
|
|
|
plt.plot(mean_log.index, mean_log['train_acc'],
|
2025-04-24 17:45:54 -04:00
|
|
|
|
color='#228B22', label='train')
|
2025-04-21 22:34:51 -04:00
|
|
|
|
plt.plot(mean_log.index, mean_log['test_acc'],
|
2025-04-24 17:45:54 -04:00
|
|
|
|
color='#800080', label='validation')
|
2025-04-19 16:52:03 -04:00
|
|
|
|
plt.xlabel("epoch")
|
|
|
|
|
|
plt.ylabel("accuracy")
|
|
|
|
|
|
plt.legend()
|
2025-04-21 22:34:51 -04:00
|
|
|
|
plt.grid(True)
|
|
|
|
|
|
ax = plt.gca()
|
|
|
|
|
|
ax.spines['top'].set_visible(False)
|
|
|
|
|
|
ax.spines['right'].set_visible(False)
|
2025-04-19 16:52:03 -04:00
|
|
|
|
plt.tight_layout()
|
2025-04-24 17:45:54 -04:00
|
|
|
|
plt.savefig(f"train_val_accuracy.png")
|
2025-04-19 16:52:03 -04:00
|
|
|
|
|
|
|
|
|
|
plt.figure()
|
2025-04-21 22:34:51 -04:00
|
|
|
|
plt.plot(mean_log.index, mean_log['train_loss'],
|
2025-04-24 17:45:54 -04:00
|
|
|
|
color='#008080', label='train') # teal
|
2025-04-21 22:34:51 -04:00
|
|
|
|
plt.plot(mean_log.index, mean_log['test_loss'],
|
2025-04-24 17:45:54 -04:00
|
|
|
|
color='#FF00FF', label='validation') # magenta
|
2025-04-19 16:52:03 -04:00
|
|
|
|
plt.xlabel("epoch")
|
|
|
|
|
|
plt.ylabel("cross‑entropy loss")
|
|
|
|
|
|
plt.legend()
|
2025-04-21 22:34:51 -04:00
|
|
|
|
plt.grid(True)
|
|
|
|
|
|
ax = plt.gca()
|
|
|
|
|
|
ax.spines['top'].set_visible(False)
|
|
|
|
|
|
ax.spines['right'].set_visible(False)
|
2025-04-19 16:52:03 -04:00
|
|
|
|
plt.tight_layout()
|
2025-04-24 17:45:54 -04:00
|
|
|
|
plt.savefig(f"train_val_loss.png")
|
2025-04-19 16:52:03 -04:00
|
|
|
|
|
|
|
|
|
|
# robustness curves
|
2025-04-24 17:45:54 -04:00
|
|
|
|
with open(f"./combined_results.json") as f:
|
2025-04-19 16:52:03 -04:00
|
|
|
|
res = json.load(f)
|
|
|
|
|
|
|
|
|
|
|
|
df = pd.DataFrame(res)
|
2025-04-21 22:34:51 -04:00
|
|
|
|
records = [] # one row per sigma
|
2025-04-19 16:52:03 -04:00
|
|
|
|
|
|
|
|
|
|
for _, row in df.iterrows():
|
|
|
|
|
|
for sigma, acc in row['robustness'].items():
|
|
|
|
|
|
records.append({
|
|
|
|
|
|
'optimizer': row['optimizer'],
|
|
|
|
|
|
'augmentation': row['augmentation'],
|
|
|
|
|
|
'sigma': float(sigma),
|
|
|
|
|
|
'acc': acc,
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
rob_df = pd.DataFrame(records)
|
|
|
|
|
|
pivot = rob_df.groupby(['optimizer', 'sigma']).acc.mean().unstack(0)
|
|
|
|
|
|
|
2025-04-24 17:45:54 -04:00
|
|
|
|
ax = pivot.plot(marker='o', linestyle='-', color=['#FF0000', '#00008B'])
|
2025-04-19 16:52:03 -04:00
|
|
|
|
plt.xlabel("Gaussian noise sigma")
|
|
|
|
|
|
plt.ylabel("accuracy")
|
|
|
|
|
|
plt.title("Noise robustness")
|
2025-04-21 22:34:51 -04:00
|
|
|
|
plt.grid(True)
|
|
|
|
|
|
ax.spines['top'].set_visible(False)
|
|
|
|
|
|
ax.spines['right'].set_visible(False)
|
2025-04-19 16:52:03 -04:00
|
|
|
|
plt.tight_layout()
|
2025-04-24 17:45:54 -04:00
|
|
|
|
plt.savefig(f"./robustness_curve.png")
|
2025-04-19 16:52:03 -04:00
|
|
|
|
print("saved robustness_curve.png")
|