From 64af665b6b1244a10b668bdfcadf6f3a75a6e4ac Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=A9dric=20Traizet?= <traizetc@cesbio.cnes.fr>
Date: Wed, 10 May 2017 11:40:15 +0200
Subject: [PATCH] bug can't find appli

---
 app/cbDimensionalityReduction.cxx        |   2 +-
 app/cbDimensionalityReductionTrainer.cxx | 141 +++++++++--------------
 include/AutoencoderModel.h               |  10 +-
 include/AutoencoderModel.txx             |   9 ++
 include/DimensionalityReductionModel.h   |  48 --------
 include/cbLearningApplicationBaseDR.h    |   4 +-
 include/cbTrainAutoencoder.txx           |  10 +-
 include/encode_filter.h                  |   8 +-
 8 files changed, 80 insertions(+), 152 deletions(-)
 delete mode 100644 include/DimensionalityReductionModel.h

diff --git a/app/cbDimensionalityReduction.cxx b/app/cbDimensionalityReduction.cxx
index b195d053ba..9dfa3d6810 100644
--- a/app/cbDimensionalityReduction.cxx
+++ b/app/cbDimensionalityReduction.cxx
@@ -93,7 +93,7 @@ private:
 
 	FilterType::Pointer filter_dim_reduc;
 	ExtractROIFilterType::Pointer m_ExtractROIFilter;
-
+	//d
 };
 }
 }
diff --git a/app/cbDimensionalityReductionTrainer.cxx b/app/cbDimensionalityReductionTrainer.cxx
index 9c7cc45142..98b88417f1 100644
--- a/app/cbDimensionalityReductionTrainer.cxx
+++ b/app/cbDimensionalityReductionTrainer.cxx
@@ -6,6 +6,9 @@
 
 #include "itkVariableLengthVector.h"
 
+#include "otbShiftScaleSampleListFilter.h"
+#include "otbStatisticsXMLFileReader.h"
+
 //#include "AutoencoderModel.h"
 
 #include "otbSharkUtils.h"
@@ -29,39 +32,6 @@
 
 #include "cbLearningApplicationBaseDR.h"
 
-template<class AutoencoderModel>
-AutoencoderModel trainAutoencoderModel(
-        shark::UnlabeledData<shark::RealVector> const& data,//the data to train with
-        std::size_t numHidden,//number of features in the autoencoder
-        std::size_t iterations, //number of iterations to optimize
-        double regularisation//strength of the regularisation
-){
-	//create the model
-	std::size_t inputs = dataDimension(data);
-	AutoencoderModel model;
-	model.setStructure(inputs, numHidden);
-	initRandomUniform(model,-0.1*std::sqrt(1.0/inputs),0.1*std::sqrt(1.0/inputs));
-	//create the objective function
-
-	shark::LabeledData<shark::RealVector,shark::RealVector> trainSet(data,data);//labels identical to inputs
-	shark::SquaredLoss<shark::RealVector> loss;
-	shark::ErrorFunction error(trainSet, &model, &loss);
-	shark::TwoNormRegularizer regularizer(error.numberOfVariables());
-	error.setRegularizer(regularisation,&regularizer);
-
-	shark::IRpropPlusFull optimizer;
-	error.init();
-	optimizer.init(error);
-	std::cout<<"Optimizing model: "+model.name()<<std::endl;
-	for(std::size_t i = 0; i != iterations; ++i){
-		optimizer.step(error);
-		std::cout<<i<<" "<<optimizer.solution().value<<std::endl;
-	}
-	//std::cout<<optimizer.solution().value<<std::endl;
-	model.setParameterVector(optimizer.solution().point);
-	return model;
-}
-
 shark::Normalizer<shark::RealVector> trainNormalizer(const shark::UnlabeledData<shark::RealVector>& data)
 {	
 	bool removeMean = true;
@@ -88,11 +58,24 @@ public:
 	itkTypeMacro(CbDimensionalityReductionTrainer, otb::Application);
 
 
-	typedef float ValueType;
-	typedef itk::VariableLengthVector<ValueType> InputSampleType;
-	typedef itk::Statistics::ListSample<InputSampleType> ListSampleType;
-	
+	typedef Superclass::SampleType              SampleType;
+	typedef Superclass::ListSampleType          ListSampleType;
+	typedef Superclass::SampleImageType         SampleImageType;
+	  
+	typedef double ValueType;
 	typedef itk::VariableLengthVector<ValueType> MeasurementType;
+
+	typedef otb::StatisticsXMLFileReader<SampleType> StatisticsReader;
+
+	typedef otb::Statistics::ShiftScaleSampleListFilter<ListSampleType, ListSampleType> ShiftScaleFilterType;
+
+
+
+	//typedef float ValueType;
+	//typedef itk::VariableLengthVector<ValueType> InputSampleType;
+	//typedef itk::Statistics::ListSample<InputSampleType> ListSampleType;
+	
+	//typedef itk::VariableLengthVector<ValueType> MeasurementType;
 	  
 	typedef otb::MachineLearningModelFactory<ValueType, ValueType>  ModelFactoryType;
 		
@@ -105,16 +88,7 @@ private:
 	{
 		SetName("CbDimensionalityReductionTrainer");
 		SetDescription("Trainer for the dimensionality reduction algorithms used in the cbDimensionalityReduction application.");
-		/*
-		AddParameter(ParameterType_InputVectorData, "train", "Name of the input training vector data");
-		SetParameterDescription("train","The vector data used for training.");
 	
-		AddParameter(ParameterType_StringList, "feat", "Field names to be calculated."); //
-		SetParameterDescription("feat","List of field names in the input vector data used as features for training."); //
-		
-		AddParameter(ParameterType_Int, "k","target dimension");
-		SetParameterDescription("k", "Dimension of the output feature vectors");
-*/
 		AddParameter(ParameterType_Group, "io", "Input and output data");
 		SetParameterDescription("io", "This group of parameters allows setting input and output data.");
 
@@ -124,6 +98,9 @@ private:
 		AddParameter(ParameterType_OutputFilename, "io.out", "Output model");
 		SetParameterDescription("io.out", "Output file containing the model estimated (.txt format).");
 
+		AddParameter(ParameterType_InputFilename, "io.stats", "Input XML image statistics file");
+		MandatoryOff("io.stats");
+		SetParameterDescription("io.stats", "XML file containing mean and variance of each feature.");
 
 		AddParameter(ParameterType_StringList, "feat", "Field names to be calculated."); //
 		SetParameterDescription("feat","List of field names in the input vector data used as features for training."); //
@@ -153,7 +130,7 @@ private:
 	void DoExecute()
 	{	
 
-		std::cout << "Appli !" << std::endl;
+		std::cout << "Appli Training!" << std::endl;
 
 		std::string shapefile = GetParameterString("io.vd");
 
@@ -175,51 +152,37 @@ private:
 			}
 			input->PushBack(mv);
 		}
-		/*
-		std::cout << input << std::endl;
-		std::vector<shark::RealVector> features;
-		otb::Shark::ListSampleToSharkVector<ListSampleType>( input, features);
-		shark::Data<shark::RealVector> inputSamples = shark::createDataFromRange( features );
 		
-		std::size_t numHidden= GetParameterInt("k");   
-		std::size_t iterations = 100;
-		double regularisation = 0; 
-		
-
-		shark::Normalizer<shark::RealVector> normalizer = trainNormalizer(inputSamples);
-		inputSamples = normalizer(inputSamples);
+// Statistics for shift/scale
+		MeasurementType meanMeasurementVector;
+		MeasurementType stddevMeasurementVector;
+		if (HasValue("io.stats") && IsParameterEnabled("io.stats"))
+		{
+			StatisticsReader::Pointer statisticsReader = StatisticsReader::New();
+			std::string XMLfile = GetParameterString("io.stats");
+			statisticsReader->SetFileName(XMLfile);
+			meanMeasurementVector = statisticsReader->GetStatisticVectorByName("mean");
+			stddevMeasurementVector = statisticsReader->GetStatisticVectorByName("stddev");
+		}
+		else
+		{
+			meanMeasurementVector.SetSize(nbFeatures);
+			meanMeasurementVector.Fill(0.);
+			stddevMeasurementVector.SetSize(nbFeatures);
+			stddevMeasurementVector.Fill(1.);
+		}
+    
+		ShiftScaleFilterType::Pointer trainingShiftScaleFilter = ShiftScaleFilterType::New();
+		trainingShiftScaleFilter->SetInput(input);
+		trainingShiftScaleFilter->SetShifts(meanMeasurementVector);
+		trainingShiftScaleFilter->SetScales(stddevMeasurementVector);
+		trainingShiftScaleFilter->Update();
+
+		ListSampleType::Pointer trainingListSample= trainingShiftScaleFilter->GetOutput();
 	
-		std::cout << "normalizer trained and training set normalized" << std::endl;
-
-		AutoencoderType net = trainAutoencoderModel<AutoencoderType>(inputSamples,numHidden,iterations,regularisation);
-		std::cout << "autoencoder trained !!!!" << std::endl;
-
-		// save the model to the file "net.model"
-		std::ofstream ofs("net.model");
-		shark::TextOutArchive oa(ofs);
-		net.write(oa);
-		ofs.close();
 	
-		// save the model to the file "net.model"
-		std::ofstream norm_ofs("normalizer.model");
-		boost::archive::polymorphic_text_oarchive onorm(norm_ofs);
-		normalizer.write(onorm);
-		norm_ofs.close();
-	*/
-
-		std::cout << "Using a Machine learning model" << std::endl;
-		/*
-		AutoencoderModelType::Pointer dimredTrainer = AutoencoderModelType::New();
-		dimredTrainer->SetNumberOfHiddenNeurons(5);
-		dimredTrainer->SetNumberOfIterations(50);
-		dimredTrainer->SetRegularization(0.1);
-		dimredTrainer->SetInputListSample(input);
-		dimredTrainer->Train();
-		dimredTrainer->Save("net.model");
-		std::cout << "ok" << std::endl;
-		*/
-		this->Train(input,GetParameterString("io.out"));
-		
+		this->Train(trainingListSample,GetParameterString("io.out"));
+		// d
 	}
 
 
diff --git a/include/AutoencoderModel.h b/include/AutoencoderModel.h
index 603f0dc4d2..e77e901ae1 100644
--- a/include/AutoencoderModel.h
+++ b/include/AutoencoderModel.h
@@ -51,10 +51,14 @@ public:
 	 
 
 protected:
-	AutoencoderModel(){};	
-private:
+	AutoencoderModel();	
+	//~AutoencoderModel() ITK_OVERRIDE;
+ 
 	virtual TargetSampleType DoPredict(const InputSampleType& input, ConfidenceValueType *quality=ITK_NULLPTR) const ITK_OVERRIDE;
-
+	virtual void DoPredictBatch(const InputListSampleType *, const unsigned int & startIndex, const unsigned int & size, TargetListSampleType *, ConfidenceListSampleType * = ITK_NULLPTR) const ITK_OVERRIDE;
+  
+private:
+	
 	AutoencoderType m_net;
 	unsigned int m_NumberOfHiddenNeurons;
 	unsigned int m_NumberOfIterations;
diff --git a/include/AutoencoderModel.txx b/include/AutoencoderModel.txx
index 50fc9940f9..72f9e2ef8f 100644
--- a/include/AutoencoderModel.txx
+++ b/include/AutoencoderModel.txx
@@ -14,6 +14,15 @@
 namespace otb
 {
 
+
+template <class TInputValue, class AutoencoderType>
+AutoencoderModel<TInputValue,AutoencoderType>::AutoencoderModel()
+{
+	//this->m_IsRegressionSupported = true;
+}
+
+
+
 template <class TInputValue, class AutoencoderType>
 void AutoencoderModel<TInputValue,AutoencoderType>::Train()
 {
diff --git a/include/DimensionalityReductionModel.h b/include/DimensionalityReductionModel.h
deleted file mode 100644
index 05066e976b..0000000000
--- a/include/DimensionalityReductionModel.h
+++ /dev/null
@@ -1,48 +0,0 @@
-#ifndef DimensionalityReductionModel_h
-#define DimensionalityReductionModel_h
-#include "itkObject.h"
-#include "itkListSample.h"
-namespace otb
-{
-template <class TInputValue>
-class DimensionalityReductionModel: public itk::Object
-{
-
-public:
-	
-	typedef DimensionalityReductionModel Self;
-	typedef itk::Object Superclass;
-	typedef itk::SmartPointer<Self> Pointer;
-	typedef itk::SmartPointer<const Self> ConstPointer;
-
-	typedef TInputValue InputValueType;
-	typedef itk::VariableLengthVector<InputValueType> InputSampleType;
-	typedef itk::Statistics::ListSample<InputSampleType> InputListSampleType;
-
-
-		
-	itkSetObjectMacro(InputListSample,InputListSampleType);
-	itkGetObjectMacro(InputListSample,InputListSampleType);
-	itkGetConstObjectMacro(InputListSample,InputListSampleType);
-
-	virtual void Save(const std::string & filename, const std::string & name="") = 0;
-	virtual void Load(const std::string & filename, const std::string & name="") = 0;
-
-	virtual void Train() = 0;
-	virtual void Dimensionality_reduction() = 0;
-
-protected:
-	DimensionalityReductionModel(){};	
-	typename InputListSampleType::Pointer m_InputListSample;
-
-
-};
-} // end namespace otb
-
-
-//#ifndef OTB_MANUAL_INSTANTIATION
-//#include "DimensionalityReductionModel.txx"
-//#endif
-
-
-#endif
diff --git a/include/cbLearningApplicationBaseDR.h b/include/cbLearningApplicationBaseDR.h
index fe42e2d875..ab359092d2 100644
--- a/include/cbLearningApplicationBaseDR.h
+++ b/include/cbLearningApplicationBaseDR.h
@@ -117,7 +117,7 @@ private:
 #ifdef OTB_USE_SHARK
   void InitAutoencoderParams();
   template <class autoencoderchoice>
-  void TrainAutoencoder(typename ListSampleType::Pointer trainingListSample, std::string modelPath){
+  void TrainAutoencoder(typename ListSampleType::Pointer trainingListSample, std::string modelPath);/*{
  // typename AutoencoderModelType::Pointer dimredTrainer = AutoencoderModelType::New();
   typename autoencoderchoice::Pointer dimredTrainer = autoencoderchoice::New();
 		dimredTrainer->SetNumberOfHiddenNeurons(GetParameterInt("model.autoencoder.nbneuron"));
@@ -126,7 +126,7 @@ private:
 		dimredTrainer->SetInputListSample(trainingListSample);
 		dimredTrainer->Train();
 		dimredTrainer->Save(modelPath);
-}; // !!!!!!!!!!!!!!!!! How to declare this method body in the .txx ? (double template...) 
+}; // !!!!!!!!!!!!!!!!! How to declare this method body in the .txx ? (double template...) */
 #endif
   //@}
 };
diff --git a/include/cbTrainAutoencoder.txx b/include/cbTrainAutoencoder.txx
index f28bad3297..feed6cf75e 100644
--- a/include/cbTrainAutoencoder.txx
+++ b/include/cbTrainAutoencoder.txx
@@ -44,13 +44,13 @@ cbLearningApplicationBaseDR<TInputValue,TOutputValue>
   SetParameterDescription("model.autoencoder.normalizer", 
                          "Strength of the L2 normalization used during training");
 }
-/*
+
 template <class TInputValue, class TOutputValue>
+template <typename autoencoderchoice>
 void cbLearningApplicationBaseDR<TInputValue,TOutputValue>
-::template < autoencoderchoice> TrainAutoencoder(typename ListSampleType::Pointer trainingListSample,std::string modelPath)
+::TrainAutoencoder(typename ListSampleType::Pointer trainingListSample,std::string modelPath)
 {
- // typename AutoencoderModelType::Pointer dimredTrainer = AutoencoderModelType::New();
-  autoencoderchoice::Pointer dimredTrainer = autoencoderchoice::New();
+		typename autoencoderchoice::Pointer dimredTrainer = autoencoderchoice::New();
 		dimredTrainer->SetNumberOfHiddenNeurons(GetParameterInt("model.autoencoder.nbneuron"));
 		dimredTrainer->SetNumberOfIterations(GetParameterInt("model.autoencoder.nbiter"));
 		dimredTrainer->SetRegularization(GetParameterFloat("model.autoencoder.normalizer"));
@@ -58,7 +58,7 @@ void cbLearningApplicationBaseDR<TInputValue,TOutputValue>
 		dimredTrainer->Train();
 		dimredTrainer->Save(modelPath);
 }
-*/
+
 } //end namespace wrapper
 } //end namespace otb
 
diff --git a/include/encode_filter.h b/include/encode_filter.h
index cd8f523505..903912be28 100644
--- a/include/encode_filter.h
+++ b/include/encode_filter.h
@@ -9,15 +9,15 @@ template< class TImage, class AutoencoderModel, class NormalizerModel>
 class ITK_EXPORT EncodeFilter:public itk::ImageToImageFilter< TImage, TImage >
 {
 	public:
-	  /** Standard class typedefs. */
+	  // Standard class typedefs. 
 		typedef EncodeFilter                                   	Self;
 		typedef itk::ImageToImageFilter< TImage, TImage > 	Superclass;
 		typedef itk::SmartPointer< Self >                       Pointer;
 	 
-	/** Method for creation through the object factory. */
+	// Method for creation through the object factory. 
 		itkNewMacro(Self);
 	 
-	/** Run-time type information (and related methods). */
+	// Run-time type information (and related methods). 
 		itkTypeMacro(EncodeFilter, ImageToImageFilter);
 		
 		//void SetInputImage(const TImage* image);
@@ -33,7 +33,7 @@ class ITK_EXPORT EncodeFilter:public itk::ImageToImageFilter< TImage, TImage >
 		AutoencoderModel GetAutoencoderModel();
 		NormalizerModel GetNormalizerModel();
  		
-	/** Does the real work. */
+	// Does the real work. 
 		virtual void GenerateOutputInformation();
 		virtual void BeforeThreadedGenerateData();
 	  	void ThreadedGenerateData(const typename TImage::RegionType &outputRegionForThread, unsigned int threadId) ITK_OVERRIDE;
-- 
GitLab