Skip to content

Meta-issue: nD registration in scikit-image #3810

Open

Description

@jni

Description

One of my major goals for this year (this statement will come back to bite me I'm sure!) is to add registration capabilities to scikit-image. Registration should at a minimum work in both 2D and 3D, and preferably in nD. I've laid out a few goals for the API, which are open for discussion:

  • distinguish between two categories of transformations: linear and nonlinear. Linear transforms can be represented by a matrix and can be executed by SciPy's ndimage.affine_transform. Nonlinear transforms can be arbitrary mappings of output to input coordinates and can be executed by ndimage.geometric_transform, given an arbitrary Python callable. Although a callable-based API is possible for affine transforms, these use highly optimized linear algebra systems and can be much faster than geometric_transforms, so it makes sense to provide a different API for them.
  • distinguish between two categories of objective functions: image matching, and control point matching. These have different API requirements, but we will aim to keep the two consistent.
    maximize compatibility with the SciPy library, using SciPy functions wherever possible. Specifically, scipy.linalg and scipy.sparse.linalg will be used to optimize closed-form objective functions (such as similarity transforms in keypoint matching using least squares), while scipy.optimize will be used for custom objective functions (such as normalized mutual information on the full images). The values returned by the API will be compatible with the corresponding transformation functions (affine_transform and geometric_transform). For example:
    matrix = registration.affine(reference, target)
    registered = ndi.affine_transform(target, matrix)
    or:
    spline = registration.bspline(reference_points, target_points)
    registered = ndi.geometric_transform(target, spline) # spline is LowLevelCallable
  • maximize compatibility with the scikit-learn API. scikit-learn is an extremely popular library whose main strength is a consistent API throughout the whole library. In some cases, mimicking this API would be beneficial for transforms: different classes of transforms would have .fit() and .transform() methods, just like scikit-learn transformers, which would make it very easy to try out different registration methods. It might even be possible to use scikit-learn's cross-validation machinery directly to find the "best" registration method for a particular dataset. This API might present some tension with scikit-image's preference for functions over classes, as well as with the SciPy direct compatibility, but these challenges could be overcome with adapter functions to make both APIs accessible.

Last semester, I worked with a couple of students, @seanbudd and @leGIT-bot, to start on this, see #3544. Comments there would be most welcome! As well as on this issue, of course.

Finally, I've also set up a project board here:

https://github.com/scikit-image/scikit-image/projects/8

Looking forward to everyone's thoughts!

Activity

  1. seanbudd commented on Mar 19, 2019

    @seanbudd
    Contributor

    Although it's been a while, I'm happy to continue working on #3544 given a suggested direction to improve the PR. With more time at hand and a better understanding of scikit-image and registration itself, hopefully I'll be more helpful

  2. soupault commented on Mar 21, 2019

    @soupault
    Member

    @jni Thank you for the write-up! I think your plan sound great, but I, personally, have some concerns about the competitiveness of the proposed method. Below are some of my thoughts on the topic, but, please, don't take them as a criticism.

    In my opinion, our implementation should:

    1. mainly serve educational purposes;
    2. be limited to affine registration, preferably, over image pyramid.

    Currently, one of the best "classic ML" methods in registration is elastix (http://elastix.isi.uu.nl/) (another is ANTs, I think, https://github.com/ANTsX/ANTs). I had a short discussion with the author, and he shared that their method is rather complex and is comprised of several steps, including automatic optimal learning rate (LR) search and non-trivial LR scheduling. And those steps are there for a reason 😉!
    FYI, non-rigid registration with elastix works quite reliable for 2D/3D data. Although, it often needs to be validated on the target data - http://elastix.bigr.nl/wiki/index.php/Parameter_file_database. Registering 1 volumetric MRI scans in elastix takes 1+ min. The software is written in C++ and is well-optimized.
    Having elastix freely available, I'm quite sceptical about replicating it's functionality inside skimage. That would be a hell of a lot of work. If we're going to have our own implementation for non-rigid case, we have to be sure that it works, at least, in comparable (same order of) time.

    Furthermore, 'deep learning'(DL)-based methods are currently getting a massive momentum (see https://arxiv.org/abs/1903.02026). The methods are computationally heavy in training, but can be easily accelerated by leveraging the DL frameworks (PyTorch, TensorFlow, etc) and GPUs. Inference is, typically, blazingly fast. These methods are still not widely adopted, but who knows, maybe we'll have one-shot registration already in 2020...

    P.S. I remember the preliminary discussion on adapting Numba/CyPy, but, I think, it is still in a very early stage.
    P.P.S. Having a competitive registration method in-house might set a good rationale for adding pydicom/something-something-NIfTI as an optional dependency.

  3. grlee77 commented on Mar 21, 2019

    @grlee77
    Contributor

    I agree with @soupault that getting these algorithms right is not trivial and would take substantial effort and expertise.

    The DiPy project has some fairly good code for registering 3D brain volumes that should also work for 2D images.

    They have

    1.) Affine Registration (can be limited to a rigid registration if requested).

    2.) Symmetric Diffeomorphic Registration (SyN) which is the same algorithm ANTs uses for non-rigid registration. It is built using Cython and OpenMP, so it is pretty fast. In my experience it ran more quickly than ANTs itself, although this might be due to less strict default stopping criteria, etc. I never did a close 1:1 comparison.

    Another option is to use registration functions from ITK directly in Python via SimpleITK. ANTs is an example of a library built on top of ITK (albeit in C++ rather than via the Python interface).

  4. jni commented on Mar 22, 2019

    @jni
    MemberAuthor

    @soupault

    mainly serve educational purposes;

    I agree that this is an important component, but I think we can do better than that. @grlee77 pointed to DiPy and I think that's a good model, but there is value in providing an API that is more functional (in the "functional programming" sense of the term) and consistent with the rest of scikit-image.

    be limited to affine registration, preferably, over image pyramid.

    Please review #3544 ! It provides exactly this!

    FYI, non-rigid registration with elastix works quite reliably for 2D/3D data.

    The idea here is to provide an easy-to-install, native Python interface. If we can do less fancy things that satisfy 90% or even 80% of use cases, but within skimage, that's a huge win. When I tried to get simpleelastix to work from Python it was a huge pain in the ass. Sure, maybe you only need to set it up once, but that's not great consolation when you are trying to help lots of users.

    If we're going to have our own implementation for non-rigid case, we have to be sure that it works, at least, in comparable (same order of) time.

    In terms of speed, it's a recurring theme that we are slower than other libraries. Nevertheless, we still have lots of users because of the more convenient API, reliability, and good integration with the rest of the ecosystem. The same value would translate very well to registration.

    I'll also point out that I have unrelated plans to optionally dispatch some critical functionality (convolutions, fft, affine transforms, integral image) to the GPU using gputools (https://github.com/maweigert/gputools), if it is present in the current install. That should offer massive speed improvements in many cases, including cases related to registration.

    I think DiPy is the best starting model for what we are trying to achieve. Thanks @grlee77 for pointing to it!

  5. hmaarrfk commented on Mar 22, 2019

    @hmaarrfk
    Member

    I still cant build an install elastix

  6. soupault commented on Mar 28, 2019

    @soupault
    Member

    @jni

    Please review #3544 ! It provides exactly this!

    Will do!

    If we can do less fancy things that satisfy 90% or even 80% of use cases, but within skimage, that's a huge win.

    I rather agree with you. Larger user base leads to a larger impact, and this Nature Methods publication is a solid proof - https://www.nature.com/articles/s41592-018-0261-2 🙂.
    Let me check #3544. I guess, we might need to come up with an abstraction for registration atlas. 🤔

    When I tried to get simpleelastix to work from Python it was a huge pain in the ass.

    Oh, YES! I can feel your pain. It seemed to me that to build SimpleElastix one needs to use a specific commit (not a release) of the package, same for SimpleITK, specific version of GCC, kernel, hardware, be in a good mood, do sports and eat healthy 😆. Even using Docker does not help. On the other hand, binary distributions of elastix work like a charm. The situation is changing for the better, however - SuperElastix/SimpleElastix#34 (comment).

  7. jni commented on Oct 15, 2019

    @jni
    MemberAuthor

    Just a note so I don't lose it: registration/transformation with anisotropic axes can be tricky, as illustrated here: VolkerH/Lattice_Lightsheet_Deskew_Deconv#22

    Doing this correctly might require some pre-filtering of reference/moving/both images.

  8. locked and limited conversation to collaborators on Oct 18, 2021
  9. unlocked this conversation on Feb 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions