Skip to content
Snippets Groups Projects
Commit f3c26934 authored by Cédric Traizet's avatar Cédric Traizet
Browse files

Dimensionality reduction app created (dummy), encode filter .h and .txx added

parent a800be90
No related branches found
No related tags found
No related merge requests found
#include "otbWrapperApplication.h"
#include "otbWrapperApplicationFactory.h"
class CbDimensionalityReduction : public otb::Wrapper::Application
{
public:
typedef CbDimensionalityReduction Self;
typedef itk::SmartPointer<Self> Pointer;
itkNewMacro(Self);
itkTypeMacro(CbDimensionalityReduction, otb::Wrapper::Application);
private:
void DoInit()
{
SetName("CbDimensionalityReduction");
SetDescription("Perform dimensionality reduction on the input image");
AddRAMParameter();
}
void DoUpdateParameters()
{
}
void DoExecute()
{
std::cout << "Appli !" << std::endl;
int ThisDoesNothing = 0;
}
};
OTB_APPLICATION_EXPORT(CbDimensionalityReduction)
#ifndef __encode_Filter_h
#define __encode_Filter_h
#include "itkImageToImageFilter.h"
#include "itkMacro.h"
template< class TImage, class AutoencoderModel, class NormalizerModel>
class ITK_EXPORT EncodeFilter:public itk::ImageToImageFilter< TImage, TImage >
{
public:
/** Standard class typedefs. */
typedef EncodeFilter Self;
typedef itk::ImageToImageFilter< TImage, TImage > Superclass;
typedef itk::SmartPointer< Self > Pointer;
/** Method for creation through the object factory. */
itkNewMacro(Self);
/** Run-time type information (and related methods). */
itkTypeMacro(EncodeFilter, ImageToImageFilter);
//void SetInputImage(const TImage* image);
void SetAutoencoderModel(const std::string encoderPath);
void SetNormalizerModel(const std::string NormalizerPath);
void SetModels( const AutoencoderModel net, const NormalizerModel normalizer);
protected:
EncodeFilter();
~EncodeFilter(){}
typename TImage::ConstPointer GetInputImage();
AutoencoderModel GetAutoencoderModel();
NormalizerModel GetNormalizerModel();
/** Does the real work. */
virtual void BeforeThreadedGenerateData();
void ThreadedGenerateData(const typename TImage::RegionType &outputRegionForThread, unsigned int threadId) ITK_OVERRIDE;
private:
EncodeFilter(const Self &); //purposely not implemented
void operator=(const Self &); //purposely not implemented
AutoencoderModel m_net;
NormalizerModel m_normalizer;
};
#ifndef ITK_MANUAL_INSTANTIATION
#include "encode_filter.txx"
#endif
#endif // __encode_Filter_h
#ifndef __encode_filter_txx
#define __encode_filter_txx
#include "encode_filter.h"
#include <fstream>
#include "itkObjectFactory.h"
#include "itkImageRegionIterator.h"
#include "itkImageRegionConstIterator.h"
#include "otbVectorImage.h"
#include <shark/Data/Csv.h>
#include <shark/Data/Pgm.h> //for exporting the learned filters
#include <shark/Data/SparseData.h>//for reading in the images as sparseData/Libsvm format
#include <shark/Models/Autoencoder.h>//normal autoencoder model
#include <shark/Models/TiedAutoencoder.h>//autoencoder with tied weights
#include <shark/Models/Normalizer.h>
#include <shark/Algorithms/Trainers/NormalizeComponentsUnitVariance.h>
//using namespace shark;
template< class TImage, class AutoencoderModel, class NormalizerModel>
EncodeFilter<TImage, AutoencoderModel, NormalizerModel>::EncodeFilter()
{
this->SetNumberOfRequiredInputs(1);
}
template< class TImage, class AutoencoderModel, class NormalizerModel>
void EncodeFilter<TImage, AutoencoderModel, NormalizerModel>::SetAutoencoderModel(const std::string encoderPath)
{
//m_net = net;
std::ifstream ifs(encoderPath);
boost::archive::polymorphic_text_iarchive ia(ifs);
m_net.read(ia);
ifs.close();
}
template< class TImage, class AutoencoderModel, class NormalizerModel>
void EncodeFilter<TImage, AutoencoderModel, NormalizerModel>::SetNormalizerModel(const std::string NormalizerPath)
{
//m_normalizer = normalizer;
std::ifstream ifs(NormalizerPath);
boost::archive::polymorphic_text_iarchive ia(ifs);
m_normalizer.read(ia);
ifs.close();
}
template< class TImage, class AutoencoderModel, class NormalizerModel>
void EncodeFilter<TImage, AutoencoderModel, NormalizerModel>::SetModels(const AutoencoderModel net, const NormalizerModel normalizer)
{
m_net = net;
m_normalizer = normalizer;
}
template< class TImage, class AutoencoderModel, class NormalizerModel>
typename TImage::ConstPointer EncodeFilter<TImage, AutoencoderModel, NormalizerModel>::GetInputImage()
{
return static_cast< const TImage * >
( this->itk::ProcessObject::GetInput(0) );
}
template< class TImage, class AutoencoderModel, class NormalizerModel>
AutoencoderModel EncodeFilter<TImage, AutoencoderModel, NormalizerModel>::GetAutoencoderModel()
{
return m_net;
}
template< class TImage, class AutoencoderModel, class NormalizerModel>
NormalizerModel EncodeFilter<TImage, AutoencoderModel, NormalizerModel>::GetNormalizerModel()
{
return m_normalizer;
}
struct Data_with_info {
shark::Data<shark::RealVector> data; // This file format can be used to do Machine Learning with the Shark ML library
otb::VectorImage<double, 2>::RegionType region;
otb::VectorImage<double, 2>::PointType origin;
otb::VectorImage<double, 2>::SpacingType spacing;
};
template< class TImage, class AutoencoderModel, class NormalizerModel>
void EncodeFilter<TImage, AutoencoderModel, NormalizerModel>::BeforeThreadedGenerateData()
{
#ifdef _OPENMP
// OpenMP will take care of threading
this->SetNumberOfThreads(1);
#endif
}
/*template< class TImage, class AutoencoderModel, class NormalizerModel>
void EncodeFilter<TImage, AutoencoderModel, NormalizerModel>::GenerateData()*/
template< class TImage, class AutoencoderModel, class NormalizerModel>
void EncodeFilter<TImage, AutoencoderModel, NormalizerModel>::ThreadedGenerateData(const typename TImage::RegionType &outputRegionForThread, unsigned int threadId)
{
//Data_with_info info;
typename TImage::ConstPointer input = this->GetInput();
typename TImage::Pointer output = this->GetOutput();
// Image to vector
const unsigned int img_bands = input->GetNumberOfComponentsPerPixel();
itk::ImageRegionConstIterator<TImage> inputIterator(input,outputRegionForThread);
std::vector<shark::RealVector> image_vect;
typename TImage::PixelType pixelValue;
while(!inputIterator.IsAtEnd()){
shark::RealVector vect;
pixelValue = inputIterator.Get();
for(unsigned int a = 0; a < img_bands; ++a){
vect.push_back(pixelValue[a]);
}
image_vect.push_back(vect);
++inputIterator;
}
shark::Data<shark::RealVector> data = shark::createDataFromRange(image_vect);
image_vect.clear();
/** Normalize the data */
data= transform(data, m_normalizer);
/** Encode the data */
data = m_net.encode(data);
/** vector to image */
std::size_t numHidden = data.element(1).size();
output->SetVectorLength(numHidden);
itk::ImageRegionIterator<TImage> imageIteratorOut(output,outputRegionForThread);
auto vect_it = data.elements().begin();
while(!imageIteratorOut.IsAtEnd() && vect_it!=data.elements().end()){
pixelValue.SetSize(numHidden);
shark::RealVector vect_out=(*vect_it);
for(unsigned int a = 0; a < numHidden; ++a){
pixelValue[a]=vect_out[a];
}
imageIteratorOut.Set(pixelValue);
++imageIteratorOut;
++vect_it;
}
}
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment