add SaveOnnxToWandbCallback
This commit is contained in:
1 parent
d013e22742
commit
f543fa5622
3 files changed
+49
-23
No files matched your search
+25
-3
@@ -1,13 +1,35 @@
|
||||
from pytorch_lightning.callbacks import Callback
|
||||
import wandb
|
||||
import os
|
||||
|
||||
|
||||
class ExampleCallback(Callback):
|
||||
|
||||
def on_init_start(self, trainer):
|
||||
print('Starting to init trainer!')
|
||||
print('Starting to initialize trainer!')
|
||||
|
||||
def on_init_end(self, trainer):
|
||||
print('Trainer is init now')
|
||||
print('Trainer is initialized now.')
|
||||
|
||||
def on_train_end(self, trainer, pl_module):
|
||||
print('Do something when training ends')
|
||||
print('Do something when training ends.')
|
||||
|
||||
|
||||
class SaveOnnxToWandbCallback(Callback):
|
||||
|
||||
def __init__(self, dataloader, wandb_save_dir):
|
||||
first_batch = next(iter(dataloader))
|
||||
x, y = first_batch
|
||||
self.dummy_input = x
|
||||
self.save_dir = wandb_save_dir
|
||||
|
||||
def on_sanity_check_end(self, trainer, pl_module):
|
||||
self.save_onnx_model(pl_module)
|
||||
|
||||
def on_train_end(self, trainer, pl_module):
|
||||
self.save_onnx_model(pl_module)
|
||||
|
||||
def save_onnx_model(self, pl_module):
|
||||
file_path = os.path.join(self.save_dir, "model.onnx")
|
||||
pl_module.to_onnx(file_path=file_path, input_sample=self.dummy_input.to(pl_module.device))
|
||||
wandb.save(file_path)
|
||||
Reference in new issue
Block a user