more silly tweaks

This commit is contained in:
hobogalaxy committed 2020-11-16 20:42:49 +01:00
1 parent 0b1723d0f8
commit 771b854443
6 files changed
+43 -25

No files matched your search

+27 -10
View File
@@ -5,6 +5,8 @@ import os
class ExampleCallback(Callback):
def __init__(self):
pass
def on_init_start(self, trainer):
print('Starting to initialize trainer!')
@@ -17,11 +19,12 @@ class ExampleCallback(Callback):
class SaveOnnxToWandbCallback(Callback):
def __init__(self, dataloader, wandb_save_dir):
first_batch = next(iter(dataloader))
"""Save model in .onnx format."""
def __init__(self, datamodule, save_dir):
first_batch = next(iter(datamodule.train_dataloader()))
x, y = first_batch
self.dummy_input = x
self.save_dir = wandb_save_dir
self.save_dir = save_dir
def on_sanity_check_end(self, trainer, pl_module):
self.save_onnx_model(pl_module)
@@ -36,6 +39,7 @@ class SaveOnnxToWandbCallback(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):
super().__init__()
self.val_imgs, self.val_labels = val_samples[:num_samples]
@@ -46,13 +50,13 @@ class ImagePredictionLoggerCallback(Callback):
logits = pl_module(val_imgs)
preds = torch.argmax(logits, -1)
trainer.logger.experiment.log({
"examples": [wandb.Image(x, caption=f"Pred:{pred}, Label:{y}")
for x, pred, y in zip(val_imgs, preds, self.val_labels)],
})
trainer.logger.experiment.log({"examples": [
wandb.Image(x, caption=f"Pred:{pred}, Label:{y}") for x, pred, y in zip(val_imgs, preds, self.val_labels)
]})
class UnFreezeModelCallback(Callback):
"""Unfreeze model after a few epochs."""
def __init__(self, wait_epochs=5):
self.wait_epochs = wait_epochs
@@ -63,9 +67,22 @@ class UnFreezeModelCallback(Callback):
class ConfusionMatrixLoggerCallback(Callback):
"""Log confusion matrix calculated from each validation epoch."""
def __init__(self):
pass
self.val_loop_outputs = []
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.val_loop_outputs.append(outputs)
def on_validation_epoch_end(self, trainer, pl_module):
print(trainer.callback_metrics)
if self.ready:
for outputs in self.val_loop_outputs:
preds, targets = outputs
self.val_loop_outputs = []