这一部分只要讲解怎么把我们的图片数据变成matconvnet可以用来训练的数据格式。主要是套用minst的数据结构。
第一种是把所有的图片放在一个文件夹中,但同一类命名相似,比如第一类:1_1.bmp ,1_2.bmp···第二类:2_1.bmp,2_2.bmp···
srcDir=uigetdir('F:\手掌静脉图像\增强500类未分组\'); %获得选择的文件夹
cd(srcDir);%需要手动点击选择
allnames=struct2cell(dir('*.bmp')); %只处理8位的bmp文件
len=size(allnames,2); %获得bmp文件的个数
Sample_num = 12;%这种处理方法最好每类的样本数量是一致的,比如我这里每类都有12张图片
Num = len/Sample_num;%用文件夹内总的图片数len除以每类的图片数Sample_num得到类数Num
Images = zeros(128,128,len);
Labels = zeros(1,len);
for k=1:len %逐次取出文件
name=allnames{1,k};%这里可以自己显示一下allnames看一下图片的结构
Images(:,:,k)=imread(name);
Labels(1,k) = ceil((k)/Sample_num);%天花板取整,给每类打上标签
end
train_sample_num = 8; %标定每类中的训练样本数量
images = zeros(128,128,train_sample_num * Num);
labels = zeros(1,train_sample_num * Num);
for n = 0 : Num-1
labels(1,(n * train_sample_num+1):(n * train_sample_num +train_sample_num))=Labels(1,(n * Sample_num+1):(n * Sample_num + train_sample_num));
%取出Labels中每类的前train_sample_num个标签赋值给训练集的labels
images(:,:,(n * train_sample_num+1):((n * train_sample_num +train_sample_num))) = Images(:,:,(n * Sample_num+1):(n * Sample_num + train_sample_num));
%同理,取出Images中每类的前train_sample_num个图像赋值给训练集的images
end
save('train_data','images','labels')
val_sample_num = 2; %标定每类中的验证样本数量
images = zeros(128,128,val_sample_num * Num);
labels = zeros(1,val_sample_num * Num);
for n = 0 : Num-1
labels(1,(n * val_sample_num+1):(n * val_sample_num + val_sample_num))=Labels(1,(n * Sample_num+train_sample_num+1):(n * Sample_num + train_sample_num+val_sample_num));
images(:,:,(n * val_sample_num+1):(n * val_sample_num + val_sample_num))=Images(:,:,(n * Sample_num+train_sample_num+1):(n * Sample_num + train_sample_num+val_sample_num));
end
save('val_data','images','labels')
test_sample_num = 4; %标定测试样本
images = zeros(128,128,test_sample_num * Num);
labels = zeros(1,test_sample_num * Num);
for n = 0 : Num-1
labels(1,(n * test_sample_num+1):(n * test_sample_num + test_sample_num))=Labels(1,((n+1) * Sample_num - test_sample_num +1):((n+1) * Sample_num ));
images(:,:,(n * test_sample_num+1):(n * test_sample_num + test_sample_num))=Images(:,:,((n+1) * Sample_num - test_sample_num +1):((n+1) * Sample_num ));
end
save('test_data','images','labels')
%注意读懂在3个for循环中是怎样把Labels和Images分成train、val、test三个部分的
与此种处理方式相承接的主程序写法:
data1=load('val_data.mat');
data2=load('train_data.mat');
data3=load('test_data.mat');
set=[3*ones(1,numel(data1.labels)) 1*ones(1,numel(data2.labels)) 2*ones(1,numel(data3.labels))];
data=single(reshape(cat(3,data1.images,data2.images,data3.images),128,128,1,[]));
dataMean=mean(data(:,:,:,set==1),4);
data=bsxfun(@minus,data,dataMean);
labels=cat(2,data1.labels,data2.labels,data3.labels);
imdb.images.data=data;
imdb.images.data_mean=dataMean;
imdb.images.labels=labels;
imdb.images.set=set;
imdb.meta.sets={'train','val','test'};%??存疑
save( 'imdb.mat', '-struct', 'imdb') ;