-
Notifications
You must be signed in to change notification settings - Fork 0
/
vis.py
executable file
·259 lines (198 loc) · 7.39 KB
/
vis.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import os
import sys
import shutil
import numpy as np
import pandas as pd
import shutil
import matplotlib
matplotlib.use("Agg")
from yattag import Doc
from yattag import indent
import matplotlib.pyplot as plt
##################################################
class Plotter(object):
'''
class Plotter
将训练过程中的数值化输出保存成html以及csv文件进行记录
用法:
1. 初始化:
plotter = Plotter()
2. 记录:
plotter.scalar('loss1', step=10, value=0.1)
3. 输出到html文件:
plotter.to_html_report('./experiment/plt')
4. 输出所有数据分别到单独的csv文件中:
plotter.to_csv('./experiment/plt')
'''
def __init__(self, args=None):
self._scalar_data_frame_dict = {}
self._upper_bound = {}
self._lower_bound = {}
self.args = args
def _check_dir(self, path):
p = os.path.dirname(path)
if not os.path.exists(p):
os.mkdir(p)
return p
def set_min_max(self, name, min_val=None, max_val=None):
if min_val is not None:
self._lower_bound[name] = min_val
if max_val is not None:
self._upper_bound[name] = max_val
@property
def scalar_names(self):
return list(self._scalar_data_frame_dict.keys())
def get(self, name):
if name in self._scalar_data_frame_dict:
return self._scalar_data_frame_dict[name]
else:
return None
def has(self, name):
return name in self._scalar_data_frame_dict
def keys(self):
return list(self._scalar_data_frame_dict.keys())
def scalar(self, name, step, value, epoch=None):
if isinstance(value, dict):
data = value.copy()
data.update({
'step' : step
})
else:
data = {
'step' : step,
name : value,
}
if epoch is not None:
data['epoch'] = epoch
df = pd.DataFrame(data, index=[0])
if name not in self._scalar_data_frame_dict:
self._scalar_data_frame_dict[name] = df
else:
self._scalar_data_frame_dict[name] = self._scalar_data_frame_dict[name].append(df, ignore_index=True)
def scalar_probs(self, name, step, value_array, epoch=None):
data_dict = {}
for i in range(1, 10):
thres = float(i) / 10.0
data_dict[str(thres)] = float((value_array < thres).sum()) / float(len(value_array))
self.scalar(name, step, data_dict, epoch=epoch)
self.set_min_max(name, 0.0, 1.0)
def to_csv(self, output_dir):
" 将记录保存到多个csv文件里面,csv文件放在output_dir下面。"
if not os.path.exists(output_dir):
os.mkdir(output_dir)
for name, data_frame in self._scalar_data_frame_dict.items():
csv_filepath = os.path.join(output_dir, 'scalar_'+name+'.csv')
data_frame.to_csv(csv_filepath, index=False)
def from_csv(self, output_dir):
" 从output_dir下面的csv文件里面读取并恢复记录 "
csv_name_list = [fn[:-4] for fn in os.listdir(output_dir) if fn.endswith('csv')]
for name in csv_name_list:
if name.startswith('scalar_'):
in_csv = pd.read_csv(os.path.join(output_dir, name+'.csv'))
self._scalar_data_frame_dict[name[len('scalar_'):]] = in_csv
def to_zip(self, output_path):
# base_path, file_name = os.path.split(output_path)
assert output_path.endswith(".zip")
if not os.path.exists(output_path[:-4]):
os.mkdir(output_path[:-4])
self.to_csv(output_path[:-4])
shutil.make_archive(output_path, 'zip', output_path[:-4])
shutil.rmtree(output_path[:-4])
def write_svg_all(self, output_dir):
" 将所有记录绘制成svg图片 "
for ind, (name, data_frame) in enumerate(self._scalar_data_frame_dict.items()):
min_val = self._lower_bound.get(name, None)
max_val = self._upper_bound.get(name, None)
output_svg_filepath = os.path.join(output_dir, name+'.svg')
plt.figure()
plt.clf()
headers = [hd for hd in data_frame.columns if hd not in ['step', 'epoch']]
if len(headers) == 1:
plt.plot(data_frame['step'], data_frame[name])
else:
for hd in headers:
plt.plot(data_frame['step'], data_frame[hd])
plt.legend(headers)
if min_val is not None:
plt.ylim(bottom=min_val)
if max_val is not None:
plt.ylim(top=max_val)
plt.grid(axis='y')
plt.grid(axis='x', which='major')
plt.grid(axis='x', which='minor', color=[0.9, 0.9, 0.9])
plt.tight_layout()
plt.savefig(output_svg_filepath)
plt.close()
def to_html_report(self, output_filepath):
" 将所有记录整理成一个html报告 "
self.write_svg_all(self._check_dir(output_filepath))
doc, tag, text = Doc().tagtext()
with open(output_filepath, 'w') as outfile:
with tag('html'):
with tag('body'):
title_idx = 1
if self.args is not None:
with tag('h3'):
text('{}. args'.format(title_idx))
title_idx += 1
key_list = list(self.args.keys())
key_list = sorted(key_list)
with tag('div', style='display:inline-block;width:850px;padding:5px;margin-left:20px'):
for key in key_list:
with tag('div', style='display:inline-block;width:400px;'):
text('{} : {}\n'.format(key, self.args[key]))
with tag('h3'):
text('{}. scalars'.format(title_idx))
title_idx += 1
data_frame_name_list = [n for n, d in self._scalar_data_frame_dict.items()]
data_frame_name_list = sorted(data_frame_name_list)
for ind, name in enumerate(data_frame_name_list):
with tag('div', style='display:inline-block'):
with tag('h4', style='margin-left:20px'):
text('(%d). '%(ind+1)+name)
doc.stag("embed", style="width:800px;padding:5px;margin-left:20px", src=name+'.svg', type="image/svg+xml")
result = indent(doc.getvalue())
outfile.write(result)
def img_vertical_concat(images, pad=0, pad_value=255, pad_right=False):
nb_images = len(images)
h_list = [i.shape[0] for i in images]
w_list = [w.shape[1] for w in images]
if pad_right:
max_w = np.max(w_list)
images = [i if i.shape[1] == max_w else np.hstack([i, np.ones([i.shape[0], max_w-i.shape[1]]+list(i.shape[2:]), dtype=i.dtype)*pad_value])
for i in images]
else:
assert np.all(np.equal(w_list, w_list[0]))
if pad != 0:
images = [np.vstack([i, np.ones([pad,]+list(i.shape[1:]), dtype=i.dtype)*pad_value]) for i in images[:-1]] + [images[-1],]
if not isinstance(images, list):
images = [i for i in images]
return np.vstack(images)
def img_horizontal_concat(images, pad=0, pad_value=255, pad_bottom=False):
nb_images = len(images)
h_list = [i.shape[0] for i in images]
w_list = [w.shape[1] for w in images]
if pad_bottom:
max_h = np.max(h_list)
images = [i if i.shape[0] == max_h else np.vstack([i, np.ones([max_h-i.shape[0]]+list(i.shape[1:]), dtype=i.dtype)*pad_value])
for i in images]
else:
assert np.all(np.equal(h_list, h_list[0]))
if pad != 0:
images = [np.hstack([i, np.ones([i.shape[0], pad,]+list(i.shape[2:]), dtype=i.dtype)*pad_value]) for i in images[:-1]] + [images[-1],]
if not isinstance(images, list):
images = [i for i in images]
return np.hstack(images)
def img_grid(images, nb_images_per_row=10, pad=0, pad_value=255):
ret = []
while len(images) >= nb_images_per_row:
ret.append(img_horizontal_concat(images[0:nb_images_per_row], pad_bottom=True, pad=pad, pad_value=pad_value))
images = images[nb_images_per_row:]
if len(images) != 0:
ret.append(img_horizontal_concat(images, pad_bottom=True, pad=pad, pad_value=pad_value))
return img_vertical_concat(ret, pad=pad, pad_right=True, pad_value=pad_value)
def zoom_img(img, times=5):
h, w, c = img.shape
img = img.reshape([h, 1, w, 1, c])
img = np.tile(img, (1, times, 1, times, 1))
return img.reshape([h*times, w*times, c])