Repository navigation
PERF: Keep the fixed image value of each metric sample #7223
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| schema_version: 1 | ||
| title: Image metrics keep the fixed image values of sampled points | ||
| summary: | | ||
| With a sampled point set and a linear fixed transform, `ImageToImageMetricv4` | ||
| now keeps the fixed image value of each sample after the first evaluation, | ||
| instead of interpolating the fixed image again on every evaluation. An edit of | ||
| the fixed image or fixed mask image buffer in place is seen only after | ||
| `Modified()` is called on that image. | ||
| details: | | ||
| The kept values are recomputed when the fixed image, fixed interpolator, fixed | ||
| mask, fixed transform (including the transforms inside a composite transform) | ||
| or sampled point set is modified, so code that changes these objects through | ||
| their setters gets the same results as before. Only code that writes into an | ||
| image buffer directly, for example through `GetBufferPointer()` or an image | ||
| iterator, between two evaluations of the same metric must call `Modified()`. | ||
| Few users should notice: registration methods do not write into the fixed | ||
| image during a run, and the usual image filters and readers mark their | ||
| output modified. | ||
|
|
||
| This applies to metrics whose sparse threader uses the default | ||
| `ProcessVirtualPoint()`: mean squares, Mattes mutual information, joint | ||
| histogram mutual information and Demons. Correlation and ANTS neighborhood | ||
| correlation, and fixed transforms that are not linear, such as displacement | ||
| fields, evaluate every point as before. | ||
| actions: | ||
| - After writing into the fixed image or fixed mask image buffer in place, call `Modified()` on that image before the metric is evaluated again. | ||
| - A threader that overrides `ProcessVirtualPoint()` without calling the superclass should override `UsesFixedSampleCache()` to return false, so that the metric does not allocate the cache. | ||
| categories: | ||
| - ResultsChange | ||
| - Performance | ||
| environment: | ||
| - cxx | ||
| impact: low | ||
| pr: 7223 | ||
| examples: | ||
| - language: cpp | ||
| before: | | ||
| fixedImage->GetBufferPointer()[0] = 0; | ||
| metric->GetValueAndDerivative(value, derivative); | ||
| after: | | ||
| fixedImage->GetBufferPointer()[0] = 0; | ||
| fixedImage->Modified(); | ||
| metric->GetValueAndDerivative(value, derivative); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -169,6 +169,10 @@ ImageToImageMetricv4<TFixedImage, TMovingImage, TVirtualImage, TInternalComputat | |
| itkDebugMacro("Initialize: ComputeMovingImageGradientFilterImage"); | ||
| this->ComputeMovingImageGradientFilterImage(); | ||
| } | ||
|
|
||
| /* Initialize() can change what the fixed samples map to, so start with an empty cache. */ | ||
| this->m_FixedSampleCacheIsEnabled = false; | ||
| this->m_FixedSampleCacheKey = FixedSampleCacheKey{}; | ||
| } | ||
|
|
||
| template <typename TFixedImage, | ||
|
|
@@ -280,6 +284,92 @@ ImageToImageMetricv4<TFixedImage, TMovingImage, TVirtualImage, TInternalComputat | |
| } | ||
| } | ||
|
|
||
| template <typename TFixedImage, | ||
| typename TMovingImage, | ||
| typename TVirtualImage, | ||
| typename TInternalComputationValueType, | ||
| typename TMetricTraits> | ||
| bool | ||
| ImageToImageMetricv4<TFixedImage, TMovingImage, TVirtualImage, TInternalComputationValueType, TMetricTraits>:: | ||
| InspectFixedTransform(const FixedTransformType * transform, ModifiedTimeType & time, bool & identity) | ||
| { | ||
| time = std::max(time, transform->GetMTime()); | ||
| if constexpr (VirtualImageDimension == FixedImageDimension) | ||
| { | ||
| using ScalarType = typename FixedTransformType::ScalarType; | ||
| if (dynamic_cast<const IdentityTransform<ScalarType, FixedImageDimension> *>(transform) != nullptr) | ||
| { | ||
| return true; | ||
| } | ||
| // A composite transform keeps its modified time when one of its transforms is modified. | ||
| using MultiTransformType = MultiTransform<ScalarType, FixedImageDimension, FixedImageDimension>; | ||
| if (const auto * const multiTransform = dynamic_cast<const MultiTransformType *>(transform)) | ||
| { | ||
| for (SizeValueType n = 0; n < multiTransform->GetNumberOfTransforms(); ++n) | ||
| { | ||
| if (!InspectFixedTransform(multiTransform->GetNthTransformConstPointer(n), time, identity)) | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
| } | ||
| identity = false; | ||
| return transform->GetTransformCategory() == FixedTransformType::TransformCategoryEnum::Linear; | ||
| } | ||
|
|
||
| template <typename TFixedImage, | ||
| typename TMovingImage, | ||
| typename TVirtualImage, | ||
| typename TInternalComputationValueType, | ||
| typename TMetricTraits> | ||
| void | ||
| ImageToImageMetricv4<TFixedImage, TMovingImage, TVirtualImage, TInternalComputationValueType, TMetricTraits>:: | ||
| UpdateFixedSampleCache(const bool used) const | ||
| { | ||
| ModifiedTimeType transformTime{}; | ||
| bool identity = true; | ||
| if (!used || !this->m_UseSampledPointSet || this->m_VirtualSampledPointSet.IsNull() || | ||
| !InspectFixedTransform(this->m_FixedTransform.GetPointer(), transformTime, identity)) | ||
| { | ||
| if (this->m_FixedSampleCacheIsEnabled) | ||
| { | ||
| this->m_FixedSampleCacheIsEnabled = false; | ||
| this->m_FixedSampleCacheKey = FixedSampleCacheKey{}; | ||
| std::vector<FixedSampleState>().swap(this->m_FixedSampleStates); | ||
| std::vector<FixedImagePixelType>().swap(this->m_FixedSampleValues); | ||
| std::vector<FixedImagePointType>().swap(this->m_FixedSamplePoints); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| FixedSampleCacheKey key; | ||
| for (const Object * const object : { static_cast<const Object *>(this->m_FixedImage.GetPointer()), | ||
| static_cast<const Object *>(this->m_FixedInterpolator.GetPointer()), | ||
| static_cast<const Object *>(this->m_FixedImageMask.GetPointer()), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Include the mask transforms' modified times in the key and add a test for this path. |
||
| static_cast<const Object *>(this->m_VirtualSampledPointSet.GetPointer()), | ||
| // SetPoint() modifies the points container, not the point set. | ||
| static_cast<const Object *>(this->m_VirtualSampledPointSet->GetPoints()) }) | ||
|
hjmjohnson marked this conversation as resolved.
Resolved
|
||
| { | ||
| key.Objects.push_back(object); | ||
| key.Times.push_back(object != nullptr ? object->GetMTime() : ModifiedTimeType{}); | ||
|
hjmjohnson marked this conversation as resolved.
Resolved
|
||
| } | ||
| key.Objects.push_back(this->m_FixedTransform.GetPointer()); | ||
| key.Times.push_back(transformTime); | ||
| key.NumberOfSamples = this->m_VirtualSampledPointSet->GetNumberOfPoints(); | ||
|
|
||
| if (this->m_FixedSampleCacheIsEnabled && key == this->m_FixedSampleCacheKey) | ||
| { | ||
| return; | ||
| } | ||
| this->m_FixedSampleStates.assign(key.NumberOfSamples, FixedSampleState::Unknown); | ||
| this->m_FixedSampleValues.resize(key.NumberOfSamples); | ||
| this->m_FixedSamplePoints.resize(identity ? 0 : key.NumberOfSamples); | ||
|
hjmjohnson marked this conversation as resolved.
Resolved
|
||
| this->m_FixedSampleCacheKey = std::move(key); | ||
| this->m_FixedSampleCacheIsEnabled = true; | ||
| } | ||
|
|
||
| template <typename TFixedImage, | ||
| typename TMovingImage, | ||
| typename TVirtualImage, | ||
|
|
@@ -321,6 +411,47 @@ ImageToImageMetricv4<TFixedImage, TMovingImage, TVirtualImage, TInternalComputat | |
| return pointIsValid; | ||
| } | ||
|
|
||
| template <typename TFixedImage, | ||
| typename TMovingImage, | ||
| typename TVirtualImage, | ||
| typename TInternalComputationValueType, | ||
| typename TMetricTraits> | ||
| bool | ||
| ImageToImageMetricv4<TFixedImage, TMovingImage, TVirtualImage, TInternalComputationValueType, TMetricTraits>:: | ||
| TransformAndEvaluateFixedSample(const SizeValueType sampleIndex, | ||
| const VirtualPointType & virtualPoint, | ||
| FixedImagePointType & mappedFixedPoint, | ||
| FixedImagePixelType & mappedFixedPixelValue) const | ||
| { | ||
| if (!this->m_FixedSampleCacheIsEnabled) | ||
| { | ||
| return this->TransformAndEvaluateFixedPoint(virtualPoint, mappedFixedPoint, mappedFixedPixelValue); | ||
| } | ||
| FixedSampleState & state = this->m_FixedSampleStates[sampleIndex]; | ||
| if (state == FixedSampleState::Unknown) | ||
| { | ||
| const bool pointIsValid = | ||
| this->TransformAndEvaluateFixedPoint(virtualPoint, mappedFixedPoint, mappedFixedPixelValue); | ||
| this->m_FixedSampleValues[sampleIndex] = mappedFixedPixelValue; | ||
| if (!this->m_FixedSamplePoints.empty()) | ||
| { | ||
| this->m_FixedSamplePoints[sampleIndex] = mappedFixedPoint; | ||
| } | ||
| state = pointIsValid ? FixedSampleState::Valid : FixedSampleState::Invalid; | ||
| return pointIsValid; | ||
| } | ||
| if (this->m_FixedSamplePoints.empty()) | ||
| { | ||
| this->LocalTransformPoint(virtualPoint, mappedFixedPoint); | ||
| } | ||
| else | ||
| { | ||
| mappedFixedPoint = this->m_FixedSamplePoints[sampleIndex]; | ||
| } | ||
| mappedFixedPixelValue = this->m_FixedSampleValues[sampleIndex]; | ||
| return state == FixedSampleState::Valid; | ||
| } | ||
|
|
||
| template <typename TFixedImage, | ||
| typename TMovingImage, | ||
| typename TVirtualImage, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.