HAT_dataset compute accuracy for caffe_deep learning approach of matlab interface--->>> code record

来源:互联网 发布:php能实现js定时器 编辑:程序博客网 时间:2024/05/20 06:39
<span style="font-family:SimHei;font-size:24px;">%%clc;  close all;  clear all;use_gpu=1; % GPU 模式if exist('../+caffe', 'dir')    addpath('..');else    error('Please run this demo from caffe/matlab/demo');endif exist('use_gpu', 'var') && use_gpu    caffe.set_mode_gpu();    gpu_id = 0;  % we will use the first gpu in this demo    caffe.set_device(gpu_id);else    caffe.set_mode_cpu();endmodel_dir = '../../models/bvlc_alexnet/';net_model = [model_dir 'train_val.prototxt'];% net_model = [model_dir '__deploy.prototxt'];net_weights = [model_dir 'caffe_alexnet_train_iter_310000.caffemodel'];% net_weights = [model_dir 'bvlc_alexnet.caffemodel'];phase = 'train';if ~exist(net_weights, 'file')    error('Please download CaffeNet from Model Zoo before you run this demo');end% Initialize a networknet = caffe.Net(net_model, net_weights, phase);fprintf('Extract Predict Scores ...\n');% dataPath = '../../Link to test_code/traindataset/';dataPath = '../../fine_tuning_data/HAT_fineTuning_data/test_data/';images = dir([dataPath,'*.png']);for num=1:length(images)    %     disp('read the', num2str(num) ,'th', '/', num2str(num1) ,'image, please waiting ...');    im = imread([dataPath, images(num).name]);    %     fid1 = fopen('train_ids.txt', 'a');    %     fprintf(fid1, '%s \n', images(num).name );    %     fclose(fid1);    train_image = im;    train_image = imresize(train_image, [320, 240]);    %     imshow(train_image);    image_name = images(num).name;    id = strtok(image_name,'_');    id = str2double(id);    im = train_image;    % prepare oversampled input    % input_data is Height x Width x Channel x Num    %     tic;    input_data = {prepare_image(im)};    %     toc;    % do forward pass to get scores  前向传播    % scores are now Channels x Num, where Channels == 1000    %     tic;    % The net forward function. It takes in a cell array of N-D arrays    % (where N == 4 here) containing data of input blob(s) and outputs a cell    % array containing data from output blob(s)    savePath = '/home/d302/wangxiao/caffe-master/wangxiao/bvlc_alexnet/';    scores = net.forward(input_data);    Score = scores{1,1};    tmp = zeros([27, 1]);    matrix = zeros([1900, 27]);    for i = 1:size(Score, 1)       all_vector = Score(i, :);       mean_vector = mean(all_vector);       tmp(i, 1) = mean_vector;    end    tmp = tmp';    fid = fopen([savePath, 'predict_score.txt'], 'a');    fprintf(fid, '%f ', tmp);    fprintf(fid, '\n');    fclose(fid);    %     toc;    %     scores = scores{1};    %     scores = mean(scores, 2);  % take average scores over 10 crops    %     [~, maxlabel] = max(scores);    %     call caffe.reset_all() to reset caffe    %     caffe.reset_all();enddisp('extract predict score, done !');predict_score = importdata([savePath, 'predict_score.txt']);gt_data = '/home/d302/wangxiao/caffe-master/fine_tuning_data/HAT_fineTuning_data/test_data_fineTuning.txt';gt_score = importdata(gt_data);tmp_predict_score = predict_score ;% for threshold = 0.4:0.05:0.55for threshold = 0.5    for i = 1:size(tmp_predict_score, 1)        for j = 1:size(tmp_predict_score, 2)                        if (tmp_predict_score(i, j) >threshold)                tmp_predict_score(i, j) = 1;            else                tmp_predict_score(i, j) = 0;            end        end    endendfor j = 1:size(gt_score.data, 2)  % column        gt_vector = gt_score.data(:, j);    predict_vector = tmp_predict_score(:, j);        TP = 0;   FP = 0;   TN = 0;   FN = 0;        for i = 1:size(gt_score.data, 1)        true_label = gt_vector(i);        predict_label = predict_vector(i);                if (predict_label==1) && (true_label==1)            TP =TP+1;        end        if (predict_label==1) && (true_label==0)            FP = FP+1;        end        if (predict_label==0) && (true_label==0)            TN = TN+1;        end        if (predict_label==0) && (true_label==1)            FN = FN+1;        end            end        all_positive = TP + FP;    all_negative = TN + FN;    accuracy = 0.5*(TP/all_positive + TN/all_negative);    disp([num2str(accuracy)]);    end</span>

0 0
原创粉丝点击