-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImpactMetricExample.cxx
More file actions
137 lines (120 loc) · 5.9 KB
/
Copy pathImpactMetricExample.cxx
File metadata and controls
137 lines (120 loc) · 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
// IMPACT: semantic similarity registration from pretrained TorchScript features.
//
// This example mirrors ImpactMetricExample.py and shows the two ITK-facing pieces of
// the module. No torch type ever crosses the API: it speaks only ITK/STL types (images,
// a model-path string, a device string).
//
// 1. the core: itk::ImageToFeaturesMap extracts a feature map from an image;
// 2. the metric: itk::ImpactImageToImageMetricv4 plugs into
// itk::ImageRegistrationMethodv4 to register two images by comparing
// those features instead of raw intensities.
#include "itkImageToFeaturesMap.h"
#include "itkImpactImageToImageMetricv4.h"
#include "itkImpactModelConfiguration.h"
#include <itkImageFileReader.h>
#include <itkImageFileWriter.h>
#include <itkImageRegistrationMethodv4.h>
#include <itkRegularStepGradientDescentOptimizerv4.h>
#include <itkTranslationTransform.h>
#include <itkResampleImageFilter.h>
#include <itkBSplineInterpolateImageFunction.h>
int
main(int argc, char * argv[])
{
if (argc < 5)
{
std::cerr << "IMPACT: semantic similarity registration from pretrained features.\n";
std::cerr << "Usage: " << argv[0] << " model.pt fixedImage movingImage outputWarpedImage [device]\n";
std::cerr << " device: \"cpu\" (default), \"cuda\", \"cuda:0\", ...\n";
return EXIT_FAILURE;
}
const char * const modelPath = argv[1];
const char * const fixedPath = argv[2];
const char * const movingPath = argv[3];
const char * const outputPath = argv[4];
const std::string device = (argc > 5) ? argv[5] : "cpu";
constexpr unsigned int Dimension = 3;
using PixelType = float;
using ImageType = itk::Image<PixelType, Dimension>;
const auto fixed = itk::ReadImage<ImageType>(fixedPath);
const auto moving = itk::ReadImage<ImageType>(movingPath);
// A TorchScript model configuration: (path, dimension, channels, patchSize, voxelSize,
// overlap, layersMask, mixedPrecision). Only POD/STL types cross the API.
const itk::ImpactModelConfiguration config(
modelPath, Dimension, 1, { 0, 0, 0 }, { 1.0f, 1.0f, 1.0f }, { 2, 2, 2 }, { true }, false);
// --- 1. core: extract a dense feature map from the fixed image --------------------
using InterpolatorType = itk::BSplineInterpolateImageFunction<ImageType, double>;
auto interpolator = InterpolatorType::New();
interpolator->SetSplineOrder(3);
auto features = itk::ImageToFeaturesMap<ImageType, InterpolatorType>::New();
features->SetModelConfiguration(config);
features->SetInterpolator(interpolator);
features->SetDevice(device);
features->AddInput(fixed);
features->Update();
const auto featureMap = features->GetOutput(0); // itk::VectorImage<float, 3>
std::cout << "feature map: " << featureMap->GetLargestPossibleRegion().GetSize()
<< " channels: " << featureMap->GetNumberOfComponentsPerPixel() << std::endl;
// --- 2. metric: register moving onto fixed by comparing anatomical features -------
using MetricType = itk::ImpactImageToImageMetricv4<ImageType, ImageType>;
auto metric = MetricType::New();
std::vector<itk::ImpactModelConfiguration> models{ config };
metric->SetModelsConfiguration(models);
metric->SetDistance({ "L2" }); // per-layer loss: L1, L2, NCC, Cosine, Dice, ...
metric->SetLayersWeight({ 1.0f });
metric->SetSubsetFeatures({ 4 }); // random channel subset for speed (0 = all)
metric->SetPCA({ 0 });
metric->SetMode("Static"); // "Static" (precomputed features) or "Jacobian"
metric->SetDevice(device);
using TransformType = itk::TranslationTransform<double, Dimension>;
auto transform = TransformType::New();
transform->SetIdentity();
using OptimizerType = itk::RegularStepGradientDescentOptimizerv4<double>;
auto optimizer = OptimizerType::New();
optimizer->SetNumberOfIterations(200);
optimizer->SetLearningRate(2.0);
optimizer->SetMinimumStepLength(1e-4);
using RegistrationType = itk::ImageRegistrationMethodv4<ImageType, ImageType, TransformType>;
auto registration = RegistrationType::New();
registration->SetFixedImage(fixed);
registration->SetMovingImage(moving);
registration->SetMetric(metric);
registration->SetOptimizer(optimizer);
registration->SetInitialTransform(transform);
// Single resolution level (no shrink, no smoothing).
RegistrationType::ShrinkFactorsArrayType shrinkFactors(1);
RegistrationType::SmoothingSigmasArrayType smoothingSigmas(1);
shrinkFactors[0] = 1;
smoothingSigmas[0] = 0;
registration->SetNumberOfLevels(1);
registration->SetShrinkFactorsPerLevel(shrinkFactors);
registration->SetSmoothingSigmasPerLevel(smoothingSigmas);
registration->Update();
std::cout << "recovered parameters: " << registration->GetTransform()->GetParameters() << std::endl;
// --- 3. resample the moving image with the recovered transform --------------------
auto resample = itk::ResampleImageFilter<ImageType, ImageType>::New();
resample->SetInput(moving);
resample->SetTransform(registration->GetTransform());
resample->SetUseReferenceImage(true);
resample->SetReferenceImage(fixed);
resample->SetDefaultPixelValue(0);
itk::WriteImage(resample->GetOutput(), outputPath);
return EXIT_SUCCESS;
}