File size: 5,670 Bytes
54d9099 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# -*- coding: utf-8 -*-
# Document info
__author__ = 'Andreas Sjölander, Gemini'
__version__ = ['1.0']
__version_date__ = '2025-11-25'
__maintainer__ = 'Andreas Sjölander'
__email__ = 'asjola@kth.se'
"""
2b_plot_training.py
This script reads the csv output from training and creates a plot of training
and validation loss in one plot and IoU and F1-score in a second plot.
User only needs to change "TRAINING_DATA" to correct training set.
"""
import pandas as pd
import matplotlib.pyplot as plt
import os
# ==========================================
# 🎨 CONFIGURATION & AESTHETICS SECTION
# ==========================================
# Change these variables to customize the look of your plots
# --- File Settings ---
TRAINING_DATA = 'TA' # Options: 'TA', 'TB', 'TC', etc.
# Dynamic path setup
script_location = os.path.dirname(os.path.abspath(__file__))
# Assuming the script is inside a subfolder, go up one level to root
root_dir = os.path.dirname(script_location)
# Input Folder
file_folder = os.path.join(root_dir, '5_model_output', TRAINING_DATA, 'Training')
FILE_PATH = os.path.join(file_folder, 'training_log.csv')
# Output Folder for Plots
PLOT_OUTPUT_DIR = os.path.join(root_dir, '5_model_output', 'Plots')
# --- General Plot Settings ---
FIG_SIZE = (6, 6) # Width, Height in inches
DPI = 100 # Resolution
USE_GRID = True # Show grid lines?
GRID_STYLE = '--' # Grid line style
GRID_ALPHA = 0.5 # Grid transparency
# --- Font Sizes ---
FONT_TITLE = 16
FONT_AXIS_LABEL = 14
FONT_LEGEND = 12
FONT_TICKS = 12
# --- Colors & Line Styles ---
# You can use color names ('red', 'blue') or Hex codes ('#FF5733')
LINE_WIDTH = 2.5
# Plot 1: Loss Configuration
COLOR_TRAIN_LOSS = '#1f77b4'
COLOR_VALID_LOSS = '#ff7f0e'
TITLE_LOSS = f"Training vs Validation Loss ({TRAINING_DATA})"
Y_LABEL_LOSS = "Loss Value"
# Plot 2: Metrics Configuration
COLOR_IOU = '#2ca02c'
COLOR_F1 = '#d62728'
TITLE_METRICS = f"IoU and F1 Score over Epochs ({TRAINING_DATA})"
Y_LABEL_METRICS = "Score"
# ==========================================
# 🚀 MAIN SCRIPT LOGIC
# ==========================================
def plot_training_results():
# 1. Check if input file exists
if not os.path.exists(FILE_PATH):
print(f"Error: The file '{FILE_PATH}' was not found.")
print(f"Constructed path: {FILE_PATH}")
print("Please check the 'TRAINING_DATA' variable or folder structure.")
return
# 2. Create Output Directory if it doesn't exist
if not os.path.exists(PLOT_OUTPUT_DIR):
try:
os.makedirs(PLOT_OUTPUT_DIR)
print(f"Created output directory: {PLOT_OUTPUT_DIR}")
except OSError as e:
print(f"Error creating directory {PLOT_OUTPUT_DIR}: {e}")
return
# 3. Read the CSV file
try:
df = pd.read_csv(FILE_PATH)
print(f"Successfully loaded data for {TRAINING_DATA}. Found {len(df)} epochs.")
except Exception as e:
print(f"Error reading CSV: {e}")
return
# Apply global font sizes using rcParams
plt.rcParams.update({
'font.size': FONT_TICKS,
'axes.titlesize': FONT_TITLE,
'axes.labelsize': FONT_AXIS_LABEL,
'legend.fontsize': FONT_LEGEND,
'xtick.labelsize': FONT_TICKS,
'ytick.labelsize': FONT_TICKS
})
# -------------------------------------------------------
# PLOT 1: Training and Validation Loss
# -------------------------------------------------------
plt.figure(figsize=FIG_SIZE, dpi=DPI)
plt.plot(df['epoch'], df['train_loss'],
label='Training Loss',
color=COLOR_TRAIN_LOSS,
linewidth=LINE_WIDTH)
plt.plot(df['epoch'], df['valid_loss'],
label='Validation Loss',
color=COLOR_VALID_LOSS,
linewidth=LINE_WIDTH,
linestyle='--')
plt.title(TITLE_LOSS, fontweight='bold')
plt.xlabel("Epochs")
plt.ylabel(Y_LABEL_LOSS)
plt.legend()
if USE_GRID:
plt.grid(True, linestyle=GRID_STYLE, alpha=GRID_ALPHA)
plt.tight_layout()
# Save Plot 1
save_name_loss = f"{TRAINING_DATA}_loss_plot.png"
save_path_loss = os.path.join(PLOT_OUTPUT_DIR, save_name_loss)
plt.savefig(save_path_loss)
print(f"Saved Loss plot to: {save_path_loss}")
# -------------------------------------------------------
# PLOT 2: IoU and F1 Score
# -------------------------------------------------------
plt.figure(figsize=FIG_SIZE, dpi=DPI)
plt.plot(df['epoch'], df['iou_crack'],
label='IoU (Crack)',
color=COLOR_IOU,
linewidth=LINE_WIDTH)
plt.plot(df['epoch'], df['f1_score_crack'],
label='F1 Score (Crack)',
color=COLOR_F1,
linewidth=LINE_WIDTH)
plt.title(TITLE_METRICS, fontweight='bold')
plt.xlabel("Epochs")
plt.ylabel(Y_LABEL_METRICS)
plt.legend()
if USE_GRID:
plt.grid(True, linestyle=GRID_STYLE, alpha=GRID_ALPHA)
plt.tight_layout()
# Save Plot 2
save_name_metrics = f"{TRAINING_DATA}_metrics_plot.png"
save_path_metrics = os.path.join(PLOT_OUTPUT_DIR, save_name_metrics)
plt.savefig(save_path_metrics)
print(f"Saved Metrics plot to: {save_path_metrics}")
# Show the plots
print("Displaying plots...")
plt.show()
if __name__ == "__main__":
plot_training_results() |