• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

matlab批量提取CNN特征

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

无类别,图像混合放置:

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;
    


 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Delphi基础-数据类型发布时间:2022-07-18
下一篇:
Delphi的RzDbgrid改变某行的背景色发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap