go home Home | Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Globals | Related Pages
elxAdaptiveStochasticGradientDescent.h
Go to the documentation of this file.
00001 /*======================================================================
00002 
00003   This file is part of the elastix software.
00004 
00005   Copyright (c) University Medical Center Utrecht. All rights reserved.
00006   See src/CopyrightElastix.txt or http://elastix.isi.uu.nl/legal.php for
00007   details.
00008 
00009      This software is distributed WITHOUT ANY WARRANTY; without even
00010      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00011      PURPOSE. See the above copyright notices for more information.
00012 
00013 ======================================================================*/
00014 
00015 #ifndef __elxAdaptiveStochasticGradientDescent_h
00016 #define __elxAdaptiveStochasticGradientDescent_h
00017 
00018 #include "itkAdaptiveStochasticGradientDescentOptimizer.h"
00019 #include "itkImageGridSampler.h"
00020 #include "itkImageRandomCoordinateSampler.h"
00021 #include "elxIncludes.h"
00022 #include "elxProgressCommand.h"
00023 #include "itkAdvancedTransform.h"
00024 #include "itkMersenneTwisterRandomVariateGenerator.h"
00025 
00026 namespace elastix
00027 {
00028   using namespace itk;
00029 
00030 
00155 template <class TElastix>
00156 class AdaptiveStochasticGradientDescent :
00157   public itk::AdaptiveStochasticGradientDescentOptimizer,
00158   public OptimizerBase<TElastix>
00159 {
00160 public:
00161 
00163   typedef AdaptiveStochasticGradientDescent           Self;
00164   typedef AdaptiveStochasticGradientDescentOptimizer  Superclass1;
00165   typedef OptimizerBase<TElastix>                     Superclass2;
00166   typedef SmartPointer<Self>                          Pointer;
00167   typedef SmartPointer<const Self>                    ConstPointer;
00168 
00170   itkNewMacro( Self );
00171 
00173   itkTypeMacro( AdaptiveStochasticGradientDescent,
00174     AdaptiveStochasticGradientDescentOptimizer );
00175 
00180   elxClassNameMacro( "AdaptiveStochasticGradientDescent" );
00181 
00183   typedef Superclass1::CostFunctionType               CostFunctionType;
00184   typedef Superclass1::CostFunctionPointer            CostFunctionPointer;
00185   typedef Superclass1::StopConditionType              StopConditionType;
00186 
00188   typedef typename Superclass2::ElastixType           ElastixType;
00189   typedef typename Superclass2::ElastixPointer        ElastixPointer;
00190   typedef typename Superclass2::ConfigurationType     ConfigurationType;
00191   typedef typename Superclass2::ConfigurationPointer  ConfigurationPointer;
00192   typedef typename Superclass2::RegistrationType      RegistrationType;
00193   typedef typename Superclass2::RegistrationPointer   RegistrationPointer;
00194   typedef typename Superclass2::ITKBaseType           ITKBaseType;
00195 
00197   typedef typename Superclass1::ParametersType        ParametersType;
00198 
00202   virtual void BeforeRegistration( void );
00203   virtual void BeforeEachResolution( void );
00204   virtual void AfterEachResolution( void );
00205   virtual void AfterEachIteration( void );
00206   virtual void AfterRegistration( void );
00207 
00211   virtual void StartOptimization( void );
00212 
00217   virtual void ResumeOptimization( void );
00218 
00220   virtual void MetricErrorResponse( ExceptionObject & err );
00221 
00231   itkSetMacro( AutomaticParameterEstimation, bool );
00232   itkGetConstMacro( AutomaticParameterEstimation, bool );
00233 
00235   itkSetMacro( MaximumStepLength, double );
00236   itkGetConstMacro( MaximumStepLength, double );
00237 
00239   itkSetMacro( MaximumNumberOfSamplingAttempts, unsigned long );
00240 
00242   itkGetConstReferenceMacro( MaximumNumberOfSamplingAttempts, unsigned long );
00243 
00244 protected:
00245 
00247   typedef typename RegistrationType::FixedImageType   FixedImageType;
00248   typedef typename RegistrationType::MovingImageType  MovingImageType;
00249   typedef typename FixedImageType::RegionType         FixedImageRegionType;
00250   typedef typename FixedImageType::IndexType          FixedImageIndexType;
00251   typedef typename FixedImageType::PointType          FixedImagePointType;
00252   typedef typename RegistrationType::ITKBaseType      itkRegistrationType;
00253   typedef typename itkRegistrationType::TransformType TransformType;
00254   typedef typename TransformType::JacobianType        JacobianType;
00255   typedef typename JacobianType::ValueType            JacobianValueType;
00256   struct SettingsType { double a, A, alpha, fmax, fmin, omega; };
00257   typedef typename std::vector<SettingsType>          SettingsVectorType;
00258 
00260   typedef itk::ImageSamplerBase<FixedImageType>       ImageSamplerBaseType;
00261   typedef typename ImageSamplerBaseType::Pointer      ImageSamplerBasePointer;
00262   typedef itk::ImageRandomSamplerBase<FixedImageType> ImageRandomSamplerBaseType;
00263   typedef typename
00264     ImageRandomSamplerBaseType::Pointer               ImageRandomSamplerBasePointer;
00265   typedef
00266     itk::ImageRandomCoordinateSampler<FixedImageType> ImageRandomCoordinateSamplerType;
00267   typedef typename
00268     ImageRandomCoordinateSamplerType::Pointer         ImageRandomCoordinateSamplerPointer;
00269   typedef ImageGridSampler< FixedImageType >          ImageGridSamplerType;
00270   typedef typename ImageGridSamplerType::Pointer      ImageGridSamplerPointer;
00271   typedef typename
00272     ImageGridSamplerType::ImageSampleContainerType    ImageSampleContainerType;
00273   typedef typename ImageSampleContainerType::Pointer  ImageSampleContainerPointer;
00274 
00276   typedef itk::Statistics::MersenneTwisterRandomVariateGenerator RandomGeneratorType;
00277   typedef ProgressCommand                             ProgressCommandType;
00278   typedef typename ProgressCommand::Pointer           ProgressCommandPointer;
00279 
00281   typedef JacobianType                                TransformJacobianType;
00282   itkStaticConstMacro( FixedImageDimension, unsigned int, FixedImageType::ImageDimension );
00283   itkStaticConstMacro( MovingImageDimension, unsigned int, MovingImageType::ImageDimension );
00284   typedef typename TransformType::ScalarType          CoordinateRepresentationType;
00285   typedef AdvancedTransform<
00286     CoordinateRepresentationType,
00287     itkGetStaticConstMacro(FixedImageDimension),
00288     itkGetStaticConstMacro(MovingImageDimension) >    AdvancedTransformType;
00289   typedef typename
00290     AdvancedTransformType::NonZeroJacobianIndicesType NonZeroJacobianIndicesType;
00291 
00292   AdaptiveStochasticGradientDescent();
00293   virtual ~AdaptiveStochasticGradientDescent() {};
00294 
00296   SettingsVectorType m_SettingsVector;
00297 
00299   unsigned int m_NumberOfGradientMeasurements;
00300   unsigned int m_NumberOfJacobianMeasurements;
00301   unsigned int m_NumberOfSamplesForExactGradient;
00302 
00304   typename AdvancedTransformType::Pointer           m_AdvancedTransform;
00305 
00307   typename RandomGeneratorType::Pointer             m_RandomGenerator;
00308 
00310   virtual void CheckForAdvancedTransform( void );
00311 
00313   virtual void PrintSettingsVector( const SettingsVectorType & settings ) const;
00314 
00319   virtual void AutomaticParameterEstimation( void );
00320 
00328   virtual void SampleGradients( const ParametersType & mu0,
00329     double perturbationSigma, double & gg, double & ee );
00330 
00339   virtual void SampleFixedImageForJacobianTerms(
00340     ImageSampleContainerPointer & sampleContainer );
00341 
00345   virtual void ComputeJacobianTerms( double & TrC, double & TrCC,
00346     double & maxJJ, double & maxJCJ );
00347 
00351   virtual void GetScaledDerivativeWithExceptionHandling(
00352     const ParametersType & parameters, DerivativeType & derivative );
00353 
00357   virtual void AddRandomPerturbation( ParametersType & parameters, double sigma );
00358 
00359 private:
00360 
00361   AdaptiveStochasticGradientDescent( const Self& );  // purposely not implemented
00362   void operator=( const Self& );              // purposely not implemented
00363 
00364   bool m_AutomaticParameterEstimation;
00365   double m_MaximumStepLength;
00366 
00368   unsigned long m_MaximumNumberOfSamplingAttempts;
00369   unsigned long m_CurrentNumberOfSamplingAttempts;
00370   unsigned long m_PreviousErrorAtIteration;
00371   bool          m_AutomaticParameterEstimationDone;
00372 
00374   unsigned long m_MaxBandCovSize;
00375   unsigned long m_NumberOfBandStructureSamples;
00376 
00377 }; // end class AdaptiveStochasticGradientDescent
00378 
00379 
00380 } // end namespace elastix
00381 
00382 #ifndef ITK_MANUAL_INSTANTIATION
00383 #include "elxAdaptiveStochasticGradientDescent.hxx"
00384 #endif
00385 
00386 #endif // end #ifndef __elxAdaptiveStochasticGradientDescent_h


Generated on 11-05-2011 for elastix by doxygen 1.7.4 elastix logo