caffe绘制训练过程中的accuracy、loss曲线

21 篇文章 0 订阅

利用caffe中的脚本工具绘制出来的accuracy、loss曲线分别在两张图中,本人通过代码实现了在一张图中同时绘制两条曲线。

本文以mnist的训练为例说明:

1、保存训练模型时的日志文件

修改train_lenet.sh文件,加入“2>&1   | tee examples/mnist/mnist_train_log.log” 保存日志到mnist_train_log.log中

2、提取日志文件训练数据(accuracy、loss)

caffe/tools/extra/parse_log.sh examples/mnist/mnist_train_log.log

脚本运行后会生成两个文件mnist_train_log.log.test和mnist_train_log.log.train

test部分的数据截图如下:

train部分的数据截图如下:

3、解析上述两个文件中的数据并绘制曲线

实现的代码示例如下:

import math
import os
import re
import sys
import matplotlib.pyplot as plt
import numpy as np
import pylab
from mpl_toolkits.axes_grid1 import host_subplot
from pylab import figure, show, legend

train_iterations = []
train_loss = []
test_iterations = []
test_accuracy = []

train_log_path = 'mnist/mnist_train_log.log.train'
test_log_path = 'mnist/mnist_train_log.log.test'
ftrain = open(test_log_path, 'r')
for index, train_line in enumerate(ftrain.readlines()):
    if index == 0:
        continue
    iteration = int(train_line.strip().split(' ')[0])
    accuracy = float(train_line.strip()[18:24])
    test_iterations.append(iteration)
    test_accuracy.append(accuracy)
ftrain.close()

ftest = open(train_log_path, 'r')
for index, test_line in enumerate(ftest.readlines()):
    if index == 0:
        continue
    iteration = int(test_line.strip().split(' ')[0])
    loss = float(test_line.strip()[18:29])
    train_iterations.append(iteration)
    train_loss.append(loss)
ftest.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("loss")
par1.set_ylabel("accuracy")
 
# plot curves
p1, = host.plot(train_iterations, train_loss, 'ob-',label="loss")
p2, = par1.plot(test_iterations, test_accuracy, 'xg-',label="accuracy")
#p2, = par1.plot(test_iterations, test_accuracy, 'xg-',label="accuracy")
 
# set location of the legend, 
# 1->rightup corner, 2->leftup corner, 3->leftdown corner
# 4->rightdown corner, 5->rightmid ...
host.legend(loc=5)
 
# set label color
host.axis["left"].label.set_color(p1.get_color())
par1.axis["right"].label.set_color(p2.get_color())
# set the range of x axis of host and y axis of par1
host.set_xlim([0, 10000])
par1.set_ylim([0., 2.6])
plt.draw()
plt.show()

绘制的路线图如下:

4、caffe中自带工具绘制曲线

      首先将文件夹caffe/tools/extra下的parse_log.sh 、extract_seconds.py、plot_training_log.py.example复制到上一步log日志文件的保存目录下,将plot_training_log.py.example改为plot_training_log.py,并执行以下命令就可以绘制曲线:
python plot_training_log.py 6 train_loss.png mnist_train_log.log

caffe支持多种曲线绘制,指定不同的类型参数即可,具体参数如下:
Notes: 
    1. Supporting multiple logs. 
    2. Log file name must end with the lower-cased ".log". 
Supported chart types: 
    0: Test accuracy  vs. Iters 
    1: Test accuracy  vs. Seconds 
    2: Test loss  vs. Iters 
    3: Test loss  vs. Seconds 
    4: Train learning rate  vs. Iters 
    5: Train learning rate  vs. Seconds 
    6: Train loss  vs. Iters 
    7: Train loss  vs. Seconds

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值