File: itkMultiLabelSTAPLEImageFilter.hxx

package info (click to toggle)
insighttoolkit5 5.4.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 704,384 kB
  • sloc: cpp: 783,592; ansic: 628,724; xml: 44,704; fortran: 34,250; python: 22,874; sh: 4,078; pascal: 2,636; lisp: 2,158; makefile: 464; yacc: 328; asm: 205; perl: 203; lex: 146; tcl: 132; javascript: 98; csh: 81
file content (423 lines) | stat: -rw-r--r-- 14,479 bytes parent folder | download
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*=========================================================================
 *
 *  Copyright NumFOCUS
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         https://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/
#ifndef itkMultiLabelSTAPLEImageFilter_hxx
#define itkMultiLabelSTAPLEImageFilter_hxx


#include "itkLabelVotingImageFilter.h"

#include "itkMath.h"
#include "itkMakeUniqueForOverwrite.h"

namespace itk
{

template <typename TInputImage, typename TOutputImage, typename TWeights>
void
MultiLabelSTAPLEImageFilter<TInputImage, TOutputImage, TWeights>::PrintSelf(std::ostream & os, Indent indent) const
{
  Superclass::PrintSelf(os, indent);
  os << indent << "HasLabelForUndecidedPixels: " << this->m_HasLabelForUndecidedPixels << std::endl;
  using OutputPixelPrintType = typename NumericTraits<OutputPixelType>::PrintType;
  os << indent << "LabelForUndecidedPixels: " << static_cast<OutputPixelPrintType>(this->m_LabelForUndecidedPixels)
     << std::endl;
  os << indent << "HasPriorProbabilities: " << this->m_PriorProbabilities << std::endl;
  os << indent << "PriorProbabilities: " << this->m_PriorProbabilities << std::endl;
  os << indent << "HasMaximumNumberOfIterations: " << this->m_HasMaximumNumberOfIterations << std::endl;
  os << indent << "MaximumNumberOfIterations: " << this->m_MaximumNumberOfIterations << std::endl;
  os << indent << "ElapsedNumberOfIterations: " << m_ElapsedNumberOfIterations << std::endl;
  os << indent << "TerminationUpdateThreshold: " << this->m_TerminationUpdateThreshold << std::endl;
}

template <typename TInputImage, typename TOutputImage, typename TWeights>
void
MultiLabelSTAPLEImageFilter<TInputImage, TOutputImage, TWeights>::GenerateInputRequestedRegion()
{
  Superclass::GenerateInputRequestedRegion();

  for (unsigned int k = 0; k < this->GetNumberOfInputs(); ++k)
  {
    InputImagePointer input = const_cast<InputImageType *>(this->GetInput(k));
    input->SetRequestedRegionToLargestPossibleRegion();
  }
}

template <typename TInputImage, typename TOutputImage, typename TWeights>
void
MultiLabelSTAPLEImageFilter<TInputImage, TOutputImage, TWeights>::EnlargeOutputRequestedRegion(DataObject * data)
{
  Superclass::EnlargeOutputRequestedRegion(data);
  data->SetRequestedRegionToLargestPossibleRegion();
}

template <typename TInputImage, typename TOutputImage, typename TWeights>
typename TInputImage::PixelType
MultiLabelSTAPLEImageFilter<TInputImage, TOutputImage, TWeights>::ComputeMaximumInputValue()
{
  InputPixelType maxLabel = 0;

  // Record the number of input files.
  const size_t numberOfInputs = this->GetNumberOfInputs();

  for (size_t k = 0; k < numberOfInputs; ++k)
  {
    InputConstIteratorType it(this->GetInput(k), this->GetInput(k)->GetBufferedRegion());

    for (it.GoToBegin(); !it.IsAtEnd(); ++it)
    {
      maxLabel = std::max(maxLabel, it.Get());
    }
  }

  return maxLabel;
}

template <typename TInputImage, typename TOutputImage, typename TWeights>
void
MultiLabelSTAPLEImageFilter<TInputImage, TOutputImage, TWeights>::AllocateConfusionMatrixArray()
{
  // we need one confusion matrix for every input
  const ProcessObject::DataObjectPointerArraySizeType numberOfInputs = this->GetNumberOfInputs();

  this->m_ConfusionMatrixArray.clear();
  this->m_UpdatedConfusionMatrixArray.clear();

  // create the confusion matrix and space for updated confusion matrix for
  // each of the input images
  for (unsigned int k = 0; k < numberOfInputs; ++k)
  {
    // the confusion matrix has as many rows as there are input labels, and
    // one more column to accommodate "reject" classifications by the combined
    // classifier.
    this->m_ConfusionMatrixArray.push_back(ConfusionMatrixType(static_cast<unsigned int>(this->m_TotalLabelCount) + 1,
                                                               static_cast<unsigned int>(this->m_TotalLabelCount)));
    this->m_UpdatedConfusionMatrixArray.push_back(ConfusionMatrixType(
      static_cast<unsigned int>(this->m_TotalLabelCount) + 1, static_cast<unsigned int>(this->m_TotalLabelCount)));
  }
}

template <typename TInputImage, typename TOutputImage, typename TWeights>
void
MultiLabelSTAPLEImageFilter<TInputImage, TOutputImage, TWeights>::InitializeConfusionMatrixArrayFromVoting()
{
  const auto numberOfInputs = static_cast<const unsigned int>(this->GetNumberOfInputs());

  using LabelVotingFilterType = LabelVotingImageFilter<TInputImage, TOutputImage>;
  using LabelVotingFilterPointer = typename LabelVotingFilterType::Pointer;

  typename OutputImageType::Pointer votingOutput;

  { // begin scope for local filter allocation
    LabelVotingFilterPointer votingFilter = LabelVotingFilterType::New();

    for (unsigned int k = 0; k < numberOfInputs; ++k)
    {
      votingFilter->SetInput(k, this->GetInput(k));
    }
    votingFilter->Update();
    votingOutput = votingFilter->GetOutput();
  } // begin scope for local filter allocation; de-allocate filter

  OutputIteratorType out(votingOutput, votingOutput->GetRequestedRegion());

  for (unsigned int k = 0; k < numberOfInputs; ++k)
  {
    this->m_ConfusionMatrixArray[k].Fill(0.0);

    InputConstIteratorType in(this->GetInput(k), votingOutput->GetRequestedRegion());

    for (out.GoToBegin(); !out.IsAtEnd(); ++out, ++in)
    {
      ++(this->m_ConfusionMatrixArray[k][in.Get()][out.Get()]);
    }
  }

  // normalize matrix rows to unit probability sum
  for (unsigned int k = 0; k < numberOfInputs; ++k)
  {
    for (InputPixelType inLabel = 0; inLabel < this->m_TotalLabelCount + 1; ++inLabel)
    {
      // compute sum over all output labels for given input label
      WeightsType sum = 0;
      for (OutputPixelType outLabel = 0; outLabel < this->m_TotalLabelCount; ++outLabel)
      {
        sum += this->m_ConfusionMatrixArray[k][inLabel][outLabel];
      }
      // make sure that this input label did in fact show up in the input!!
      if (sum > 0)
      {
        // normalize
        for (OutputPixelType outLabel = 0; outLabel < this->m_TotalLabelCount; ++outLabel)
        {
          this->m_ConfusionMatrixArray[k][inLabel][outLabel] /= sum;
        }
      }
    }
  }
}

template <typename TInputImage, typename TOutputImage, typename TWeights>
void
MultiLabelSTAPLEImageFilter<TInputImage, TOutputImage, TWeights>::InitializePriorProbabilities()
{
  // test for user-defined prior probabilities and create an estimated one if
  // none exists
  if (this->m_HasPriorProbabilities)
  {
    if (this->m_PriorProbabilities.GetSize() < this->m_TotalLabelCount)
    {
      itkExceptionMacro("m_PriorProbabilities array has wrong size " << m_PriorProbabilities << "; should be at least "
                                                                     << 1 + this->m_TotalLabelCount);
    }
  }
  else
  {
    this->m_PriorProbabilities.SetSize(1 + static_cast<SizeValueType>(this->m_TotalLabelCount));
    this->m_PriorProbabilities.Fill(0.0);

    const size_t numberOfInputs = this->GetNumberOfInputs();
    for (size_t k = 0; k < numberOfInputs; ++k)
    {
      InputConstIteratorType in(this->GetInput(k), this->GetOutput()->GetRequestedRegion());
      for (in.GoToBegin(); !in.IsAtEnd(); ++in)
      {
        ++(this->m_PriorProbabilities[in.Get()]);
      }
    }

    WeightsType totalProbMass = 0.0;
    for (InputPixelType l = 0; l < this->m_TotalLabelCount; ++l)
    {
      totalProbMass += this->m_PriorProbabilities[l];
    }
    for (InputPixelType l = 0; l < this->m_TotalLabelCount; ++l)
    {
      this->m_PriorProbabilities[l] /= totalProbMass;
    }
  }
}

template <typename TInputImage, typename TOutputImage, typename TWeights>
void
MultiLabelSTAPLEImageFilter<TInputImage, TOutputImage, TWeights>::GenerateData()
{
  // determine the maximum label in all input images
  this->m_TotalLabelCount = static_cast<size_t>(this->ComputeMaximumInputValue()) + 1;

  if (!this->m_HasLabelForUndecidedPixels)
  {
    this->m_LabelForUndecidedPixels = static_cast<OutputPixelType>(this->m_TotalLabelCount);
  }

  // allocate and initialize the confusion matrices
  this->AllocateConfusionMatrixArray();
  this->InitializeConfusionMatrixArrayFromVoting();

  // test existing or allocate and initialize new array with prior class
  // probabilities
  this->InitializePriorProbabilities();

  // Allocate the output image.
  typename TOutputImage::Pointer output = this->GetOutput();
  output->SetBufferedRegion(output->GetRequestedRegion());
  output->Allocate();

  // Record the number of input files.
  const size_t numberOfInputs = this->GetNumberOfInputs();

  // create and initialize all input image iterators
  const auto it = make_unique_for_overwrite<InputConstIteratorType[]>(numberOfInputs);
  for (size_t k = 0; k < numberOfInputs; ++k)
  {
    it[k] = InputConstIteratorType(this->GetInput(k), output->GetRequestedRegion());
  }

  // allocate array for pixel class weights
  const auto W = make_unique_for_overwrite<WeightsType[]>(this->m_TotalLabelCount);

  unsigned int iteration = 0;
  for (; (!this->m_HasMaximumNumberOfIterations) || (iteration < this->m_MaximumNumberOfIterations); ++iteration)
  {
    // reset updated confusion matrix
    for (unsigned int k = 0; k < numberOfInputs; ++k)
    {
      this->m_UpdatedConfusionMatrixArray[k].Fill(0.0);
    }

    // reset all input iterators to start
    for (unsigned int k = 0; k < numberOfInputs; ++k)
    {
      it[k].GoToBegin();
    }

    // use it[0] as indicator for image pixel count
    while (!it[0].IsAtEnd())
    {
      // the following is the E step
      for (OutputPixelType ci = 0; ci < this->m_TotalLabelCount; ++ci)
      {
        W[ci] = this->m_PriorProbabilities[ci];
      }
      for (unsigned int k = 0; k < numberOfInputs; ++k)
      {
        const InputPixelType j = it[k].Get();
        for (OutputPixelType ci = 0; ci < this->m_TotalLabelCount; ++ci)
        {
          W[ci] *= this->m_ConfusionMatrixArray[k][j][ci];
        }
      }

      // the following is the M step
      WeightsType sumW = W[0];
      for (OutputPixelType ci = 1; ci < this->m_TotalLabelCount; ++ci)
      {
        sumW += W[ci];
      }

      if (sumW)
      {
        for (OutputPixelType ci = 0; ci < this->m_TotalLabelCount; ++ci)
        {
          W[ci] /= sumW;
        }
      }

      for (unsigned int k = 0; k < numberOfInputs; ++k)
      {
        const InputPixelType j = it[k].Get();
        for (OutputPixelType ci = 0; ci < this->m_TotalLabelCount; ++ci)
        {
          this->m_UpdatedConfusionMatrixArray[k][j][ci] += W[ci];
        }

        // we're now done with this input pixel, so update.
        ++(it[k]);
      }
    }

    // Normalize matrix elements of each of the updated confusion matrices
    // with sum over all expert decisions.
    for (unsigned int k = 0; k < numberOfInputs; ++k)
    {
      // compute sum over all output classifications
      for (OutputPixelType ci = 0; ci < this->m_TotalLabelCount; ++ci)
      {
        WeightsType sumW = this->m_UpdatedConfusionMatrixArray[k][0][ci];
        for (InputPixelType j = 1; j < 1 + this->m_TotalLabelCount; ++j)
        {
          sumW += this->m_UpdatedConfusionMatrixArray[k][j][ci];
        }

        // normalize with for each class ci
        if (sumW)
        {
          for (InputPixelType j = 0; j < 1 + this->m_TotalLabelCount; ++j)
          {
            this->m_UpdatedConfusionMatrixArray[k][j][ci] /= sumW;
          }
        }
      }
    }

    // now we're applying the update to the confusion matrices and compute the
    // maximum parameter change in the process.
    WeightsType maximumUpdate = 0;
    for (unsigned int k = 0; k < numberOfInputs; ++k)
    {
      for (InputPixelType j = 0; j < 1 + this->m_TotalLabelCount; ++j)
      {
        for (OutputPixelType ci = 0; ci < this->m_TotalLabelCount; ++ci)
        {
          const WeightsType thisParameterUpdate =
            itk::Math::abs(this->m_UpdatedConfusionMatrixArray[k][j][ci] - this->m_ConfusionMatrixArray[k][j][ci]);

          maximumUpdate = std::max(maximumUpdate, thisParameterUpdate);

          this->m_ConfusionMatrixArray[k][j][ci] = this->m_UpdatedConfusionMatrixArray[k][j][ci];
        }
      }
    }

    this->InvokeEvent(IterationEvent());
    if (this->GetAbortGenerateData())
    {
      this->ResetPipeline();
      // fake this to cause termination; we could really just break
      maximumUpdate = 0;
    }

    // if all confusion matrix parameters changes by less than the defined
    // threshold, we're done.
    if (maximumUpdate < this->m_TerminationUpdateThreshold)
    {
      break;
    }
  } // end for ( iteration )

  // now we'll build the combined output image based on the estimated
  // confusion matrices

  // reset all input iterators to start
  for (unsigned int k = 0; k < numberOfInputs; ++k)
  {
    it[k].GoToBegin();
  }

  for (OutputIteratorType out(output, output->GetRequestedRegion()); !out.IsAtEnd(); ++out)
  {
    // basically, we'll repeat the E step from above
    for (OutputPixelType ci = 0; ci < this->m_TotalLabelCount; ++ci)
    {
      W[ci] = this->m_PriorProbabilities[ci];
    }

    for (unsigned int k = 0; k < numberOfInputs; ++k)
    {
      const InputPixelType j = it[k].Get();
      for (OutputPixelType ci = 0; ci < this->m_TotalLabelCount; ++ci)
      {
        W[ci] *= this->m_ConfusionMatrixArray[k][j][ci];
      }
      ++it[k];
    }

    // now determine the label with the maximum W
    auto        winningLabel = this->m_LabelForUndecidedPixels;
    WeightsType winningLabelW = 0;
    for (OutputPixelType ci = 0; ci < this->m_TotalLabelCount; ++ci)
    {
      if (W[ci] > winningLabelW)
      {
        winningLabelW = W[ci];
        winningLabel = ci;
      }
      else if (!(W[ci] < winningLabelW))
      {
        winningLabel = this->m_LabelForUndecidedPixels;
      }
    }

    out.Set(winningLabel);
  }

  m_ElapsedNumberOfIterations = iteration;
}

} // end namespace itk

#endif