Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
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
00168 template <class TElastix>
00169 class AdaptiveStochasticGradientDescent :
00170 public itk::AdaptiveStochasticGradientDescentOptimizer,
00171 public OptimizerBase<TElastix>
00172 {
00173 public:
00174
00176 typedef AdaptiveStochasticGradientDescent Self;
00177 typedef AdaptiveStochasticGradientDescentOptimizer Superclass1;
00178 typedef OptimizerBase<TElastix> Superclass2;
00179 typedef SmartPointer<Self> Pointer;
00180 typedef SmartPointer<const Self> ConstPointer;
00181
00183 itkNewMacro( Self );
00184
00186 itkTypeMacro( AdaptiveStochasticGradientDescent,
00187 AdaptiveStochasticGradientDescentOptimizer );
00188
00193 elxClassNameMacro( "AdaptiveStochasticGradientDescent" );
00194
00196 typedef Superclass1::CostFunctionType CostFunctionType;
00197 typedef Superclass1::CostFunctionPointer CostFunctionPointer;
00198 typedef Superclass1::StopConditionType StopConditionType;
00199
00201 typedef typename Superclass2::ElastixType ElastixType;
00202 typedef typename Superclass2::ElastixPointer ElastixPointer;
00203 typedef typename Superclass2::ConfigurationType ConfigurationType;
00204 typedef typename Superclass2::ConfigurationPointer ConfigurationPointer;
00205 typedef typename Superclass2::RegistrationType RegistrationType;
00206 typedef typename Superclass2::RegistrationPointer RegistrationPointer;
00207 typedef typename Superclass2::ITKBaseType ITKBaseType;
00208
00210 typedef typename Superclass1::ParametersType ParametersType;
00211
00215 virtual void BeforeRegistration( void );
00216 virtual void BeforeEachResolution( void );
00217 virtual void AfterEachResolution( void );
00218 virtual void AfterEachIteration( void );
00219 virtual void AfterRegistration( void );
00220
00224 virtual void StartOptimization( void );
00225
00230 virtual void ResumeOptimization( void );
00231
00233 virtual void MetricErrorResponse( ExceptionObject & err );
00234
00244 itkSetMacro( AutomaticParameterEstimation, bool );
00245 itkGetConstMacro( AutomaticParameterEstimation, bool );
00246
00248 itkSetMacro( MaximumStepLength, double );
00249 itkGetConstMacro( MaximumStepLength, double );
00250
00252 itkSetMacro( MaximumNumberOfSamplingAttempts, unsigned long );
00253
00255 itkGetConstReferenceMacro( MaximumNumberOfSamplingAttempts, unsigned long );
00256
00257 protected:
00258
00260 typedef typename RegistrationType::FixedImageType FixedImageType;
00261 typedef typename RegistrationType::MovingImageType MovingImageType;
00262 typedef typename FixedImageType::RegionType FixedImageRegionType;
00263 typedef typename FixedImageType::IndexType FixedImageIndexType;
00264 typedef typename FixedImageType::PointType FixedImagePointType;
00265 typedef typename RegistrationType::ITKBaseType itkRegistrationType;
00266 typedef typename itkRegistrationType::TransformType TransformType;
00267 typedef typename TransformType::JacobianType JacobianType;
00268 typedef typename JacobianType::ValueType JacobianValueType;
00269 struct SettingsType { double a, A, alpha, fmax, fmin, omega; };
00270 typedef typename std::vector<SettingsType> SettingsVectorType;
00271
00273 typedef itk::ImageSamplerBase<FixedImageType> ImageSamplerBaseType;
00274 typedef typename ImageSamplerBaseType::Pointer ImageSamplerBasePointer;
00275 typedef itk::ImageRandomSamplerBase<FixedImageType> ImageRandomSamplerBaseType;
00276 typedef typename
00277 ImageRandomSamplerBaseType::Pointer ImageRandomSamplerBasePointer;
00278 typedef
00279 itk::ImageRandomCoordinateSampler<FixedImageType> ImageRandomCoordinateSamplerType;
00280 typedef typename
00281 ImageRandomCoordinateSamplerType::Pointer ImageRandomCoordinateSamplerPointer;
00282 typedef ImageGridSampler< FixedImageType > ImageGridSamplerType;
00283 typedef typename ImageGridSamplerType::Pointer ImageGridSamplerPointer;
00284 typedef typename
00285 ImageGridSamplerType::ImageSampleContainerType ImageSampleContainerType;
00286 typedef typename ImageSampleContainerType::Pointer ImageSampleContainerPointer;
00287
00289 typedef itk::Statistics::MersenneTwisterRandomVariateGenerator RandomGeneratorType;
00290 typedef ProgressCommand ProgressCommandType;
00291 typedef typename ProgressCommand::Pointer ProgressCommandPointer;
00292
00294 typedef JacobianType TransformJacobianType;
00295 itkStaticConstMacro( FixedImageDimension, unsigned int, FixedImageType::ImageDimension );
00296 itkStaticConstMacro( MovingImageDimension, unsigned int, MovingImageType::ImageDimension );
00297 typedef typename TransformType::ScalarType CoordinateRepresentationType;
00298 typedef AdvancedTransform<
00299 CoordinateRepresentationType,
00300 itkGetStaticConstMacro(FixedImageDimension),
00301 itkGetStaticConstMacro(MovingImageDimension) > AdvancedTransformType;
00302 typedef typename
00303 AdvancedTransformType::NonZeroJacobianIndicesType NonZeroJacobianIndicesType;
00304
00305 AdaptiveStochasticGradientDescent();
00306 virtual ~AdaptiveStochasticGradientDescent() {};
00307
00309 SettingsVectorType m_SettingsVector;
00310
00312 unsigned int m_NumberOfGradientMeasurements;
00313 unsigned int m_NumberOfJacobianMeasurements;
00314 unsigned int m_NumberOfSamplesForExactGradient;
00315
00317 typename AdvancedTransformType::Pointer m_AdvancedTransform;
00318
00320 typename RandomGeneratorType::Pointer m_RandomGenerator;
00321
00322 double m_SigmoidScaleFactor;
00323
00325 virtual void CheckForAdvancedTransform( void );
00326
00328 virtual void PrintSettingsVector( const SettingsVectorType & settings ) const;
00329
00334 virtual void AutomaticParameterEstimation( void );
00335
00343 virtual void SampleGradients( const ParametersType & mu0,
00344 double perturbationSigma, double & gg, double & ee );
00345
00354 virtual void SampleFixedImageForJacobianTerms(
00355 ImageSampleContainerPointer & sampleContainer );
00356
00360 virtual void ComputeJacobianTerms( double & TrC, double & TrCC,
00361 double & maxJJ, double & maxJCJ );
00362
00366 virtual void GetScaledDerivativeWithExceptionHandling(
00367 const ParametersType & parameters, DerivativeType & derivative );
00368
00372 virtual void AddRandomPerturbation( ParametersType & parameters, double sigma );
00373
00374 private:
00375
00376 AdaptiveStochasticGradientDescent( const Self& );
00377 void operator=( const Self& );
00378
00379 bool m_AutomaticParameterEstimation;
00380 double m_MaximumStepLength;
00381
00383 unsigned long m_MaximumNumberOfSamplingAttempts;
00384 unsigned long m_CurrentNumberOfSamplingAttempts;
00385 unsigned long m_PreviousErrorAtIteration;
00386 bool m_AutomaticParameterEstimationDone;
00387
00389 unsigned long m_MaxBandCovSize;
00390 unsigned long m_NumberOfBandStructureSamples;
00391
00392 };
00393
00394
00395 }
00396
00397 #ifndef ITK_MANUAL_INSTANTIATION
00398 #include "elxAdaptiveStochasticGradientDescent.hxx"
00399 #endif
00400
00401 #endif // end #ifndef __elxAdaptiveStochasticGradientDescent_h