2023-04-14 08:27:48 +00:00
|
|
|
import os
|
2022-11-18 06:04:49 +00:00
|
|
|
import time
|
2023-04-14 08:27:48 +00:00
|
|
|
|
2022-11-18 06:04:49 +00:00
|
|
|
import wandb
|
|
|
|
from torch.utils.tensorboard import SummaryWriter
|
|
|
|
|
2023-04-14 08:27:48 +00:00
|
|
|
|
2022-11-18 06:04:49 +00:00
|
|
|
class WandbLog:
|
|
|
|
@classmethod
|
|
|
|
def init_wandb(cls, project, notes=None, name=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), config=None):
|
|
|
|
wandb.init(project=project, notes=notes, name=name, config=config)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def log(cls, result, model=None, gradient=None):
|
|
|
|
wandb.log(result)
|
|
|
|
|
|
|
|
if model:
|
|
|
|
wandb.watch(model)
|
2023-04-14 08:27:48 +00:00
|
|
|
|
2022-11-18 06:04:49 +00:00
|
|
|
if gradient:
|
|
|
|
wandb.watch(gradient)
|
|
|
|
|
|
|
|
|
|
|
|
class TensorboardLog:
|
|
|
|
def __init__(self, location, name=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), config=None):
|
|
|
|
if not os.path.exists(location):
|
|
|
|
os.mkdir(location)
|
|
|
|
self.writer = SummaryWriter(location, comment=name)
|
|
|
|
|
|
|
|
def log_train(self, result, step):
|
|
|
|
for k, v in result.items():
|
2023-09-19 06:20:26 +00:00
|
|
|
self.writer.add_scalar(f"{k}/train", v, step)
|
2023-04-14 08:27:48 +00:00
|
|
|
|
2022-11-18 06:04:49 +00:00
|
|
|
def log_eval(self, result, step):
|
|
|
|
for k, v in result.items():
|
2023-09-19 06:20:26 +00:00
|
|
|
self.writer.add_scalar(f"{k}/eval", v, step)
|
2022-11-18 06:04:49 +00:00
|
|
|
|
|
|
|
def log_zeroshot(self, result, step):
|
|
|
|
for k, v in result.items():
|
2023-09-19 06:20:26 +00:00
|
|
|
self.writer.add_scalar(f"{k}_acc/eval", v, step)
|