1、问题描述
在caffe中微调自己的模型时,将日志文件保存为log.txt,再对其进行解析,绘制accuracy及loss曲线;
2、matlab实现
clc;
clear;
% load the log file of caffe model
fid = fopen('log.txt', 'r');
tline = fgetl(fid);
%get arrays to draw figures
accuracyIter = [0];%accuracy横坐标
accuracyArray = [0];%accuracy纵坐标
lossIter = [0];%loss横坐标
lossArray = [0];%loss纵坐标
%record the last line
lastLine = '';
%read line
while ischar(tline)
%%%%%%%%%%%%%% the accuracy line %%%%%%%%%%%%%%
k = strfind(tline, 'Test net output');
if (k)
k = strfind(tline, 'accuracy');
if (k)
% If the string contain test and accuracy at the same time
% The bias from 'accuracy' to the float number
indexStart = k + 11;
indexEnd = size(tline);
str = tline(indexStart : indexEnd(2));
accuracyArray = [accuracyArray, str2num(str)];
end
% Get the number of index
k = strfind(lastLine, 'Iteration');
if (k)
indexStart = k + 10;
indexEnd = strfind(lastLine, ',');
str2 = lastLine(indexStart : indexEnd - 1);
accuracyIter = [accuracyIter, str2num(str2)];
end
% Concatenation of two string
res_str = strcat(str2, '/', str);
end
%%%%%%%%%%%%%% the loss line %%%%%%%%%%%%%%
k1 = strfind(tline, 'Iteration');
if (k1)
k2 = strfind(tline, 'loss');
if (k2)
indexStart = k2 + 7;
indexEnd = size(tline);
str1 = tline(indexStart:indexEnd(2));
indexStart = k1 + 10;
indexEnd = strfind(tline, ',') - 1;
str2 = tline(indexStart:indexEnd);
res_str1 = strcat(str2, '/', str1);
lossIter = [lossIter, str2num(str2)];
lossArray = [lossArray, str2num(str1)];
end
end
lastLine = tline;
tline = fgetl(fid);
end
%draw figure
figure;h1 = plot(accuracyIter, accuracyArray);title('iteration vs accurancy');%绘制accuracy曲线
figure;h2 = plot(lossIter, lossArray);title('iteration vs loss');%绘制loss曲线