Caffe——Matlab 读取日志文件的 损失与准确率
读取日志文件的 损失与准确率
如果使用的是dos窗口caffe -train命令训练,那么提取loss和accuracy就需要定向到caffe默认的日志文件去,找的方法很简单

按时间排序,找到最近的以caffe.exe开头的文件名称,用notepad++打开可以看到日志信息:

读文件的方法有很多,我用正则表达式去匹配loss信息:
先将这个记录log的文件拷贝出来,

源码:
%损失率与准确率图
clear;
clc;
close all;
train_log_file = 'caffe.exe.DESKTOP-I092MGR.YSG.log.INFO.20170602-100709.9672' ;
%train_interval = 100 ;
%test_interval = 500 ;
[~, string_output] = dos(['type ' , train_log_file ]) ;
%画训练损失图
pat='#0: loss_attr = .*? (';
o1=regexp(string_output,pat,'start');%用'start'参数指定输出o1为匹配正则表达式的子串的起始位置
o2=regexp(string_output,pat,'end');%用'start'参数指定输出o1为匹配正则表达式的子串的结束位置
o3=regexp(string_output,pat,'match');%用'match'参数指定输出o2为匹配正则表达式的子串
loss=zeros(1,size(o1,2));
for i=1:size(o1,2)
loss(i)=str2num(string_output(o1(i)+15:o2(i)-1));
end
subplot(2,2,1)
plot(loss)
xlabel('训练迭代次数');
ylabel('损失');
title('训练损失图');
%画测试损失图
pat='#1: loss_attr = .*? (';
o1=regexp(string_output,pat,'start');%用'start'参数指定输出o1为匹配正则表达式的子串的起始位置
o2=regexp(string_output,pat,'end');%用'start'参数指定输出o1为匹配正则表达式的子串的结束位置
o3=regexp(string_output,pat,'match');%用'match'参数指定输出o2为匹配正则表达式的子串
loss=zeros(1,size(o1,2));
for i=1:size(o1,2)
loss(i)=str2num(string_output(o1(i)+15:o2(i)-1));
end
subplot(2,2,2)
plot(loss)
xlabel('测试次数');
ylabel('损失');
title('测试损失图');
%画准确率图
pat_accuracy = 'accuracy = .*?\n';
a1 = regexp(string_output,pat_accuracy,'start');
a2 = regexp(string_output,pat_accuracy,'end');
a3 = regexp(string_output,pat_accuracy,'match');
accuracy = zeros(1,size(a1,2));
base = zeros(1,size(a1,2));
for i=1:size(a1,2)
accuracy(i)=str2num(string_output(a1(i)+11:a2(i)));
base(i) = 0.8;
end
subplot(2,2,3)
x = 0:i-1;
plot(x,accuracy,x,base,'--r')
ylim([0,1]);
xlabel('测试次数');
ylabel('准确率');
title('测试准确率图');
结果显示:

浙公网安备 33010602011771号