继上一篇Segnet分割网络caffe教程(一)对segnet的介绍以及操作步骤的说明,下面讲解对自己数据的一个说明。
对利用segnet进行图像分割需要的数据要求一个是原始图像,二是图像的标记即所说的mask,以我的的一个工程为例说明
(1)图像的处理,我的mask是二值图像因而需要转换,这个我就不说了,只需要把mask转化为0、1两种值
(2)图像和mask的list的建立,这个我的做法是写的一个脚本,当然了也可用MATLAB或者Python等一些各位熟悉的语法进行操作,MATLAB和Python我也都尝试过,可以的,我只是为了方便才用脚本写的,代码如下
#!/usr/bin/env sh
DATA_train=/home/ccf/CCF/Cell_segnet/data/data_train_enhancement/train/image
MASK_train=/home/ccf/CCF/Cell_segnet/data/data_train_enhancement/train/mask
DATA_test=/home/ccf/CCF/Cell_segnet/data/data_train_enhancement/test/image
MASK_test=/home/ccf/CCF/Cell_segnet/data/data_train_enhancement/test/mask
MY=/home/ccf/CCF/Cell_segnet/data/data_train_enhancement
################################################
rm -rf $MY/train.txt
echo "Create train.txt"
find $DATA_train/ -name "*.tif">>$MY/img.txt
find $MASK_train/ -name "*.tif">>$MY/mask.txt
paste -d " " $MY/img.txt $MY/mask.txt>$MY/train.txt
rm -rf $MY/img.txt
rm -rf $MY/mask.txt
##################################################
rm -rf $MY/test.txt
echo "Create test.txt"
find $DATA_test/ -name "*.tif">>$MY/img.txt
find $MASK_test/ -name "*.tif">>$MY/mask.txt
paste -d " " $MY/img.txt $MY/mask.txt>$MY/test.txt
rm -rf $MY/img.txt
rm -rf $MY/mask.txt
大家只需要改一下自己数据的路径就可以生成自己的图片和mask的list
(3)下面就说训练了,在训练的时候我们可以根据自己训练的要求更改分割的类型,segnet对原来是11中类型,在我的工程中我只有两种类型,这就会遇到对网络的修改,同时数据输入的也是一样原来的是360*480,我用的是400*400,网络中的修改根据个人的要求以及效果进行修改,要注意的是上采样upsample这个参数的修改,以及最后的class_weighting,对于class_weighting个数以及参数是根据自己的数据以及要求设定,输出几个类别class_weighting就有几个,对于class_weighting参数的确定是根据训练数据的mask中每一种类型的label确定的,就算方法:(all_label/class)/label,下面是计算的算法代码:
clear;
clc;
Path='/home/ccf/CCF/Cell_segnet/data/data_train_enhancement/train/mask/';
% save_mask_path='/home/ccf/CCF/Cell_segnet/data/mask_change/'
files=dir(Path);
for k=3:length(files)
subpath=[Path,files(k).name];
name=files(k).name;
image=imread(subpath);
I=image;
img=uint8(zeros(400,400))
[x,y]=find(I==0);
for i=1:length(x)
img(x(i),y(i))=0;
end
[x,y]=find(I==1);
for i=1:length(x)
img(x(i),y(i))=1;
end
% imwrite(img,[save_mask_path,name]);
label_num=double(unique(img));
element(:,1)=[0;1];
if (length(element(:,1))==length(label_num))
element(:,1)=label_num;
end
for j=1:length(label_num)
a=label_num(j);
e=length(find(img==a));
element(j,i-1)=e;
end
end
num=element(:,2:end);
sum_num=sum(num,2);
median=sum(sum_num)/length(sum_num);
class_weighting=median./sum_num;
total=[element(:,1),class_weighting];
save('class_weight.mat','total');
(4)接下来就是训练了,训练的脚本如下
/home/ccf/caffe-segnet/build/tools/caffe train -gpu 0 -solver /home/ccf/CCF/Cell_segnet/code/segnet_solver.prototxt -weights /home/ccf/CCF/Cell_segnet/code/VGG_ILSVRC_16_layers.caffemodel 2>&1|tee /home/ccf/CCF/Cell_segnet/code/train_enhancement.txt
(5)测试,与博文Segnet分割网络caffe教程(一)一样用到的几个文件,测试时候的test_weigths.caffemodel的生成
python /home/ccf/CCF/Cell_segnet/code/compute_bn_statistics.py /home/ccf/CCF/Cell_segnet/code/segnet_train.prototxt /home/ccf/CCF/Cell_segnet/models/segnet_iter_40000.caffemodel /home/ccf/CCF/Cell_segnet/code/Inference/
(6)测试结果的显示和保存
python /home/ccf/CCF/Cell_segnet/code/test_segmentation_ccf.py --model /home/ccf/CCF/Cell_segnet/code/segnet_inference.prototxt --weights /home/ccf/CCF/Cell_segnet/code/Inference/test_weights.caffemodel --iter 400
附上我的test_segmentation_ccf.py里面修改的具体细节大家可以自己看。
python /home/ccf/CCF/Cell_segnet/code/test_segmentation_ccf.py --model /home/ccf/CCF/Cell_segnet/code/segnet_inference.prototxt --weights /home/ccf/CCF/Cell_segnet/code/Inference/test_weights.caffemodel --iter 400