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
|
/*
* Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
*
* This file is part of Orfeo Toolbox
*
* https://www.orfeo-toolbox.org/
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 otbImageClassificationFilter_hxx
#define otbImageClassificationFilter_hxx
#include "otbImageClassificationFilter.h"
#include "itkImageRegionIterator.h"
#include "itkProgressReporter.h"
namespace otb
{
/**
* Constructor
*/
template <class TInputImage, class TOutputImage, class TMaskImage>
ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>::ImageClassificationFilter()
{
this->SetNumberOfIndexedInputs(2);
this->SetNumberOfRequiredInputs(1);
m_DefaultLabel = itk::NumericTraits<LabelType>::ZeroValue();
this->SetNumberOfRequiredOutputs(3);
this->SetNthOutput(0, TOutputImage::New());
this->SetNthOutput(1, ConfidenceImageType::New());
this->SetNthOutput(2, ProbaImageType::New());
m_UseConfidenceMap = false;
m_UseProbaMap = false;
m_BatchMode = true;
m_NumberOfClasses = 1;
}
template <class TInputImage, class TOutputImage, class TMaskImage>
void ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>::SetInputMask(const MaskImageType* mask)
{
this->itk::ProcessObject::SetNthInput(1, const_cast<MaskImageType*>(mask));
}
template <class TInputImage, class TOutputImage, class TMaskImage>
const typename ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>::MaskImageType*
ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>::GetInputMask()
{
if (this->GetNumberOfInputs() < 2)
{
return nullptr;
}
return static_cast<const MaskImageType*>(this->itk::ProcessObject::GetInput(1));
}
template <class TInputImage, class TOutputImage, class TMaskImage>
typename ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>::ConfidenceImageType*
ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>::GetOutputConfidence()
{
if (this->GetNumberOfOutputs() < 2)
{
return nullptr;
}
return static_cast<ConfidenceImageType*>(this->itk::ProcessObject::GetOutput(1));
}
template <class TInputImage, class TOutputImage, class TMaskImage>
typename ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>::ProbaImageType*
ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>::GetOutputProba()
{
if (this->GetNumberOfOutputs() < 2)
{
return nullptr;
}
return static_cast<ProbaImageType*>(this->itk::ProcessObject::GetOutput(2));
}
template <class TInputImage, class TOutputImage, class TMaskImage>
void ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>::BeforeThreadedGenerateData()
{
if (!m_Model)
{
itkGenericExceptionMacro(<< "No model for classification");
}
if (m_BatchMode)
{
#ifdef _OPENMP
// OpenMP will take care of threading
this->SetNumberOfThreads(1);
#endif
}
}
template <class TInputImage, class TOutputImage, class TMaskImage>
void ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>::ClassicThreadedGenerateData(const OutputImageRegionType& outputRegionForThread,
itk::ThreadIdType threadId)
{
// Get the input pointers
InputImageConstPointerType inputPtr = this->GetInput();
MaskImageConstPointerType inputMaskPtr = this->GetInputMask();
OutputImagePointerType outputPtr = this->GetOutput();
ConfidenceImagePointerType confidencePtr = this->GetOutputConfidence();
ProbaImagePointerType probaPtr = this->GetOutputProba();
// Progress reporting
itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
// Define iterators
typedef itk::ImageRegionConstIterator<InputImageType> InputIteratorType;
typedef itk::ImageRegionConstIterator<MaskImageType> MaskIteratorType;
typedef itk::ImageRegionIterator<OutputImageType> OutputIteratorType;
typedef itk::ImageRegionIterator<ConfidenceImageType> ConfidenceMapIteratorType;
typedef itk::ImageRegionIterator<ProbaImageType> ProbaMapIteratorType;
InputIteratorType inIt(inputPtr, outputRegionForThread);
OutputIteratorType outIt(outputPtr, outputRegionForThread);
// Eventually iterate on masks
MaskIteratorType maskIt;
if (inputMaskPtr)
{
maskIt = MaskIteratorType(inputMaskPtr, outputRegionForThread);
maskIt.GoToBegin();
}
// setup iterator for confidence map
bool computeConfidenceMap(m_UseConfidenceMap && m_Model->HasConfidenceIndex() && !m_Model->GetRegressionMode());
ConfidenceMapIteratorType confidenceIt;
if (computeConfidenceMap)
{
confidenceIt = ConfidenceMapIteratorType(confidencePtr, outputRegionForThread);
confidenceIt.GoToBegin();
}
// setup iterator for proba map
bool computeProbaMap(m_UseProbaMap && m_Model->HasProbaIndex() && !m_Model->GetRegressionMode());
ProbaMapIteratorType probaIt;
if (computeProbaMap)
{
probaIt = ProbaMapIteratorType(probaPtr, outputRegionForThread);
probaIt.GoToBegin();
}
bool validPoint = true;
double confidenceIndex = 0.0;
ProbaSampleType probaVector{m_NumberOfClasses};
probaVector.Fill(0);
// Walk the part of the image
for (inIt.GoToBegin(), outIt.GoToBegin(); !inIt.IsAtEnd() && !outIt.IsAtEnd(); ++inIt, ++outIt)
{
// Check pixel validity
if (inputMaskPtr)
{
validPoint = maskIt.Get() > 0;
++maskIt;
}
// If point is valid
if (validPoint)
{
// Classifify
if (computeProbaMap)
{
outIt.Set(m_Model->Predict(inIt.Get(), &confidenceIndex, &probaVector)[0]);
}
else if (computeConfidenceMap)
{
outIt.Set(m_Model->Predict(inIt.Get(), &confidenceIndex)[0]);
}
else
{
outIt.Set(m_Model->Predict(inIt.Get())[0]);
}
}
else
{
// else, set default value
outIt.Set(m_DefaultLabel);
confidenceIndex = 0.0;
}
if (computeConfidenceMap)
{
confidenceIt.Set(confidenceIndex);
++confidenceIt;
}
if (computeProbaMap)
{
probaIt.Set(probaVector);
++probaIt;
}
progress.CompletedPixel();
}
}
template <class TInputImage, class TOutputImage, class TMaskImage>
void ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>::BatchThreadedGenerateData(const OutputImageRegionType& outputRegionForThread,
itk::ThreadIdType threadId)
{
bool computeConfidenceMap(m_UseConfidenceMap && m_Model->HasConfidenceIndex() && !m_Model->GetRegressionMode());
bool computeProbaMap(m_UseProbaMap && m_Model->HasProbaIndex() && !m_Model->GetRegressionMode());
// Get the input pointers
InputImageConstPointerType inputPtr = this->GetInput();
MaskImageConstPointerType inputMaskPtr = this->GetInputMask();
OutputImagePointerType outputPtr = this->GetOutput();
ConfidenceImagePointerType confidencePtr = this->GetOutputConfidence();
ProbaImagePointerType probaPtr = this->GetOutputProba();
// Progress reporting
itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
// Define iterators
typedef itk::ImageRegionConstIterator<InputImageType> InputIteratorType;
typedef itk::ImageRegionConstIterator<MaskImageType> MaskIteratorType;
typedef itk::ImageRegionIterator<OutputImageType> OutputIteratorType;
typedef itk::ImageRegionIterator<ConfidenceImageType> ConfidenceMapIteratorType;
typedef itk::ImageRegionIterator<ProbaImageType> ProbaMapIteratorType;
InputIteratorType inIt(inputPtr, outputRegionForThread);
OutputIteratorType outIt(outputPtr, outputRegionForThread);
MaskIteratorType maskIt;
if (inputMaskPtr)
{
maskIt = MaskIteratorType(inputMaskPtr, outputRegionForThread);
maskIt.GoToBegin();
}
typedef typename ModelType::InputSampleType InputSampleType;
typedef typename ModelType::InputListSampleType InputListSampleType;
typedef typename ModelType::TargetValueType TargetValueType;
typedef typename ModelType::TargetListSampleType TargetListSampleType;
typedef typename ModelType::ConfidenceListSampleType ConfidenceListSampleType;
typedef typename ModelType::ProbaListSampleType ProbaListSampleType;
typename InputListSampleType::Pointer samples = InputListSampleType::New();
unsigned int num_features = inputPtr->GetNumberOfComponentsPerPixel();
samples->SetMeasurementVectorSize(num_features);
InputSampleType sample(num_features);
// Fill the samples
bool validPoint = true;
for (inIt.GoToBegin(); !inIt.IsAtEnd(); ++inIt)
{
// Check pixel validity
if (inputMaskPtr)
{
validPoint = maskIt.Get() > 0;
++maskIt;
}
if (validPoint)
{
typename InputImageType::PixelType pix = inIt.Get();
for (size_t feat = 0; feat < num_features; ++feat)
{
sample[feat] = pix[feat];
}
samples->PushBack(sample);
}
}
// Make the batch prediction
typename TargetListSampleType::Pointer labels;
typename ConfidenceListSampleType::Pointer confidences;
typename ProbaListSampleType::Pointer probas;
if (computeConfidenceMap)
confidences = ConfidenceListSampleType::New();
if (computeProbaMap)
probas = ProbaListSampleType::New();
// This call is threadsafe
labels = m_Model->PredictBatch(samples, confidences, probas);
// Set the output values
ConfidenceMapIteratorType confidenceIt;
if (computeConfidenceMap)
{
confidenceIt = ConfidenceMapIteratorType(confidencePtr, outputRegionForThread);
confidenceIt.GoToBegin();
}
ProbaMapIteratorType probaIt;
if (computeProbaMap)
{
probaIt = ProbaMapIteratorType(probaPtr, outputRegionForThread);
probaIt.GoToBegin();
}
typename TargetListSampleType::ConstIterator labIt = labels->Begin();
maskIt.GoToBegin();
for (outIt.GoToBegin(); !outIt.IsAtEnd(); ++outIt)
{
double confidenceIndex = 0.0;
TargetValueType labelValue(m_DefaultLabel);
ProbaSampleType probaValues{m_NumberOfClasses};
if (inputMaskPtr)
{
validPoint = maskIt.Get() > 0;
++maskIt;
}
if (validPoint && labIt != labels->End())
{
labelValue = labIt.GetMeasurementVector()[0];
if (computeConfidenceMap)
{
confidenceIndex = confidences->GetMeasurementVector(labIt.GetInstanceIdentifier())[0];
}
if (computeProbaMap)
{
// The probas may have different size than the m_NumberOfClasses set by the user
auto tempProbaValues = probas->GetMeasurementVector(labIt.GetInstanceIdentifier());
for (unsigned int i = 0; i < m_NumberOfClasses; ++i)
{
if (i < tempProbaValues.Size())
probaValues[i] = tempProbaValues[i];
else
probaValues[i] = 0;
}
}
++labIt;
}
else
{
labelValue = m_DefaultLabel;
}
outIt.Set(labelValue);
if (computeConfidenceMap)
{
confidenceIt.Set(confidenceIndex);
++confidenceIt;
}
if (computeProbaMap)
{
probaIt.Set(probaValues);
++probaIt;
}
progress.CompletedPixel();
}
}
template <class TInputImage, class TOutputImage, class TMaskImage>
void ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>::ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread,
itk::ThreadIdType threadId)
{
if (m_BatchMode)
{
this->BatchThreadedGenerateData(outputRegionForThread, threadId);
}
else
{
this->ClassicThreadedGenerateData(outputRegionForThread, threadId);
}
}
/**
* PrintSelf Method
*/
template <class TInputImage, class TOutputImage, class TMaskImage>
void ImageClassificationFilter<TInputImage, TOutputImage, TMaskImage>::PrintSelf(std::ostream& os, itk::Indent indent) const
{
Superclass::PrintSelf(os, indent);
}
} // End namespace otb
#endif
|