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
|
/*
* 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.
*/
#include "otbWrapperApplication.h"
#include "otbWrapperApplicationFactory.h"
#include "otbSOMMap.h"
#include "otbSOM.h"
#include "otbSOMImageClassificationFilter.h"
#include "otbRAMDrivenAdaptativeStreamingManager.h"
#include "itkImageRegionConstIterator.h"
#include "itkImageRandomNonRepeatingConstIteratorWithIndex.h"
#include <ctime>
namespace otb
{
namespace Wrapper
{
class SOMClassification : public Application
{
public:
/** Standard class typedefs. */
typedef SOMClassification Self;
typedef Application Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
/** Standard macro */
itkNewMacro(Self);
itkTypeMacro(SOMClassification, otb::Application);
/** Filters typedef */
typedef UInt16ImageType LabeledImageType;
typedef itk::VariableLengthVector<double> SampleType;
typedef itk::Statistics::EuclideanDistanceMetric<SampleType> DistanceType;
typedef otb::SOMMap<SampleType, DistanceType, 2> SOMMapType;
typedef itk::Statistics::ListSample<SampleType> ListSampleType;
typedef otb::SOM<ListSampleType, SOMMapType> EstimatorType;
typedef RAMDrivenAdaptativeStreamingManager<FloatVectorImageType> RAMDrivenAdaptativeStreamingManagerType;
typedef FloatVectorImageType::RegionType RegionType;
typedef itk::ImageRegionConstIterator<FloatVectorImageType> IteratorType;
typedef itk::ImageRegionConstIterator<LabeledImageType> LabeledIteratorType;
typedef itk::ImageRegionConstIterator<SOMMapType> SOMIteratorType;
typedef otb::SOMImageClassificationFilter<FloatVectorImageType, LabeledImageType, SOMMapType> ClassificationFilterType;
private:
SOMClassification()
{
m_UseMask = false;
m_Classifier = ClassificationFilterType::New();
}
void DoInit() override
{
SetName("SOMClassification");
SetDescription("SOM image classification.");
// Documentation
SetDocLongDescription("Unsupervised Self Organizing Map image classification.");
SetDocLimitations("None");
SetDocAuthors("OTB-Team");
SetDocSeeAlso(" ");
AddDocTag(Tags::Learning);
AddDocTag(Tags::Segmentation);
AddParameter(ParameterType_InputImage, "in", "InputImage");
SetParameterDescription("in", "Input image to classify.");
AddParameter(ParameterType_OutputImage, "out", "OutputImage");
SetParameterDescription("out", "Output classified image (each pixel contains the index of its corresponding vector in the SOM).");
SetDefaultOutputPixelType("out", ImagePixelType_uint8);
AddParameter(ParameterType_InputImage, "vm", "ValidityMask");
SetParameterDescription("vm", "Validity mask (only pixels corresponding to a mask value greater than 0 will be used for learning)");
MandatoryOff("vm");
AddParameter(ParameterType_Float, "tp", "TrainingProbability");
SetParameterDescription("tp", "Probability for a sample to be selected in the training set");
MandatoryOff("tp");
AddParameter(ParameterType_Int, "ts", "TrainingSetSize");
SetParameterDescription("ts", "Maximum training set size (in pixels)");
MandatoryOff("ts");
AddParameter(ParameterType_OutputImage, "som", "SOM Map");
SetParameterDescription("som", "Output image containing the Self-Organizing Map");
MandatoryOff("som");
AddParameter(ParameterType_Int, "sx", "SizeX");
SetParameterDescription("sx", "X size of the SOM map");
MandatoryOff("sx");
AddParameter(ParameterType_Int, "sy", "SizeY");
SetParameterDescription("sy", "Y size of the SOM map");
MandatoryOff("sy");
AddParameter(ParameterType_Int, "nx", "NeighborhoodX");
SetParameterDescription("nx", "X size of the initial neighborhood in the SOM map");
MandatoryOff("nx");
AddParameter(ParameterType_Int, "ny", "NeighborhoodY");
SetParameterDescription("ny", "Y size of the initial neighborhood in the SOM map");
MandatoryOff("nx");
AddParameter(ParameterType_Int, "ni", "NumberIteration");
SetParameterDescription("ni", "Number of iterations for SOM learning");
MandatoryOff("ni");
AddParameter(ParameterType_Float, "bi", "BetaInit");
SetParameterDescription("bi", "Initial learning coefficient");
MandatoryOff("bi");
AddParameter(ParameterType_Float, "bf", "BetaFinal");
SetParameterDescription("bf", "Final learning coefficient");
MandatoryOff("bf");
AddParameter(ParameterType_Float, "iv", "InitialValue");
SetParameterDescription("iv", "Maximum initial neuron weight");
MandatoryOff("iv");
AddRANDParameter();
AddRAMParameter();
// TODO : replace StreamingLines by RAM param ?
// Default parameters
SetDefaultParameterFloat("tp", 1.0);
SetDefaultParameterInt("sx", 32);
SetDefaultParameterInt("sy", 32);
SetDefaultParameterInt("nx", 10);
SetDefaultParameterInt("ny", 10);
SetDefaultParameterInt("ni", 5);
SetDefaultParameterFloat("bi", 1.0);
SetDefaultParameterFloat("bf", 0.1);
SetDefaultParameterFloat("iv", 0.0);
// Doc example parameter settings
SetDocExampleParameterValue("in", "QB_1_ortho.tif");
SetDocExampleParameterValue("out", "SOMClassification.tif");
SetDocExampleParameterValue("tp", "1.0");
SetDocExampleParameterValue("ts", "16384");
SetDocExampleParameterValue("sx", "32");
SetDocExampleParameterValue("sy", "32");
SetDocExampleParameterValue("nx", "10");
SetDocExampleParameterValue("ny", "10");
SetDocExampleParameterValue("ni", "5");
SetDocExampleParameterValue("bi", "1.0");
SetDocExampleParameterValue("bf", "0.1");
SetDocExampleParameterValue("iv", "0");
SetOfficialDocLink();
}
void DoUpdateParameters() override
{
}
void DoExecute() override
{
// initiating random number generation
itk::Statistics::MersenneTwisterRandomVariateGenerator::Pointer randomGen = itk::Statistics::MersenneTwisterRandomVariateGenerator::GetInstance();
FloatVectorImageType::Pointer input = GetParameterImage("in");
LabeledImageType::Pointer mask;
m_UseMask = false;
if (HasValue("vm"))
{
mask = GetParameterUInt16Image("vm");
if (input->GetLargestPossibleRegion() != mask->GetLargestPossibleRegion())
{
otbAppLogFATAL("Mask image and input image have different sizes.");
}
m_UseMask = true;
}
/*******************************************/
/* Sampling data */
/*******************************************/
otbAppLogINFO("-- SAMPLING DATA --");
RegionType largestRegion = input->GetLargestPossibleRegion();
// Setting up local streaming capabilities
RAMDrivenAdaptativeStreamingManagerType::Pointer streamingManager = RAMDrivenAdaptativeStreamingManagerType::New();
int availableRAM = GetParameterInt("ram");
streamingManager->SetAvailableRAMInMB(availableRAM);
float bias = 2.0; // empiric value;
streamingManager->SetBias(bias);
streamingManager->PrepareStreaming(input, largestRegion);
unsigned long numberOfStreamDivisions = streamingManager->GetNumberOfSplits();
otbAppLogINFO("The images will be streamed into " << numberOfStreamDivisions << " parts.");
// Training sample lists
ListSampleType::Pointer sampleList = ListSampleType::New();
sampleList->SetMeasurementVectorSize(input->GetNumberOfComponentsPerPixel());
const double trainingProb = static_cast<double>(GetParameterFloat("tp"));
unsigned int nbsamples;
if (HasValue("ts"))
{
nbsamples = GetParameterInt("ts");
}
else
{
nbsamples = largestRegion.GetNumberOfPixels();
}
// Sample dimension and max dimension
unsigned int sampleSize = input->GetNumberOfComponentsPerPixel();
unsigned int totalSamples = 0;
otbAppLogINFO("The following sample size will be used: " << sampleSize);
// local streaming variables
unsigned int piece = 0;
RegionType streamingRegion;
// create a random permutation to explore
itk::RandomPermutation randPerm(numberOfStreamDivisions);
unsigned int index = 0;
// Randomize seed if not given
if (!HasUserValue("rand")) {
SetParameterInt("rand", std::time(0));
}
// reset seed and step once (itk::RandomPermutation may have used it)
randomGen->SetSeed(GetParameterInt("rand"));
randomGen->GetVariateWithClosedRange();
// TODO : maybe change the approach: at the moment, the sampling process is able to pick a sample twice or more
while (totalSamples < nbsamples)
{
unsigned int localNbSamples = 0;
piece = randPerm[index];
streamingRegion = streamingManager->GetSplit(piece);
// otbAppLogINFO("Processing region: "<<streamingRegion);
input->SetRequestedRegion(streamingRegion);
input->PropagateRequestedRegion();
input->UpdateOutputData();
IteratorType it(input, streamingRegion);
it.GoToBegin();
if (m_UseMask)
{
mask->SetRequestedRegion(streamingRegion);
mask->PropagateRequestedRegion();
mask->UpdateOutputData();
LabeledIteratorType maskIt(mask, streamingRegion);
maskIt.GoToBegin();
// Loop on the image and the mask
while (!it.IsAtEnd() && !maskIt.IsAtEnd() && (totalSamples < nbsamples))
{
// If the current pixel is labeled
if (maskIt.Get() > 0)
{
if (randomGen->GetVariateWithClosedRange() < trainingProb)
{
SampleType newSample;
newSample.SetSize(sampleSize);
// build the sample
newSample.Fill(0);
for (unsigned int i = 0; i < sampleSize; ++i)
{
newSample[i] = it.Get()[i];
}
// Update the sample lists
sampleList->PushBack(newSample);
++totalSamples;
++localNbSamples;
}
}
++it;
++maskIt;
}
index++;
// we could break out of the while loop here, once the entire image has been streamed once
if (index == numberOfStreamDivisions)
index = 0;
}
else
{
// Loop on the image
while (!it.IsAtEnd() && (totalSamples < nbsamples))
{
if (randomGen->GetVariateWithClosedRange() < trainingProb)
{
SampleType newSample;
newSample.SetSize(sampleSize);
// build the sample
newSample.Fill(0);
for (unsigned int i = 0; i < sampleSize; ++i)
{
newSample[i] = it.Get()[i];
}
// Update the sample lists
sampleList->PushBack(newSample);
++totalSamples;
++localNbSamples;
}
++it;
}
index++;
// we could break out of the while loop here, once the entire image has been streamed once
if (index == numberOfStreamDivisions)
index = 0;
}
}
otbAppLogINFO("The final training set contains " << totalSamples << " samples.");
/*******************************************/
/* Learning */
/*******************************************/
otbAppLogINFO("-- LEARNING --");
EstimatorType::Pointer estimator = EstimatorType::New();
estimator->SetListSample(sampleList);
EstimatorType::SizeType size;
size[0] = GetParameterInt("sx");
size[1] = GetParameterInt("sy");
estimator->SetMapSize(size);
EstimatorType::SizeType radius;
radius[0] = GetParameterInt("nx");
radius[1] = GetParameterInt("ny");
estimator->SetNeighborhoodSizeInit(radius);
estimator->SetNumberOfIterations(GetParameterInt("ni"));
estimator->SetBetaInit(GetParameterFloat("bi"));
estimator->SetBetaEnd(GetParameterFloat("bf"));
estimator->SetMaxWeight(GetParameterFloat("iv"));
AddProcess(estimator, "Learning");
estimator->Update();
m_SOMMap = estimator->GetOutput();
if (HasValue("som"))
{
otbAppLogINFO("-- Using Leaning image --");
SetParameterOutputImage<DoubleVectorImageType>("som", m_SOMMap);
}
/*******************************************/
/* Classification */
/*******************************************/
otbAppLogINFO("-- CLASSIFICATION --");
m_Classifier->SetInput(input);
m_Classifier->SetMap(m_SOMMap);
if (m_UseMask)
m_Classifier->SetInputMask(mask);
AddProcess(m_Classifier, "Classification");
SetParameterOutputImage<LabeledImageType>("out", m_Classifier->GetOutput());
}
bool m_UseMask;
SOMMapType::Pointer m_SOMMap;
ClassificationFilterType::Pointer m_Classifier;
};
}
}
OTB_APPLICATION_EXPORT(otb::Wrapper::SOMClassification)
|