Created
          June 8, 2018 02:31 
        
      - 
      
 - 
        
Save yxchng/e46674b00f60e6e8d7696344e2dd2a1c to your computer and use it in GitHub Desktop.  
    save_extracted_features
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | function save_extracted_features() | |
| clear;clc;close all; | |
| cd('../') | |
| %% caffe settings | |
| matCaffe = fullfile(pwd, '../tools/caffe-sphereface/matlab'); | |
| addpath(genpath(matCaffe)); | |
| gpu = 1; | |
| if gpu | |
| gpu_id = 0; | |
| caffe.set_mode_gpu(); | |
| caffe.set_device(gpu_id); | |
| else | |
| caffe.set_mode_cpu(); | |
| end | |
| caffe.reset_all(); | |
| model = '../train/code/sphereface_deploy.prototxt'; | |
| weights = '../train/result/sphereface_model.caffemodel'; | |
| net = caffe.Net(model, weights, 'test'); | |
| %% recursively compute and save features | |
| dirs = genpath('data/vggface2_test-112x96/'); % all folders recursively | |
| dirs = regexp(dirs, pathsep, 'split'); % split into cellstr | |
| dirs = dirs(2:end); | |
| for i = 1:numel(dirs) | |
| tokens = split(dirs{i}, '/'); | |
| personName = string(tokens(end)); | |
| dirpath = 'extracted_features/vggface2_test-mtcnn-112x96/sphereface-20/' + personName; | |
| if ~exist(dirpath, 'dir') | |
| mkdir(dirpath); | |
| end | |
| dirImage = dir([dirs{i} '/*.jpg']); % jpg in one sub-folder | |
| for j = 1:numel(dirImage) | |
| imgPath = [dirs{i} '/' dirImage(j).name]; | |
| feature = extractDeepFeature(imgPath, net); | |
| fileID = fopen('extracted_features/vggface2_test-mtcnn-112x96/sphereface-20/' + personName + '/' + dirImage(j).name + '.txt', 'w'); | |
| for k=1:numel(feature) | |
| if k ~= numel(feature) | |
| fprintf(fileID, '%.15f ', feature(k)); | |
| else | |
| fprintf(fileID, '%.15f', feature(k)); | |
| end | |
| end | |
| fclose(fileID); | |
| end | |
| end | |
| end | |
| function feature = extractDeepFeature(file, net) | |
| img = single(imread(file)); | |
| img = (img - 127.5)/128; | |
| img = permute(img, [2,1,3]); | |
| img = img(:,:,[3,2,1]); | |
| res = net.forward({img}); | |
| res_ = net.forward({flip(img, 1)}); | |
| feature = double([res{1}; res_{1}]); | |
| end | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment