matlab 批量提取CNN特征

时间:2022-04-06 03:40:06

无类别,图像混合放置:

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([ ]);
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()=[]; frames()=[];
c_num = length(frames);
gray_num=;error_num_CMYK_JPEG=;
% database.path=[]; for jj = :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 = ; height = ;
im_data = imresize(im_data, [width, height]); % resize using Matlab's imresize
feaSet.iscolor=;
if size(im_data,)==
imdata=zeros([size(im_data),]);
imdata(:,:,)=im_data;
imdata(:,:,)=im_data;
imdata(:,:,)=im_data;
im_data=imdata;
feaSet.iscolor=;
gray_num=gray_num+;
end
im_data = im_data - (mean_data); width = ; height = ;
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+;
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',).get_data();
%vis_square(pdata,,0.5); net.blobs('data').reshape([ ]);
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 = ; % 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 = ;
error_num_CMYK_JPEG=;
for ii = :length(subfolders),
subname = subfolders(ii).name; if ~strcmp(subname, '.') & ~strcmp(subname, '..'),
database.nclass = database.nclass + ; database.cname{database.nclass} = subname; frames = dir(fullfile(rt_img_dir, subname, '*'));
frames()=[];frames()=[];
c_num = length(frames);
database.imnum = database.imnum + c_num;
database.label = [database.label; ones(c_num, )*database.nclass]; siftpath = fullfile(rt_data_dir, subname);
if ~isdir(siftpath),
mkdir(siftpath);
end; for jj = :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 = ; height = ;
im_data = imresize(im_data, [width, height]); % resize using Matlab's imresize
feaSet.iscolor=;
if size(im_data,)==
imdata=zeros([size(im_data),]);
imdata(:,:,)=im_data;
imdata(:,:,)=im_data;
imdata(:,:,)=im_data;
im_data=imdata;
feaSet.iscolor=;
gray_num=gray_num+;
end
feaSet.label= database.nclass; im_data = im_data - (mean_data); width = ; height = ;
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+;
error_CMYK_JPEG{error_num_CMYK_JPEG}=str;
end end;
end;
end;