-
-
Notifications
You must be signed in to change notification settings - Fork 670
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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.
@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. |
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. |
There was a problem hiding this 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) |
There was a problem hiding this comment.
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]; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 * ...
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 :).
There was a problem hiding this comment.
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!
Thank you for your review, I very much appreciate your time. I will get to this in the coming week. |
@ljm898 thank you! Your efforts are greatly appreciated. |
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]; |
There was a problem hiding this comment.
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 :).
Something has gone wrong on my end. GitHub sure can be fun :). |
Modules/Registration/Common/include/itkMeanSquaresImageToImageMetric.hxx
Outdated
Show resolved
Hide resolved
Modules/Registration/Common/test/itkMeanSquaresImageMetricTest.cxx
Outdated
Show resolved
Hide resolved
Modules/Registration/Common/test/itkMeanSquaresImageMetricTest.cxx
Outdated
Show resolved
Hide resolved
There was a problem hiding this 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
.
Modules/Registration/Common/test/itkMeanSquaresImageMetricTest.cxx
Outdated
Show resolved
Hide resolved
Modules/Registration/Common/test/itkMeanSquaresImageMetricTest.cxx
Outdated
Show resolved
Hide resolved
Modules/Registration/Common/test/itkMeanSquaresImageMetricTest.cxx
Outdated
Show resolved
Hide resolved
b0c0f7a
to
5effe1a
Compare
There was a problem hiding this 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.
Modules/Registration/Common/test/itkMeanSquaresImageMetricTest.cxx
Outdated
Show resolved
Hide resolved
…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.
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 ITK.macOS |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Just for my understanding, it looks like the 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. |
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
Refer to the ITK Software Guide for
further development details if necessary.