-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlogger.py
151 lines (134 loc) · 6.16 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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.allegro_clearml_task = None
if cfg.project.logger.use_clearml:
from clearml import Task
# Automagic Allegro clearml Task setup
self.allegro_clearml_task = Task.init(
project_name=cfg.project.name,
task_name=cfg.project.experiment_name,
task_type=Task.TaskTypes.training,
reuse_last_task_id=False,
continue_last_task=False,
output_uri=None,
auto_connect_arg_parser=True,
auto_connect_frameworks=True,
auto_resource_monitoring=True
)
# Register hyper-parameters
self.allegro_clearml_task.connect(cfg)
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.allegro_clearml_task is not None:
self.allegro_clearml_task.logger.report_matplotlib_figure(
title=tag,
# series="Iteration {:06d}".format(self.global_step()),
series="",
iteration=step,
figure=figure,
report_image=False
)
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.allegro_clearml_task is not None:
self.allegro_clearml_task.logger.report_image(
title=group,
series=name,
iteration=step,
# local_path=None,
image=image,
# matrix=None,
max_image_history=50,
# delete_after_upload=False,
# url=None
)
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.allegro_clearml_task is not None:
self.allegro_clearml_task.upload_artifact('Tensorboard folder', artifact_object=self.tensorboard_folder)
self.allegro_clearml_task.close()
if self.use_wandb and wandb.run is not None:
wandb.finish()