阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

caffe的python接口绘制loss和accuracy曲线示例

32次阅读
没有评论

共计 2103 个字符,预计需要花费 6 分钟才能阅读完成。

导读 这篇文章主要为大家介绍了 caffe 的 python 接口绘制 loss 和 accuracy 曲线示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
引言

使用 python 接口来运行 caffe 程序,主要的原因是 python 非常容易可视化。所以不推荐大家在命令行下面运行 python 程序。如果非要在命令行下面运行,还不如直接用 c++ 算了。

推荐使用 jupyter notebook,spyder 等工具来运行 python 代码,这样才和它的可视化完美结合起来。

anaconda 库

因为我是用 anaconda 来安装一系列 python 第三方库的,所以我使用的是 spyder, 与 matlab 界面类似的一款编辑器,在运行过程中,可以查看各变量的值,便于理解,如下图:

caffe 的 python 接口绘制 loss 和 accuracy 曲线示例

只要安装了 anaconda,运行方式也非常方便,直接在终端输入 spyder 命令就可以了。

python 接口实现

在 caffe 的训练过程中,我们如果想知道某个阶段的 loss 值和 accuracy 值,并用图表画出来,用 python 接口就对了。

# -*- coding: utf-8 -*-
"""
Created on Tue Jul 19 16:22:22 2016
@author: root
"""
import matplotlib.pyplot as plt  
import caffe   
caffe.set_device(0)  
caffe.set_mode_gpu()   
# 使用 SGDSolver,即随机梯度下降算法  
solver = caffe.SGDSolver('/home/xxx/mnist/solver.prototxt')  
# 等价于 solver 文件中的 max_iter,即最大解算次数  
niter = 9380 
# 每隔 100 次收集一次数据  
display= 100 
# 每次测试进行 100 次解算,10000/100  
test_iter = 100 
# 每 500 次训练进行一次测试(100 次解算),60000/64  
test_interval =938 
#初始化 
train_loss = zeros(ceil(niter * 1.0 / display))   
test_loss = zeros(ceil(niter * 1.0 / test_interval))  
test_acc = zeros(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 张图片  
    _train_loss += solver.net.blobs['SoftmaxWithLoss1'].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['SoftmaxWithLoss1'].data  
            # 计算 test accuracy  
            _accuracy += solver.test_nets[0].blobs['Accuracy1'].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 * arange(len(train_loss)), train_loss, 'g')  
# test loss -> 黄色  
ax1.plot(test_interval * arange(len(test_loss)), test_loss, 'y')  
# test accuracy -> 红色  
ax2.plot(test_interval * arange(len(test_acc)), test_acc, 'r')  
ax1.set_xlabel('iteration')  
ax1.set_ylabel('loss')  
ax2.set_ylabel('accuracy')  
plt.show()

最后生成的图表在上图中已经显示出来了。

阿里云 2 核 2G 服务器 3M 带宽 61 元 1 年,有高配

腾讯云新客低至 82 元 / 年,老客户 99 元 / 年

代金券:在阿里云专用满减优惠券

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2024-07-25发表,共计2103字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中