Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PERF: Make AdvancedNormalizedCorrelationImageToImageMetric faster #556

Merged
merged 1 commit into from
Nov 26, 2021

Conversation

N-Dekker
Copy link
Member

Added a new member function to AdvancedImageToImageMetric, FastEvaluateMovingImageValueAndDerivative, which calls the multi-threaded overload of ITK's itk::BSplineInterpolateImageFunction::EvaluateValueAndDerivativeAtContinuousIndex, indirectly. (It does so via EvaluateMovingImageValueAndDerivativeWithOptionalThreadId, another new member function.)

Made AdvancedNormalizedCorrelationImageToImageMetric::ThreadedGetValueAndDerivative faster, by calling this new FastEvaluateMovingImageValueAndDerivative.

A large performance improvement was observed for GoogleTest unit test itkElastixRegistrationMethod.EulerDiscRotation2D (which uses the "AdvancedNormalizedCorrelation" metric), from ~1.5 second before this commit down to ~0.9 second after this commit, using Visual Studio 2019, Release configuration. (For a Debug configuration even from ~15 seconds before, down to ~4 seconds after this commit.)

Added a new member function to `AdvancedImageToImageMetric`, `FastEvaluateMovingImageValueAndDerivative`, which calls the multi-threaded overload of ITK's `itk::BSplineInterpolateImageFunction::EvaluateValueAndDerivativeAtContinuousIndex`, indirectly. (It does so via `EvaluateMovingImageValueAndDerivativeWithOptionalThreadId`, another new member function.)

Made `AdvancedNormalizedCorrelationImageToImageMetric::ThreadedGetValueAndDerivative` faster, by calling this new `FastEvaluateMovingImageValueAndDerivative`.

A large performance improvement was observed for GoogleTest unit test `itkElastixRegistrationMethod.EulerDiscRotation2D` (which uses the "AdvancedNormalizedCorrelation" metric), from ~1.5 second before this commit down to ~0.9 second after this commit, using Visual Studio 2019, Release configuration. (For a Debug configuration even from ~15 seconds before, down to ~4 seconds after this commit.)
@N-Dekker
Copy link
Member Author

N-Dekker commented Nov 26, 2021

@mstaring @stefanklein Here is the EvaluateValueAndDerivativeAtContinuousIndex overload that was called before, by the metric:

"Slower, but safer"
https://github.com/InsightSoftwareConsortium/ITK/blob/v5.3rc02/Modules/Core/ImageFunction/include/itkBSplineInterpolateImageFunction.h#L239-L257

Here is the faster overload (which has a threadId parameter):

"FIXME -- Review this "fix" and ensure it works."
https://github.com/InsightSoftwareConsortium/ITK/blob/v5.3rc02/Modules/Core/ImageFunction/include/itkBSplineInterpolateImageFunction.hxx#L276-L284

The performance improvement comes from skipping the memory allocation of local vnl_matrix variables, not just from using multiple threads!


For the record, the "FIXME" was there already with commit InsightSoftwareConsortium/ITK@6abbc79 (6 August 2010): https://github.com/InsightSoftwareConsortium/ITK/blob/6abbc7969a90786c4c73f5d191f634db536c2d1d/Code/BasicFilters/itkBSplineInterpolateImageFunction.txx#L297

@N-Dekker N-Dekker merged commit c8c4a6a into develop Nov 26, 2021
@N-Dekker N-Dekker deleted the FastEvaluateMovingImageValueAndDerivative branch November 26, 2021 16:18
N-Dekker added a commit that referenced this pull request Nov 27, 2021
Replace `this->EvaluateMovingImageValueAndDerivative(...)` calls by `this->FastEvaluateMovingImageValueAndDerivative(..., threadId)` in four more metrics:

   itkAdvancedKappaStatisticImageToImageMetric
   itkParzenWindowMutualInformationImageToImageMetric
   itkAdvancedMeanSquaresImageToImageMetric
   itkPCAMetric_F_multithreaded

Follow-up to pull request #556 commit c8c4a6a "PERF: Make AdvancedNormalizedCorrelationImageToImageMetric faster"
N-Dekker added a commit that referenced this pull request Nov 27, 2021
Replace `this->EvaluateMovingImageValueAndDerivative(...)` calls by `this->FastEvaluateMovingImageValueAndDerivative(..., threadId)` in the "Threaded" member functions of six more metrics:

    itkAdvancedKappaStatisticImageToImageMetric
    itkAdvancedMeanSquaresImageToImageMetric
    itkParzenWindowHistogramImageToImageMetric
    itkParzenWindowMutualInformationImageToImageMetric
    itkPCAMetric_F_multithreaded
    itkSumSquaredTissueVolumeDifferenceImageToImageMetric

Expected a significant performance gain on the run-time duration.

Follow-up to pull request #556 commit c8c4a6a "PERF: Make AdvancedNormalizedCorrelationImageToImageMetric faster"
N-Dekker added a commit that referenced this pull request Nov 27, 2021
Replace `this->EvaluateMovingImageValueAndDerivative(...)` calls by `this->FastEvaluateMovingImageValueAndDerivative(..., threadId)` in the "Threaded" member functions of six more metrics:

    itkAdvancedKappaStatisticImageToImageMetric
    itkAdvancedMeanSquaresImageToImageMetric
    itkParzenWindowHistogramImageToImageMetric
    itkParzenWindowMutualInformationImageToImageMetric
    itkPCAMetric_F_multithreaded
    itkSumSquaredTissueVolumeDifferenceImageToImageMetric

Expected a significant performance gain on the run-time duration.

Follow-up to pull request #556 commit c8c4a6a "PERF: Make AdvancedNormalizedCorrelationImageToImageMetric faster"
N-Dekker added a commit that referenced this pull request Nov 27, 2021
Replaced `this->EvaluateMovingImageValueAndDerivative(...)` calls by `this->FastEvaluateMovingImageValueAndDerivative(..., threadId)` in the "Threaded" member functions of six more metrics:

    itkAdvancedKappaStatisticImageToImageMetric
    itkAdvancedMeanSquaresImageToImageMetric
    itkParzenWindowHistogramImageToImageMetric
    itkParzenWindowMutualInformationImageToImageMetric
    itkPCAMetric_F_multithreaded
    itkSumSquaredTissueVolumeDifferenceImageToImageMetric

Expected a significant performance gain on the run-time duration.

`AdvancedKappaStatisticImageToImageMetric::ThreadedGetValueAndDerivative` is being tested by 55/161 - elastix_run_3DCT_lung.Kappa.bspline.ASGD.001_OUTPUT

`AdvancedMeanSquaresImageToImageMetric::ThreadedGetValueAndDerivative` is being tested by:

 - 43/161 - elastix_run_3DCT_lung.SSD.bspline.ASGD.001_OUTPUT
 - 98/161 - elastix_run_3DCT_lung.SSD.bspline.ASGD.002_OUTPUT
 - 102/161 - elastix_run_3DCT_lung.SSD.bspline.ASGD.003_OUTPUT
 - 106/161 - elastix_run_3DCT_lung.SSD.bspline.ASGD.001-Threads1_OUTPUT
 - 110/161 - elastix_run_3DCT_lung.SSD.bspline.ASGD.001-Threads2_OUTPUT
 - 114/161 - elastix_run_3DCT_lung.SSD.bspline.ASGD.001-Threads4_OUTPUT

`ParzenWindowHistogramImageToImageMetric::ThreadedComputePDFs` is being tested by:

 - 20/161 - elastix_run_example_OUTPUT
 - 47/161 - elastix_run_3DCT_lung.MI.bspline.ASGD.001_OUTPUT
 - 82/161 - elastix_run_3DCT_lung.MI.bspline.SGD.001_OUTPUT
 - 86/161 - elastix_run_3DCT_lung.MI.bspline.SGD.002_OUTPUT
 - 90/161 - elastix_run_3DCT_lung.MI.bspline.SGD.003_OUTPUT
 - 94/161 - elastix_run_3DCT_lung.MI.bspline.SGD.004_OUTPUT
 - 130/161 - elastix_run_3DCT_lung.MI.bspline.SGD.001-Threads1_OUTPUT
 - 134/161 - elastix_run_3DCT_lung.MI.bspline.SGD.001-Threads2_OUTPUT
 - 138/161 - elastix_run_3DCT_lung.MI.bspline.SGD.001-Threads4_OUTPUT

Follow-up to pull request #556 commit c8c4a6a "PERF: Make AdvancedNormalizedCorrelationImageToImageMetric faster"
N-Dekker added a commit that referenced this pull request Nov 28, 2021
Replaced `this->EvaluateMovingImageValueAndDerivative(...)` calls by `this->FastEvaluateMovingImageValueAndDerivative(..., threadId)` in the "Threaded" member functions of six more metrics:

    itkAdvancedKappaStatisticImageToImageMetric
    itkAdvancedMeanSquaresImageToImageMetric
    itkParzenWindowHistogramImageToImageMetric
    itkParzenWindowMutualInformationImageToImageMetric
    itkPCAMetric_F_multithreaded
    itkSumSquaredTissueVolumeDifferenceImageToImageMetric

Expected a significant performance gain on the run-time duration.

`AdvancedKappaStatisticImageToImageMetric::ThreadedGetValueAndDerivative` is being tested by 55/161 - elastix_run_3DCT_lung.Kappa.bspline.ASGD.001_OUTPUT

`AdvancedMeanSquaresImageToImageMetric::ThreadedGetValueAndDerivative` is being tested by:

 - 43/161 - elastix_run_3DCT_lung.SSD.bspline.ASGD.001_OUTPUT
 - 98/161 - elastix_run_3DCT_lung.SSD.bspline.ASGD.002_OUTPUT
 - 102/161 - elastix_run_3DCT_lung.SSD.bspline.ASGD.003_OUTPUT
 - 106/161 - elastix_run_3DCT_lung.SSD.bspline.ASGD.001-Threads1_OUTPUT
 - 110/161 - elastix_run_3DCT_lung.SSD.bspline.ASGD.001-Threads2_OUTPUT
 - 114/161 - elastix_run_3DCT_lung.SSD.bspline.ASGD.001-Threads4_OUTPUT

`ParzenWindowHistogramImageToImageMetric::ThreadedComputePDFs` is being tested by:

 - 20/161 - elastix_run_example_OUTPUT
 - 47/161 - elastix_run_3DCT_lung.MI.bspline.ASGD.001_OUTPUT
 - 82/161 - elastix_run_3DCT_lung.MI.bspline.SGD.001_OUTPUT
 - 86/161 - elastix_run_3DCT_lung.MI.bspline.SGD.002_OUTPUT
 - 90/161 - elastix_run_3DCT_lung.MI.bspline.SGD.003_OUTPUT
 - 94/161 - elastix_run_3DCT_lung.MI.bspline.SGD.004_OUTPUT
 - 130/161 - elastix_run_3DCT_lung.MI.bspline.SGD.001-Threads1_OUTPUT
 - 134/161 - elastix_run_3DCT_lung.MI.bspline.SGD.001-Threads2_OUTPUT
 - 138/161 - elastix_run_3DCT_lung.MI.bspline.SGD.001-Threads4_OUTPUT

Follow-up to pull request #556 commit c8c4a6a "PERF: Make AdvancedNormalizedCorrelationImageToImageMetric faster"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant