fix ImagePredictionLoggerCallback
This commit is contained in:
1 parent
a77d9c435e
commit
73c0691b55
3 files changed
+34
-17
No files matched your search
@@ -1,6 +1,6 @@
|
|||||||
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, multilabel_confusion_matrix
|
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
|
||||||
from pytorch_lightning.callbacks import Callback
|
from pytorch_lightning.callbacks import Callback
|
||||||
from torch.nn.functional import one_hot
|
import seaborn
|
||||||
import torch
|
import torch
|
||||||
import wandb
|
import wandb
|
||||||
import os
|
import os
|
||||||
@@ -21,7 +21,9 @@ class ExampleCallback(Callback):
|
|||||||
|
|
||||||
|
|
||||||
class SaveModelOnnxCallback(Callback):
|
class SaveModelOnnxCallback(Callback):
|
||||||
"""Save model in .onnx format."""
|
"""
|
||||||
|
Save model in .onnx format.
|
||||||
|
"""
|
||||||
def __init__(self, datamodule, save_dir):
|
def __init__(self, datamodule, save_dir):
|
||||||
first_batch = next(iter(datamodule.train_dataloader()))
|
first_batch = next(iter(datamodule.train_dataloader()))
|
||||||
x, y = first_batch
|
x, y = first_batch
|
||||||
@@ -41,24 +43,36 @@ class SaveModelOnnxCallback(Callback):
|
|||||||
|
|
||||||
|
|
||||||
class ImagePredictionLoggerCallback(Callback):
|
class ImagePredictionLoggerCallback(Callback):
|
||||||
"""Each epoch upload to wandb a couple of the same images with predicted labels."""
|
"""
|
||||||
def __init__(self, val_samples, num_samples=32):
|
Each epoch upload to wandb a couple of the same images with predicted labels.
|
||||||
super().__init__()
|
"""
|
||||||
self.val_imgs, self.val_labels = val_samples[:num_samples]
|
def __init__(self, datamodule, num_samples=8):
|
||||||
|
first_batch = next(iter(datamodule.train_dataloader()))
|
||||||
|
self.imgs, self.labels = first_batch
|
||||||
|
self.imgs, self.labels = self.imgs[:num_samples], self.labels[:num_samples]
|
||||||
|
self.ready = True
|
||||||
|
|
||||||
|
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_epoch_end(self, trainer, pl_module):
|
def on_validation_epoch_end(self, trainer, pl_module):
|
||||||
val_imgs = self.val_imgs.to(device=pl_module.device)
|
if self.ready:
|
||||||
|
imgs = self.imgs.to(device=pl_module.device)
|
||||||
logits = pl_module(val_imgs)
|
logits = pl_module(imgs)
|
||||||
preds = torch.argmax(logits, -1)
|
preds = torch.argmax(logits, -1)
|
||||||
|
trainer.logger.experiment.log({f"img_examples": [
|
||||||
trainer.logger.experiment.log({"examples": [
|
wandb.Image(
|
||||||
wandb.Image(x, caption=f"Pred:{pred}, Label:{y}") for x, pred, y in zip(val_imgs, preds, self.val_labels)
|
x,
|
||||||
|
caption=f"Epoch: {trainer.current_epoch} Pred:{pred}, Label:{y}"
|
||||||
|
) for x, pred, y in zip(imgs, preds, self.labels)
|
||||||
]}, commit=False)
|
]}, commit=False)
|
||||||
|
|
||||||
|
|
||||||
class UnfreezeModelCallback(Callback):
|
class UnfreezeModelCallback(Callback):
|
||||||
"""Unfreeze model after a few epochs."""
|
"""
|
||||||
|
Unfreeze model after a few epochs.
|
||||||
|
"""
|
||||||
def __init__(self, wait_epochs=5):
|
def __init__(self, wait_epochs=5):
|
||||||
self.wait_epochs = wait_epochs
|
self.wait_epochs = wait_epochs
|
||||||
|
|
||||||
@@ -99,7 +113,7 @@ class MetricsHeatmapLoggerCallback(Callback):
|
|||||||
p = precision_score(self.preds, self.targets, average=None)
|
p = precision_score(self.preds, self.targets, average=None)
|
||||||
|
|
||||||
trainer.logger.experiment.log({
|
trainer.logger.experiment.log({
|
||||||
f"f1_p_r_heatmap{pl_module.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=[str(i) for i in range(len(f1))], # class names can be hardcoded here
|
||||||
y_labels=["f1", "precision", "recall"],
|
y_labels=["f1", "precision", "recall"],
|
||||||
matrix_values=[f1, p, r],
|
matrix_values=[f1, p, r],
|
||||||
|
|||||||
+3
-2
@@ -38,8 +38,9 @@ def train(config):
|
|||||||
mode=config["callbacks"]["checkpoint"]["mode"],
|
mode=config["callbacks"]["checkpoint"]["mode"],
|
||||||
save_last=config["callbacks"]["checkpoint"]["save_last"],
|
save_last=config["callbacks"]["checkpoint"]["save_last"],
|
||||||
),
|
),
|
||||||
MetricsHeatmapLoggerCallback(),
|
# MetricsHeatmapLoggerCallback(),
|
||||||
UnfreezeModelCallback(wait_epochs=1),
|
# UnfreezeModelCallback(wait_epochs=5),
|
||||||
|
# ImagePredictionLoggerCallback(datamodule=datamodule),
|
||||||
# SaveModelOnnxCallback(datamodule=datamodule, save_dir=wandb_logger.save_dir)
|
# SaveModelOnnxCallback(datamodule=datamodule, save_dir=wandb_logger.save_dir)
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
|
scikit-learn>=0.23.2
|
||||||
torchvision>=0.8.1
|
torchvision>=0.8.1
|
||||||
pytorch-lightning>=1.0.6
|
pytorch-lightning>=1.0.6
|
||||||
pytorch-lightning-bolts>=0.2.5
|
pytorch-lightning-bolts>=0.2.5
|
||||||
wandb>=0.10.10
|
wandb>=0.10.10
|
||||||
PyYAML>=5.3.1
|
PyYAML>=5.3.1
|
||||||
efficientnet-pytorch>=0.7.0
|
efficientnet-pytorch>=0.7.0
|
||||||
|
seaborn>=0.11.0
|
||||||
Reference in new issue
Block a user