python 绘制 Caffe 的trainloss testloss testaccuracy 曲线

本文介绍了一个使用Python脚本解析Caffe训练日志文件的方法,并将训练过程中的损失值与测试精度可视化展示。通过该脚本,可以轻松获取训练迭代次数、损失变化及测试准确率等关键信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import numpy as np
import matplotlib.pyplot as plt  
#from urlgrabber.grabber import _file_object_test
from cProfile import label


############################################################
"""
在终端中输入
python C:\MYCaffe\caffe-master\tools\extra\parse_log.py D:\CaffeInfo\D_TrainVal\train_output.log  D:\CaffeInfo\D_TrainVal


得到
train_output.log.test
train_output.log.train
"""
############################################################








def size_text_file (file_object):
    str_file_object = str(file_object)
    s1 = "open file '/"
    s2 = "', mode"


    a=0
    b=0
    i=0
    for ch in str_file_object:
        if str_file_object[i : i+len(s1) - 1] == s1[0:-1] :
            a = i+len(s1) - 1
        elif str_file_object[i : i+len(s2) - 1] == s2[0:-1] : 
            b=i
        i = i + 1
    
    
    #close the object
    file_object.close()
    print("path2text_file/name2text_file: " + str_file_object[a:b] + "\n")
    #open the object
    file_object = open(str_file_object[a:b])
        


     
    count_rows = 0
    count_cols = 0


    line = file_object.readline()


    for ch in line:
        if ch == ',' or ch == '\r' :
            count_cols = count_cols+1
    
    while line:
        line = file_object.readline()
        count_rows = count_rows + 1
    
    #close the object
    file_object.close()
    return [ count_rows, count_cols ]
 
############################################################


def data_text_file(file_object):


    str_file_object = str(file_object)
    s1 = "open file '/"
    s2 = "', mode"


    a=0
    b=0
    i=0
    for ch in str_file_object:
        if str_file_object[i : i+len(s1) - 1] == s1[0:-1] :
            a = i+len(s1) - 1
        elif str_file_object[i : i+len(s2) - 1] == s2[0:-1] : 
            b=i
        i = i + 1
                
    [ count_rows, count_cols ] = size_text_file (file_object)
    file_object.close()
    
    print("path2text_file/name2text_file: " + str_file_object[a:b] + "\n")
    
    file_object = open(str_file_object[a:b])
    
    line = file_object.readline()
    k=0 
    p=-1
    i=0  
    j=0


    data = np.zeros( ( count_rows - 1, count_cols ) )  


    while line: 
        line = file_object.readline()
    
        for ch in line:
            if ch == ',' or ch == '\r' :
                data[i][j] =float ( line[p+1:k] )
                p=k
                j=j+1
            k=k+1
        
        i=i+1
        k=0
        p=-1
        j=0
     
    file_object.close()   
    return data


############################################################
############################################################


file_train_log = open("D:\CaffeInfo\\D_TrainVal\\train_output.log.train")      
file_train_log_data = data_text_file( file_train_log )
print(file_train_log_data)


############################################################


file_test_log = open( "D:\\CaffeInfo\\D_TrainVal\\train_output.log.test" )
file_test_log_data = data_text_file( file_test_log )


############################################################


print '\nplot the train loss and test accuracy\n'    
_,ax1 = plt.subplots()    
ax2 = ax1.twinx()
          
ax1.plot(file_train_log_data[0:, 0], file_train_log_data[0:, 3], 'k', label = 'train loss')
ax1.plot(file_test_log_data[0:, 0], file_test_log_data[0:, 4], 'g', label = 'test loss')    
ax2.plot(file_test_log_data[0:, 0], file_test_log_data[0:, 3], 'r', label = 'test accuracy')     
        
ax1.set_xlabel('iteration')    
ax1.set_ylabel('loss')   
ax2.set_ylabel('accuracy')  




handles1, labels1 = ax1.get_legend_handles_labels()
handles2, labels2 = ax2.get_legend_handles_labels()


ax1.legend(handles1[::-1], labels1[::-1], bbox_to_anchor=(0.74,0.11)) #left right, up down
ax2.legend(handles2[::-1], labels2[::-1], bbox_to_anchor=(0.6,0.8))


plt.show()






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值