ITK simplifies integrating your C++ algorithms into Python for AI R&D

You’re making great strides in your AI research using PyTorch, TensorFlow, scikit-learn, and other Python libraries; but for years you’ve been developing algorithms in C++; and to reach the next level in your AI research, you want to bring those C++ algorithms into your Python scripts. You could spend days/weeks/months re-implementing your C++ algorithms in Python, or you could simply convert your algorithms into Insight Toolkit (ITK) functions (or perhaps they already are!), and ITK Python can now automatically wrap them as Python functions!

Medical computing researchers and product developers already know that the ITK provides image normalization and augmentation techniques, supports over 50 medical image formats, and provides hundreds of image analysis methods for extracting medical imaging biomarkers.  What most of them do not know, however, are two critical facts:

  1. ITK is now available in Python, and it integrates closely with most popular AI toolkits.  While ITK is written in C++, for the past 15+ years it has used SWIG and CastXML-related tooling to provide some level of Python bindings. The important new news is that since the release of ITKv5.1, those ITK Python bindings have been honed to produce code and functions that look and feel like native Python library calls. These new interfaces also seamlessly support the processing of numpy arrays, dask arrays, xarray DataArray’s and numerous other constructs, as well as ITK Images. For example, if a numpy ndarray is passed as an input to an ITK image-to-image filter, a numpy ndarray is returned as the output. Consider the following code from the ITK Python Quick Start Guide:

image = itk.imread(input_filename)
median = itk.median_image_filter(image, radius=2)
itk.imwrite(median, output_filename)

  1. ITK’s remote modules provide an easy-to-use conduit for integrating your efficient C++ image processing algorithms with Python.   Let’s say that as part of your medical-imaging AI research, you would like to simulate body mass index (BMI) variations for data augmentation when training a deep learning method. Or you are dealing with an obscure microscopy imaging format. Or you have identified a bottleneck in your Python algorithm that you would like to implement in multi-threaded C++; then you should consider writing an ITK remote module that will automatically be included into ITK Python.

    To create an ITK remote module, follow the documentation provided with the ITKModuleTemplate. As explained in the documentation, the template module uses a Python package called “CookieCutter” that asks you a series of simple questions about the module that you want to build. CookieCutter then uses your answers to create a custom build environment for that module. Using that environment, you need only follow the well-documented ITK process for building a standard ITK class, e.g., an itkImageToImageFilter. Once you’ve defined that class, use CMake to enable ITK Python wrapping, and then compile it. Your class will then be available as an ITK Python function:

import itk
filtered_image = itk.your_image_filter(image)

That’s it! Using ITK Python in your AI research and creating your own ITK remote modules will enable you to effectively combine your C++ algorithms and expertise with cutting-edge AI libraries in Python.  You can also publish your custom ITK on PyPI and make your software accessible to the world….

Blended rendering of an MRA and a few segmented vessels. The segmented vessels (shown in red) were extracted using C++ methods in the TubeTK module that is distributed with ITK. This result, however, was generated by a Jupyter Notebook Python script. That script is available in the TubeTK/examples directory. Your C++ methods can likewise be made into ITK modules and then called from Python scripts.

Leave a Reply