-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpixelClassifierTrain.m
executable file
·195 lines (179 loc) · 6.41 KB
/
pixelClassifierTrain.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
function model = pixelClassifierTrain(trainPath,varargin)
% model = pixelClassifierTrain(trainPath,varargin)
% trains a single-layer random forest for image segmentation (pixel classification)
% use the 'pixelClassifier' function to apply the model
% see ...IDAC_git/common/imageSegmentation/PixelClassifier/ReadMe.txt for more details
%
% trainPath
% where images and labes are;
% images are assumed to have the same size;
% every image should have the same number of accompanied label masks,
% labeled <image name>_ClassX.png, where X is the index of the label;
% labels can be created using ImageAnnotationBot:
% https://www.mathworks.com/matlabcentral/fileexchange/64719-imageannotationbot
%
% ----- varargin -----
%
% sigmas, default [1 2 4 8]
% basic image features are simply derivatives (up to second order) in different scales;
% this parameter specifies such scales; details in pcImageFeatures.m
%
% offsets, default []
% in pixels; for offset features (see pcImageFeatures.m)
% set to [] to ignore offset features
%
% osSigma, default 2
% sigma for offset features
%
% radii, default []
% range of radii on which to compute circularity features (see pcImageFeatures.m)
% set to [] to ignore circularity features
%
% cfSigma, default 2
% sigma for circularity features
%
% logSigmas, default []
% sigmas for LoG features (see pcImageFeatures.m)
% set to [] to ignore LoG features
%
% sfSigmas, default []
% steerable filter features sigmas (see pcImageFeatures.m)
% set to [] to ignore steerable filter features
%
% nhoodEntropy, default []
% local entropy filter neighborhood sizes (see pcImageFeatures.m)
% must be an array of odd numbers (to satisfy entropyfilt.m restrictions)
% set to [] to ignore local entropy filter features
%
% nhoodStd, default []
% local standard deviation filter neighborhood sizes (see pcImageFeatures.m)
% must be an array of odd numbers (to satisfy stdfilt.m srestrictions)
% set to [] to ignore local standard deviation features
%
% nTrees, default 20
% number of decision trees in the random forest ensemble
%
% minLeafSize, default 60
% minimum number of observations per tree leaf
%
% pctMaxNPixelsPerLabel, default 1
% percentage of max number of pixels per label (w.r.t. num of pixels in image);
% this puts a cap on the number of training samples and can improve training speed
%
% balanceClasses, default true
% if to balance number of pixels per label
%
% ----- output -----
% model
% structure containing model parameters
%
% ...
%
% Marcelo Cicconet, Dec 11 2017 (release)
%
% Clarence Yapp, Apr 3 2018 (added local entropy and std features)
%
% Marcelo Cicconet, Aug 17 2018 (added contrast adjustment option)
%% parameters
ip = inputParser;
ip.addParameter('sigmas',[1 2 4 8]);
ip.addParameter('offsets',[]);
ip.addParameter('osSigma',2);
ip.addParameter('radii',[]);
ip.addParameter('cfSigma',2);
ip.addParameter('logSigmas',[]);
ip.addParameter('sfSigmas',[]);
ip.addParameter('nhoodEntropy',[]);
ip.addParameter('nhoodStd',[]);
ip.addParameter('edgeSigmas',[]);
ip.addParameter('edgenangs',16);
ip.addParameter('ridgeSigmas',[]);
ip.addParameter('ridgenangs',16);
ip.addParameter('nTrees',20);
ip.addParameter('minLeafSize',60);
ip.addParameter('pctMaxNPixelsPerLabel',1);
ip.addParameter('balanceClasses',true);
ip.addParameter('adjustContrast',true);
ip.addParameter('adjustContrastThr',[]);
% if 'adjustContrast' is true and 'adjustContrastThr' is empty,
% 'adjustContrastThr is computed automatically as 0.1 of the average 99th percentiles of training images;
% at test time, if 'adjustContrast' is true images are only adjusted if prctile(image,99) > 'adjustContrastThr'
ip.parse(varargin{:});
p = ip.Results;
sigmas = p.sigmas;
offsets = p.offsets;
osSigma = p.osSigma;
radii = p.radii;
cfSigma = p.cfSigma;
logSigmas = p.logSigmas;
sfSigmas = p.sfSigmas;
nhoodEntropy = p.nhoodEntropy;
nhoodStd = p.nhoodStd;
edgeSigmas = p.edgeSigmas;
edgenangs = p.edgenangs;
ridgeSigmas = p.ridgeSigmas;
ridgenangs = p.ridgenangs;
nTrees = p.nTrees;
minLeafSize = p.minLeafSize;
pctMaxNPixelsPerLabel = p.pctMaxNPixelsPerLabel;
balanceClasses = p.balanceClasses;
adjustContrast = p.adjustContrast;
adjustContrastThr = p.adjustContrastThr;
%% read images/labels
[imageList,labelList,labels,adjustContrastThr] = pcParseLabelFolder(trainPath,balanceClasses,adjustContrast,adjustContrastThr);
nLabels = length(labels);
%% training samples cap
maxNPixelsPerLabel = (pctMaxNPixelsPerLabel/100)*size(imageList{1},1)*size(imageList{1},2);
nImages = length(imageList);
for imIndex = 1:nImages
L = labelList{imIndex};
for labelIndex = 1:nLabels
LLI = L == labelIndex;
nPixels = sum(sum(LLI));
rI = rand(size(L)) < maxNPixelsPerLabel/nPixels;
L(LLI) = 0;
LLI2 = rI & (LLI > 0);
L(LLI2) = labelIndex;
end
labelList{imIndex} = L;
end
%% construct train matrix
ft = [];
lb = [];
tic
for imIndex = 1:nImages
fprintf('computing features from image %d of %d\n', imIndex, nImages);
% [F,featNames] = pcImageFeatures(imageList{imIndex},sigmas,offsets,osSigma,radii,cfSigma,logSigmas,sfSigmas,[],[],[],[],nhoodEntropy,nhoodStd);
[F,featNames] = pcImageFeatures(imageList{imIndex},sigmas,offsets,osSigma,radii,cfSigma,logSigmas,sfSigmas,ridgeSigmas,ridgenangs,edgeSigmas,edgenangs,nhoodEntropy,nhoodStd);
L = labelList{imIndex};
[rfFeat,rfLbl] = rfFeatAndLab(F,L);
ft = [ft; rfFeat];
lb = [lb; rfLbl];
end
fprintf('time spent computing features: %f s\n', toc);
%% training
fprintf('training...'); tic
[treeBag,featImp,oobPredError] = rfTrain(ft,lb,nTrees,minLeafSize);
figureQSS
subplot(1,2,1), barh(featImp), set(gca,'yticklabel',featNames'), set(gca,'YTick',1:length(featNames)), title('feature importance')
subplot(1,2,2), plot(oobPredError), title('out-of-bag classification error')
fprintf('training time: %f s\n', toc);
%% model
model.treeBag = treeBag;
model.sigmas = sigmas;
model.offsets = offsets;
model.osSigma = osSigma;
model.radii = radii;
model.cfSigma = cfSigma;
model.logSigmas = logSigmas;
model.sfSigmas = sfSigmas;
model.nhoodEntropy = nhoodEntropy;
model.nhoodStd = nhoodStd;
model.ridgeSigmas = ridgeSigmas;
model.ridgenangs = ridgenangs;
model.edgeSigmas = edgeSigmas;
model.edgenangs = edgenangs;
model.adjustContrast = adjustContrast;
model.adjustContrastThr = adjustContrastThr;
disp('done training')
end