Metadata-Version: 2.1
Name: itk-impact
Version: 0.1.0
Summary: A semantic similarity metric for multimodal image registration using features from pretrained TorchScript models.
Keywords: itk
Author-Email: Valentin Boussot <boussot.v@gmail.com>
License: 
                                          Apache License
                                    Version 2.0, January 2004
                                 https://www.apache.org/licenses/
         
            TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
         
            1. Definitions.
         
               "License" shall mean the terms and conditions for use, reproduction,
               and distribution as defined by Sections 1 through 9 of this document.
         
               "Licensor" shall mean the copyright owner or entity authorized by
               the copyright owner that is granting the License.
         
               "Legal Entity" shall mean the union of the acting entity and all
               other entities that control, are controlled by, or are under common
               control with that entity. For the purposes of this definition,
               "control" means (i) the power, direct or indirect, to cause the
               direction or management of such entity, whether by contract or
               otherwise, or (ii) ownership of fifty percent (50%) or more of the
               outstanding shares, or (iii) beneficial ownership of such entity.
         
               "You" (or "Your") shall mean an individual or Legal Entity
               exercising permissions granted by this License.
         
               "Source" form shall mean the preferred form for making modifications,
               including but not limited to software source code, documentation
               source, and configuration files.
         
               "Object" form shall mean any form resulting from mechanical
               transformation or translation of a Source form, including but
               not limited to compiled object code, generated documentation,
               and conversions to other media types.
         
               "Work" shall mean the work of authorship, whether in Source or
               Object form, made available under the License, as indicated by a
               copyright notice that is included in or attached to the work
               (an example is provided in the Appendix below).
         
               "Derivative Works" shall mean any work, whether in Source or Object
               form, that is based on (or derived from) the Work and for which the
               editorial revisions, annotations, elaborations, or other modifications
               represent, as a whole, an original work of authorship. For the purposes
               of this License, Derivative Works shall not include works that remain
               separable from, or merely link (or bind by name) to the interfaces of,
               the Work and Derivative Works thereof.
         
               "Contribution" shall mean any work of authorship, including
               the original version of the Work and any modifications or additions
               to that Work or Derivative Works thereof, that is intentionally
               submitted to Licensor for inclusion in the Work by the copyright owner
               or by an individual or Legal Entity authorized to submit on behalf of
               the copyright owner. For the purposes of this definition, "submitted"
               means any form of electronic, verbal, or written communication sent
               to the Licensor or its representatives, including but not limited to
               communication on electronic mailing lists, source code control systems,
               and issue tracking systems that are managed by, or on behalf of, the
               Licensor for the purpose of discussing and improving the Work, but
               excluding communication that is conspicuously marked or otherwise
               designated in writing by the copyright owner as "Not a Contribution."
         
               "Contributor" shall mean Licensor and any individual or Legal Entity
               on behalf of whom a Contribution has been received by Licensor and
               subsequently incorporated within the Work.
         
            2. Grant of Copyright License. Subject to the terms and conditions of
               this License, each Contributor hereby grants to You a perpetual,
               worldwide, non-exclusive, no-charge, royalty-free, irrevocable
               copyright license to reproduce, prepare Derivative Works of,
               publicly display, publicly perform, sublicense, and distribute the
               Work and such Derivative Works in Source or Object form.
         
            3. Grant of Patent License. Subject to the terms and conditions of
               this License, each Contributor hereby grants to You a perpetual,
               worldwide, non-exclusive, no-charge, royalty-free, irrevocable
               (except as stated in this section) patent license to make, have made,
               use, offer to sell, sell, import, and otherwise transfer the Work,
               where such license applies only to those patent claims licensable
               by such Contributor that are necessarily infringed by their
               Contribution(s) alone or by combination of their Contribution(s)
               with the Work to which such Contribution(s) was submitted. If You
               institute patent litigation against any entity (including a
               cross-claim or counterclaim in a lawsuit) alleging that the Work
               or a Contribution incorporated within the Work constitutes direct
               or contributory patent infringement, then any patent licenses
               granted to You under this License for that Work shall terminate
               as of the date such litigation is filed.
         
            4. Redistribution. You may reproduce and distribute copies of the
               Work or Derivative Works thereof in any medium, with or without
               modifications, and in Source or Object form, provided that You
               meet the following conditions:
         
               (a) You must give any other recipients of the Work or
                   Derivative Works a copy of this License; and
         
               (b) You must cause any modified files to carry prominent notices
                   stating that You changed the files; and
         
               (c) You must retain, in the Source form of any Derivative Works
                   that You distribute, all copyright, patent, trademark, and
                   attribution notices from the Source form of the Work,
                   excluding those notices that do not pertain to any part of
                   the Derivative Works; and
         
               (d) If the Work includes a "NOTICE" text file as part of its
                   distribution, then any Derivative Works that You distribute must
                   include a readable copy of the attribution notices contained
                   within such NOTICE file, excluding those notices that do not
                   pertain to any part of the Derivative Works, in at least one
                   of the following places: within a NOTICE text file distributed
                   as part of the Derivative Works; within the Source form or
                   documentation, if provided along with the Derivative Works; or,
                   within a display generated by the Derivative Works, if and
                   wherever such third-party notices normally appear. The contents
                   of the NOTICE file are for informational purposes only and
                   do not modify the License. You may add Your own attribution
                   notices within Derivative Works that You distribute, alongside
                   or as an addendum to the NOTICE text from the Work, provided
                   that such additional attribution notices cannot be construed
                   as modifying the License.
         
               You may add Your own copyright statement to Your modifications and
               may provide additional or different license terms and conditions
               for use, reproduction, or distribution of Your modifications, or
               for any such Derivative Works as a whole, provided Your use,
               reproduction, and distribution of the Work otherwise complies with
               the conditions stated in this License.
         
            5. Submission of Contributions. Unless You explicitly state otherwise,
               any Contribution intentionally submitted for inclusion in the Work
               by You to the Licensor shall be under the terms and conditions of
               this License, without any additional terms or conditions.
               Notwithstanding the above, nothing herein shall supersede or modify
               the terms of any separate license agreement you may have executed
               with Licensor regarding such Contributions.
         
            6. Trademarks. This License does not grant permission to use the trade
               names, trademarks, service marks, or product names of the Licensor,
               except as required for reasonable and customary use in describing the
               origin of the Work and reproducing the content of the NOTICE file.
         
            7. Disclaimer of Warranty. Unless required by applicable law or
               agreed to in writing, Licensor provides the Work (and each
               Contributor provides its Contributions) on an "AS IS" BASIS,
               WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
               implied, including, without limitation, any warranties or conditions
               of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
               PARTICULAR PURPOSE. You are solely responsible for determining the
               appropriateness of using or redistributing the Work and assume any
               risks associated with Your exercise of permissions under this License.
         
            8. Limitation of Liability. In no event and under no legal theory,
               whether in tort (including negligence), contract, or otherwise,
               unless required by applicable law (such as deliberate and grossly
               negligent acts) or agreed to in writing, shall any Contributor be
               liable to You for damages, including any direct, indirect, special,
               incidental, or consequential damages of any character arising as a
               result of this License or out of the use or inability to use the
               Work (including but not limited to damages for loss of goodwill,
               work stoppage, computer failure or malfunction, or any and all
               other commercial damages or losses), even if such Contributor
               has been advised of the possibility of such damages.
         
            9. Accepting Warranty or Additional Liability. While redistributing
               the Work or Derivative Works thereof, You may choose to offer,
               and charge a fee for, acceptance of support, warranty, indemnity,
               or other liability obligations and/or rights consistent with this
               License. However, in accepting such obligations, You may act only
               on Your own behalf and on Your sole responsibility, not on behalf
               of any other Contributor, and only if You agree to indemnify,
               defend, and hold each Contributor harmless for any liability
               incurred by, or claims asserted against, such Contributor by reason
               of your accepting any such warranty or additional liability.
         
            END OF TERMS AND CONDITIONS
         
            APPENDIX: How to apply the Apache License to your work.
         
               To apply the Apache License to your work, attach the following
               boilerplate notice, with the fields enclosed by brackets "[]"
               replaced with your own identifying information. (Don't include
               the brackets!)  The text should be enclosed in the appropriate
               comment syntax for the file format. We also recommend that a
               file or class name and description of purpose be included on the
               same "printed page" as the copyright notice for easier
               identification within third-party archives.
         
            Copyright [yyyy] [name of copyright owner]
         
            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
         
            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.
         
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Education
Classifier: Intended Audience :: Healthcare Industry
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Operating System :: Android
Classifier: Operating System :: MacOS
Classifier: Operating System :: Microsoft :: Windows
Classifier: Operating System :: POSIX
Classifier: Operating System :: Unix
Classifier: Programming Language :: C++
Classifier: Programming Language :: Python
Classifier: Topic :: Scientific/Engineering
Classifier: Topic :: Scientific/Engineering :: Information Analysis
Classifier: Topic :: Scientific/Engineering :: Medical Science Apps.
Classifier: Topic :: Software Development :: Libraries
Project-URL: Download, https://github.com/InsightSoftwareConsortium/ITKIMPACT
Project-URL: Homepage, https://github.com/InsightSoftwareConsortium/ITKIMPACT
Requires-Python: >=3.8
Requires-Dist: itk==5.4.*
Requires-Dist: torch==2.12.*
Description-Content-Type: text/markdown

<p align="center">
  <img src="logo.png" alt="IMPACT-Reg logo" width="300"/>
</p>

# ITKImpact

[![Build Status](https://github.com/InsightSoftwareConsortium/ITKIMPACT/actions/workflows/build-test-package.yml/badge.svg)](https://github.com/InsightSoftwareConsortium/ITKIMPACT/actions/workflows/build-test-package.yml)
[![PyPI Version](https://img.shields.io/pypi/v/itk-impact.svg)](https://pypi.python.org/pypi/itk-impact)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://github.com/InsightSoftwareConsortium/ITKIMPACT/blob/main/LICENSE)

<p align="center"><em>Compare and register medical images in the deep-feature space of pretrained
models — semantic, model-agnostic, and native to ITK.</em></p>

**At a glance**

- **Semantic, model-agnostic comparison** — match anatomy in the internal features of *any*
  pretrained TorchScript model (TotalSegmentator, SAM 2.1, DINOv2, MIND, …), robust across
  modality, contrast, noise, and artifacts.
- **Drop-in ITK v4 metric** — `itk::ImpactImageToImageMetricv4` plugs straight into
  `itk::ImageRegistrationMethodv4` and any v4 optimizer; only the comparison changes.
- **Torch-backed dense registration** — a self-contained ConvexAdam-style coarse→fine pipeline,
  exposed as ITK filters, refined on GPU with `torch::optim::Adam`.
- **A deep-learning inference engine for ITK** — `itk::ImageToFeaturesMap` runs *any* TorchScript
  model patch-wise (segmentation, synthesis, denoising, …): image in, `itk::VectorImage` out.
- **Python & C++, CPU & CUDA** — one small wheel that reuses the LibTorch inside your installed
  `torch`; the same core also powers the elastix plugin.

## Overview

**ITKImpact** brings the **IMPACT** ecosystem into the heart of [ITK](https://itk.org): an
official ITK remote module that makes **anatomical comparison in the deep-feature space of
pretrained models** a first-class ITK capability, instead of comparing images at the
intensity level.

The same anatomy looks very different across modality (CT / MR / CBCT), contrast, noise, and
artifacts, so a pixel-wise intensity comparison is unreliable. The internal features of a
pretrained model — a segmentation backbone especially — make anatomical structures stand out
and attenuate artifacts, separating *anatomy* (shapes, structure, spatial organization) from
*appearance* (intensity, contrast, noise). Comparing images there stays stable when the
appearance changes — the common thread of the whole IMPACT ecosystem.

**IMPACT** (*Image Metric with Pretrained model-Agnostic Comparison for Transmodality
registration*) is **model-agnostic**: any model mapping an image to features works — a
`TotalSegmentator` / `nnU-Net` backbone, `SAM`, `DINOv2`, MIND descriptors, a self-supervised
encoder — once exported to TorchScript. IMPACT is already an official metric in
[elastix](https://github.com/SuperElastix/elastix); this module puts the *shared core* inside
ITK, so the ITK-native tools and the [**ImpactElastix**](https://github.com/vboussot/ImpactElastix)
plugin build on one implementation.

### The IMPACT ecosystem

Three layers, from the shared foundation up to the applications:

1. **Core — anatomy comparison.** The framework-neutral foundation: **load** a TorchScript
   model (`itk::ModelConfiguration`), **extract** dense feature maps (`itk::ImageToFeaturesMap`),
   and **compare** them with differentiable distances — L1, L2, NCC, cosine, L1-cosine,
   dot-product, Dice (`ImpactLoss.h`). Runs on LibTorch (CPU/CUDA) with ITK images as the
   boundary (`itk::ImageToTensorFilter` / `itk::TensorToImageFilter`). It depends only on ITK
   and LibTorch, and is the backend shared with **ImpactElastix**.

   > **More than features — a deep-learning inference engine for ITK.**
   > `itk::ImageToFeaturesMap` is really a **patch-based TorchScript inference engine**: it tiles
   > the image, runs *any* TorchScript model on each patch (CPU/CUDA, mixed precision), blends the
   > overlaps, and returns the result as an `itk::VectorImage`. Feature extraction for comparison
   > is just one use — the same class runs **segmentation, image synthesis / modality translation,
   > denoising, super-resolution**, any image→image or image→tensor model — so it doubles as a
   > general model-inference filter you can drop into ITK pipelines.

2. **ITK metric — IMPACT-Reg for the v4 framework.** `itk::ImpactImageToImageMetricv4`, a
   drop-in semantic similarity metric for `itk::ImageRegistrationMethodv4` and any
   `itk::*Optimizerv4`. The registration engine stays standard; only the comparison changes.

3. **Registration framework — Torch-backed dense registration.** A self-contained
   [ConvexAdam](https://github.com/multimodallearning/convexAdam)-style pipeline (coarse
   discrete initialization → GPU Adam refinement), exposed as ITK filters but driven by
   *anatomical features* rather than intensities, kept in a single autograd graph for speed.

### Pretrained models

You supply the TorchScript feature extractor. Ready-to-use models are on Hugging Face —
[**VBoussot/impact-torchscript-models**](https://huggingface.co/VBoussot/impact-torchscript-models):
**TotalSegmentator**, **MRSegmentator**, **SAM 2.1**, **DINOv2**, **Anatomix**, and a
TorchScript **MIND** descriptor. The best model / layer / distance is problem-dependent (see
the paper's ablations).

## Installation

### Python

```bash
pip install itk-impact torch
```

The wheel does **not** bundle LibTorch: it links the libtorch that ships inside the `torch`
package, so a single small wheel inherits **CPU or GPU from whichever torch you installed** —
install a CUDA build (`pip install torch --index-url https://download.pytorch.org/whl/cuXXX`)
plus a matching NVIDIA driver for GPU execution, or the default CPU torch otherwise. The torch
version is ABI-coupled to the wheel; `pip` enforces the compatible range. Because the loader
resolves libtorch from the installed torch, `import torch` before the IMPACT filters if you do
not otherwise import it (KonfAI already does).

### C++ (from source)

`itk-impact` is a standard [ITK remote module](https://github.com/InsightSoftwareConsortium/ITKModuleTemplate),
with the one extra dependency of **LibTorch**. By default `find_package(Torch)` is auto-located
from the installed `torch` Python package (`torch.utils.cmake_prefix_path`) — just
`pip install torch` first. Build it against an existing ITK build:

```bash
git clone https://github.com/InsightSoftwareConsortium/ITKIMPACT
cmake -B ITKIMPACT-build -S ITKIMPACT \
  -DCMAKE_BUILD_TYPE=Release \
  -DITK_DIR=/path/to/ITK-build
cmake --build ITKIMPACT-build -j
```

To use a manually downloaded LibTorch C++ distribution instead of the pip package, pass
`-DCMAKE_PREFIX_PATH=/path/to/libtorch` (it takes precedence over the auto-detection). Build it
inside the ITK source tree by enabling `-DModule_Impact=ON`; with `-DITK_WRAP_PYTHON=ON` this
also produces the Python wrapping.

## The Torch registration pipeline

Layer 3 aligns images by their anatomical features in two stages — a coarse discrete
initializer, then a GPU Adam refinement:

```
fixed image ─┐
moving image ┼─▶ itk::ImpactCoarseRegistration ─▶ initial displacement field
             │     (coarse: discrete cost volume +
             │      coupled-convex global regularization)
             └─▶ itk::ImpactFineRegistration ──▶ refined displacement field
                   (fine: grid_sample warp + IMPACT
                    feature loss + diffusion reg, Adam)
```

- **`itk::ImpactCoarseRegistration`** — coarse stage. A discrete SSD cost volume over a dense
  displacement window on a pooled coarse grid, coupled-convex global regularization, then
  upsampling to a full-resolution field. Robust to large misalignments; runs on raw
  intensities or any IMPACT model's features; 2D and 3D.
- **`itk::ImpactFineRegistration`** — fine stage. Holds the field as a GPU leaf tensor, warps
  the moving image/features with `grid_sample`, and minimizes a similarity loss (intensity MSE
  or IMPACT feature loss) plus a diffusion regularizer with `torch::optim::Adam` — all on
  device, no per-iteration CPU↔GPU round trip. Optional low-resolution control grid
  (`GridShrinkFactor`) and PCA channel reduction.

Both output a geometry-correct `itk::DisplacementFieldTransform` (physical millimetres,
fixed→moving); the ITK↔Torch axis/units/direction conventions are handled internally.

## Quick start (Python)

```python
import itk

ImageType = itk.Image[itk.F, 3]
fixed  = itk.imread("fixed.mha",  itk.F)
moving = itk.imread("moving.mha", itk.F)

# Stage 1 — coarse initialization (intensity SSD cost volume)
coarse = itk.ImpactCoarseRegistration[ImageType, ImageType].New()
coarse.SetFixedImage(fixed)
coarse.SetMovingImage(moving)
coarse.SetGridSpacing(4)
coarse.SetDisplacementHalfWidth(5)
coarse.SetDevice("cuda:0")          # or "cpu"
coarse.Update()

# Stage 2 — fast Adam refinement on IMPACT features
cfg = itk.ModelConfiguration("features_model.pt", 3, 1,
                             [0, 0, 0], [1.0, 1.0, 1.0], 0, [True, False], False)
fine = itk.ImpactFineRegistration[ImageType, ImageType].New()
fine.SetFixedImage(fixed)
fine.SetMovingImage(moving)
fine.SetDevice("cuda:0")
fine.SetInitialDisplacementField(coarse.GetDisplacementField())  # warm start
fine.AddModelConfiguration(cfg)
fine.SetDistance(["L2"])            # per-layer loss: L1, L2, NCC, Cosine, Dice, ...
fine.SetNumberOfIterations(100)
fine.SetLearningRate(0.1)
fine.SetRegularizationWeight(1.0)
fine.Update()

field     = fine.GetDisplacementField()           # itk.Image[itk.Vector[itk.F,3],3]
transform = fine.GetDisplacementFieldTransform()   # ready for itk.ResampleImageFilter
warped    = fine.GetWarpedMovingImage()
```

Leaving the model configuration empty makes both filters use a raw-intensity (MSE/SSD)
similarity instead of features. See [`examples/`](examples/) for a metric-based registration
demo (C++ and Python).

## Quick start (C++)

```cpp
using ImageType = itk::Image<float, 3>;

auto coarse = itk::ImpactCoarseRegistration<ImageType>::New();
coarse->SetFixedImage(fixed);
coarse->SetMovingImage(moving);
coarse->SetGridSpacing(4);
coarse->SetDisplacementHalfWidth(5);
coarse->SetDevice("cuda:0");
coarse->Update();

auto fine = itk::ImpactFineRegistration<ImageType>::New();
fine->SetFixedImage(fixed);
fine->SetMovingImage(moving);
fine->SetDevice("cuda:0");
fine->SetInitialDisplacementField(coarse->GetDisplacementField());
itk::ModelConfiguration cfg("features_model.pt", 3, 1, {0,0,0}, {1,1,1}, 0, {true,false}, false);
fine->AddModelConfiguration(cfg);
fine->SetDistance({"L2"});
fine->SetNumberOfIterations(100);
fine->Update();

auto * transform = fine->GetDisplacementFieldTransform();
```

## Components

| Layer | Class / file | Role |
|---|---|---|
| Core | `itk::ModelConfiguration` | Configures and loads a TorchScript feature model. |
| Core | `itk::ImageToFeaturesMap` | Patch-based TorchScript inference engine (tiling, overlap blending, PCA): dense feature maps — or any model output (segmentation, synthesis, denoising…). |
| Core | `itk::ImageToTensorFilter` / `itk::TensorToImageFilter` | ITK image ↔ `torch::Tensor` bridge. |
| Core | `ImpactLoss.h` (`itk::Impact`) | Differentiable feature losses: L1, L2, NCC, Cosine, L1Cosine, DotProduct, Dice. |
| Metric | `itk::ImpactImageToImageMetricv4` | Semantic similarity metric for the ITK v4 framework. |
| Registration | `itk::ImpactCoarseRegistration` | ConvexAdam-style coarse discrete initializer (stage 1). |
| Registration | `itk::ImpactFineRegistration` | Torch-backed Adam dense registration (fine stage). |

## Dependencies

- **ITK** (the metric uses the v4 registration framework).
- **LibTorch** (the C++ PyTorch distribution), built with the matching CUDA toolkit for GPU
  support. Found via `find_package(Torch)`; every client links LibTorch.
- A **pretrained feature model exported to TorchScript** (`.pt`) for the feature modes (not
  needed for the raw-intensity modes) —
  [ready-to-use models](https://huggingface.co/VBoussot/impact-torchscript-models).

The public, Python-wrapped headers are intentionally free of any LibTorch include (torch state
lives behind opaque handles), so the Python bindings build without exposing `torch::*`.

## Notes

- Devices: `"cpu"`, `"cuda"`, `"cuda:0"`, … via `SetDevice`.
- Displacement fields are `itk::Image<itk::Vector<float, N>, N>`.
- The feature path runs each model once on the whole volume and warps the resulting feature
  maps; layers may be coarser than the input and are handled at their native (possibly
  downsampled) resolution, without ever upsampling the features.

## References

If you use IMPACT, please cite the paper ([arXiv:2503.24121](https://arxiv.org/abs/2503.24121)):

```bibtex
@article{boussot2025impact,
  title   = {IMPACT: A Generic Semantic Loss for Multimodal Medical Image Registration},
  author  = {Boussot, Valentin and H{\'e}mon, C{\'e}dric and Nunes, Jean-Claude and
             Dowling, Jason and Rouz{\'e}, Simon and Lafond, Caroline and
             Barateau, Ana{\"i}s and Dillenseger, Jean-Louis},
  journal = {arXiv preprint arXiv:2503.24121},
  year    = {2025}
}
```

The IMPACT ecosystem:

- **Paper:** [arXiv:2503.24121](https://arxiv.org/abs/2503.24121)
- **Reference implementation (PyTorch):** [vboussot/ImpactLoss](https://github.com/vboussot/ImpactLoss)
- **elastix integration:** [vboussot/ImpactElastix](https://github.com/vboussot/ImpactElastix)
- **Pretrained TorchScript models:** [VBoussot/impact-torchscript-models](https://huggingface.co/VBoussot/impact-torchscript-models)

Applications built on IMPACT:

- **3D Slicer extension** — [vboussot/SlicerImpactReg](https://github.com/vboussot/SlicerImpactReg): multimodal registration with the IMPACT metric, directly in 3D Slicer.
- **KonfAI CLI** — [vboussot/KonfAI](https://github.com/vboussot/KonfAI) (PyPI [`impact_reg_konfai`](https://pypi.org/project/impact_reg_konfai/)): ready-to-run IMPACT-Reg registration presets in the KonfAI framework.
