Caffe 的可视化 (五)Caffe 中绘制 accuracy 和 loss 曲线

caffe 中网络训练完后,把accuracy 和 loss 曲线画出来,
以LeNet为例:
可以使用Caffe自带的tools来产生loss和accuracy曲线
(1) 训练时保存log
        mark@ubuntu:~/caffe$ ./build/tools/caffe train --solver=examples/mnist/lenet_solver.prototxt 2>&1 | tee ./log/out.log
(2) 利用caffe中tools/extra文件夹下的parse_log.py来解析日志文件
        mark@ubuntu:~/caffe$ python ./tools/extra/parse_log.py         ./log/out.log            ./log/  
                                         parse_log.py  有两个参数,  第一个参数是日志文件,    第二个参数是结果存放的目录
        运行结束之后会发现在你输入的保存的目录中会生成两个文件out.log.train和out.log.test
(3) 生成曲线
        mark@ubuntu:~/caffe$ python ./tools/extra/plot_training_log.py 2 testloss.png ./log/out.log
 其中数字 2 代表曲线的类型,
 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  
 Notes:
    1. Supporting multiple logs.

    2. Log file name must end with the lower-cased ".log".


testloss.png



也可以通过自己写代码或脚本来画loss曲线:
例如:
首先使用Shell 命令把Log中的迭代次数和loss 的值找出来
mark@ubuntu:~/caffe$ grep Iteration ./log/out.log | grep loss | sed 's/^.*Iteration //' | awk -F'[, ]'  '{print $1, $NF}' > loss.data
或者
mark@ubuntu:~/caffe$ grep Iteration ./log/out.log | grep loss | sed 's/.*Iteration \([0-9]*\).*loss = \([0-9.+-Ee]*\)$/\1 \2/g' > loss.data
然后写个python脚本(使用matplotlib.pyplot )将Loss曲线画出来

import matplotlib.pyplot as plt
x = []
y = []
with open('loss.data') as f:
    for line in f:
        sps = line.split()
        x.append(int(sps[0]))
        y.append(float(sps[1]))
plt.plot(x,y)
plt.show()


下面的方法可以把accuracy 和 loss 统计到一个文件中:
mark@ubuntu:~/caffe$ grep Iteration ./log/out.log | grep Testing | sed 's/.*Iteration \([0-9]*\).*/\1 /g' > ./log/aux0.txt
mark@ubuntu:~/caffe$ grep 'Test net output #0:' ./log/out.log | awk '{print $11 }' | tee ./log/aux1.txt
mark@ubuntu:~/caffe$ grep 'Test net output #1:' ./log/out.log | awk '{print $11 }' | tee ./log/aux2.txt
mark@ubuntu:~/caffe$ paste ./log/aux0.txt ./log/aux1.txt ./log/aux2.txt | tee ./log/aux.txt


import matplotlib.pyplot as plt
x_iter = []
y_accuracy = []
y_loss = []
with open('./log/aux.txt') as f:
    for line in f:
        sps = line.split()
        x_iter.append(int(sps[0]))
        y_loss.append(float(sps[2]))
        y_accuracy.append(float(sps[1]))
#plt.plot(x_iter, y_accuracy, 'r--')
plt.title("loss cuve")
plt.xlabel("iters")
plt.ylabel("loss")
plt.plot(x_iter, y_loss, 'b')
plt.savefig("loss-iter.png")
plt.show()

loss 曲线:


accuracy 曲线:



©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页