-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathvisualizer.py
82 lines (70 loc) · 1.97 KB
/
visualizer.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
"""
This module creates visualizers.
Author: wangning([email protected])
Date : 2022/12/30 15:57
"""
import paddle.static
from visualdl import LogWriter
from collections import defaultdict
class Visualizer(object):
""" Wraps a visualdl for tasks use.
"""
def __init__(self,
log_dir,
name=""):
self.writer = LogWriter(logdir=log_dir)
self.name = name
self.tag_step = defaultdict(int)
def update_hparams(self,
args):
"""
Add hyper-parameters information.
Args:
args: Arg parser of task settings.
Returns:
None
"""
args_dict = vars(args)
self.writer.add_hparams(
hparams_dict=args_dict,
metrics_list=['none']
)
def update_scalars(self,
tag_value,
step):
"""
Update multiple tags at one time.
Args:
tag_value: {tag: value} dict for multiple updates
step: update interval instead of absolute steps
Returns:
None
"""
for tag, value in tag_value.items():
self.writer.add_scalar(tag=tag, step=self.tag_step[tag], value=value)
self.tag_step[tag] += step
def update_model(self,
model,
input_shapes):
"""
Add model structure in visualdl.
Args:
model: network to display
input_shapes: shapes of all inputs, list
Returns:
None
"""
input_spec = []
for s in input_shapes:
input_spec.append(paddle.static.InputSpec(shape=s, dtype='int64'))
self.writer.add_graph(
model=model,
input_spec=input_spec
)
def get_name(self):
"""
Get descriptor name of visualizer.
Returns:
instance name
"""
return self.name