caffe的matlab接口实现绘画accuracy和loos:
在实现caffe的matlab接口实现绘画accuracy和loos时,要先保存好caffe的训练日志:
代码如下:
clc;
clear;
% load the log file of caffe model
fid = fopen('./log.txt', 'r');
tline = fgetl(fid);
accuracyIter =[];
accuracyArray =[];
lossIter = [];
lossArray = [];
%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曲线