[Insight-users] How to get number of iterations from optimizer?

Luis Ibanez luis.ibanez at kitware.com
Wed Dec 9 09:40:22 EST 2009


Hi Motes,


1) If you want to get the last value from the Optimizer,
    you should use the call:

            optimizer->GetValue()

    (with no arguments).

    or, you could do:

    metic->GetValue( registration->GetLastTransformParameters() );

     this second form is a safer way to get the value.
     Note that it actually goes back and computes the
     Metric at that point of the parametric space, while
     the call to GetValue() in the optimizer, will simply use
     a cached value


2)  The problem that you are facing with getting the number
     of iterations, is because the method

            registration->GetOptimizer()

      return a pointer to the BASE class of optimizers.

      In your case you will have to down cast it to a
      RegularGradientDescentOptimizerType with code
      such as

      RegularGradientDescentOptimizerType::Pointer optimizer =
        dynamic_cast< RegularGradientDescentOptimizerType>(
           registration->GetOptimizer() );

     if( optimizer != NULL )
       {
       optimizer->GetCurrentIteration();
       }

      That said,... you probably have the pointer to the original
      optimizer around (since you had to use it when configuring
      the registration process...). Why are you not using the original
      pointer to the optimizer ?


    Regards,

          Luis


--------------------------------------------------------
On Tue, Dec 8, 2009 at 1:17 PM, motes motes <mort.motes at gmail.com> wrote:
> I have an image registration process where I use the
> RegularGradientDescentOptimizer:
>
>  typename RegularGradientDescentOptimizerType::Pointer optimizer =
> RegularGradientDescentOptimizerType::New();
>
>          optimizer->SetMaximumStepLength( 0.5 );
>          optimizer->SetMinimumStepLength( 0.0001 );
>          optimizer->SetGradientMagnitudeTolerance( 0.0001 );
>          optimizer->SetNumberOfIterations( 1000 );
>          optimizer->MinimizeOn();
>          ObserverType::Pointer Observer = ObserverType::New();
>          optimizer->AddObserver( itk::IterationEvent(), Observer );
>          registration->SetOptimizer( optimizer );
>
>
> When the image registration method is done:
>
>    try {
>      timeProbe.Start();
>      registration->Update();
>      timeProbe.Stop();
>    }
>    catch( itk::ExceptionObject & err ) {
>      std::cerr << "ExceptionObject caught !" << std::endl;
>      std::cerr << err << std::endl;
>      return;
>    }
>
>
> its possible to get the final metric value with:
>
>
>   double final_metric_value =
> registration->GetOptimizer()->GetValue(registration->GetLastTransformParameters());
>
>
> But its not possible to get the number of used iterations like
> (sometimes the optimizer terminates before the maximum number of
> iterations has been used):
>
>
>  int used_iterations =  registration->GetOptimizer()->GetNumberOfIterations();
>
> Error:
>
> Error   12      error C2039: 'GetNumberOfIterations' : is not a member of
> 'itk::SingleValuedNonLinearOptimizer'
>
> It there someway to cast the base optimizer class to the
> RegularGradientDescentOptimizerType (this subclass has the function
> GetNumberOfIterations) ?
> _____________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Kitware offers ITK Training Courses, for more information visit:
> http://www.kitware.com/products/protraining.html
>
> Please keep messages on-topic and check the ITK FAQ at:
> http://www.itk.org/Wiki/ITK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.itk.org/mailman/listinfo/insight-users
>


More information about the Insight-users mailing list