Caffe的可视化训练:绘制loss和accuracy曲线

曾经用caffe自带的提取训练log的脚本以及画图的脚本,发现plot_traning_lo.py老报错。  就改用Spyder直接运行Python 脚本的方式了。


本文参考了 徐其华的blog :http://www.cnblogs.com/denny402/p/5686067.html  ,并修改了相关的错误

1. import import numpy as np, 然后zeros,ceil,arange前面加np.

2.

    _train_loss += solver.net.blobs['SoftmaxWithLoss1'].data    中 'SoftmaxWithLoss1' 改为'loss' ,这是这一层的名字而不是类型。
   同理:Accuracy1 改为  accuracy

3.给出网络solver.prototxt的路径时候,最好用绝对路径不要用相对路径。用~/caffe等相对路径,我这里报出了“Kernel died. Restarting”的错误

下面贴出原始代码:

# -*- coding: utf-8 -*-
"""


@author: root
"""

import matplotlib.pyplot as plt  
import numpy as np
import caffe   
caffe.set_device(0)  
caffe.set_mode_gpu()   
# 使用SGDSolver,即随机梯度下降算法  
#  下面这里用绝对路径。用~/caffe等相对路径,我这里报出了“Kernel died. Restarting”的错误
solver = caffe.SGDSolver('/home/XX/caffe/workplace/XX/cifar10_quick_solver.prototxt')  
  
# 等价于solver文件中的max_iter,即最大解算次数  
niter = 1000 
# 每隔100次收集一次数据  
display= 100  
  
# 每次测试进行100次解算,10000/100  
test_iter = 2  
# 每500次训练进行一次测试(100次解算),60000/64  
test_interval =10
  
#初始化 
train_loss = np.zeros(np.ceil(niter * 1.0 / display))   
test_loss = np.zeros(np.ceil(niter * 1.0 / test_interval))  
test_acc = np.zeros(np.ceil(niter * 1.0 / test_interval))  
  
# iteration 0,不计入  
solver.step(1)  
  
# 辅助变量  
_train_loss = 0; _test_loss = 0; _accuracy = 0  
# 进行解算  
for it in range(niter):  
    # 进行一次解算  
    solver.step(1)  
    # 每迭代一次,训练batch_size张图片  “loss”为名字而不是类型
    _train_loss += solver.net.blobs['loss'].data  
    if it % display == 0:  
        # 计算平均train loss  
        train_loss[it // display] = _train_loss / display  
        _train_loss = 0  
  
    if it % test_interval == 0:  
        for test_it in range(test_iter):  
            # 进行一次测试  
            solver.test_nets[0].forward()  
            # 计算test loss  
            _test_loss += solver.test_nets[0].blobs['loss'].data  
            # 计算test accuracy  
            _accuracy += solver.test_nets[0].blobs['accuracy'].data  
        # 计算平均test loss  
        test_loss[it / test_interval] = _test_loss / test_iter  
        # 计算平均test accuracy  
        test_acc[it / test_interval] = _accuracy / test_iter  
        _test_loss = 0  
        _accuracy = 0  
  
# 绘制train loss、test loss和accuracy曲线  
print '\nplot the train loss and test accuracy\n'  
_, ax1 = plt.subplots()  
ax2 = ax1.twinx()  
  
# train loss -> 绿色  
ax1.plot(display * np.arange(len(train_loss)), train_loss, 'g')  
# test loss -> 黄色  
ax1.plot(test_interval * np.arange(len(test_loss)), test_loss, 'y')  
# test accuracy -> 红色  
ax2.plot(test_interval * np.arange(len(test_acc)), test_acc, 'r')  
  
ax1.set_xlabel('iteration')  
ax1.set_ylabel('loss')  
ax2.set_ylabel('accuracy')  
plt.show()  


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值