* refactor folder names * define python logger correctly * refactor rich printing method * specify model parameters explicitly * specify datamodule params explicitly * refactor * update readme
206 lines
6.6 KiB
Python
206 lines
6.6 KiB
Python
import glob
|
|
import os
|
|
from typing import List
|
|
|
|
import matplotlib.pyplot as plt
|
|
import seaborn as sn
|
|
import torch
|
|
import wandb
|
|
from pytorch_lightning import Callback, Trainer
|
|
from pytorch_lightning.loggers import LoggerCollection, WandbLogger
|
|
from sklearn import metrics
|
|
from sklearn.metrics import f1_score, precision_score, recall_score
|
|
|
|
|
|
def get_wandb_logger(trainer: Trainer) -> WandbLogger:
|
|
if isinstance(trainer.logger, WandbLogger):
|
|
return trainer.logger
|
|
|
|
if isinstance(trainer.logger, LoggerCollection):
|
|
for logger in trainer.logger:
|
|
if isinstance(logger, WandbLogger):
|
|
return logger
|
|
|
|
raise Exception(
|
|
"You are using wandb related callback,"
|
|
"but WandbLogger was not found for some reason..."
|
|
)
|
|
|
|
|
|
class UploadCodeToWandbAsArtifact(Callback):
|
|
"""Upload all *.py files to wandb as an artifact, at the beginning of the run."""
|
|
|
|
def __init__(self, code_dir: str):
|
|
self.code_dir = code_dir
|
|
|
|
def on_train_start(self, trainer, pl_module):
|
|
logger = get_wandb_logger(trainer=trainer)
|
|
experiment = logger.experiment
|
|
|
|
code = wandb.Artifact("project-source", type="code")
|
|
for path in glob.glob(os.path.join(self.code_dir, "**/*.py"), recursive=True):
|
|
code.add_file(path)
|
|
|
|
experiment.use_artifact(code)
|
|
|
|
|
|
class UploadCheckpointsToWandbAsArtifact(Callback):
|
|
"""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
|
|
self.upload_best_only = upload_best_only
|
|
|
|
def on_train_end(self, trainer, pl_module):
|
|
logger = get_wandb_logger(trainer=trainer)
|
|
experiment = logger.experiment
|
|
|
|
ckpts = wandb.Artifact("experiment-ckpts", type="checkpoints")
|
|
|
|
if self.upload_best_only:
|
|
ckpts.add_file(trainer.checkpoint_callback.best_model_path)
|
|
else:
|
|
for path in glob.glob(
|
|
os.path.join(self.ckpt_dir, "**/*.ckpt"), recursive=True
|
|
):
|
|
ckpts.add_file(path)
|
|
|
|
experiment.use_artifact(ckpts)
|
|
|
|
|
|
class WatchModelWithWandb(Callback):
|
|
"""Make WandbLogger watch model at the beginning of the run."""
|
|
|
|
def __init__(self, log: str = "gradients", log_freq: int = 100):
|
|
self.log = log
|
|
self.log_freq = log_freq
|
|
|
|
def on_train_start(self, trainer, pl_module):
|
|
logger = get_wandb_logger(trainer=trainer)
|
|
logger.watch(model=trainer.model, log=self.log, log_freq=self.log_freq)
|
|
|
|
|
|
class LogConfusionMatrixToWandb(Callback):
|
|
"""Generate confusion matrix every epoch and send it to wandb.
|
|
Expects validation step to return predictions and targets.
|
|
"""
|
|
|
|
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):
|
|
"""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 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."""
|
|
if self.ready:
|
|
logger = get_wandb_logger(trainer=trainer)
|
|
experiment = logger.experiment
|
|
|
|
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/{experiment.name}": wandb.Image(plt)}, commit=False
|
|
)
|
|
|
|
# reset plot
|
|
plt.clf()
|
|
|
|
self.preds.clear()
|
|
self.targets.clear()
|