1. 首先导入一些python画图的包,读取txt文件,假设我现在有两个模型训练结果的records.txt文件
import numpy as np
import matplotlib.pyplot as plt
import pylab as pl
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
data1_loss =np.loadtxt("valid_RCSCA_records.txt")
data2_loss = np.loadtxt("valid_SCRCA_records.txt")
2. 我自己的数据第一列是训练步数,第二列的loss,所以取出相应列的数据作为绘图的x和y
x = data1_loss[:,0]
y = data1_loss[:,1]
x1 = data2_loss[:,0]
y1 = data2_loss[:,1]
3. 先创建一幅图,再在这幅图上添加一个小图,小图用来显示部分放大的曲线
fig = plt.figure(figsize = (7,5))
ax1 = fig.add_subplot(1, 1, 1)
4. 先画出整体的loss曲线
pl.plot(x,y,'g-',label=u'Dense_Unet(block layer=5)')`
p2 = pl.plot(x1, y1,'r-', label = u'RCSCA_Net')
pl.legend()
p3 = pl.plot(x2,y2, 'b-', label = u'SCRCA_Net')
pl.legend()
pl.xlabel(u'iters')
pl.ylabel(u'loss')
plt.title('Compare loss for different models in training')
画出曲线图:
5. 显示放大的部分曲线
tx0 = 0
tx1 = 10000
ty0 = 0.000
ty1 = 0.12
sx = [tx0,tx1,tx1,tx0,tx0]
sy = [ty0,ty0,ty1,ty1,ty0]
pl.plot(sx,sy,"purple")
axins = inset_axes(ax1, width=1.5, height=1.5, loc='right')
axins.plot(x1,y1 , color='red', ls='-')
axins.plot(x2,y2 , color='blue', ls='-')
axins.axis([0,20000,0.000,0.12])
plt.savefig("train_results_loss.png")
pl.show
转载自https://www.jb51.net/article/164018.htm