基于卷积神经网络的物体分类识别(二)---绘制loss和accuracy曲线

版权声明:学无止境,好好学习 https://blog.csdn.net/m0_38116269/article/details/88362992

接上一篇博客。

五、绘制loss和accuracy曲线

上面介绍的是利用caffe自带的工具绘制,一方面绘制出来的图片太丑了 ,另一方面出来的test loss图片还有问题,那咱们准备上手Python自己弄一下。
参考:https://www.cnblogs.com/denny402/p/5110204.html
这个博客写的不错,需要注意的一点是,blobs里面的名称要改成自己网络结构定义的名称。
训练是出现了点问题:
在这里插入图片描述目测是我估计是数据类型没有给我弄好,估计把精确度给我偷偷转换为int型了?
好像应该不是,难道说,我还要分训练网络和测试网络吗?吃完饭可以回来看看官方demo找找启发看看。
噗。。。这个博客写的不好,咱不参考它了,真的是要参考许多东西,才知道最合适的是哪个啊。
现在参考这个,感觉这个不错啊Caffe—Pycaffe 绘制loss和accuracy曲线
在这里插入图片描述
I have succeeded !!!
part of my code:

part of my code:

import numpy as np
import matplotlib.pyplot as plt
import sys
import os
import re
caffe_root = '/media/xxx/Linux-doc/xxx/caffe/'
sys.path.insert(0, caffe_root + 'python')
import caffe
%matplotlib inline
os.chdir(caffe_root)

caffe.set_device(0)
caffe.set_mode_gpu()
solver = caffe.SGDSolver('models/animal_classifier/solver.prototxt')
#solver = caffe.SGDSolver('examples/mnist/lenet_solver.prototxt')

in_log_path = caffe_root + 'models/animal_classifier/plot_tools/resnet18_train_log.txt'
out_fig_path = caffe_root + 'models/animal_classifier/plot_tools/resne18_record.png'

f = open(in_log_path, 'r')
accuracy = []
train_loss = []
test_loss = []
max_iter = 0
test_iter = 0
test_interval = 0
display = 0
target_str = ['Test net output #0: acc/top-1 = ',
              'Test net output #2: loss = ',
              'Train net output #0: loss = ',
              'max_iter: ', 'test_iter: ', 'test_interval: ', 'display: ']
print "ok1" #only for test the code


while True:
    line = f.readline()  #read a line every time

    if len(line) < 1:
        print "ok3"
        break
    for i in range(len(target_str)):     
        str = target_str[i]
        idx = line.find(str)     
        if idx != -1:
            num=float(line[idx + len(str) : idx + len(str) + 6])
            if i == 0:
                accuracy.append(num)
            elif i == 1:
                test_loss.append(num)
            elif i == 2:
                train_loss.append(num)
            elif i == 3:
                max_iter = int(line[idx + len(str):])
            elif i == 4:
                test_iter = int(line[idx + len(str):])
            elif i == 5:
                test_interval = int(line[idx + len(str):])
            elif i == 6:
                display = int(line[idx + len(str):])
            else:
                pass        
f.close()

ax1 = plt.subplot()
ax2 = ax1.twinx() #shared x axis with each other
ax1.plot(display * np.arange(len(train_loss)), train_loss, color = 'g', label = 'train loss', linestyle = '-', linewidth = 3)
ax1.plot(test_interval * np.arange(len(test_loss)), test_loss, color = 'r', label = 'test loss', linestyle = '-', linewidth = 3)
ax2.plot(test_interval * np.arange(len(accuracy)), accuracy, color = 'b', label = 'accuracy', linestyle = '-', linewidth = 3)
ax1.legend(loc=(0.7,0.8))  #使用二元组(0.7,0.8)定义标签位置
ax2.legend(loc=(0.7,0.7))
ax1.set_xlabel('iteration')
ax1.set_ylabel('loss')
ax2.set_ylabel('accuracy')
plt.savefig(out_fig_path, dpi = 400)
plt.show()

结果如下:
在这里插入图片描述

这个图片真漂亮!

发现了个很好的博客:https://www.cnblogs.com/yinheyi/p/6062488.html
,里面有各种对Python接口用法的解释,挺详细,多看几遍就记住了。

猜你喜欢

转载自blog.csdn.net/m0_38116269/article/details/88362992
今日推荐