未验证 提交 de2ec2ea 编写于 作者: F Feiyu Chan 提交者: GitHub

add add_figure for LogWriter (#941)

* add add_figure for LogWriter

* format code

* update date
Co-authored-by: 走神的阿圆's avatarShenYuhan <yhan_shen@163.com>
上级 a794e08f
# Copyright (c) 2021 VisualDL Authors. All Rights Reserve.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =======================================================================
# coding=utf-8
from visualdl import LogWriter
import numpy as np
from matplotlib import pyplot as plt
if __name__ == '__main__':
with LogWriter(logdir="./log/audio_test/train") as writer:
x = np.arange(100)
y = x ** 2 + 1
plt.plot(x, y)
fig = plt.gcf()
writer.add_figure(tag="figure", figure=fig, step=0)
......@@ -9,3 +9,4 @@ protobuf >= 3.11.0
requests
shellcheck-py
six >= 1.14.0
matplotlib
# Copyright (c) 2021 VisualDL Authors. All Rights Reserve.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =======================================================================
def figure_to_image(figures, close=True):
"""Render matplotlib figure to numpy format.
Note that this requires the ``matplotlib`` package.
Args:
figure (matplotlib.pyplot.figure) : figure
close (bool): Flag to automatically close the figure
Returns:
numpy.array: image in [HWC] order
"""
import numpy as np
try:
import matplotlib.pyplot as plt
import matplotlib.backends.backend_agg as plt_backend_agg
except ModuleNotFoundError:
print('please install matplotlib')
def render_to_rgb(figure):
canvas = plt_backend_agg.FigureCanvasAgg(figure)
canvas.draw()
data = np.frombuffer(canvas.buffer_rgba(), dtype=np.uint8)
w, h = figure.canvas.get_width_height()
image_hwc = data.reshape([h, w, 4])[:, :, 0:3]
if close:
plt.close(figure)
return image_hwc
image = render_to_rgb(figures)
return image
......@@ -19,6 +19,7 @@ import numpy as np
from visualdl.writer.record_writer import RecordFileWriter
from visualdl.server.log import logger
from visualdl.utils.img_util import merge_images
from visualdl.utils.figure_util import figure_to_image
from visualdl.component.base_component import scalar, image, embedding, audio, \
histogram, pr_curve, roc_curve, meta_data, text
......@@ -192,6 +193,33 @@ class LogWriter(object):
image(tag=tag, image_array=img, step=step, walltime=walltime,
dataformats=dataformats))
def add_figure(self, tag, figure, step, walltime=None):
"""Add an figure to vdl record file.
Args:
tag (string): Data identifier
figure (matplotlib.figure.Figure): Image represented by a Figure
step (int): Step of image
walltime (int): Wall time of image
dataformats (string): Format of image
Example:
form matplotlib import pyplot as plt
import numpy as np
x = np.arange(100)
y = x ** 2 + 1
plt.plot(x, y)
fig = plt.gcf()
writer.add_figure(tag="lll", figure=fig, step=0)
"""
if '%' in tag:
raise RuntimeError("% can't appear in tag!")
walltime = round(time.time() * 1000) if walltime is None else walltime
img = figure_to_image(figure)
self._get_file_writer().add_record(
image(tag=tag, image_array=img, step=step, walltime=walltime))
def add_text(self, tag, text_string, step=None, walltime=None):
"""Add an text to vdl record file.
Args:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册