caffe 用matlab解析日志畫loss和accuracy
clc;
clear;
% load the log file of caffe model
fid = fopen('log-previous-insulator.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; %loss位置到數據位置起始位置相差7位
indexEnd = size(tline);
str1 = tline(indexStart:indexEnd(2)); %數據開始位置到結束位置,就是loss,也就是縱坐標
indexStart = k1 + 10; %從iteration到迭代次數數據起始位相差10位
indexEnd = strfind(tline, '(') - 1; %找到左括號位置-1:根據你的txt來看是括號還是逗號
str2 = tline(indexStart:indexEnd); %從起始位置到結束位置為迭代次數,也就是橫坐標
res_str1 = strcat(str2, '/', str1); %把橫縱坐標連接起來
lossIter = [lossIter, str2num(str2)]; %把迭代次數轉化為數據賦值給lossiter
lossArray = [lossArray, str2num(str1)]; %把loss轉化為數據復給lossArray
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曲線
print(2,'-dpng','iteration vs loss')%保存
caffe保存訓練log日志文件並利用保存的log文件繪制accuary loss曲線圖
1、訓練模型時保存log日志文件
方法1 一般情況下我們的訓練模型標准語句是:$ sudo ./build/tools/caffe train -solver=xxx/xxx/solver.prototxt xxx/xxx/表示你的solver.prototxt文件所在位置
需要保存log文件時的命令是:$ sudo GLOG_logtostderr=0 GLOG_log_dir='xxx/xxx/xxx/' build/tools/caffe train -solver=xxx/xxx/solver.prototxt ’xxx/xxx/xxx/‘表示你所保存的log文件所在位置。
訓練完成后發現在我們保存的目錄xxx/xxx/xxx/下生成了兩個上鎖log文件caffe.INFO和caffe.ubuntu.root.log.INFO.20170611-103712.5383。點擊打開后我們可以看到我們所訓練的日志文件。
方法2 ./build/tools/caffe train -solver=xn/PENLU/neural/nin/nin_solver.prototxt 2>&1 | tee xn/PENLU/snapshot/nin/nin_relu.log
2、利用生成的log文件繪制accuary loss曲線圖
首先繪制圖,caffe中其實已經自帶了這樣的小工具 caffe-master/tools/extra/parse_log.sh 和caffe-master/tools/extra/extract_seconds.py還有 caffe-master/tools/extra/plot_training_log.py.example;拷貝以上文件到當前訓練模型的目錄下。
然后我們到你保存的log文件目錄下將1中保存的log文件解鎖,解鎖命令:sudo chmod -R 777 ./caffe.ubuntu.root.log.INFO.20170611-103712.5383
解鎖后我們就可以更改該log文件名為xxx.log(注意:要畫圖一定是.log文件,所以不改名不可以畫)。
然后復制該xxx.log文件到你訓練模型所在目錄下。
然后就可以利用命令畫圖了:在模型所在目錄下命令: ./plot_training_log.py.example y xxx.png xxx.log xxx.png是你保存的繪制出的圖片名稱,xxx.log是你保存的log文件名稱。y表示的是你的所繪制的圖片到底是什么圖片,具體解釋如下:
y的數字代表意義(0~7):
Supported chart types: 0: Test accuracy vs. Iters (准確率與迭代次數圖)
1: Test accuracy vs. Seconds (准確率與時間圖)
2: Test loss vs. Iters (測試損失與迭代次數圖)
3: Test loss vs. Seconds (測試損失與時間圖)
4: Train learning rate vs. Iters (學習率與迭代次數圖)
5: Train learning rate vs. Seconds (學習率與時間圖)
6: Train loss vs. Iters (訓練損失與迭代次數圖)
7: Train loss vs. Seconds (訓練損失與時間圖)
運行后生成的文件有:log-data.log.test和log-data.log.test和xxx.png
3、test測試log日志文件保存與繪圖類似過程
Ps: windows記錄訓練日志
caffe中其實已經自帶了這樣的小工具 caffe-master/tools/extra/parse_log.sh caffe-master/tools/extra/extract_seconds.py和 caffe-master/tools/extra/plot_training_log.py.example ,使用方法如下:1.windows記錄訓練日志:在訓練過程中的命令中加入一行參數 ,實現Log日志的記錄,這里我使用的.bat。其實一可以像前面某位大哥一樣,直接copy輸出的類容。
caffe train --solver=deepid/deepid2/deepid_solver.prototxt >log/XXXXX.log 2>&1
pause
