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 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
|
/*
* Copyright (C) 2005-2017 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 <iostream>
#include "otbWrapperApplication.h"
#include "otbWrapperApplicationFactory.h"
#include "otbWrapperStringListParameter.h"
#include "otbImageToEnvelopeVectorDataFilter.h"
#include "otbVectorDataToRandomLineGenerator.h"
#include "itkAmoebaOptimizer.h"
#include "otbStandardDSCostFunction.h"
namespace otb
{
namespace Wrapper
{
#include "itkCommand.h"
class CommandIterationUpdate : public itk::Command
{
public:
typedef CommandIterationUpdate Self;
typedef itk::Command Superclass;
typedef itk::SmartPointer<Self> Pointer;
itkNewMacro( Self );
protected:
CommandIterationUpdate() {};
public:
typedef itk::AmoebaOptimizer OptimizerType;
typedef const OptimizerType * OptimizerPointer;
void Execute(itk::Object *caller, const itk::EventObject & event) override
{
Execute( (const itk::Object *)caller, event);
}
void Execute(const itk::Object * object, const itk::EventObject & event) override
{
OptimizerPointer optimizer =
dynamic_cast< OptimizerPointer >( object );
if( ! itk::IterationEvent().CheckEvent( &event ) )
{
return;
}
std::ostringstream message;
message << optimizer->GetCachedValue() << " ";
message << optimizer->GetCachedCurrentPosition() << std::endl;
std::cout<<message.str()<<std::endl;
}
};
class DSFuzzyModelEstimation: public Application
{
public:
/** Standard class typedefs. */
typedef DSFuzzyModelEstimation Self;
typedef Application Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
typedef VectorData<double> VectorDataType;
typedef VectorDataType::DataTreeType DataTreeType;
typedef VectorDataType::DataNodeType DataNodeType;
typedef VectorDataType::ValuePrecisionType PrecisionType;
typedef VectorDataType::PrecisionType CoordRepType;
typedef otb::Wrapper::StringListParameter::StringListType StringListType;
typedef otb::VectorDataToDSValidatedVectorDataFilter<VectorDataType, PrecisionType>
ValidationFilterType;
typedef otb::StandardDSCostFunction<ValidationFilterType> CostFunctionType;
typedef CostFunctionType::LabelSetType LabelSetType;
typedef itk::AmoebaOptimizer OptimizerType;
typedef otb::FuzzyDescriptorsModelManager::DescriptorsModelType
DescriptorsModelType;
typedef otb::FuzzyDescriptorsModelManager::DescriptorListType
DescriptorListType;
typedef itk::PreOrderTreeIterator<VectorDataType::DataTreeType>
TreeIteratorType;
/** Standard macro */
itkNewMacro(Self);
itkTypeMacro(DSFuzzyModelEstimation, otb::Application);
private:
void DoInit() override
{
SetName("DSFuzzyModelEstimation");
SetDescription("Estimate feature fuzzy model parameters using 2 vector data (ground truth samples and wrong samples).");
SetDocName("Fuzzy Model estimation");
SetDocLongDescription("Estimate feature fuzzy model parameters using 2 vector data (ground truth samples and wrong samples).");
SetDocLimitations("None.");
SetDocAuthors("OTB-Team");
SetDocSeeAlso(" ");
AddDocTag(Tags::FeatureExtraction);
AddParameter(ParameterType_InputVectorData, "psin", "Input Positive Vector Data");
SetParameterDescription("psin", "Ground truth vector data for positive samples");
AddParameter(ParameterType_InputVectorData, "nsin", "Input Negative Vector Data");
SetParameterDescription("nsin", "Ground truth vector data for negative samples");
AddParameter(ParameterType_StringList, "belsup", "Belief Support");
SetParameterDescription("belsup", "Dempster Shafer study hypothesis to compute belief");
AddParameter(ParameterType_StringList, "plasup", "Plausibility Support");
SetParameterDescription("plasup", "Dempster Shafer study hypothesis to compute plausibility");
AddParameter(ParameterType_String, "cri", "Criterion");
SetParameterDescription("cri", "Dempster Shafer criterion (by default (belief+plausibility)/2)");
MandatoryOff("cri");
SetParameterString("cri","((Belief + Plausibility)/2.)");
AddParameter(ParameterType_Float,"wgt","Weighting");
SetParameterDescription("wgt","Coefficient between 0 and 1 to promote undetection or false detections (default 0.5)");
MandatoryOff("wgt");
SetParameterFloat("wgt",0.5);
AddParameter(ParameterType_InputFilename,"initmod","initialization model");
SetParameterDescription("initmod","Initialization model (xml file) to be used. If the xml initialization model is set, the descriptor list is not used (specified using the option -desclist)");
MandatoryOff("initmod");
AddParameter(ParameterType_StringList, "desclist","Descriptor list");
SetParameterDescription("desclist","List of the descriptors to be used in the model (must be specified to perform an automatic initialization)");
MandatoryOff("desclist");
SetParameterString("desclist","");
AddParameter(ParameterType_Int,"maxnbit","Maximum number of iterations");
MandatoryOff("maxnbit");
SetParameterDescription("maxnbit","Maximum number of optimizer iteration (default 200)");
SetParameterInt("maxnbit",200);
AddParameter(ParameterType_Bool,"optobs","Optimizer Observer");
SetParameterDescription("optobs","Activate the optimizer observer");
AddParameter(ParameterType_OutputFilename,"out","Output filename");
SetParameterDescription("out","Output model file name (xml file) contains the optimal model to perform information fusion.");
// Doc example parameter settings
SetDocExampleParameterValue("psin", "cdbTvComputePolylineFeatureFromImage_LI_NOBUIL_gt.shp");
SetDocExampleParameterValue("nsin", "cdbTvComputePolylineFeatureFromImage_LI_NOBUIL_wr.shp");
SetDocExampleParameterValue("belsup", "\"ROADSA\"");
SetDocExampleParameterValue("plasup", "\"NONDVI\" \"ROADSA\" \"NOBUIL\"");
SetDocExampleParameterValue("initmod", "Dempster-Shafer/DSFuzzyModel_Init.xml");
SetDocExampleParameterValue("maxnbit", "4");
SetDocExampleParameterValue("optobs", "true");
SetDocExampleParameterValue("out", "DSFuzzyModelEstimation.xml");
SetOfficialDocLink();
}
void DoUpdateParameters() override
{
// Nothing to do here : all parameters are independent
// .. //
}
void DoExecute() override
{
//Instantiate
m_CostFunction = CostFunctionType::New();
m_Optimizer = OptimizerType::New();
//Read the vector datas
VectorDataType::Pointer psVectorData = GetParameterVectorData("psin");
psVectorData->Update();
VectorDataType::Pointer nsVectorData = GetParameterVectorData("nsin");
nsVectorData->Update();
// Load the initial descriptor model
DescriptorListType descList;
DescriptorsModelType descMod;
if (IsParameterEnabled("initmod"))
{
std::string descModFile = GetParameterString("initmod");
descMod = FuzzyDescriptorsModelManager::Read(descModFile.c_str());
descList = FuzzyDescriptorsModelManager::GetDescriptorList(descMod);
}
else
{
StringListType stringList = GetParameterStringList("desclist");
int nbsdDesc = stringList.size();
for (int i = 0; i < nbsdDesc; i++)
{
descList.push_back(stringList[i]);
}
}
m_CostFunction->SetDescriptorList(descList);
// Compute statistics of all the descriptors
std::vector<double> accFirstOrderPS, accSecondOrderPS, minPS, maxPS;
accFirstOrderPS.resize(descList.size());
accSecondOrderPS.resize(descList.size());
std::fill(accFirstOrderPS.begin(), accFirstOrderPS.end(), 0);
std::fill(accSecondOrderPS.begin(), accSecondOrderPS.end(), 0);
minPS.resize(descList.size());
maxPS.resize(descList.size());
unsigned int accNbElemPS = 0;
TreeIteratorType itVectorPS(psVectorData->GetDataTree());
for (itVectorPS.GoToBegin(); !itVectorPS.IsAtEnd(); ++itVectorPS)
{
if (!itVectorPS.Get()->IsRoot() && !itVectorPS.Get()->IsDocument() && !itVectorPS.Get()->IsFolder())
{
DataNodeType::Pointer currentGeometry = itVectorPS.Get();
for (unsigned int i = 0; i < descList.size(); ++i)
{
double desc = currentGeometry->GetFieldAsDouble(descList[i]);
accFirstOrderPS[i] += desc;
accSecondOrderPS[i] += desc * desc;
if (desc < minPS[i])
{
minPS[i] = desc;
}
if (desc > maxPS[i])
{
maxPS[i] = desc;
}
}
accNbElemPS++;
}
}
if (accNbElemPS == 0)
{
otbAppLogFATAL(<< "Error : no element found in positive vector data!");
}
TreeIteratorType itVectorNS(nsVectorData->GetDataTree());
std::vector<double> accFirstOrderNS, accSecondOrderNS, minNS, maxNS;
minNS.resize(descList.size());
maxNS.resize(descList.size());
accFirstOrderNS.resize(descList.size());
accSecondOrderNS.resize(descList.size());
std::fill(accFirstOrderNS.begin(), accFirstOrderNS.end(), 0);
std::fill(accSecondOrderNS.begin(), accSecondOrderNS.end(), 0);
std::fill(minNS.begin(), minNS.end(), 1);
std::fill(maxNS.begin(), maxNS.end(), 0);
unsigned int accNbElemNS = 0;
for (itVectorNS.GoToBegin(); !itVectorNS.IsAtEnd(); ++itVectorNS)
{
if (!itVectorNS.Get()->IsRoot() && !itVectorNS.Get()->IsDocument() && !itVectorNS.Get()->IsFolder())
{
DataNodeType::Pointer currentGeometry = itVectorNS.Get();
for (unsigned int i = 0; i < descList.size(); ++i)
{
double desc = currentGeometry->GetFieldAsDouble(descList[i]);
accFirstOrderNS[i] += desc;
accSecondOrderNS[i] += desc * desc;
if (desc < minNS[i])
{
minNS[i] = desc;
}
if (desc > maxNS[i])
{
maxNS[i] = desc;
}
}
accNbElemNS++;
}
}
if (accNbElemNS == 0)
{
otbAppLogFATAL(<< "Error : no element found in negative vector data!");
}
otbAppLogINFO( << "Descriptors Stats : ");
otbAppLogINFO( << "Positive Samples");
for (unsigned int i = 0; i < descList.size(); ++i)
{
double mean = accFirstOrderPS[i] / accNbElemPS;
double stddev = vcl_sqrt(accSecondOrderPS[i] / accNbElemPS - mean * mean);
otbAppLogINFO( << descList[i] << " : " << mean << " +/- " << stddev << " (min: " << minPS[i] << " max: " << maxPS[i] << ")"<< std::endl);
}
otbAppLogINFO( << "Negative Samples" << std::endl);
for (unsigned int i = 0; i < descList.size(); ++i)
{
double mean = accFirstOrderNS[i] / accNbElemNS;
double stddev = vcl_sqrt(accSecondOrderNS[i] / accNbElemNS - mean * mean);
otbAppLogINFO(<< descList[i] << " : " << mean << " +/- " << stddev << " (min: " << minNS[i] << " max: " << maxNS[i] << ")"<< std::endl);
}
OptimizerType::ParametersType initialPosition(4 * descList.size());
if (IsParameterEnabled("initmod"))
{
for (unsigned int i = 0; i < 4; i++)
{
for (unsigned int j = 0; j < descList.size(); j++)
{
initialPosition.SetElement(
i + 4 * j,
otb::FuzzyDescriptorsModelManager::GetDescriptor(descList[j].c_str(), descMod).second[i]);
}
}
}
else
{
for (unsigned int j = 0; j < descList.size(); j++)
{
initialPosition.SetElement((j * 4), std::min(minNS[j], maxPS[j]));
initialPosition.SetElement((j * 4) + 2, std::max(minNS[j], maxPS[j]));
initialPosition.SetElement(
(j * 4) + 1,
0.5
* (initialPosition.GetElement((j * 4)) + initialPosition.GetElement((j * 4) + 2)));
initialPosition.SetElement((j * 4) + 3, 0.95);
}
}
otbAppLogINFO(<<"Initial model: "<<initialPosition);
//Cost Function
//Format Hypothesis
LabelSetType Bhyp, Phyp;
int nbSet;
StringListType stringList = GetParameterStringList("belsup");
nbSet = stringList.size();
for (int i = 0; i < nbSet; i++)
{
std::string str = stringList[i];
Bhyp.insert(str);
}
m_CostFunction->SetBeliefHypothesis(Bhyp);
stringList = GetParameterStringList("plasup");
nbSet = stringList.size();
for (int i = 0; i < nbSet; i++)
{
std::string str = stringList[i];
Phyp.insert(str);
}
m_CostFunction->SetPlausibilityHypothesis(Phyp);
m_CostFunction->SetWeight(GetParameterFloat("wgt"));
m_CostFunction->SetCriterionFormula(GetParameterString("cri"));
m_CostFunction->SetGTVectorData(psVectorData);
m_CostFunction->SetNSVectorData(nsVectorData);
//Optimizer
m_Optimizer->SetCostFunction(m_CostFunction);
m_Optimizer->SetMaximumNumberOfIterations(GetParameterInt("maxnbit"));
OptimizerType::ParametersType simplexDelta(m_CostFunction->GetNumberOfParameters());
simplexDelta.Fill(0.1);
m_Optimizer->AutomaticInitialSimplexOff();
m_Optimizer->SetInitialSimplexDelta(simplexDelta);
m_Optimizer->SetInitialPosition(initialPosition);
// Create the Command observer and register it with the optimizer.
CommandIterationUpdate::Pointer observer = CommandIterationUpdate::New();
if (GetParameterInt("optobs"))
{
m_Optimizer->AddObserver(itk::IterationEvent(), observer);
}
try
{
// do the optimization
m_Optimizer->StartOptimization();
}
catch (itk::ExceptionObject& err)
{
// An error has occurred in the optimization.
// Update the parameters
otbAppLogFATAL("ERROR: Exception Caught : "<< err.GetDescription() << std::endl
<< "numberOfIterations : " << m_Optimizer->GetOptimizer()->get_num_evaluations() << std::endl
<< "Results : " << m_Optimizer->GetCurrentPosition() << std::endl);
}
// get the results
const unsigned int numberOfIterations = m_Optimizer->GetOptimizer()->get_num_evaluations();
otbAppLogINFO("numberOfIterations : " << numberOfIterations << std::endl);
otbAppLogINFO("Results : " << m_Optimizer->GetCurrentPosition() << std::endl);
for (unsigned int i = 0; i < descList.size(); i++)
{
otb::FuzzyDescriptorsModelManager::ParameterType tmpParams;
for (unsigned int j = 0; j < 4; j++)
{
tmpParams.push_back(m_Optimizer->GetCurrentPosition()[(i * 4) + j]);
}
otb::FuzzyDescriptorsModelManager::AddDescriptor(descList[i], tmpParams, m_Model);
}
otb::FuzzyDescriptorsModelManager::Save(GetParameterString("out"), m_Model);
};
CostFunctionType::Pointer m_CostFunction;
OptimizerType::Pointer m_Optimizer;
otb::FuzzyDescriptorsModelManager::DescriptorsModelType m_Model;
};
}
}
OTB_APPLICATION_EXPORT(otb::Wrapper::DSFuzzyModelEstimation)
|