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

encode_filter and dummy_filter have been removed, as they are not used in the applications anymore.

parent 4db350d3
No related branches found
No related tags found
No related merge requests found
#ifndef __dummy_Filter_h
#define __dummy_Filter_h
#include "itkImageToImageFilter.h"
#include "itkMacro.h"
template< class TImage>
class ITK_EXPORT DummyFilter:public itk::ImageToImageFilter< TImage, TImage >
{
public:
/** Standard class typedefs. */
typedef DummyFilter 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(DummyFilter, ImageToImageFilter);
//void SetInputImage(const TImage* image);
protected:
DummyFilter();
~DummyFilter(){}
typename TImage::ConstPointer GetInputImage();
/** Does the real work. */
virtual void BeforeThreadedGenerateData();
void ThreadedGenerateData(const typename TImage::RegionType &outputRegionForThread, unsigned int threadId) ITK_OVERRIDE;
private:
DummyFilter(const Self &); //purposely not implemented
void operator=(const Self &); //purposely not implemented
};
#ifndef ITK_MANUAL_INSTANTIATION
#include "dummy_filter.txx"
#endif
#endif // __dummy_Filter_h
#ifndef __dummy_filter_txx
#define __dummy_filter_txx
#include "dummy_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>
DummyFilter<TImage>::DummyFilter()
{
this->SetNumberOfRequiredInputs(1);
}
template< class TImage>
typename TImage::ConstPointer DummyFilter<TImage>::GetInputImage()
{
return static_cast< const TImage * >
( this->itk::ProcessObject::GetInput(0) );
}
template< class TImage>
void DummyFilter<TImage>::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>
void DummyFilter<TImage>::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);
itk::ImageRegionIterator<TImage> imageIteratorOut(output,outputRegionForThread);
typename TImage::PixelType pixelValue;
while(!inputIterator.IsAtEnd()){
imageIteratorOut.Set(inputIterator.Get());
++inputIterator;
++imageIteratorOut;
}
}
#endif
#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);
std::size_t GetDimension(){return m_hidden_neuron;};
protected:
EncodeFilter();
~EncodeFilter(){}
typename TImage::ConstPointer GetInputImage();
AutoencoderModel GetAutoencoderModel();
NormalizerModel GetNormalizerModel();
// Does the real work.
virtual void GenerateOutputInformation();
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;
std::size_t m_hidden_neuron;
};
#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();
m_hidden_neuron = m_net.numberOfHiddenNeurons();
}
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>::GenerateOutputInformation()
{
Superclass::GenerateOutputInformation();
this->GetOutput()->SetNumberOfComponentsPerPixel( m_hidden_neuron );
}
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