-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathlogger.py
109 lines (93 loc) · 4.47 KB
/
logger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import os
import cv2
import wandb
import matplotlib.pyplot as plt
from typing import Optional
from pandas.io.json._normalize import nested_to_record
from torch.utils.tensorboard import SummaryWriter
class Logger:
""" A class to encapsulate external loggers such as Tensorboard, Allegro ClearML, Neptune, Weight and Biases,
Comet, ...
"""
__main_logger = None # type: Optional[Logger]
@classmethod
def current_logger(cls):
# type: () -> Logger
return cls.__main_logger
def __init__(self, cfg):
# self.cfg = cfg
# self.model_name = cfg.project.start_time + cfg.project.experiment_id
# configs
self.save_disk = cfg.project.logger.save_disk
self.save_dir = cfg.data.save_dir
self.matplotlib_show = cfg.project.logger.matplotlib_show
# init external loggers
self.tensorboard_logger = None
if cfg.project.logger.use_tensorboard:
self.tensorboard_folder = os.path.join(cfg.data.save_dir, 'tensorboard')
self.tensorboard_logger = SummaryWriter(self.tensorboard_folder)
self.use_wandb = cfg.project.logger.use_wandb
if self.use_wandb:
# os.environ["WANDB_SILENT"] = "true"
# wandb.init(config=cfg, sync_tensorboard=True, project=cfg.project.name, dir=cfg.data.save_dir, reinit=False)
if cfg.project.logger.use_tensorboard:
wandb.tensorboard.patch(pytorch=True, save=True, root_logdir=self.tensorboard_folder)
wandb.init(config=cfg,
project=cfg.project.name,
dir=cfg.data.save_dir,
reinit=False,
name=str(cfg.project.job_id),
notes=cfg.project.notes,
tags=cfg.project.tags
)
# wandb.tensorboard.patch(save=True, tensorboardX=False)
Logger.__main_logger = self
def add_model(self, model):
if self.use_wandb and wandb.run is not None:
wandb.watch(model)
def add_text(self, tag, value):
if self.use_wandb and wandb.run is not None:
wandb.log({tag: value})
def add_scalar(self, tag, scalar_value, step):
if self.tensorboard_logger is not None:
self.tensorboard_logger.add_scalar(tag, scalar_value, step)
if self.use_wandb and wandb.run is not None:
wandb.log({tag: scalar_value})
def add_figure(self, tag, figure, step):
if self.matplotlib_show:
figure.show()
plt.waitforbuttonpress()
if self.tensorboard_logger is not None:
self.tensorboard_logger.add_figure(tag, figure, step)
if self.use_wandb and wandb.run is not None:
wandb.log({tag: wandb.Image(
figure)}) # FIXME cannot give "figure" directly: Invalid value of type 'builtins.str' received for the 'color' property of scatter.marker Received value: 'none'
if self.save_disk:
figure_path = os.path.join(self.save_dir, 'figures', tag + '.png')
os.makedirs(os.path.dirname(figure_path), exist_ok=True)
plt.savefig(figure_path)
plt.close(figure)
def add_image(self, group, name, image, step):
"""Input image must be in RGB format"""
# if self.tensorboard_logger is not None:
# self.tensorboard_logger.add_figure(tag, figure, self.global_step())
if self.use_wandb and wandb.run is not None:
wandb.log({group + name: wandb.Image(image)})
if self.save_disk:
image_path = os.path.join(self.save_dir, 'images', f"{group}_{name}.jpg")
os.makedirs(os.path.dirname(image_path), exist_ok=True)
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
cv2.imwrite(image_path, image)
def add_embeddings(self, tag, embeddings, labels, imgs, step):
if self.tensorboard_logger is not None:
self.tensorboard_logger.add_embedding(embeddings,
metadata=labels,
label_img=imgs,
global_step=step,
tag=tag,
metadata_header=None)
def close(self):
if self.tensorboard_logger is not None:
self.tensorboard_logger.close()
if self.use_wandb and wandb.run is not None:
wandb.finish()