用faster rcnn对自己的数据集训练,得到训练模型的同时,训练日志也会自动保存在./experiments/logs文件夹下。要想知道整个训练过程中训练损失的变化,可通过画Loss曲线图观察,并以此调整学习率、迭代次数等参数。具体代码如下:
#!/usr/bin/env python
import os
import sys
import numpy as np
import matplotlib.pyplot as plt
import math
import re
import pylab
from pylab import figure, show, legend
from mpl_toolkits.axes_grid1 import host_subplot
fp = open('E:/fastrcnn/py-faster-rcnn/experiments/logs/faster_rcnn_end2end_VGG16_.txt.2019-07-14_11-25-04', 'r')
# 改成自己的日志文件地址
train_iterations = []
train_loss = []
test_iterations = []
#test_accuracy = []
for ln in fp:
if '] Iteration ' in ln and 'loss = ' in ln:
arr = re.findall(r'ion \b\d+\b,',ln)
train_iterations.append(int(arr[0].strip(',')[4:]))
train_loss.append(float(ln.strip().split(' = ')[-1]))
fp.close()
host = host_subplot(111)
plt.subplots_adjust(right=0.8) # ajust the right boundary of the plot window
#par1 = host.twinx()
# set labels
host.set_xlabel("iterations")
host.set_ylabel("RPN loss")
#par1.set_ylabel("validation accuracy")
# plot curves
p1, = host.plot(train_iterations, train_loss, label="train RPN loss")
host.legend(loc=1)
# set label color
host.axis["left"].label.set_color(p1.get_color())
host.set_xlim([-1000, 150000])
host.set_ylim([0., 3.5])
plt.draw()
plt.show()