使用matplotlib和tensorboardx记录pytorch的训练过程

使用matplotlib和tensorboardx记录pytorch的训练过程

目的: 利用tensorboardx对pytorch的训练过程进行可视化,主要是可视化loss和image

  1. 修改matplotlib,使matplotlib支持中文显示,因为网络训练中有中文的label,如中文的OCR识别。
    参考:修改matplotlib使其支持中文

  2. 向python程序中向tensorboardx中添加loss和image等想可视化的变量。
    参考: tensorboardx-github

  3. 例子:向tensorboardx中添加一个figure

import matplotlib.pyplot as plt
from PIL import Image
plt.rcParams['font.family'] = ['Microsoft YaHei']       # 为了显示中文
plt.rcParams['axes.unicode_minus'] = False

plt.figure()

plt.subplot(1,2,1)
img = Image.open('./imgs/img_1011.jpg')     # 打开图像
plt.imshow(img)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.xlabel('王苹朱')                         # 显示label

ax2 = plt.subplot(1,2,2)
img2 = Image.open('./imgs/img_1012.jpg')
ax2.imshow(img2)

fig2 = plt.gcf()              # 获取当前的figure
plt.show()

from tensorboardX import SummaryWriter
writer = SummaryWriter()
writer.add_figure('matplotlib', fig2, 2)
writer.close()

结果:在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/u011622208/article/details/84449607