Dev (#105)
* add printing ckpt path * refactor wandb callbacks * fix code formatting
This commit is contained in:
1 parent
755cd89cf0
commit
fca999f527
6 files changed
+114
-93
No files matched your search
@@ -143,3 +143,4 @@ dmypy.json
|
||||
# Lightning-Hydra-Template
|
||||
data/
|
||||
logs/
|
||||
wandb/
|
||||
@@ -14,4 +14,4 @@ early_stopping:
|
||||
monitor: "val/acc" # name of the logged metric which determines when model is improving
|
||||
patience: 100 # how many epochs of not improving until training stops
|
||||
mode: "max" # can be "max" or "min"
|
||||
min_delta: 0.0 # minimum change in the monitored metric needed to qualify as an improvement
|
||||
min_delta: 0 # minimum change in the monitored metric needed to qualify as an improvement
|
||||
@@ -10,7 +10,7 @@ upload_code_to_wandb_as_artifact:
|
||||
upload_ckpts_to_wandb_as_artifact:
|
||||
_target_: src.callbacks.wandb_callbacks.UploadCheckpointsToWandbAsArtifact
|
||||
ckpt_dir: "checkpoints/"
|
||||
upload_best_only: False
|
||||
upload_best_only: True
|
||||
|
||||
|
||||
watch_model_with_wandb:
|
||||
@@ -19,16 +19,9 @@ watch_model_with_wandb:
|
||||
log_freq: 100
|
||||
|
||||
|
||||
# BUGGED :(
|
||||
# save_best_metric_scores_to_wandb:
|
||||
# _target_: src.callbacks.wandb_callbacks.LogBestMetricScoresToWandb
|
||||
|
||||
|
||||
save_f1_precision_recall_heatmap_to_wandb:
|
||||
_target_: src.callbacks.wandb_callbacks.LogF1PrecisionRecallHeatmapToWandb
|
||||
class_names: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
|
||||
_target_: src.callbacks.wandb_callbacks.LogF1PrecRecHeatmapToWandb
|
||||
|
||||
|
||||
save_confusion_matrix_to_wandb:
|
||||
_target_: src.callbacks.wandb_callbacks.LogConfusionMatrixToWandb
|
||||
class_names: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
|
||||
@@ -24,3 +24,6 @@ python-dotenv
|
||||
pre-commit
|
||||
dvc
|
||||
scikit-learn
|
||||
pandas
|
||||
matplotlib
|
||||
seaborn
|
||||
@@ -2,28 +2,26 @@ import glob
|
||||
import os
|
||||
from typing import List
|
||||
|
||||
import pytorch_lightning as pl
|
||||
import matplotlib.pyplot as plt
|
||||
import seaborn as sn
|
||||
import torch
|
||||
import wandb
|
||||
from pytorch_lightning import Callback
|
||||
from pytorch_lightning import Callback, Trainer
|
||||
from pytorch_lightning.loggers import WandbLogger
|
||||
from sklearn import metrics
|
||||
from sklearn.metrics import f1_score, precision_score, recall_score
|
||||
|
||||
|
||||
def get_wandb_logger(trainer: pl.Trainer) -> WandbLogger:
|
||||
logger = None
|
||||
for lg in trainer.logger:
|
||||
if isinstance(lg, WandbLogger):
|
||||
logger = lg
|
||||
def get_wandb_logger(trainer: Trainer) -> WandbLogger:
|
||||
for logger in trainer.logger:
|
||||
if isinstance(logger, WandbLogger):
|
||||
return logger
|
||||
|
||||
if not logger:
|
||||
raise Exception(
|
||||
"You are using wandb related callback,"
|
||||
"but WandbLogger was not found for some reason..."
|
||||
)
|
||||
|
||||
return logger
|
||||
|
||||
|
||||
class UploadCodeToWandbAsArtifact(Callback):
|
||||
"""Upload all *.py files to wandb as an artifact, at the beginning of the run."""
|
||||
@@ -43,7 +41,7 @@ class UploadCodeToWandbAsArtifact(Callback):
|
||||
|
||||
|
||||
class UploadCheckpointsToWandbAsArtifact(Callback):
|
||||
"""Upload checkpoints to wandb as an artifact, at the end of training."""
|
||||
"""Upload checkpoints to wandb as an artifact, at the end of run."""
|
||||
|
||||
def __init__(self, ckpt_dir: str = "checkpoints/", upload_best_only: bool = False):
|
||||
self.ckpt_dir = ckpt_dir
|
||||
@@ -78,16 +76,17 @@ class WatchModelWithWandb(Callback):
|
||||
logger.watch(model=trainer.model, log=self.log, log_freq=self.log_freq)
|
||||
|
||||
|
||||
class LogF1PrecisionRecallHeatmapToWandb(Callback):
|
||||
"""
|
||||
Generate f1, precision and recall heatmap from validation step outputs.
|
||||
class LogConfusionMatrixToWandb(Callback):
|
||||
"""Generate confusion matrix every epoch and send it to wandb.
|
||||
Expects validation step to return predictions and targets.
|
||||
"""
|
||||
|
||||
def __init__(self, class_names: List[str] = None):
|
||||
self.class_names = class_names
|
||||
def __init__(self):
|
||||
self.preds = []
|
||||
self.targets = []
|
||||
self.ready = True
|
||||
|
||||
def on_sanity_check_start(self, trainer, pl_module) -> None:
|
||||
self.ready = False
|
||||
|
||||
def on_sanity_check_end(self, trainer, pl_module):
|
||||
@@ -99,9 +98,68 @@ class LogF1PrecisionRecallHeatmapToWandb(Callback):
|
||||
):
|
||||
"""Gather data from single batch."""
|
||||
if self.ready:
|
||||
preds, targets = outputs["preds"], outputs["targets"]
|
||||
self.preds.append(preds)
|
||||
self.targets.append(targets)
|
||||
self.preds.append(outputs["preds"])
|
||||
self.targets.append(outputs["targets"])
|
||||
|
||||
def on_validation_epoch_end(self, trainer, pl_module):
|
||||
"""Generate confusion matrix."""
|
||||
if self.ready:
|
||||
logger = get_wandb_logger(trainer)
|
||||
experiment = logger.experiment
|
||||
|
||||
preds = torch.cat(self.preds).cpu().numpy()
|
||||
targets = torch.cat(self.targets).cpu().numpy()
|
||||
|
||||
confusion_matrix = metrics.confusion_matrix(y_true=targets, y_pred=preds)
|
||||
|
||||
# set figure size
|
||||
plt.figure(figsize=(14, 8))
|
||||
|
||||
# set labels size
|
||||
sn.set(font_scale=1.4)
|
||||
|
||||
# set font size
|
||||
sn.heatmap(confusion_matrix, annot=True, annot_kws={"size": 8}, fmt="g")
|
||||
|
||||
# names should be uniqe or else charts from different experiments in wandb will overlap
|
||||
experiment.log(
|
||||
{f"confusion_matrix/{experiment.name}": wandb.Image(plt)}, commit=False
|
||||
)
|
||||
|
||||
# according to wandb docs this should also work but it crashes
|
||||
# experiment.log(f{"confusion_matrix/{experiment.name}": plt})
|
||||
|
||||
# reset plot
|
||||
plt.clf()
|
||||
|
||||
self.preds.clear()
|
||||
self.targets.clear()
|
||||
|
||||
|
||||
class LogF1PrecRecHeatmapToWandb(Callback):
|
||||
"""Generate f1, precision, recall heatmap every epoch and send it to wandb.
|
||||
Expects validation step to return predictions and targets.
|
||||
"""
|
||||
|
||||
def __init__(self, class_names: List[str] = None):
|
||||
self.preds = []
|
||||
self.targets = []
|
||||
self.ready = True
|
||||
|
||||
def on_sanity_check_start(self, trainer, pl_module):
|
||||
self.ready = False
|
||||
|
||||
def on_sanity_check_end(self, trainer, pl_module):
|
||||
"""Start executing this callback only after all validation sanity checks end."""
|
||||
self.ready = True
|
||||
|
||||
def on_validation_batch_end(
|
||||
self, trainer, pl_module, outputs, batch, batch_idx, dataloader_idx
|
||||
):
|
||||
"""Gather data from single batch."""
|
||||
if self.ready:
|
||||
self.preds.append(outputs["preds"])
|
||||
self.targets.append(outputs["targets"])
|
||||
|
||||
def on_validation_epoch_end(self, trainer, pl_module):
|
||||
"""Generate f1, precision and recall heatmap."""
|
||||
@@ -109,72 +167,35 @@ class LogF1PrecisionRecallHeatmapToWandb(Callback):
|
||||
logger = get_wandb_logger(trainer=trainer)
|
||||
experiment = logger.experiment
|
||||
|
||||
self.preds = torch.cat(self.preds).cpu()
|
||||
self.targets = torch.cat(self.targets).cpu()
|
||||
f1 = f1_score(self.preds, self.targets, average=None)
|
||||
r = recall_score(self.preds, self.targets, average=None)
|
||||
p = precision_score(self.preds, self.targets, average=None)
|
||||
preds = torch.cat(self.preds).cpu().numpy()
|
||||
targets = torch.cat(self.targets).cpu().numpy()
|
||||
f1 = f1_score(preds, targets, average=None)
|
||||
r = recall_score(preds, targets, average=None)
|
||||
p = precision_score(preds, targets, average=None)
|
||||
data = [f1, p, r]
|
||||
|
||||
# set figure size
|
||||
plt.figure(figsize=(14, 3))
|
||||
|
||||
# set labels size
|
||||
sn.set(font_scale=1.2)
|
||||
|
||||
# set font size
|
||||
sn.heatmap(
|
||||
data,
|
||||
annot=True,
|
||||
annot_kws={"size": 10},
|
||||
fmt=".3f",
|
||||
yticklabels=["F1", "Precision", "Recall"],
|
||||
)
|
||||
|
||||
# names should be uniqe or else charts from different experiments in wandb will overlap
|
||||
experiment.log(
|
||||
{
|
||||
f"f1_p_r_heatmap/{trainer.current_epoch}_{experiment.id}": wandb.plots.HeatMap(
|
||||
x_labels=self.class_names,
|
||||
y_labels=["f1", "precision", "recall"],
|
||||
matrix_values=[f1, p, r],
|
||||
show_text=True,
|
||||
)
|
||||
},
|
||||
commit=False,
|
||||
{f"f1_p_r_heatmap/{experiment.name}": wandb.Image(plt)}, commit=False
|
||||
)
|
||||
|
||||
self.preds = []
|
||||
self.targets = []
|
||||
# reset plot
|
||||
plt.clf()
|
||||
|
||||
|
||||
class LogConfusionMatrixToWandb(Callback):
|
||||
"""
|
||||
Generate Confusion Matrix.
|
||||
Expects validation step to return predictions and targets.
|
||||
"""
|
||||
|
||||
def __init__(self, class_names: List[str] = None):
|
||||
self.class_names = class_names
|
||||
self.preds = []
|
||||
self.targets = []
|
||||
self.ready = False
|
||||
|
||||
def on_sanity_check_end(self, trainer, pl_module):
|
||||
"""Start executing this callback only after all validation sanity checks end."""
|
||||
self.ready = True
|
||||
|
||||
def on_validation_batch_end(
|
||||
self, trainer, pl_module, outputs, batch, batch_idx, dataloader_idx
|
||||
):
|
||||
"""Gather data from single batch."""
|
||||
if self.ready:
|
||||
preds, targets = outputs["preds"], outputs["targets"]
|
||||
self.preds.append(preds)
|
||||
self.targets.append(targets)
|
||||
|
||||
def on_validation_epoch_end(self, trainer, pl_module):
|
||||
"""Generate confusion matrix."""
|
||||
if self.ready:
|
||||
logger = get_wandb_logger(trainer=trainer)
|
||||
experiment = logger.experiment
|
||||
|
||||
self.preds = torch.cat(self.preds).tolist()
|
||||
self.targets = torch.cat(self.targets).tolist()
|
||||
|
||||
experiment.log(
|
||||
{
|
||||
f"confusion_matrix/{trainer.current_epoch}_{experiment.id}": wandb.plot.confusion_matrix(
|
||||
preds=self.preds,
|
||||
y_true=self.targets,
|
||||
class_names=self.class_names,
|
||||
)
|
||||
},
|
||||
commit=False,
|
||||
)
|
||||
|
||||
self.preds = []
|
||||
self.targets = []
|
||||
self.preds.clear()
|
||||
self.targets.clear()
|
||||
@@ -93,6 +93,9 @@ def train(config: DictConfig) -> Optional[float]:
|
||||
logger=logger,
|
||||
)
|
||||
|
||||
# Print path to best checkpoint
|
||||
log.info(f"Best checkpoint path:\n{trainer.checkpoint_callback.best_model_path}")
|
||||
|
||||
# Return metric score for Optuna optimization
|
||||
optimized_metric = config.get("optimized_metric")
|
||||
if optimized_metric:
|
||||
|
||||
Reference in new issue
Block a user