add ConfusionMatrixLoggerCallback
This commit is contained in:
1 parent
41b66d43b7
commit
03055295e6
3 files changed
+50
-4
No files matched your search
@@ -45,6 +45,11 @@ MNIST_CLASSIFIER_V2:
|
|||||||
train_val_split_ratio: 0.9
|
train_val_split_ratio: 0.9
|
||||||
num_workers: 1
|
num_workers: 1
|
||||||
pin_memory: False
|
pin_memory: False
|
||||||
|
callbacks:
|
||||||
|
ConfusionMatrixLoggerCallback:
|
||||||
|
class_names: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
|
||||||
|
MetricsHeatmapLoggerCallback:
|
||||||
|
class_names: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
|
||||||
wandb:
|
wandb:
|
||||||
group: None
|
group: None
|
||||||
tags: ["v2", "uwu"]
|
tags: ["v2", "uwu"]
|
||||||
|
|||||||
+1
-1
@@ -68,7 +68,7 @@ def main(run_config_name: str, use_wandb: bool):
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
parser = ArgumentParser()
|
parser = ArgumentParser()
|
||||||
parser.add_argument("-r", "--run_config", type=str, default="MNIST_CLASSIFIER_V1")
|
parser.add_argument("-r", "--run_config", type=str, default="MNIST_CLASSIFIER_V2")
|
||||||
parser.add_argument("-n", "--no_wandb", dest='use_wandb', action='store_false')
|
parser.add_argument("-n", "--no_wandb", dest='use_wandb', action='store_false')
|
||||||
parser.set_defaults(use_wandb=True)
|
parser.set_defaults(use_wandb=True)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|||||||
@@ -86,11 +86,12 @@ class UnfreezeModelCallback(pl.Callback):
|
|||||||
|
|
||||||
class MetricsHeatmapLoggerCallback(pl.Callback):
|
class MetricsHeatmapLoggerCallback(pl.Callback):
|
||||||
"""
|
"""
|
||||||
Generate f1, precision and recall heatmap calculated from each validation epoch outputs.
|
Generate f1, precision and recall heatmap from validation epoch outputs.
|
||||||
Expects validation step to return predictions and targets.
|
Expects validation step to return predictions and targets.
|
||||||
Works only for single label classification!
|
Works only for single label classification!
|
||||||
"""
|
"""
|
||||||
def __init__(self):
|
def __init__(self, class_names):
|
||||||
|
self.class_names = class_names
|
||||||
self.preds = []
|
self.preds = []
|
||||||
self.targets = []
|
self.targets = []
|
||||||
self.ready = False
|
self.ready = False
|
||||||
@@ -117,7 +118,7 @@ class MetricsHeatmapLoggerCallback(pl.Callback):
|
|||||||
|
|
||||||
trainer.logger.experiment.log({
|
trainer.logger.experiment.log({
|
||||||
f"f1_p_r_heatmap_{trainer.current_epoch}": wandb.plots.HeatMap(
|
f"f1_p_r_heatmap_{trainer.current_epoch}": wandb.plots.HeatMap(
|
||||||
x_labels=[str(i) for i in range(len(f1))], # class names can be hardcoded here
|
x_labels=self.class_names,
|
||||||
y_labels=["f1", "precision", "recall"],
|
y_labels=["f1", "precision", "recall"],
|
||||||
matrix_values=[f1, p, r],
|
matrix_values=[f1, p, r],
|
||||||
show_text=True,
|
show_text=True,
|
||||||
@@ -127,6 +128,46 @@ class MetricsHeatmapLoggerCallback(pl.Callback):
|
|||||||
self.targets = []
|
self.targets = []
|
||||||
|
|
||||||
|
|
||||||
|
class ConfusionMatrixLoggerCallback(pl.Callback):
|
||||||
|
"""
|
||||||
|
Generate Confusion Matrix.
|
||||||
|
Expects validation step to return predictions and targets.
|
||||||
|
Works only for single label classification!
|
||||||
|
"""
|
||||||
|
def __init__(self, class_names):
|
||||||
|
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
|
||||||
|
self.preds.append(preds)
|
||||||
|
self.targets.append(targets)
|
||||||
|
|
||||||
|
def on_validation_epoch_end(self, trainer, pl_module):
|
||||||
|
"""Generate f1, precision and recall heatmap."""
|
||||||
|
if self.ready:
|
||||||
|
self.preds = torch.cat(self.preds).tolist()
|
||||||
|
self.targets = torch.cat(self.targets).tolist()
|
||||||
|
|
||||||
|
trainer.logger.experiment.log({
|
||||||
|
f"conf_mat{trainer.current_epoch}": wandb.plot.confusion_matrix(
|
||||||
|
self.preds,
|
||||||
|
self.targets,
|
||||||
|
class_names=self.class_names)
|
||||||
|
}, commit=False)
|
||||||
|
|
||||||
|
self.preds = []
|
||||||
|
self.targets = []
|
||||||
|
|
||||||
|
|
||||||
class SaveCodeToWandbCallback(pl.Callback):
|
class SaveCodeToWandbCallback(pl.Callback):
|
||||||
"""
|
"""
|
||||||
Upload specified code files to wandb at the beginning of the run.
|
Upload specified code files to wandb at the beginning of the run.
|
||||||
|
|||||||
Reference in new issue
Block a user