caffe 日志保存以及matlab绘制方法,windows以及ubuntu下

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')%保存

  

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