|
|
|
|
| __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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| TRAINING_DATA = 'TA'
|
|
|
|
|
| script_location = os.path.dirname(os.path.abspath(__file__))
|
|
|
| root_dir = os.path.dirname(script_location)
|
|
|
|
|
| file_folder = os.path.join(root_dir, '5_model_output', TRAINING_DATA, 'Training')
|
| FILE_PATH = os.path.join(file_folder, 'training_log.csv')
|
|
|
|
|
| PLOT_OUTPUT_DIR = os.path.join(root_dir, '5_model_output', 'Plots')
|
|
|
|
|
| FIG_SIZE = (6, 6)
|
| DPI = 100
|
| USE_GRID = True
|
| GRID_STYLE = '--'
|
| GRID_ALPHA = 0.5
|
|
|
|
|
| FONT_TITLE = 16
|
| FONT_AXIS_LABEL = 14
|
| FONT_LEGEND = 12
|
| FONT_TICKS = 12
|
|
|
|
|
|
|
| LINE_WIDTH = 2.5
|
|
|
|
|
| COLOR_TRAIN_LOSS = '#1f77b4'
|
| COLOR_VALID_LOSS = '#ff7f0e'
|
| TITLE_LOSS = f"Training vs Validation Loss ({TRAINING_DATA})"
|
| Y_LABEL_LOSS = "Loss Value"
|
|
|
|
|
| COLOR_IOU = '#2ca02c'
|
| COLOR_F1 = '#d62728'
|
| TITLE_METRICS = f"IoU and F1 Score over Epochs ({TRAINING_DATA})"
|
| Y_LABEL_METRICS = "Score"
|
|
|
|
|
|
|
|
|
|
|
| def plot_training_results():
|
|
|
| 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
|
|
|
|
|
| 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
|
|
|
|
|
| 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
|
|
|
|
|
| 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
|
| })
|
|
|
|
|
|
|
|
|
| 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_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}")
|
|
|
|
|
|
|
|
|
| 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_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}")
|
|
|
|
|
| print("Displaying plots...")
|
| plt.show()
|
|
|
| if __name__ == "__main__":
|
| plot_training_results() |