matlab 批量提取CNN特征

无类别,图像混合放置:

clear
close all


addpath ./matlab

model= './models/bvlc_reference_caffenet/deploy.prototxt';
weights= './models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel';
mean = load('./matlab/+caffe/imagenet/ilsvrc_2012_mean.mat');
net = caffe.Net(model, weights, 'test'); % create net and load weights
mean_data = mean.mean_data;
  
net.blobs('data').reshape([227 227 3 1]);
net.reshape();

rt_img_dir='/ImageNet/ILSVRC2012_img_val/';
rt_data_dir='ImageNet/Fea/ILSVRC2012_img_val/';



%disp('Extracting CNN features...');
        
        frames = dir(fullfile(rt_img_dir, '*'));    frames(1)=[];  frames(1)=[];      
        c_num = length(frames);           
        gray_num=0;error_num_CMYK_JPEG=0;
%        database.path=[];  
        
  for jj = 1:c_num,
            imgpath = fullfile(rt_img_dir, frames(jj).name);
      try                    
      %% prepare the image
        im_data = caffe.io.load_image(imgpath);
        %% subtract mean_data (already in W x H x C, BGR)
        width = 256; height = 256;
        im_data = imresize(im_data, [width, height]); % resize using Matlab's imresize 
        feaSet.iscolor=1;
        if size(im_data,3)==1
         imdata=zeros([size(im_data),3]);
         imdata(:,:,1)=im_data;
         imdata(:,:,2)=im_data;
         imdata(:,:,3)=im_data;
         im_data=imdata;
         feaSet.iscolor=0;
         gray_num=gray_num+1;
        end
         im_data = im_data - (mean_data);   


        width = 227; height = 227;
        im_data = imresize(im_data, [width, height]); % resize using Matlab's imresize
 
        res = net.forward({im_data});
        fc6_data = net.blobs('fc6').get_data();
        fc7_data = net.blobs('fc7').get_data();
        
            
        feaSet.fc6_data = fc6_data;
        feaSet.fc7_data = fc7_data;
         
        [pdir, fname] = fileparts(frames(jj).name);                        
        fpath = fullfile(rt_data_dir, [fname, '.mat']);
            
        save(fpath, 'feaSet');
        %database.path = [database.path; fpath];
      catch
         str= fullfile(frames(jj).name);
         disp(str);
         error_num_CMYK_JPEG=error_num_CMYK_JPEG+1;
         error_CMYK_JPEG{error_num_CMYK_JPEG}=str;
      end
 end;    

    

有类别,不同类图像按不同文件夹放置

 

clear
close all


addpath ./matlab

model= './models/bvlc_reference_caffenet/deploy.prototxt';
weights= './models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel';
mean = load('./matlab/+caffe/imagenet/ilsvrc_2012_mean.mat');
net = caffe.Net(model, weights, 'test'); % create net and load weights
mean_data = mean.mean_data;
%% obtain params in diff layers and show
%pdata = net.params('conv1',1).get_data();
%vis_square(pdata,2,0.5);
  
net.blobs('data').reshape([227 227 3 1]);
net.reshape();

rt_img_dir='mageNet/ILSVRC2012_img_train/';
rt_data_dir='ImageNet/Fea/ILSVRC2012_img_train/';

 
disp('Extracting CNN features...');
subfolders = dir(rt_img_dir);

siftLens = [];

database = [];

database.imnum = 0; % total image number of the database
database.cname = {}; % name of each class
database.label = []; % label of each class
database.path = {}; % contain the pathes for each image of each class
database.nclass = 0;
error_num_CMYK_JPEG=0;
for ii = 1:length(subfolders),
    subname = subfolders(ii).name;
    
    if ~strcmp(subname, '.') & ~strcmp(subname, '..'),
        database.nclass = database.nclass + 1;
        
        database.cname{database.nclass} = subname;
        
        frames = dir(fullfile(rt_img_dir, subname, '*'));
        frames(1)=[];frames(1)=[];
        c_num = length(frames);           
        database.imnum = database.imnum + c_num;
        database.label = [database.label; ones(c_num, 1)*database.nclass];
        
        siftpath = fullfile(rt_data_dir, subname);        
        if ~isdir(siftpath),
            mkdir(siftpath);
        end;
        
        for jj = 1:c_num,
            imgpath = fullfile(rt_img_dir, subname, frames(jj).name);
     try          
            im_data = caffe.io.load_image(imgpath);
        %% subtract mean_data (already in W x H x C, BGR)
        width = 256; height = 256;
        im_data = imresize(im_data, [width, height]); % resize using Matlab's imresize 
        feaSet.iscolor=1;
        if size(im_data,3)==1
         imdata=zeros([size(im_data),3]);
         imdata(:,:,1)=im_data;
         imdata(:,:,2)=im_data;
         imdata(:,:,3)=im_data;
         im_data=imdata;
         feaSet.iscolor=0;
         gray_num=gray_num+1;
        end
        feaSet.label= database.nclass;
        
         im_data = im_data - (mean_data);   


        width = 227; height = 227;
        im_data = imresize(im_data, [width, height]); % resize using Matlab's imresize
 
        res = net.forward({im_data});
        fc6_data = net.blobs('fc6').get_data();
        fc7_data = net.blobs('fc7').get_data();
     
            
        feaSet.fc6_data = fc6_data;
        feaSet.fc7_data = fc7_data;
         
        [pdir, fname] = fileparts(frames(jj).name);                        
        fpath = fullfile(rt_data_dir, subname,  [fname, '.mat']);
            
        save(fpath, 'feaSet');
      catch
         str= fullfile(subname,frames(jj).name);
         disp(str);
         error_num_CMYK_JPEG=error_num_CMYK_JPEG+1;
         error_CMYK_JPEG{error_num_CMYK_JPEG}=str;
      end       

        end;    
    end;
end;
    


 

 

posted on 2016-01-11 20:49  Jason_Fu  阅读(5194)  评论(9编辑  收藏  举报

导航