From 82e47317d4465939c1c4f39a7aa283d4c174d703 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Traizet?= <traizetc@cesbio.cnes.fr> Date: Mon, 29 May 2017 10:53:26 +0200 Subject: [PATCH] some cmments and documentation added --- include/AutoencoderModel.h | 13 ++-- include/AutoencoderModel.txx | 8 +-- include/DimensionalityReductionModel.h | 64 +++++++------------ .../ImageDimensionalityReductionFilter.txx | 7 +- 4 files changed, 35 insertions(+), 57 deletions(-) diff --git a/include/AutoencoderModel.h b/include/AutoencoderModel.h index 81b55a5fbf..ec9b5cdb2b 100644 --- a/include/AutoencoderModel.h +++ b/include/AutoencoderModel.h @@ -27,7 +27,7 @@ public: itkNewMacro(Self); itkTypeMacro(AutoencoderModel, DimensionalityReductionModel); - unsigned int GetDimension() {return m_NumberOfHiddenNeurons;}; + unsigned int GetDimension() {return m_NumberOfHiddenNeurons;}; // Override the Dimensionality Reduction model method, it is used in the dimensionality reduction filter to set the output image size itkGetMacro(NumberOfHiddenNeurons,unsigned int); itkSetMacro(NumberOfHiddenNeurons,unsigned int); @@ -47,9 +47,7 @@ public: void Load(const std::string & filename, const std::string & name="") ITK_OVERRIDE; void Train() ITK_OVERRIDE; - //void Dimensionality_reduction() {}; // Dimensionality reduction is done by DoPredict - - + protected: AutoencoderModel(); ~AutoencoderModel() ITK_OVERRIDE; @@ -59,11 +57,14 @@ protected: private: + /** Network attributes */ AutoencoderType m_net; unsigned int m_NumberOfHiddenNeurons; + + /** Training parameters */ unsigned int m_NumberOfIterations; - double m_Regularization; - double m_Noise; + double m_Regularization; // L2 Regularization parameter + double m_Noise; // probability for an input to be set to 0 (denosing autoencoder) }; } // end namespace otb diff --git a/include/AutoencoderModel.txx b/include/AutoencoderModel.txx index 5c7bc5d97e..4305f805b8 100644 --- a/include/AutoencoderModel.txx +++ b/include/AutoencoderModel.txx @@ -6,12 +6,13 @@ #include <shark/Data/Dataset.h> #include "itkMacro.h" #include "otbSharkUtils.h" + //include train function #include <shark/ObjectiveFunctions/ErrorFunction.h> #include <shark/Algorithms/GradientDescent/Rprop.h>// the RProp optimization algorithm #include <shark/ObjectiveFunctions/Loss/SquaredLoss.h> // squared loss used for regression #include <shark/ObjectiveFunctions/Regularizer.h> //L2 regulariziation -#include <shark/Models/ImpulseNoiseModel.h>//noise source to corrupt the inputs +#include <shark/Models/ImpulseNoiseModel.h> //noise source to corrupt the inputs #include <shark/Models/ConcatenatedModel.h>//to concatenate the noise with the model namespace otb @@ -43,7 +44,7 @@ void AutoencoderModel<TInputValue,AutoencoderType>::Train() std::size_t inputs = dataDimension(inputSamples); m_net.setStructure(inputs, m_NumberOfHiddenNeurons); initRandomUniform(m_net,-0.1*std::sqrt(1.0/inputs),0.1*std::sqrt(1.0/inputs)); - shark::ImpulseNoiseModel noise(m_Noise,0.0);//set an input pixel with probability p to 0 + shark::ImpulseNoiseModel noise(m_Noise,0.0); //set an input pixel with probability m_Noise to 0 shark::ConcatenatedModel<shark::RealVector,shark::RealVector> model = noise>> m_net; shark::LabeledData<shark::RealVector,shark::RealVector> trainSet(inputSamples,inputSamples);//labels identical to inputs @@ -92,7 +93,7 @@ template <class TInputValue, class AutoencoderType> void AutoencoderModel<TInputValue,AutoencoderType>::Save(const std::string & filename, const std::string & name) { std::ofstream ofs(filename); - ofs << m_net.name() << std::endl; //first line + ofs << m_net.name() << std::endl; // the first line of the model file contains a key boost::archive::polymorphic_text_oarchive oa(ofs); m_net.write(oa); ofs.close(); @@ -163,6 +164,5 @@ void AutoencoderModel<TInputValue,AutoencoderType> } } - } // namespace otb #endif diff --git a/include/DimensionalityReductionModel.h b/include/DimensionalityReductionModel.h index 23ffad38bf..c5026551ee 100644 --- a/include/DimensionalityReductionModel.h +++ b/include/DimensionalityReductionModel.h @@ -25,42 +25,32 @@ namespace otb { -/** \class MachineLearningModel - * \brief MachineLearningModel is the base class for all classifier objects (SVM, KNN, - * Random Forests, Artificial Neural Network, ...) implemented in the supervised classification framework of the OTB. +/** \class DimensionalityReductionModel + * \brief DimensionalityReductionModel is the base class for all dimensionality Reduction objects (PCA, autoencoders and SOM) implemented in the dimensionality Reduction framework of the OTB. * - * MachineLearningModel is an abstract object that specifies behavior and - * interface of supervised classifiers (SVM, KNN, Random Forests, Artificial - * Neural Network, ...) in the generic supervised classification framework of the OTB. - * The main generic virtual methods specifically implemented in each classifier - * derived from the MachineLearningModel class are two learning-related methods: - * Train() and Save(), and three classification-related methods: Load(), + * DimensionalityReductionModel is an abstract object that specifies behavior and + * interface of dimensionality reduction algorithms (PCA, autoencoders and SOM) in the generic dimensionality Reduction framework of the OTB. + * The main generic virtual methods specifically implemented in each model + * derived from the DimensionalityReductionModel class are two learning-related methods: + * Train() and Save(), and three dimensionality reduction related methods: Load(), * DoPredict() and optionnaly DoPredictBatch(). * * Thus, each classifier derived from the MachineLearningModel class * computes its corresponding model with Train() and exports it with * the help of the Save() method. * - * It is also possible to classify any input sample composed of several + * It is also possible to reduce the dimensionality of any input sample composed of several * features (or any number of bands in the case of a pixel extracted * from a multi-band image) with the help of the Predict() method which * needs a previous loading of the classification model with the Load() method. * - * \sa MachineLearningModelFactory - * \sa LibSVMMachineLearningModel - * \sa SVMMachineLearningModel - * \sa BoostMachineLearningModel - * \sa KNearestNeighborsMachineLearningModel - * \sa DecisionTreeMachineLearningModel - * \sa RandomForestsMachineLearningModel - * \sa GradientBoostedTreeMachineLearningModel - * \sa NormalBayesMachineLearningModel - * \sa NeuralNetworkMachineLearningModel - * \sa SharkRandomForestsMachineLearningModel - * \sa ImageClassificationFilter + * \sa DimensionalityReductionModelFactory + * \sa SOMModel + * \sa PCAModel + * \sa AutoencderModel + * \sa ImageDimensionalityReductionFilter * - * - * \ingroup OTBSupervised + * \ingroup cbDimensionalityReduction */ template <class TInputValue, class TTargetValue> class ITK_EXPORT DimensionalityReductionModel @@ -121,7 +111,7 @@ public: /** Get the size of the output after dimensionality reduction */ virtual unsigned int GetDimension() = 0; - /**\name Classification model file manipulation */ + /**\name Dimensionality Reduction model file manipulation */ //@{ /** Save the model to file */ virtual void Save(const std::string & filename, const std::string & name="") = 0; @@ -132,10 +122,10 @@ public: /**\name Classification model file compatibility tests */ //@{ - /** Is the input model file readable and compatible with the corresponding classifier ? */ + /** Is the input model file readable and compatible with the corresponding model ? */ virtual bool CanReadFile(const std::string &) = 0; - /** Is the input model file writable and compatible with the corresponding classifier ? */ + /** Is the input model file writable and compatible with the corresponding model ? */ virtual bool CanWriteFile(const std::string &) = 0; //@} @@ -165,19 +155,13 @@ protected: /** Is DoPredictBatch multi-threaded ? */ bool m_IsDoPredictBatchMultiThreaded; - - - - private: /** Actual implementation of BatchPredicition * Default implementation will call DoPredict iteratively * \param input The input batch - * \param startIndex Index of the first sample to predict - * \param size Number of samples to predict - * \param target Pointer to the list of produced labels - * \param quality Pointer to the list of produced confidence - * values, or NULL + * \param startIndex Index of the first sample to reduce + * \param size Number of samples to reduce + * \param target Pointer to the list of reduced samples * * Override me if internal implementation allows for batch * prediction. @@ -187,11 +171,9 @@ private: */ virtual void DoPredictBatch(const InputListSampleType * input, const unsigned int & startIndex, const unsigned int & size, TargetListSampleType * target) const; - /** Actual implementation of single sample prediction - * \param input sample to predict - * \param quality Pointer to a variable to store confidence value, - * or NULL - * \return The predicted label + /** Actual implementation of single sample reduction + * \param input sample to reduce + * \return The reduced sample */ virtual TargetSampleType DoPredict(const InputSampleType& input) const = 0; diff --git a/include/ImageDimensionalityReductionFilter.txx b/include/ImageDimensionalityReductionFilter.txx index 3dbfd16bfe..4eee4d3c9f 100644 --- a/include/ImageDimensionalityReductionFilter.txx +++ b/include/ImageDimensionalityReductionFilter.txx @@ -163,14 +163,10 @@ ImageDimensionalityReductionFilter<TInputImage, TOutputImage, TMaskImage> InputIteratorType inIt(inputPtr, outputRegionForThread); OutputIteratorType outIt(outputPtr, outputRegionForThread); - // typedef typename ModelType::InputValueType InputValueType; typedef typename ModelType::InputSampleType InputSampleType; typedef typename ModelType::InputListSampleType InputListSampleType; typedef typename ModelType::TargetValueType TargetValueType; - // typedef typename ModelType::TargetSampleType TargetSampleType; typedef typename ModelType::TargetListSampleType TargetListSampleType; - // typedef typename ModelType::ConfidenceValueType ConfidenceValueType; - // typedef typename ModelType::ConfidenceSampleType ConfidenceSampleType; typename InputListSampleType::Pointer samples = InputListSampleType::New(); unsigned int num_features = inputPtr->GetNumberOfComponentsPerPixel(); @@ -193,11 +189,10 @@ ImageDimensionalityReductionFilter<TInputImage, TOutputImage, TMaskImage> typename TargetListSampleType::Pointer labels; // This call is threadsafe - //labels = m_Model->PredictBatch(samples,confidences); labels = m_Model->PredictBatch(samples); + // Set the output values - typename TargetListSampleType::ConstIterator labIt = labels->Begin(); for (outIt.GoToBegin(); !outIt.IsAtEnd(); ++outIt) -- GitLab