Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
otb
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
208
Issues
208
List
Boards
Labels
Milestones
Merge Requests
9
Merge Requests
9
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Container Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Main Repositories
otb
Commits
c4751d32
Commit
c4751d32
authored
Apr 24, 2017
by
Ludovic Hussonnois
Browse files
Options
Browse Files
Download
Plain Diff
MRG: Merge remote-tracking branch 'remotes/origin/contingency_table' into develop
parents
d3bd4b1f
e4edd3c8
Changes
13
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
922 additions
and
79 deletions
+922
-79
Modules/Applications/AppClassification/app/otbComputeConfusionMatrix.cxx
...tions/AppClassification/app/otbComputeConfusionMatrix.cxx
+161
-51
Modules/Applications/AppClassification/app/otbTrainVectorClassifier.cxx
...ations/AppClassification/app/otbTrainVectorClassifier.cxx
+48
-19
Modules/Applications/AppClassification/include/otbLearningApplicationBase.txx
.../AppClassification/include/otbLearningApplicationBase.txx
+14
-5
Modules/Applications/AppClassification/include/otbTrainVectorBase.h
...plications/AppClassification/include/otbTrainVectorBase.h
+1
-1
Modules/Applications/AppClassification/test/CMakeLists.txt
Modules/Applications/AppClassification/test/CMakeLists.txt
+17
-0
Modules/Learning/Unsupervised/include/otbContingencyTable.h
Modules/Learning/Unsupervised/include/otbContingencyTable.h
+171
-0
Modules/Learning/Unsupervised/include/otbContingencyTableCalculator.h
...ning/Unsupervised/include/otbContingencyTableCalculator.h
+104
-0
Modules/Learning/Unsupervised/include/otbContingencyTableCalculator.txx
...ng/Unsupervised/include/otbContingencyTableCalculator.txx
+134
-0
Modules/Learning/Unsupervised/include/otbSharkKMeansMachineLearningModel.h
...Unsupervised/include/otbSharkKMeansMachineLearningModel.h
+1
-0
Modules/Learning/Unsupervised/include/otbSharkKMeansMachineLearningModel.txx
...supervised/include/otbSharkKMeansMachineLearningModel.txx
+4
-3
Modules/Learning/Unsupervised/test/CMakeLists.txt
Modules/Learning/Unsupervised/test/CMakeLists.txt
+14
-0
Modules/Learning/Unsupervised/test/otbContingencyTableCalculatorTest.cxx
...g/Unsupervised/test/otbContingencyTableCalculatorTest.cxx
+247
-0
Modules/Learning/Unsupervised/test/otbUnsupervisedTestDriver.cxx
.../Learning/Unsupervised/test/otbUnsupervisedTestDriver.cxx
+6
-0
No files found.
Modules/Applications/AppClassification/app/otbComputeConfusionMatrix.cxx
View file @
c4751d32
This diff is collapsed.
Click to expand it.
Modules/Applications/AppClassification/app/otbTrainVectorClassifier.cxx
View file @
c4751d32
...
...
@@ -22,6 +22,7 @@
// Validation
#include "otbConfusionMatrixCalculator.h"
#include "otbContingencyTableCalculator.h"
namespace
otb
{
...
...
@@ -48,6 +49,9 @@ public:
typedef
ConfusionMatrixCalculatorType
::
ConfusionMatrixType
ConfusionMatrixType
;
typedef
ConfusionMatrixCalculatorType
::
MapOfIndicesType
MapOfIndicesType
;
typedef
ConfusionMatrixCalculatorType
::
ClassLabelType
ClassLabelType
;
typedef
ContingencyTable
<
ClassLabelType
>
ContingencyTableType
;
typedef
ContingencyTableType
::
Pointer
ContingencyTablePointerType
;
protected:
void
DoInit
()
...
...
@@ -72,32 +76,57 @@ protected:
void
DoExecute
()
{
// Enforce the need of class field name in supervised mode
if
(
GetClassifierCategory
()
==
Supervised
)
m_FeaturesInfo
.
SetClassFieldNames
(
GetChoiceNames
(
"cfield"
),
GetSelectedItems
(
"cfield"
)
);
if
(
m_FeaturesInfo
.
m_SelectedCFieldIdx
.
empty
()
&&
GetClassifierCategory
()
==
Supervised
)
{
m_FeaturesInfo
.
SetClassFieldNames
(
GetChoiceNames
(
"cfield"
),
GetSelectedItems
(
"cfield"
)
);
otbAppLogFATAL
(
<<
"No field has been selected for data labelling!"
);
}
if
(
m_FeaturesInfo
.
m_SelectedCFieldIdx
.
empty
()
)
{
otbAppLogFATAL
(
<<
"No field has been selected for data labelling!"
);
}
Superclass
::
DoExecute
();
if
(
GetClassifierCategory
()
==
Supervised
)
{
ConfusionMatrixCalculatorType
::
Pointer
confMatCalc
=
ComputeConfusionMatrix
(
m_PredictedList
,
m_ClassificationSamplesWithLabel
.
labeledListSample
);
WriteConfusionMatrix
(
confMatCalc
);
}
else
{
ContingencyTablePointerType
table
=
ComputeContingencyTable
(
m_PredictedList
,
m_ClassificationSamplesWithLabel
.
labeledListSample
);
WriteContingencyTable
(
table
);
}
}
Superclass
::
DoExecute
();
ContingencyTablePointerType
ComputeContingencyTable
(
const
TargetListSampleType
::
Pointer
&
predictedListSample
,
const
TargetListSampleType
::
Pointer
&
performanceLabeledListSample
)
{
typedef
ContingencyTableCalculator
<
ClassLabelType
>
ContigencyTableCalcutaltorType
;
if
(
GetClassifierCategory
()
==
Supervised
)
{
ConfusionMatrixCalculatorType
::
Pointer
confMatCalc
=
ComputeConfusionMatrix
(
m_PredictedList
,
m_ClassificationSamplesWithLabel
.
labeledListSample
);
WriteConfusionMatrix
(
confMatCalc
);
}
else
{
// TODO Compute Contingency Table
}
ContigencyTableCalcutaltorType
::
Pointer
contingencyTableCalculator
=
ContigencyTableCalcutaltorType
::
New
();
contingencyTableCalculator
->
Compute
(
performanceLabeledListSample
->
Begin
(),
performanceLabeledListSample
->
End
(),
predictedListSample
->
Begin
(),
predictedListSample
->
End
());
otbAppLogINFO
(
"Training performances:"
);
otbAppLogINFO
(
<<
"Contingency table: reference labels (rows) vs. produced labels (cols)
\n
"
<<
contingencyTableCalculator
->
BuildContingencyTable
());
return
contingencyTableCalculator
->
BuildContingencyTable
();
}
void
WriteContingencyTable
(
const
ContingencyTablePointerType
&
table
)
{
if
(
IsParameterEnabled
(
"io.confmatout"
))
{
// Write contingency table
std
::
ofstream
outFile
;
outFile
.
open
(
this
->
GetParameterString
(
"io.confmatout"
).
c_str
()
);
outFile
<<
table
->
ToCSV
();
}
}
ConfusionMatrixCalculatorType
::
Pointer
ComputeConfusionMatrix
(
const
TargetListSampleType
::
Pointer
&
predictedListSample
,
...
...
@@ -111,7 +140,7 @@ protected:
confMatCalc
->
SetProducedLabels
(
predictedListSample
);
confMatCalc
->
Compute
();
otbAppLogINFO
(
"
training performances
"
);
otbAppLogINFO
(
"
Training performances:
"
);
LogConfusionMatrix
(
confMatCalc
);
for
(
unsigned
int
itClasses
=
0
;
itClasses
<
confMatCalc
->
GetNumberOfClasses
();
itClasses
++
)
...
...
Modules/Applications/AppClassification/include/otbLearningApplicationBase.txx
View file @
c4751d32
...
...
@@ -33,6 +33,7 @@ namespace Wrapper
template <class TInputValue, class TOutputValue>
LearningApplicationBase<TInputValue,TOutputValue>
::LearningApplicationBase() : m_RegressionFlag(false)
{
}
...
...
@@ -59,7 +60,9 @@ LearningApplicationBase<TInputValue,TOutputValue>
InitUnsupervisedClassifierParams();
std::vector<std::string> allClassifier = GetChoiceKeys("classifier");
m_UnsupervisedClassifier.assign(allClassifier.begin() + m_SupervisedClassifier.size(), allClassifier.end());
// Check for empty unsupervised classifier
if( allClassifier.size() > m_UnsupervisedClassifier.size() )
m_UnsupervisedClassifier.assign( allClassifier.begin() + m_SupervisedClassifier.size(), allClassifier.end() );
}
template <class TInputValue, class TOutputValue>
...
...
@@ -67,10 +70,16 @@ typename LearningApplicationBase<TInputValue,TOutputValue>::ClassifierCategory
LearningApplicationBase<TInputValue,TOutputValue>
::GetClassifierCategory()
{
bool foundUnsupervised =
std::find(m_UnsupervisedClassifier.begin(), m_UnsupervisedClassifier.end(),
GetParameterString("classifier")) != m_UnsupervisedClassifier.end();
return foundUnsupervised ? Unsupervised : Supervised;
if( m_UnsupervisedClassifier.empty() )
{
return Supervised;
}
else
{
bool foundUnsupervised = std::find( m_UnsupervisedClassifier.begin(), m_UnsupervisedClassifier.end(),
GetParameterString( "classifier" ) ) != m_UnsupervisedClassifier.end();
return foundUnsupervised ? Unsupervised : Supervised;
}
}
template <class TInputValue, class TOutputValue>
...
...
Modules/Applications/AppClassification/include/otbTrainVectorBase.h
View file @
c4751d32
...
...
@@ -125,7 +125,7 @@ protected:
{
m_SelectedCFieldIdx
=
selectedCFieldIdx
;
// Handle only one class field name, if several are provided only the first one is used.
m_SelectedCFieldName
=
cFieldNames
[
selectedCFieldIdx
.
front
()];
m_SelectedCFieldName
=
selectedCFieldIdx
.
empty
()
?
cFieldNames
.
front
()
:
cFieldNames
[
selectedCFieldIdx
.
front
()];
}
};
...
...
Modules/Applications/AppClassification/test/CMakeLists.txt
View file @
c4751d32
...
...
@@ -392,6 +392,7 @@ otb_test_application(NAME apTvComputeConfusionMatrixExtraReferenceLabelsR
OPTIONS -in
${
INPUTDATA
}
/Classification/QB_1_ortho_C7.tif
-ref raster
-ref.raster.in
${
INPUTDATA
}
/Classification/clLabeledImageQB456_1_NoData_255.tif
-ref.raster.nodata 255
-nodatalabel 255
-out
${
TEMP
}
/apTvComputeConfusionMatrixExtraRefLabelsROut.csv
VALID --compare-ascii
${
NOTOL
}
...
...
@@ -403,6 +404,7 @@ otb_test_application(NAME apTvComputeConfusionMatrixR
OPTIONS -in
${
OTBAPP_BASELINE
}
/clLabeledImageQB123_1.tif
-ref raster
-ref.raster.in
${
INPUTDATA
}
/Classification/clLabeledImageQB456_1_NoData_255.tif
-ref.raster.nodata 255
-nodatalabel 255
-out
${
TEMP
}
/apTvComputeConfusionMatrixTconfusionROut.csv
VALID --compare-ascii
${
NOTOL
}
...
...
@@ -414,12 +416,27 @@ otb_test_application(NAME apTvComputeConfusionMatrixExtraProducedLabelsR
OPTIONS -in
${
INPUTDATA
}
/Classification/clLabeledImageQB456_1_NoData_255.tif
-ref raster
-ref.raster.in
${
INPUTDATA
}
/Classification/QB_1_ortho_C8.tif
-ref.raster.nodata 255
-nodatalabel 255
-out
${
TEMP
}
/apTvComputeConfusionMatrixExtraProdLabelsROut.csv
VALID --compare-ascii
${
NOTOL
}
${
OTBAPP_BASELINE_FILES
}
/apTvComputeConfusionMatrixExtraProdLabelsROut.csv
${
TEMP
}
/apTvComputeConfusionMatrixExtraProdLabelsROut.csv
)
#----------- ComputeContingencyTable TESTS ----------------
otb_test_application
(
NAME apTvComputeContingencyTableExtraProducedLabelsR
APP ComputeConfusionMatrix
OPTIONS -in
${
INPUTDATA
}
/Classification/clLabeledImageQB456_1_NoData_255.tif
-ref raster
-ref.raster.in
${
INPUTDATA
}
/Classification/QB_1_ortho_C8.tif
-ref.raster.nodata 255
-nodatalabel 255
-format contingencytable
-out
${
TEMP
}
/apTvComputeContingencyTableExtraProdLabelsROut.csv
VALID --compare-ascii
${
NOTOL
}
${
OTBAPP_BASELINE_FILES
}
/apTvComputeContingencyTableExtraProdLabelsROut.csv
${
TEMP
}
/apTvComputeContingencyTableExtraProdLabelsROut.csv
)
#----------- FusionOfClassifications TESTS ----------------
otb_test_application
(
NAME apTvFusionOfClassificationsDSPrecision6Inputs
...
...
Modules/Learning/Unsupervised/include/otbContingencyTable.h
0 → 100644
View file @
c4751d32
/*
* 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.
*/
#ifndef otbContingencyTable_h
#define otbContingencyTable_h
#include <vector>
#include <iostream>
#include <iomanip>
#include <itkObject.h>
#include <itkObjectFactory.h>
#include <itkVariableSizeMatrix.h>
namespace
otb
{
template
<
class
TClassLabel
>
class
ContingencyTable
:
public
itk
::
Object
{
public:
/** Standard class typedefs */
typedef
ContingencyTable
Self
;
typedef
itk
::
Object
Superclass
;
typedef
itk
::
SmartPointer
<
Self
>
Pointer
;
typedef
itk
::
SmartPointer
<
const
Self
>
ConstPointer
;
/** Run-time type information (and related methods). */
itkTypeMacro
(
ContingencyTableCalculator
,
itk
::
Object
);
/** Method for creation through the object factory. */
itkNewMacro
(
Self
);
typedef
itk
::
VariableSizeMatrix
<
unsigned
long
>
MatrixType
;
typedef
std
::
vector
<
TClassLabel
>
LabelList
;
MatrixType
matrix
;
void
SetLabels
(
const
LabelList
&
referenceLabels
,
const
LabelList
&
producedLabels
)
{
m_RefLabels
=
referenceLabels
;
m_ProdLabels
=
producedLabels
;
unsigned
int
rows
=
static_cast
<
unsigned
int
>
(
m_RefLabels
.
size
());
unsigned
int
cols
=
static_cast
<
unsigned
int
>
(
m_ProdLabels
.
size
());
matrix
.
SetSize
(
rows
,
cols
);
matrix
.
Fill
(
0
);
}
friend
std
::
ostream
&
operator
<<
(
std
::
ostream
&
o
,
const
ContingencyTable
<
TClassLabel
>
&
contingencyTable
)
{
// Retrieve the maximal width from the matrix and the labels
size_t
maxWidth
=
6
;
maxWidth
=
GetLabelsMaximumLength
(
contingencyTable
.
m_ProdLabels
,
maxWidth
);
maxWidth
=
GetLabelsMaximumLength
(
contingencyTable
.
m_RefLabels
,
maxWidth
);
for
(
unsigned
int
i
=
0
;
i
<
contingencyTable
.
matrix
.
Rows
();
++
i
)
{
for
(
unsigned
int
j
=
0
;
j
<
contingencyTable
.
matrix
.
Cols
();
++
j
)
{
std
::
ostringstream
oss
;
oss
<<
contingencyTable
.
matrix
(
i
,
j
);
size_t
length
=
oss
.
str
().
length
();
if
(
length
>
maxWidth
)
maxWidth
=
length
;
}
}
int
width
=
static_cast
<
int
>
(
maxWidth
)
+
1
;
// Write the first line of the matrix (produced labels)
o
<<
std
::
setfill
(
' '
)
<<
std
::
setw
(
width
)
<<
"labels"
;
for
(
size_t
i
=
0
;
i
<
contingencyTable
.
m_ProdLabels
.
size
();
++
i
)
{
o
<<
std
::
setfill
(
' '
)
<<
std
::
setw
(
width
)
<<
contingencyTable
.
m_ProdLabels
[
i
];
}
o
<<
std
::
endl
;
// For each line write the reference label, then the count value
for
(
unsigned
int
i
=
0
;
i
<
contingencyTable
.
matrix
.
Rows
();
++
i
)
{
o
<<
std
::
setfill
(
' '
)
<<
std
::
setw
(
width
)
<<
contingencyTable
.
m_RefLabels
[
i
];
for
(
unsigned
int
j
=
0
;
j
<
contingencyTable
.
matrix
.
Cols
();
++
j
)
{
o
<<
std
::
setfill
(
' '
)
<<
std
::
setw
(
width
)
<<
contingencyTable
.
matrix
(
i
,
j
);
}
o
<<
std
::
endl
;
}
return
o
;
}
std
::
string
ToCSV
()
const
{
const
char
separator
=
','
;
std
::
ostringstream
oss
;
oss
<<
"labels"
;
for
(
size_t
i
=
0
;
i
<
m_ProdLabels
.
size
();
++
i
)
{
oss
<<
separator
<<
m_ProdLabels
[
i
];
}
oss
<<
std
::
endl
;
// For each line write the reference label, then the count value
for
(
unsigned
int
i
=
0
;
i
<
matrix
.
Rows
();
++
i
)
{
oss
<<
m_RefLabels
[
i
];
for
(
unsigned
int
j
=
0
;
j
<
matrix
.
Cols
();
++
j
)
{
oss
<<
separator
<<
matrix
(
i
,
j
);
}
oss
<<
std
::
endl
;
}
oss
<<
std
::
endl
;
return
oss
.
str
();
}
protected:
ContingencyTable
()
{
SetLabels
(
LabelList
{},
LabelList
{});
}
~
ContingencyTable
()
ITK_OVERRIDE
{}
void
PrintSelf
(
std
::
ostream
&
os
,
itk
::
Indent
itkNotUsed
(
indent
))
const
ITK_OVERRIDE
{
os
<<
*
this
;
}
private:
ContingencyTable
(
const
Self
&
);
//purposely not implemented
void
operator
=
(
const
Self
&
);
//purposely not implemented
static
size_t
GetLabelsMaximumLength
(
const
LabelList
&
labels
,
size_t
maxWidth
)
{
size_t
tmpMaxWidth
=
maxWidth
;
for
(
size_t
i
=
0
;
i
<
labels
.
size
();
++
i
)
{
std
::
ostringstream
oss
;
oss
<<
labels
[
i
];
size_t
length
=
oss
.
str
().
length
();
if
(
length
>
tmpMaxWidth
)
tmpMaxWidth
=
length
;
}
return
tmpMaxWidth
;
}
LabelList
m_RefLabels
;
LabelList
m_ProdLabels
;
};
}
#endif //otbContingencyTable_h
Modules/Learning/Unsupervised/include/otbContingencyTableCalculator.h
0 → 100644
View file @
c4751d32
/*
* 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.
*/
#ifndef otbContingencyTableCalculator_h
#define otbContingencyTableCalculator_h
#include "itkObject.h"
#include "itkObjectFactory.h"
#include "otbContingencyTable.h"
#include <map>
namespace
otb
{
/**
* \brief ContingencyTableCalculator provide facilities to compute ContingencyTable.
*
* The ContingencyTableCalculator can be used with different structure type,
* the size of the label list should be the same for both list.
*
* \tparam TClassLabel the label data type
* \ingroup OTBUnsupervised
*/
template
<
class
TClassLabel
>
class
ITK_EXPORT
ContingencyTableCalculator
:
public
itk
::
Object
{
public:
/** Standard class typedefs */
typedef
ContingencyTableCalculator
Self
;
typedef
itk
::
Object
Superclass
;
typedef
itk
::
SmartPointer
<
Self
>
Pointer
;
typedef
itk
::
SmartPointer
<
const
Self
>
ConstPointer
;
/** Run-time type information (and related methods). */
itkTypeMacro
(
ContingencyTableCalculator
,
itk
::
Object
);
/** Method for creation through the object factory. */
itkNewMacro
(
Self
);
typedef
ContingencyTable
<
TClassLabel
>
ContingencyTableType
;
typedef
typename
ContingencyTableType
::
Pointer
ContingencyTablePointerType
;
typedef
typename
std
::
map
<
TClassLabel
,
unsigned
long
>
CountMapType
;
typedef
typename
std
::
map
<
TClassLabel
,
CountMapType
>
MapOfClassesType
;
/** Populate the confusion Matrix for a image iteration.
* \tparam TRefListLabel data structure type which contain the reference labels.
* \tparam TProdListLabel data structure type which contain the produced labels.
*/
template
<
class
TRefIterator
,
class
TProdIterator
>
void
Compute
(
TRefIterator
itRef
,
TProdIterator
itProd
,
bool
refHasNoData
=
false
,
typename
TRefIterator
::
InternalPixelType
refNoData
=
0
,
bool
prodHasNoData
=
false
,
typename
TProdIterator
::
InternalPixelType
prodNoData
=
0
);
/** Populate the confusion Matrix with input which provide GetMeasurementVector()[0] access
* \tparam TRefListLabel data structure type which contain the reference labels.
* \tparam TProdListLabel data structure type which contain the produced labels.
*/
template
<
class
TRefIterator
,
class
TProdIterator
>
void
Compute
(
TRefIterator
refBegin
,
TRefIterator
refEnd
,
TProdIterator
prodBegin
,
TProdIterator
prodEnd
);
itkGetConstMacro
(
NumberOfRefClasses
,
unsigned
long
);
itkGetConstMacro
(
NumberOfProdClasses
,
unsigned
long
);
itkGetConstMacro
(
NumberOfSamples
,
unsigned
long
);
void
Clear
();
ContingencyTablePointerType
BuildContingencyTable
();
protected:
ContingencyTableCalculator
();
~
ContingencyTableCalculator
()
ITK_OVERRIDE
{}
//void PrintSelf(std::ostream& os, itk::Indent indent) const ITK_OVERRIDE;
private:
ContingencyTableCalculator
(
const
Self
&
);
//purposely not implemented
void
operator
=
(
const
Self
&
);
//purposely not implemented
MapOfClassesType
m_LabelCount
;
unsigned
long
m_NumberOfRefClasses
;
unsigned
long
m_NumberOfProdClasses
;
unsigned
long
m_NumberOfSamples
;
};
}
#ifndef OTB_MANUAL_INSTANTIATION
#include "otbContingencyTableCalculator.txx"
#endif
#endif //otbContingencyTableCalculator_h
Modules/Learning/Unsupervised/include/otbContingencyTableCalculator.txx
0 → 100644
View file @
c4751d32
/*
* 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.
*/
#ifndef otbContingencyTableCalculator_txx
#define otbContingencyTableCalculator_txx
#include "otbContingencyTableCalculator.h"
#include "itkMacro.h"
#include "itkVariableLengthVector.h"
#include "itkListSample.h"
#include <set>
#include <iterator>
namespace otb
{
template<class TClassLabel>
ContingencyTableCalculator<TClassLabel>::ContingencyTableCalculator()
: m_NumberOfRefClasses(0), m_NumberOfProdClasses(0), m_NumberOfSamples(0)
{}
template<class TClassLabel>
void
ContingencyTableCalculator<TClassLabel>
::Clear()
{
m_LabelCount.clear();
m_NumberOfRefClasses = 0;
m_NumberOfProdClasses = 0;
m_NumberOfSamples = 0;
}
template<class TClassLabel>
template<class TRefIterator, class TProdIterator>
void
ContingencyTableCalculator<TClassLabel>
::Compute(TRefIterator refBegin, TRefIterator refEnd, TProdIterator prodBegin, TProdIterator prodEnd)
{
while( refBegin != refEnd && prodBegin != prodEnd )
{
++m_LabelCount[refBegin.GetMeasurementVector()[0]][prodBegin.GetMeasurementVector()[0]];
++refBegin;
++prodBegin;
++m_NumberOfSamples;
}
if( refBegin != refEnd || prodBegin != prodEnd )
itkExceptionMacro(<< "The references and produced labels did not end simultaneously.");
}
template<class TClassLabel>
template<class TRefIterator, class TProdIterator>
void
ContingencyTableCalculator<TClassLabel>
::Compute(TRefIterator itRef, TProdIterator itProd, bool refHasNoData, typename TRefIterator::InternalPixelType refNoData,
bool prodHasNoData, typename TProdIterator::InternalPixelType prodNoData)
{
while( !itRef.IsAtEnd() && !itProd.IsAtEnd() )
{
if((!prodHasNoData || itProd.Get()!=prodNoData)
&&(!refHasNoData || itRef.Get()!=refNoData))
{
++m_LabelCount[itRef.Get()][itProd.Get()];
++m_NumberOfSamples;
}
++itRef;
++itProd;
}
if( !itRef.IsAtEnd() || !itProd.IsAtEnd() )
itkExceptionMacro(<< "The references and produced labels did not end simultaneously.");
}
template<class TClassLabel>
typename ContingencyTableCalculator<TClassLabel>::ContingencyTablePointerType
ContingencyTableCalculator<TClassLabel>
::BuildContingencyTable()
{
std::set<TClassLabel> refLabels;
std::set<TClassLabel> prodLabels;
// Retrieve all labels needed to iterate over all labelCount
for(typename MapOfClassesType::const_iterator refIt = m_LabelCount.begin(); refIt != m_LabelCount.end(); ++refIt)
{
refLabels.insert(refIt->first);
CountMapType cmt = refIt->second;
for(typename CountMapType::const_iterator prodIt = cmt.begin(); prodIt != cmt.end(); ++prodIt)
{
prodLabels.insert(prodIt->first);
}
}
m_NumberOfRefClasses = refLabels.size();
m_NumberOfProdClasses = prodLabels.size();
unsigned int rows = static_cast<unsigned int>(m_NumberOfRefClasses);
unsigned int cols = static_cast<unsigned int>(m_NumberOfProdClasses);
std::vector<TClassLabel> referenceLabels(refLabels.begin(), refLabels.end());
std::vector<TClassLabel> producedLabels(prodLabels.begin(), prodLabels.end());
ContingencyTablePointerType contingencyTable = ContingencyTableType::New();
contingencyTable->SetLabels(referenceLabels, producedLabels);
for( unsigned int i = 0; i < rows; ++i )
for( unsigned int j = 0; j < cols; ++j )
contingencyTable->matrix(i,j) = m_LabelCount[referenceLabels[i]][producedLabels[j]];
return contingencyTable;
}
}
#endif
Modules/Learning/Unsupervised/include/otbSharkKMeansMachineLearningModel.h
View file @
c4751d32
...
...
@@ -20,6 +20,7 @@
#ifndef otbSharkKMeansMachineLearningModel_h
#define otbSharkKMeansMachineLearningModel_h
#include "boost/shared_ptr.hpp"
#include "itkLightObject.h"
#include "otbMachineLearningModel.h"
...
...
Modules/Learning/Unsupervised/include/otbSharkKMeansMachineLearningModel.txx
View file @
c4751d32
...
...
@@ -21,6 +21,7 @@
#define otbSharkKMeansMachineLearningModel_txx
#include <fstream>
#include "boost/make_shared.hpp"
#include "itkMacro.h"
#include "otbSharkKMeansMachineLearningModel.h"
...
...
@@ -54,7 +55,7 @@ SharkKMeansMachineLearningModel<TInputValue, TOutputValue>
m_Normalized( false ), m_K(2), m_MaximumNumberOfIterations( 10 )
{
// Default set HardClusteringModel
m_ClusteringModel = boost::
shared_ptr<ClusteringModelType>(new ClusteringModelType( &m_Centroids )
);
m_ClusteringModel = boost::
make_shared<ClusteringModelType>( &m_Centroids
);
}
...
...
@@ -81,7 +82,7 @@ SharkKMeansMachineLearningModel<TInputValue, TOutputValue>
// Use a Hard Clustering Model for classification
shark::kMeans( data, m_K, m_Centroids, m_MaximumNumberOfIterations );
m_ClusteringModel = boost::
shared_ptr<ClusteringModelType>(new ClusteringModelType( &m_Centroids )
);
m_ClusteringModel = boost::
make_shared<ClusteringModelType>( &m_Centroids
);
}
template<class TInputValue, class TOutputValue>
...
...
@@ -153,7 +154,7 @@ SharkKMeansMachineLearningModel<TInputValue, TOutputValue>
shark::Data<ClusteringOutputType> clusters;
try
{
clusters = ( *m_ClusteringModel )( inputSamples );
clusters = ( *m_ClusteringModel )( inputSamples );
}
catch( ... )
{
...
...
Modules/Learning/Unsupervised/test/CMakeLists.txt
View file @
c4751d32
...
...
@@ -3,10 +3,24 @@ set(OTBUnsupervisedTests
otbUnsupervisedTestDriver.cxx
otbMachineLearningUnsupervisedModelCanRead.cxx
otbTrainMachineLearningUnsupervisedModel.cxx
otbContingencyTableCalculatorTest.cxx
)
# Tests Declaration
otb_add_test
(
NAME leTuContingencyTableCalculatorNew COMMAND otbUnsupervisedTestDriver
otbContingencyTableCalculatorNew
)
otb_add_test
(
NAME leTvContingencyTableCalculatorSetListSamples COMMAND otbUnsupervisedTestDriver
otbContingencyTableCalculatorSetListSamples 1000 4
)
otb_add_test
(
NAME leTvContingencyTableCalculatorUpdate COMMAND otbUnsupervisedTestDriver
otbContingencyTableCalculatorCompute 4 3
)
otb_add_test
(
NAME leTvContingencyTableCalculatorUpdateWithBaseline COMMAND otbUnsupervisedTestDriver
otbContingencyTableCalculatorComputeWithBaseline
)
if
(
OTB_USE_SHARK
)
set
(
OTBUnsupervisedTests
${
OTBUnsupervisedTests
}
otbSharkUnsupervisedImageClassificationFilter.cxx
)
include
(
tests-shark.cmake
)
...
...
Modules/Learning/Unsupervised/test/otbContingencyTableCalculatorTest.cxx
0 → 100644
View file @
c4751d32
/*
* 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 "itkListSample.h"
#include "otbContingencyTableCalculator.h"
int
otbContingencyTableCalculatorNew
(
int
itkNotUsed
(
argc
),
char
*
itkNotUsed
(
argv
)
[])
{
typedef
int
ClassLabelType
;
typedef
otb
::
ContingencyTableCalculator
<
ClassLabelType
>
CalculatorType
;
CalculatorType
::
Pointer
calculator
=
CalculatorType
::
New
();