From a30fb6f27767731a488855658600a4772e853692 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=A9dric=20Traizet?= <traizetc@cesbio.cnes.fr>
Date: Tue, 20 Jun 2017 10:02:30 +0200
Subject: [PATCH] som is now a machine learning model, ae and pca have been
 deactiveted

---
 app/cbDimensionalityReductionTrainer.cxx      |  2 +-
 include/AutoencoderModel.txx                  |  1 -
 include/DimensionalityReductionModelFactory.h |  2 +-
 .../DimensionalityReductionModelFactory.txx   | 15 +++---
 include/SOMModel.h                            | 52 +++++++++++--------
 include/SOMModel.txx                          |  3 +-
 include/cbLearningApplicationBaseDR.h         | 25 +++++----
 include/cbLearningApplicationBaseDR.txx       |  7 +--
 otb-module.cmake                              |  2 +-
 9 files changed, 59 insertions(+), 50 deletions(-)

diff --git a/app/cbDimensionalityReductionTrainer.cxx b/app/cbDimensionalityReductionTrainer.cxx
index dd245a44e5..0f085d4c49 100644
--- a/app/cbDimensionalityReductionTrainer.cxx
+++ b/app/cbDimensionalityReductionTrainer.cxx
@@ -9,7 +9,7 @@
 #include "otbShiftScaleSampleListFilter.h"
 #include "otbStatisticsXMLFileReader.h"
 
-#include "otbSharkUtils.h"
+//#include "otbSharkUtils.h"
 
 #include <fstream> // write the model file
 
diff --git a/include/AutoencoderModel.txx b/include/AutoencoderModel.txx
index b415e3d1d0..a23adb9ba2 100644
--- a/include/AutoencoderModel.txx
+++ b/include/AutoencoderModel.txx
@@ -1,4 +1,3 @@
-
 #ifndef AutoencoderModel_txx
 #define AutoencoderModel_txx
 
diff --git a/include/DimensionalityReductionModelFactory.h b/include/DimensionalityReductionModelFactory.h
index e7a913cc07..791199b5e8 100644
--- a/include/DimensionalityReductionModelFactory.h
+++ b/include/DimensionalityReductionModelFactory.h
@@ -20,7 +20,7 @@
 
 #include "DimensionalityReductionModel.h"
 #include "otbMachineLearningModelFactoryBase.h"
-
+ 
 namespace otb
 {
 /** \class MachineLearningModelFactory
diff --git a/include/DimensionalityReductionModelFactory.txx b/include/DimensionalityReductionModelFactory.txx
index 5ccf34a388..a546978fb7 100644
--- a/include/DimensionalityReductionModelFactory.txx
+++ b/include/DimensionalityReductionModelFactory.txx
@@ -22,25 +22,25 @@
 #include "otbConfigure.h"
 
 #include "SOMModelFactory.h"
-
+/*
 #ifdef OTB_USE_SHARK
 #include "AutoencoderModelFactory.h"
 #include "PCAModelFactory.h"
 #endif
-
+*/
 #include "itkMutexLockHolder.h"
 
 
 namespace otb
 {
-	
+/*	
 template <class TInputValue, class TTargetValue>
 using AutoencoderModelFactory = AutoencoderModelFactoryBase<TInputValue, TTargetValue, shark::Autoencoder<shark::TanhNeuron, shark::LinearNeuron>>  ;
 
 
 template <class TInputValue, class TTargetValue>
 using TiedAutoencoderModelFactory = AutoencoderModelFactoryBase<TInputValue, TTargetValue, shark::TiedAutoencoder< shark::TanhNeuron, shark::LinearNeuron>>  ;
-
+*/
 
 template <class TInputValue, class TTargetValue>
 using SOM2DModelFactory = SOMModelFactory<TInputValue, TTargetValue, 2>  ;
@@ -115,13 +115,13 @@ DimensionalityReductionModelFactory<TInputValue,TOutputValue>
   RegisterFactory(SOM3DModelFactory<TInputValue,TOutputValue>::New());
   RegisterFactory(SOM4DModelFactory<TInputValue,TOutputValue>::New());
   RegisterFactory(SOM5DModelFactory<TInputValue,TOutputValue>::New());
-  
+  /*
 #ifdef OTB_USE_SHARK
   RegisterFactory(PCAModelFactory<TInputValue,TOutputValue>::New());
   RegisterFactory(AutoencoderModelFactory<TInputValue,TOutputValue>::New());
   RegisterFactory(TiedAutoencoderModelFactory<TInputValue,TOutputValue>::New());
 #endif
-  
+  */
 }
 
 template <class TInputValue, class TOutputValue>
@@ -182,6 +182,7 @@ DimensionalityReductionModelFactory<TInputValue,TOutputValue>
       itk::ObjectFactoryBase::UnRegisterFactory(som2dFactory);
       continue;
       }
+      /*
 #ifdef OTB_USE_SHARK
 	
 	// Autoencoder
@@ -210,7 +211,7 @@ DimensionalityReductionModelFactory<TInputValue,TOutputValue>
       continue;
       }
 #endif
-
+*/
     }
 
 }
diff --git a/include/SOMModel.h b/include/SOMModel.h
index 290ee45c60..846b0f2a2e 100644
--- a/include/SOMModel.h
+++ b/include/SOMModel.h
@@ -1,7 +1,7 @@
 #ifndef SOMModel_h
 #define SOMModel_h
 
-#include "DimensionalityReductionModel.h"
+//#include "DimensionalityReductionModel.h"
 #include "otbSOMMap.h"
 
 #include "otbSOM.h"
@@ -11,38 +11,45 @@
 #include "otbCzihoSOMLearningBehaviorFunctor.h"
 #include "otbCzihoSOMNeighborhoodBehaviorFunctor.h"
 
-
+#include "otbMachineLearningModelTraits.h"
+#include "otbMachineLearningModel.h"
 
 namespace otb
 {
 template <class TInputValue, unsigned int MapDimension>
-class ITK_EXPORT SOMModel: public  DimensionalityReductionModel<TInputValue,TInputValue>   
+class ITK_EXPORT SOMModel: public  MachineLearningModel<itk::VariableLengthVector< TInputValue> , itk::VariableLengthVector< TInputValue>>   
 {
 
 public:
 	
 	typedef SOMModel Self;
-	typedef DimensionalityReductionModel<TInputValue,TInputValue> Superclass;
+	typedef MachineLearningModel<itk::VariableLengthVector< TInputValue> , itk::VariableLengthVector< TInputValue>> Superclass;
 	typedef itk::SmartPointer<Self> Pointer;
 	typedef itk::SmartPointer<const Self> ConstPointer;
 
-	typedef typename Superclass::InputValueType InputValueType;
-	typedef typename Superclass::InputSampleType InputSampleType;
-	typedef typename Superclass::InputListSampleType InputListSampleType;
-	typedef typename InputListSampleType::Pointer ListSamplePointerType;
-	typedef typename Superclass::TargetValueType TargetValueType;
-	typedef typename Superclass::TargetSampleType TargetSampleType;
-	typedef typename Superclass::TargetListSampleType TargetListSampleType;
+	typedef typename Superclass::InputValueType 			InputValueType;
+	typedef typename Superclass::InputSampleType 			InputSampleType;
+	typedef typename Superclass::InputListSampleType 		InputListSampleType;
+	typedef typename InputListSampleType::Pointer 			ListSamplePointerType;
+	typedef typename Superclass::TargetValueType 			TargetValueType;
+	typedef typename Superclass::TargetSampleType 			TargetSampleType;
+	typedef typename Superclass::TargetListSampleType 		TargetListSampleType;
 
-	typedef SOMMap<itk::VariableLengthVector<TInputValue>,itk::Statistics::EuclideanDistanceMetric<itk::VariableLengthVector<TInputValue>>, MapDimension> MapType;
-	typedef typename MapType::SizeType       SizeType;
-	typedef typename MapType::SpacingType       SpacingType;
+	/// Confidence map related typedefs
 	
-	typedef otb::SOM<InputListSampleType, MapType> EstimatorType;
+	typedef typename Superclass::ConfidenceValueType  				ConfidenceValueType;
+	typedef typename Superclass::ConfidenceSampleType 				ConfidenceSampleType;
+	typedef typename Superclass::ConfidenceListSampleType      		ConfidenceListSampleType;
 
-	
-	typedef Functor::CzihoSOMLearningBehaviorFunctor    SOMLearningBehaviorFunctorType;
-	typedef Functor::CzihoSOMNeighborhoodBehaviorFunctor SOMNeighborhoodBehaviorFunctorType;
+
+
+	typedef SOMMap<itk::VariableLengthVector<TInputValue>,itk::Statistics::EuclideanDistanceMetric<itk::VariableLengthVector<TInputValue>>, MapDimension> MapType;
+	typedef typename MapType::SizeType       				SizeType;
+	typedef typename MapType::SpacingType       			SpacingType;
+	typedef otb::SOM<InputListSampleType, MapType> 			EstimatorType;
+
+	typedef Functor::CzihoSOMLearningBehaviorFunctor    	SOMLearningBehaviorFunctorType;
+	typedef Functor::CzihoSOMNeighborhoodBehaviorFunctor 	SOMNeighborhoodBehaviorFunctorType;
 
 	itkNewMacro(Self);
 	itkTypeMacro(SOMModel, DimensionalityReductionModel);
@@ -72,8 +79,8 @@ public:
 	bool CanReadFile(const std::string & filename);
 	bool CanWriteFile(const std::string & filename);
 
-	void Save(const std::string & filename, const std::string & name="")  ITK_OVERRIDE;
-	void Load(const std::string & filename, const std::string & name="")  ITK_OVERRIDE;
+	void Save(const std::string & filename, const std::string & name="") ;
+	void Load(const std::string & filename, const std::string & name="") ;
 
 	void Train() ITK_OVERRIDE;
 	//void Dimensionality_reduction()  {}; // Dimensionality reduction is done by DoPredict
@@ -83,11 +90,10 @@ protected:
 	SOMModel();	
 	~SOMModel() ITK_OVERRIDE;
  
-	virtual TargetSampleType DoPredict(const InputSampleType& input) const ITK_OVERRIDE;
-	
 private:
 	typename MapType::Pointer m_SOMMap;
-
+	
+	virtual TargetSampleType DoPredict(const InputSampleType& input, ConfidenceValueType * quality = ITK_NULLPTR) const;
 
 	/** Map Parameters used for training */
   
diff --git a/include/SOMModel.txx b/include/SOMModel.txx
index 13765ccbfb..29e226f4da 100644
--- a/include/SOMModel.txx
+++ b/include/SOMModel.txx
@@ -1,4 +1,3 @@
-
 #ifndef SOMModel_txx
 #define SOMModel_txx
 
@@ -177,7 +176,7 @@ void SOMModel<TInputValue, MapDimension>::Load(const std::string & filename, con
 
 template <class TInputValue, unsigned int MapDimension>
 typename SOMModel<TInputValue, MapDimension>::TargetSampleType
-SOMModel<TInputValue, MapDimension>::DoPredict(const InputSampleType & value) const
+SOMModel<TInputValue, MapDimension>::DoPredict(const InputSampleType & value, ConfidenceValueType * quality) const
 { 
 	unsigned int dimension =MapType::ImageDimension;
     TargetSampleType target;
diff --git a/include/cbLearningApplicationBaseDR.h b/include/cbLearningApplicationBaseDR.h
index a45024aa47..71c31f40fe 100644
--- a/include/cbLearningApplicationBaseDR.h
+++ b/include/cbLearningApplicationBaseDR.h
@@ -15,12 +15,12 @@
 #include "DimensionalityReductionModelFactory.h"
 
 #include "SOMModel.h"
-
+/*
 #ifdef OTB_USE_SHARK
 #include "AutoencoderModel.h"
 #include "PCAModel.h"
 #endif
-
+*/
 namespace otb
 {
 namespace Wrapper
@@ -99,7 +99,7 @@ public:
 	typedef SOMMap<itk::VariableLengthVector<TInputValue>,itk::Statistics::EuclideanDistanceMetric<itk::VariableLengthVector<TInputValue>>, 5> Map5DType;
 	typedef otb::SOMModel<InputValueType, 5> SOM5DModelType;
 	
-
+/*
 #ifdef OTB_USE_SHARK
 	typedef shark::Autoencoder< shark::TanhNeuron, shark::LinearNeuron> AutoencoderType;
 	typedef otb::AutoencoderModel<InputValueType, AutoencoderType> AutoencoderModelType;
@@ -109,7 +109,7 @@ public:
 	
 	typedef otb::PCAModel<InputValueType> PCAModelType;
 #endif
-  
+  */
 protected:
   cbLearningApplicationBaseDR();
 
@@ -131,11 +131,15 @@ private:
 
   /** Specific Init and Train methods for each machine learning model */
   //@{
-
+  
+  void InitSOMParams();
+  template <class somchoice>
+  void TrainSOM(typename ListSampleType::Pointer trainingListSample, std::string modelPath);
+  void BeforeTrainSOM(typename ListSampleType::Pointer trainingListSample, std::string modelPath);
+/*
 #ifdef OTB_USE_SHARK
   void InitAutoencoderParams();
   void InitPCAParams();
-  void InitSOMParams();
   
   void BeforeTrainAutoencoder(typename ListSampleType::Pointer trainingListSample, std::string modelPath);
   template <class autoencoderchoice>
@@ -143,10 +147,8 @@ private:
   
   void TrainPCA(typename ListSampleType::Pointer trainingListSample, std::string modelPath);
   
-  template <class somchoice>
-  void TrainSOM(typename ListSampleType::Pointer trainingListSample, std::string modelPath);
-  void BeforeTrainSOM(typename ListSampleType::Pointer trainingListSample, std::string modelPath);
-#endif
+ 
+#endif  */
   //@}
 };
 
@@ -156,10 +158,11 @@ private:
 #ifndef OTB_MANUAL_INSTANTIATION
 #include "cbLearningApplicationBaseDR.txx"
 #include "cbTrainSOM.txx"
+/*
 #ifdef OTB_USE_SHARK
 #include "cbTrainAutoencoder.txx"
 #include "cbTrainPCA.txx"
-#endif
+#endif*/
 #endif
 
 #endif
diff --git a/include/cbLearningApplicationBaseDR.txx b/include/cbLearningApplicationBaseDR.txx
index 5603cca4f6..2befe3e396 100644
--- a/include/cbLearningApplicationBaseDR.txx
+++ b/include/cbLearningApplicationBaseDR.txx
@@ -50,11 +50,12 @@ cbLearningApplicationBaseDR<TInputValue,TOutputValue>
   
 
   InitSOMParams();
+  /*
 #ifdef OTB_USE_SHARK
   InitAutoencoderParams();
   InitPCAParams();
 #endif
-  
+  */
 }
 
 template <class TInputValue, class TOutputValue>
@@ -79,7 +80,7 @@ cbLearningApplicationBaseDR<TInputValue,TOutputValue>
 	{
 		BeforeTrainSOM(trainingListSample,modelPath);
 	}
- 
+ /*
  if(modelName == "autoencoder")
     {
     #ifdef OTB_USE_SHARK
@@ -104,7 +105,7 @@ cbLearningApplicationBaseDR<TInputValue,TOutputValue>
     #else
     otbAppLogFATAL("Module SharkLearning is not installed. You should consider turning OTB_USE_SHARK on during cmake configuration.");
     #endif
-    }
+    }*/
 }
 
 }
diff --git a/otb-module.cmake b/otb-module.cmake
index f4a7c8f8ca..258fdf1c44 100644
--- a/otb-module.cmake
+++ b/otb-module.cmake
@@ -6,9 +6,9 @@ otb_module(CbDimensionalityReduction
 		OTBITK
 		OTBShark	
 		OTBBoost
-		OTBSupervised
 		OTBAppClassification
 		OTBSOM
+		OTBLearningBase
 	DESCRIPTION 
 		"${DOCUMENTATION}"
 )
-- 
GitLab