1. 程式人生 > >使用matplotlib和tensorboardx記錄pytorch的訓練過程

使用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()

結果:在這裡插入圖片描述