21 #ifndef otbSVMMarginSampler_hxx
22 #define otbSVMMarginSampler_hxx
30 template <
class TSample,
class TModel>
33 m_NumberOfCandidates = 10;
36 template <
class TSample,
class TModel>
39 Superclass::PrintSelf(os, indent);
42 template <
class TSample,
class TModel>
47 itkExceptionMacro(
"No model, can not do classification.");
50 if (m_Model->GetNumberOfSupportVectors() == 0)
52 itkExceptionMacro(<<
"SVM model does not contain any support vector, can not perform margin sampling.");
56 output->SetSample(this->GetInput());
58 this->DoMarginSampling();
61 template <
class TSample,
class TModel>
67 typename TSample::ConstIterator iter = this->GetInput()->Begin();
68 typename TSample::ConstIterator end = this->GetInput()->End();
70 typename OutputType::ConstIterator iterO = output->Begin();
71 typename OutputType::ConstIterator endO = output->End();
72 typename TSample::MeasurementVectorType measurements;
74 m_Model->SetConfidenceMode(TModel::CM_HYPER);
76 int numberOfComponentsPerSample = iter.GetMeasurementVector().Size();
78 int nbClass =
static_cast<int>(m_Model->GetNumberOfClasses());
79 std::vector<double> hdistances(nbClass * (nbClass - 1) / 2);
82 while (iter != end && iterO != endO)
85 typename SVMModelType::InputSampleType modelMeasurement(numberOfComponentsPerSample);
87 measurements = iter.GetMeasurementVector();
89 for (i = 0; i < numberOfComponentsPerSample; ++i)
91 modelMeasurement[i] = measurements[i];
95 m_Model->Predict(modelMeasurement, &(hdistances[0]));
96 double minDistance = std::abs(hdistances[0]);
99 for (
unsigned int j = 1; j < hdistances.size(); ++j)
101 if (std::abs(hdistances[j]) < minDistance)
103 minDistance = std::abs(hdistances[j]);
108 idDistVector.push_back(value);
115 sort(idDistVector.begin(), idDistVector.end(), &Compare);
121 m_MarginSamples.clear();
123 for (
unsigned int i = 0; i < m_NumberOfCandidates && i < idDistVector.size(); ++i)
125 otbMsgDevMacro(
"Sample " << idDistVector[i].first <<
" (distance= " << idDistVector[i].second <<
")") m_MarginSamples.push_back(idDistVector[i].first);