Skip to content

Commit

Permalink
Enable CUDA for NMI gradient unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
onurulgen committed Oct 19, 2023
1 parent 563a842 commit 4c1bc6a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
2 changes: 1 addition & 1 deletion niftyreg_build_version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
344
345
35 changes: 19 additions & 16 deletions reg-test/reg_test_nmi_gradient.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// OpenCL and CUDA are not supported for this test yet
// OpenCL is not supported for this test yet
#undef _USE_OPENCL
#undef _USE_CUDA

#include "reg_test_common.h"

Expand Down Expand Up @@ -82,9 +81,11 @@ class NMIGradientTest {
// Create the content
unique_ptr<DefContent> content{ contentCreator->Create(reference, floating) };
// Add some displacements to the deformation field to avoid grid effect
float *defPtr = static_cast<float*>(content->GetDeformationField()->data);
for (size_t index = 0; index < content->GetDeformationField()->nvox; ++index)
nifti_image *defField = content->Content::GetDeformationField();
float *defPtr = static_cast<float*>(defField->data);
for (size_t index = 0; index < defField->nvox; ++index)
defPtr[index] += 0.1f;
content->UpdateDeformationField();
// Compute the warped image given the current transformation
unique_ptr<Compute> compute{ platform->CreateCompute(*content) };
compute->ResampleImage(1, padding);
Expand All @@ -104,21 +105,23 @@ class NMIGradientTest {
// Create an image to store the expected gradient values
NiftiImage expectedGradientImage(content->GetDeformationField(), NiftiImage::Copy::Image);
// Apply perturbations to each value in the deformation field
float *gradPtr = static_cast<float *>(expectedGradientImage->data);
const float delta = 0.00001;
for (unsigned index = 0; index < expectedGradientImage.nVoxels(); ++index) {
float current_value = defPtr[index];
// compute the NMI when removing delta(s)
defPtr[index] = current_value - delta;
float *gradPtr = static_cast<float*>(expectedGradientImage->data);
constexpr float delta = 0.00001f;
for (auto index = 0; index < expectedGradientImage.nVoxels(); ++index) {
const float orgDefValue = defPtr[index];
// Compute the NMI when removing delta(s)
defPtr[index] = orgDefValue - delta;
content->UpdateDeformationField();
compute->ResampleImage(1, padding);
const double nmi_pre = measure_nmi->GetSimilarityMeasureValue();
// compute the NMI when adding delta(s)
defPtr[index] = current_value + delta;
const double nmiPre = measure_nmi->GetSimilarityMeasureValue();
// Compute the NMI when adding delta(s)
defPtr[index] = orgDefValue + delta;
content->UpdateDeformationField();
compute->ResampleImage(1, padding);
const double nmi_post = measure_nmi->GetSimilarityMeasureValue();
const double nmiPost = measure_nmi->GetSimilarityMeasureValue();
// Compute the difference
gradPtr[index] = -(nmi_post - nmi_pre) / (2. * delta);
defPtr[index] = current_value;
gradPtr[index] = float(-(nmiPost - nmiPre) / (2.0 * delta));
defPtr[index] = orgDefValue;
}
testCases.push_back({ testName + " "s + platform->GetName(), std::move(gradientImage), std::move(expectedGradientImage) });
}
Expand Down

0 comments on commit 4c1bc6a

Please sign in to comment.