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: Adding weights caching derivative calculation to iktMeanSquaresImageToImageMetric #4982

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ljm898
Copy link

@ljm898 ljm898 commented Nov 22, 2024

Weights caching is used for derivative calculation to reduce registration time with BSplineTransforms. Currently only implemented for MeanSquaresImageToImageMetric, other metrics to be added at a later date.

PR Checklist

  • No API changes were made (or the changes have been approved)
  • No major design changes were made (or the changes have been approved)
  • Added test (or behavior not changed)
  • Updated API documentation (or API not changed)
  • Added license to new files (if any)
  • Added Python wrapping to new files (if any) as described in ITK Software Guide Section 9.5
  • Added ITK examples for all new major features (if any)

Refer to the ITK Software Guide for
further development details if necessary.

@github-actions github-actions bot added type:Performance Improvement in terms of compilation or execution time area:Registration Issues affecting the Registration module labels Nov 22, 2024
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for contributing a pull request! 🙏

Welcome to the ITK community! 🤗👋☀️

We are glad you are here and appreciate your contribution. Please keep in mind our community participation guidelines. 📜
More support and guidance on the contribution process can be found in our contributing guide. 📖

This is an automatic message. Allow for time for the ITK community to be able to read the pull request and comment
on it.

@hjmjohnson
Copy link
Member

@ljm898 You may have missed some modifications:

Modules/Registration/Common/include/itkMeanSquaresImageToImageMetric.hxx:169:34: error: use of undeclared identifier 'm_NumBSplineWeights'

https://open.cdash.org/viewBuildError.php?buildid=10043237

The code is not compiling for this PR.

@hjmjohnson hjmjohnson marked this pull request as draft November 23, 2024 16:39
@ljm898
Copy link
Author

ljm898 commented Nov 24, 2024

Thanks in advance for your patience! Not sure why it worked on my pc when I tested it. From context and the error, it looks like it was missing a this-> at those lines. Hopefully that fixes it.

@ljm898 ljm898 marked this pull request as ready for review November 26, 2024 20:50
@thewtex thewtex requested a review from ntustison November 29, 2024 15:28
Copy link
Member

@hjmjohnson hjmjohnson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make this a single commit on top of the current master branch. Having two commits for 1 change makes it very hard to evaluate the change.

Recommended code:

  if (this->m_BSplineTransform && this->m_UseCachingOfBSplineWeights)
  {
    // using pre-computed weights and indexes to calculate only non zero elements of the derivative
    for (unsigned int w = 0; w < this->m_NumBSplineWeights; ++w)
    {
      const auto precomputedIndex = this->m_BSplineTransformIndicesArray[fixedImageSample][w];
      const auto precomputedWeight = this->m_BSplineTransformWeightsArray[fixedImageSample][w];
      for (unsigned int dim = 0; dim < MovingImageDimension; ++dim)
      {
        const int par = precomputedIndex + this->m_BSplineParametersOffset[dim];
        //threadS.m_MSEDerivative[par] += 2.0 * diff * precomputedWeight * movingImageGradientValue[dim];
        //Need to verify that a test fails with wrong answer below.
        threadS.m_MSEDerivative[par] += 99.0 * diff * precomputedWeight * movingImageGradientValue[dim];
      }
    }
  }
  else
  {
...

[ ] - Need to ensure that a test fails with a bad computation in the caching component.

// Jacobian should be evaluated at the unmapped (fixed image) point.
transform->ComputeJacobianWithRespectToParameters(fixedImagePoint, threadS.m_Jacobian);
for (unsigned int par = 0; par < this->m_NumberOfParameters; ++par)
if (this->m_TransformIsBSpline && this->m_UseCachingOfBSplineWeights)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this->m_TransformIsBSpline is deprecated.

 \deprecated `m_TransformIsBSpline` is intended to be removed, in the future. Please use `m_BSplineTransform`

for (unsigned int dim = 0; dim < MovingImageDimension; ++dim)
{
int par = this->m_BSplineTransformIndicesArray[fixedImageSample][k] + this->m_BSplineParametersOffset[dim];
threadS.m_MSEDerivative[par] += 2.0 * diff * this->m_BSplineTransformWeightsArray[fixedImageSample][k] * movingImageGradientValue[dim];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed line 174 to
threadS.m_MSEDerivative[par] = 99;

and no tests failed.

We need to have a tests that executes this code before it can be added to the toolkit. Currently the code is never run by any tests.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test has been added to itkMeanSquaresImageMetricTest.cxx that checks consistency between derivative computed omitting versus using weights caching. Tested on my end, can confirm it fails using 99.0 * ... and passes using 2.0 * ...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the test addition. Maybe you forgot to push the change, or pushed it improperly?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ljm898 We are looking forward to seeing these changes :).

Copy link
Author

@ljm898 ljm898 Dec 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the code didn't push properly. The test added to itkMeanSquaresImageMetricTest.cxx is now visible - thanks for your patience!

@hjmjohnson hjmjohnson marked this pull request as draft December 3, 2024 01:56
@ljm898
Copy link
Author

ljm898 commented Dec 5, 2024

Thank you for your review, I very much appreciate your time. I will get to this in the coming week.

@hjmjohnson
Copy link
Member

@ljm898 thank you!

Your efforts are greatly appreciated.

@ljm898 ljm898 marked this pull request as ready for review December 11, 2024 23:19
for (unsigned int dim = 0; dim < MovingImageDimension; ++dim)
{
int par = this->m_BSplineTransformIndicesArray[fixedImageSample][k] + this->m_BSplineParametersOffset[dim];
threadS.m_MSEDerivative[par] += 2.0 * diff * this->m_BSplineTransformWeightsArray[fixedImageSample][k] * movingImageGradientValue[dim];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ljm898 We are looking forward to seeing these changes :).

@github-actions github-actions bot added the type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct label Dec 12, 2024
@ljm898 ljm898 closed this Dec 13, 2024
@github-actions github-actions bot removed type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct area:Registration Issues affecting the Registration module labels Dec 13, 2024
@ljm898
Copy link
Author

ljm898 commented Dec 13, 2024

Something has gone wrong on my end. GitHub sure can be fun :).

@ljm898 ljm898 reopened this Dec 13, 2024
@github-actions github-actions bot added type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct area:Registration Issues affecting the Registration module labels Dec 13, 2024
Copy link
Member

@jhlegarreta jhlegarreta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some style comments, but mainly the adoption of ITK_TRY_EXPECT_NO_EXCEPTION.

@ljm898 ljm898 force-pushed the master branch 4 times, most recently from b0c0f7a to 5effe1a Compare December 16, 2024 04:23
Copy link
Member

@dzenanz dzenanz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly looks good to me.

…ageMetric

Weights caching is used for derivative calculation to reduce registration time with BSplineTransforms. Currently only implemented for MeanSquaresImageToImageMetric, other metrics to be added at a later date.
@ljm898
Copy link
Author

ljm898 commented Dec 17, 2024

Had a look into the failed/failing ITK.macOS check. itkNiftiReadWriteDirectionTest fails with code 255. I'd have expected the failed test to be independent of the suggested code changes. Any suggestions for fixing this compilation error?

@N-Dekker
Copy link
Contributor

Had a look into the failed/failing ITK.macOS check. itkNiftiReadWriteDirectionTest fails with code 255. I'd have expected the failed test to be independent of the suggested code changes. Any suggestions for fixing this compilation error?

You may rerun the CI for this machine, by adding a comment, saying something like /azp run <machine-name>. Well... I'll just give it a try 😃

@N-Dekker
Copy link
Contributor

/azp run ITK.macOS

@hjmjohnson hjmjohnson self-requested a review December 17, 2024 15:11
Copy link
Member

@dzenanz dzenanz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@N-Dekker
Copy link
Contributor

Just for my understanding, it looks like the UseCachingOfBSplineWeights property has been around for decades already (looking at git log). Did the original developers somehow forget to use it?

In which scenario's would you suggest users to switch it on?

@ljm898
Copy link
Author

ljm898 commented Dec 18, 2024

Just for my understanding, it looks like the UseCachingOfBSplineWeights property has been around for decades already (looking at git log). Did the original developers somehow forget to use it?

In which scenario's would you suggest users to switch it on?

Current versions use caching by default. AFAIK, caching was only used to compute deformation instead of the transform point method. Not sure why it wasn't originally used for derivatives - it could be the case that additional support has since been added to ITK that made it practical? I'm just stoked it works for derivatives.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:Registration Issues affecting the Registration module type:Performance Improvement in terms of compilation or execution time type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants