HAT_dataset compute accuracy for caffe_deep learning approach of matlab interface--->>> code record
2016-01-16 20:07
525 查看
<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'); end if 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(); end model_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 network net = 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(); end disp('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.55 for 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 end end for 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>
相关文章推荐
- Android Studio中使用JNI调用OpenCV本地代码Canny 边缘检测
- 自定义View实现微信主页渐变效果
- 向每一个错误致敬——Android HotFix
- Object-C--->Swift之(五)函数入门级讲解
- Android调试优化篇
- android 进度条ProgressBar
- Android 记住密码和自动登录界面的实现(SharedPreferences 的用法)
- 《第一行代码Android》学习日记19——基于位置的服务
- ios之TableViewCell重用机制避免反复显示问题
- windows7安装Androidstudio
- Android Studio中通过jni实现对C/C++代码的调用,HelloJni,Ubuntu
- 50.EasyGank妹纸App
- Android Studio NDK开发环境搭建及Demo示例
- ViewPager+Handler实现图片自动轮播的效果
- Android CardView使用详解
- 开发手机应用(微信公众号)
- Android开发主页框架搭建
- iOS(五)基于XMPP协议的聊天App:二
- ios 常见错误整理 持续更新
- android调用系统相机拍摄获取照片的两种方法