caffe学习(9)------利用matlab读取caffe训练日志画训练曲线

1. 首先利用matlab读取log文件里面的内容,获取loss和accuracy并依次保存在txt文件中

%% ================================================================
% get_data.m
% 该函数用于对caffe_window训练的log日志文件获取loss和accuracy数据
% 时间:2017.9.10
%% ================================================================

function get_data(logName,flag)
% Open the log file 
fid = fopen(logName, 'r');

fid_test_accuracy = fopen('output_test_accuracy.txt', flag);
fid_test_loss = fopen('output_test_loss.txt',flag);
%fid_train_accuracy = fopen('output_train_accuracy.txt',flag);
fid_train_loss = fopen('output_train_loss.txt', flag);
blank = ' ';
tline = fgetl(fid);

while ischar(tline)
    % First find the test_accuracy line
    k = strfind(tline, 'Iteration');
    if (k)
       k1 = strfind(tline, 'Testing net');
       if (k1)
           indexStart = k + 10;
           indexEnd = strfind(tline, ',') - 1;
           str = tline(indexStart:indexEnd);  %多少次迭代,测试网络
           fprintf(fid_test_accuracy, '%s%s', str,blank);
           fprintf(fid_test_loss, '%s%s', str,blank);
       end
    end
    k2 = strfind(tline, 'Test net output');
    if (k2)
        k3 = strfind(tline, 'accuracy');
        if (k3)
            % If the string contain test and accuracy at the same time
            % The bias from 'accuracy' to the float number
            indexStart = k3 + 11; 
            indexEnd = size(tline);
            str1 = tline(indexStart : indexEnd(2));
            fprintf(fid_test_accuracy, '%s\r\n', str1);
        end
        %Then find test_loss line
        k4 = strfind(tline, 'loss');
        if (k4)
            indexStart = k4 + 7; 
            k5 = strfind(tline, '(');
            if (k5)
            indexEnd = k5;
            str2 = tline(indexStart : indexEnd - 1);
            fprintf(fid_test_loss, '%s\r\n', str2);
            end
        end
        
        % Get the number of index
%         k = strfind(tline, '#');
%         if (k)
%             indexStart = k + 1;
%             indexEnd = strfind(tline, ':');
%             str2 = tline(indexStart : indexEnd - 1);
%         end
%         
%         % Concatenation of two string
%         res_str = strcat(str2, '/', str);
        
    end
    
    
     
    % Then find the train_accuracy line  
%     k2 = strfind(tline,'Train net output');
%     if (k2)
%         k2 = strfind(tline, 'accuracy');
%         if (k2)
%             indexStart = k2 + 11; 
%             indexEnd = size(tline);
%             str2 = tline(indexStart : indexEnd(2));
%         end
%         fprintf(fid_train_accuracy, '%s\r\n', str2);
%     end 
    
    % Finally, find the train_loss line
    k3 = strfind(tline, 'Iteration');
    if (k3)
       k4 = strfind(tline, 'loss');
       if (k4)
            dindexStart = k4 + 7;
            dindexEnd = size(tline);
            dstr3 = tline(dindexStart:dindexEnd(2));
%            indexStart = k3 + 10;
%            indexEnd = strfind(tline, ',') - 1;
%            str4 = tline(indexStart:indexEnd);
           %res_str1 = strcat(str2, blank, str1);
           indexStart = k3+10;
           indexEnd = strfind(tline, '(') - 1;
           str3 = tline(indexStart:indexEnd);  %多少次迭代,训练网络
           %fprintf(fid_train_accuracy, '%s%s', str3,blank);
           fprintf(fid_train_loss, '%s%s%s\r\n', str3,blank,dstr3);
       end
    end
    
%      k5 = strfind(tline, 'Train net output');
%      if (k5)
%         k6 = strfind(tline, 'accuracy');
%         if (k6)
%             % If the string contain train and accuracy at the same time
%             % The bias from 'accuracy' to the float number
%             indexStart = k6 + 11; 
%             indexEnd = size(tline);
%             str4 = tline(indexStart : indexEnd(2));
%             fprintf(fid_train_accuracy, '%s\r\n', str4);
%         end
%         %Then find test_loss line
%       k7 = strfind(tline, 'loss');
%       if (k7)
%             indexStart = k7 + 7; 
%             k8 = strfind(tline, '(');
%             if (k8)
%             indexEnd = k8;
%             str5 = tline(indexStart : indexEnd - 1);
%             fprintf(fid_train_loss, '%s\r\n', str5);
%             end
%       end
%      end
%       k3 = strfind(tline,'Train net output');
%       if(k3)
%         k3 = strfind(tline,'loss');  
%         if(k3)
%            indexStart = k3 + 7;
%            k4 = strfind(tline, '(');
%             if (k4)
%             indexEnd = k4;
%             str2 = tline(indexStart : indexEnd - 1);
%             fprintf(fid_train_loss, '%s\r\n', str2);
%             end
%         end
%        
%       end
    tline = fgetl(fid);
end

fclose(fid);
fclose(fid_test_accuracy);
fclose(fid_test_loss);
%fclose(fid_train_accuracy);
fclose(fid_train_loss);
%% ===========================================================
% get_lossdata.m
% 该程序用于对caffe_window训练的log日志文件获取loss数据
% 时间:2016.6.5
%% ===========================================================

clc;
clear;

logName = 'log_info_20160605-171239.16428';   
fid = fopen(logName, 'r');
fid_test_loss = fopen('output_test_loss.txt','w');
fid_train_loss = fopen('output_train_loss.txt', 'w');

tline = fgetl(fid);

while ischar(tline)
    %find test_loss line
     k1 = strfind(tline, 'Test loss');
      if (k1)
            indexStart = k1 + 11; 
            indexEnd = size(tline);
            str1 = tline(indexStart : indexEnd(2));
            fprintf(fid_test_loss, '%s\r\n', str1);
      end
      %find train_loss line
      k3 = strfind(tline,'Iteration');
      if(k3)
        k3 = strfind(tline,'loss');  
        if(k3)
           indexStart = k3 + 7;
           indexEnd = size(tline);
           str3 = tline(indexStart:indexEnd(2)); 
           fprintf(fid_train_loss, '%s\r\n',str3);
        end
       
      end
    tline = fgetl(fid);
end
fclose(fid);
fclose(fid_test_loss);
fclose(fid_train_loss);

2. 读取txt文件画训练曲线

%% ===============================================================
% plot_line.m
% 利用get_data.m或者get_lossdata.m函数获得的txt数据,画曲线
% 时间:2017.9.10
%% ================================================================
get_data('./log/log_info_20170910-210455.4740','w');
load output_test_accuracy.txt
load output_test_loss.txt
%load output_train_accuracy.txt
load output_train_loss.txt
Iteration = output_train_loss(:,1);
train_loss = output_train_loss(:,2);
%Iteration1 = output_train_accuracy(:,1);
%train_accuracy = output_train_accuracy(:,2);
Iteration2 = output_test_accuracy(:,1);
test_accuracy = output_test_accuracy(:,2);
Iteration3 = output_test_loss(:,1);
test_loss = output_test_loss(:,2);

figure
plot(Iteration,train_loss,'b');
hold on
plot(Iteration3,test_loss,'g');
xlabel('Iteration');
ylabel('Loss');
legend('Train Loss','Test Loss');
title('Loss VS Iteration');
hold off

figure
% plot(Iteration1,train_accuracy,'b');
% hold on
plot(Iteration2,test_accuracy,'r');
xlabel('Iteration');
ylabel('Accuracy');
legend('Test Accuracy');
title('Accuracy VS Iteration');
hold off

% % axis([0 100 0 10])
% xlabel('Iteration');
% ylabel('Accuracy');
% legend('Train Accuracy','Test Accuracy');
% title('Accuracy VS Iteration');
% hold off
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值