Repository navigation
Expand file tree
/
Copy pathitkRandomForest.txx
More file actions
341 lines (267 loc) · 11.7 KB
/
Copy pathitkRandomForest.txx
File metadata and controls
341 lines (267 loc) · 11.7 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#ifndef itkRandomForest_txx
#define itkRandomForest_txx
#include "RFLibrary/RandomForestClassifyImageFilter.h"
#include "itkObjectFactory.h"
#include "itkImageRegionConstIterator.h"
#include <math.h>
#include "IRISSlicer/IRISSlicer.h"
#include <algorithm>
namespace itk
{
template< class ImageScalarType, class ImageVectorType, class TLabelImage>
RandomForest<ImageScalarType, ImageVectorType, TLabelImage>
::RandomForest()
{
this->SetNumberOfRequiredInputs(2);
}
template< class ImageScalarType, class ImageVectorType, class TLabelImage>
void
RandomForest<ImageScalarType, ImageVectorType, TLabelImage>
::AddScalarImage(ImageScalarType *image)
{
this->SetNthInput(0, const_cast<ImageScalarType*>(image));
//this->AddInput(image);
}
template< class ImageScalarType, class ImageVectorType, class TLabelImage>
void
RandomForest<ImageScalarType, ImageVectorType, TLabelImage>
::AddVectorImage(ImageVectorType *image)
{
this->SetNthInput(0, const_cast<ImageVectorType*>(image));
//this->AddInput(image);
}
template< class ImageScalarType, class ImageVectorType, class TLabelImage>
void
RandomForest<ImageScalarType, ImageVectorType, TLabelImage>
::SetLabelMap(const TLabelImage* mask)
{
this->SetNthInput(1, const_cast<TLabelImage*>(mask));
}
template< class ImageScalarType, class ImageVectorType, class TLabelImage>
typename TLabelImage::Pointer RandomForest<ImageScalarType, ImageVectorType, TLabelImage>
::GetLabelMap()
{
return static_cast< TLabelImage * >
( this->ProcessObject::GetInput(1) );
}
template< class ImageScalarType, class ImageVectorType, class TLabelImage>
void
RandomForest<ImageScalarType, ImageVectorType, TLabelImage>
::GenerateData()
{
DataObject *intensity_obj = this->ProcessObject::GetInput(0);
typename TLabelImage::Pointer label_image = this->GetLabelMap();
// Setup output 1
ProbabilityType::Pointer output = this->GetOutput();
output->SetBufferedRegion(output->GetRequestedRegion());
output->Allocate();
/* std::string rf_file = "myforest.rf";
const char * train_file = rf_file.c_str()*/;
const int VDim = 3;
RFParameters<TPixel, VDim> param;
// Get the segmentation image - which determines the samples
typedef itk::ImageRegionConstIteratorWithIndex<TLabelImage> LabelIter;
// Shrink the buffered region by radius because we can't handle BCs
itk::ImageRegion<VDim> reg = label_image->GetBufferedRegion();
reg.ShrinkByRadius(param.patch_radius);
// We need to iterate throught the label image once to determine the
// number of samples to allocate.
unsigned long nSamples = 0;
for(LabelIter lit(label_image, reg); !lit.IsAtEnd(); ++lit)
if( (int) (0.5 + lit.Value()) > 0)
nSamples++;
// Iterator for grouping images into a multi-component image
typedef ImageCollectionConstRegionIteratorWithIndex<
ImageScalarType,
ImageVectorType> CollectionIter;
// Create an iterator for going over all the anatomical image data
CollectionIter cit(reg);
param.patch_radius.Fill(2); // Use a neighborhood patch for features
cit.SetRadius(param.patch_radius);
cit.AddImage(intensity_obj);
// Get the number of components
int nComp = cit.GetTotalComponents();
int nPatch = cit.GetNeighborhoodSize();
int nColumns = nComp * nPatch;
// Are we using coordinate informtion
if(param.use_coordinate_features)
nColumns += VDim;
// Create a new sample
typedef MLData<TPixel, TPixel> SampleType;
SampleType *sample = new SampleType(nSamples, nColumns);
// Now fill out the samples
int iSample = 0;
for(LabelIter lit(label_image, reg); !lit.IsAtEnd(); ++lit, ++cit)
{
int label = (int) (lit.Value() + 0.5);
if(label > 0)
{
// Fill in the data
std::vector<TPixel> &column = sample->data[iSample];
int k = 0;
for(int i = 0; i < nComp; i++)
for(int j = 0; j < nPatch; j++)
column[k++] = cit.NeighborValue(i,j);
// Add the coordinate features if used
if(param.use_coordinate_features)
for(int d = 0; d < VDim; d++)
column[k++] = lit.GetIndex()[d];
// Fill in the label
sample->label[iSample] = label;
++iSample;
}
}
// Check that the sample has at least two distinct labels
bool isValidSample = false;
for(int iSample = 1; iSample < sample->Size(); iSample++)
if(sample->label[iSample] != sample->label[iSample-1])
{ isValidSample = true; break; }
// Set up the classifier parameters
TrainingParameters params;
// TODO:
params.treeDepth = param.tree_depth;
params.treeNum = param.forest_size;
params.candidateNodeClassifierNum = 10;
params.candidateClassifierThresholdNum = 10;
params.subSamplePercent = 0;
params.splitIG = 0.1;
params.leafEntropy = 0.05;
params.verbose = true;
// Cap the number of training voxels at some reasonable number
if(sample->Size() > 10000)
params.subSamplePercent = 100 * 10000.0 / sample->Size();
else
params.subSamplePercent = 0;
// Create the classification engine
typedef typename RFClassifierType::RFAxisClassifierType RFAxisClassifierType;
typedef Classification<TPixel, TPixel, RFAxisClassifierType> ClassificationType;
typename RFClassifierType::Pointer classifier = RFClassifierType::New();
ClassificationType classification;
// Perform classifier training
classification.Learning(
params, *sample,
*classifier->GetForest(),
classifier->GetValidLabel(),
classifier->GetClassToLabelMapping());
// Reset the class weights to the number of classes and assign default
int n_classes = classifier->GetClassToLabelMapping().size(), n_fore = 0, n_back = 0;
classifier->GetClassWeights().resize(n_classes, -1.0);
// Store the patch radius in the classifier - this remains fixed until
// training is repeated
classifier->SetPatchRadius(param.patch_radius);
classifier->SetUseCoordinateFeatures(param.use_coordinate_features);
// Dump the classifier to a file
// std::ofstream out_file(train_file);
// classifier->Write(out_file);
// out_file.close();
/** Apply classifier */
// Apply bounding box and input cropped image into the random forest classifier
typename TLabelImage::SizeType bbox_size = m_boundingbox.GetSize();
// Set up requested region
typename TLabelImage::SizeType regionSize;
regionSize[0] = 1;
regionSize[1] = bbox_size[1];
regionSize[2] = bbox_size[2];
typename TLabelImage::IndexType bbox_index = m_boundingbox.GetIndex();
// Define the random forest classification filter
typedef RandomForestClassifyImageFilter <TLabelImage, ImageVectorType, ProbabilityType, TPixel> FilterType;
// Create the filter for this label (TODO: this is wasting computation)
typename FilterType::Pointer filter = FilterType::New();
if(m_intermediateslices == true){
for ( unsigned int i = 0; i < m_SegmentationIndices.size()-1; i++ ){ // Need to extract intermediate slice
const int numSlices = m_SegmentationIndices[i+1] - m_SegmentationIndices[i];
int intermediate_slice = numSlices/2;
typename TLabelImage::IndexType regionIndex = bbox_index;
regionIndex[0] = bbox_index[0] + m_SegmentationIndices[i] + intermediate_slice; // took out -1
typename TLabelImage::RegionType slice_region(regionIndex, regionSize);
// Add all the images on the stack to the filter
ImageScalarType *image = dynamic_cast<ImageScalarType *>(intensity_obj);
if(image)
{
filter->AddScalarImage(image);
}
else
{
ImageVectorType *vecImage = dynamic_cast<ImageVectorType *>(intensity_obj);
if(vecImage)
{
filter->AddVectorImage(vecImage);
}
else
{
itkAssertInDebugOrThrowInReleaseMacro(
"Wrong input type to ImageCollectionConstRegionIteratorWithIndex");
}
}
// Pass the classifier to the filter
filter->SetClassifier(classifier);
// Set the filter behavior
filter->SetGenerateClassProbabilities(true);
filter->GetOutput()->SetRequestedRegion(slice_region);
// Run the filter for this set of weights
filter->Update();
ProbabilityType::Pointer RFprobability = filter->GetOutput(1);
RFprobability->DisconnectPipeline();
// Copy the probability map to the original image space
ImageAlgorithm::Copy< ProbabilityType, ProbabilityType >( RFprobability.GetPointer(), output.GetPointer(),
RFprobability->GetRequestedRegion(), slice_region);
} // end of slice iterator
}
else{
//Loop through all the slices not contained in m_SegmentationIndices;
std::vector<int> sliceRange;
for( int i = 0; i < bbox_size[m_slicingaxis]; i++ )
sliceRange.push_back( i );
std::vector<int> noSegmentations;
std::set_difference(sliceRange.begin(), sliceRange.end(), m_SegmentationIndices.begin(), m_SegmentationIndices.end(),
std::inserter(noSegmentations, noSegmentations.begin()));
for ( unsigned int i = 0; i < noSegmentations.size(); i++ ){ // took out -1
int slice_index = noSegmentations[i];
typename TLabelImage::IndexType regionIndex;
regionIndex[0] = bbox_index[0] + slice_index; // took out -1
regionIndex[1] = bbox_index[1];
regionIndex[2] = bbox_index[2];
typename TLabelImage::RegionType slice_region(regionIndex, regionSize);
// Add all the images on the stack to the filter
ImageScalarType *image = dynamic_cast<ImageScalarType *>(intensity_obj);
if(image)
{
filter->AddScalarImage(image);
}
else
{
ImageVectorType *vecImage = dynamic_cast<ImageVectorType *>(intensity_obj);
if(vecImage)
{
filter->AddVectorImage(vecImage);
}
else
{
itkAssertInDebugOrThrowInReleaseMacro(
"Wrong input type to ImageCollectionConstRegionIteratorWithIndex");
}
}
// Pass the classifier to the filter
filter->SetClassifier(classifier);
// Set the filter behavior
filter->SetGenerateClassProbabilities(true);
filter->GetOutput()->SetRequestedRegion(slice_region);
// Run the filter for this set of weights
filter->Update();
ProbabilityType::Pointer RFprobability = filter->GetOutput(1);
RFprobability->DisconnectPipeline();
// Copy the probability map to the original image space
ImageAlgorithm::Copy< ProbabilityType, ProbabilityType >( RFprobability.GetPointer(), output.GetPointer(),
RFprobability->GetRequestedRegion(), slice_region);
} // end of slice iterator
} // end of intermediateslices = false
} //End of GenerateData
}// end namespace
#endif
// Create a classifier object - Can be removed
// typedef typename FilterType::ClassifierType ApplyRFClassifierType;
// typename ApplyRFClassifierType::Pointer applyclassifier = ApplyRFClassifierType::New();
// // Read the classifier object from disk
// std::ifstream in_file(train_file);
// applyclassifier->Read(in_file);
// in_file.close();