Nitrain: A Medical Imaging-Native AI Framework

Feb 17, 2021 | Data Science

Nitrain is a robust framework designed specifically for handling the nuances of medical imaging with ease. Formerly known as itorchsamplei, this Python library stands out by offering incredible flexibility and simplicity for sampling, augmenting medical images, and training models on various medical imaging datasets.

What Makes Nitrain Unique?

The Nitrain library’s charm lies in its ability to abstract complexity. It offers reasonable defaults that simplify the model training process, making it accessible even for those new to the field. Nitrain supports multiple deep learning frameworks, including Torch, TensorFlow, and Keras, ensuring versatility. Whether you’re experimenting with semantic segmentation or other models, Nitrain turns a daunting task into a manageable one.

Getting Started with Nitrain

Let’s dive into a typical use case involving semantic segmentation. Through this example, you’ll see the power and simplicity Nitrain brings to the table.

import nitrain as nt
from nitrain.readers import ImageReader, ColumnReader

# Step 1: Create dataset 
dataset = nt.Dataset(
    inputs=ImageReader(sub-*anat*_T1w.nii.gz),
    outputs=ImageReader(sub-*anat*_aparc+aseg.nii.gz),
    transforms=[
        inputs: tx.NormalizeIntensity(0,1),
        (inputs, outputs): tx.Resize((64,64,64))
    ],
    base_dir=~desktopds004711
)

# Step 2: Create loader with random transforms 
loader = nt.Loader(
    dataset,
    images_per_batch=4,
    sampler=nt.SliceSampler(batch_size=32, axis=2),
    transforms=[inputs: tx.RandomNoise(sd=0.2)]
)

# Step 3: Create model from architecture 
arch_fn = nt.fetch_architecture(unet, dim=2)
model = arch_fn(input_image_size=(64,64,1), mode=segmentation)

# Step 4: Create trainer and fit model 
trainer = nt.Trainer(model, task=segmentation)
trainer.fit(loader, epochs=100)

Breaking Down the Code: An Analogy

Imagine you are a chef preparing a three-course meal. First, you gather your ingredients (create a dataset) by choosing the freshest vegetables and proteins (inputs and outputs) available. Then, you organize your kitchen (loader) so that each ingredient is prepared perfectly. Finally, you follow the recipe to create a delightful dish (model training) that astounds your guests (achieves high performance on model tasks).

Installation Instructions

Installing Nitrain is straightforward. You can get the latest release from PyPI:

pip install nitrain

Alternatively, if you want the latest features directly from the development branch, run:

python -m pip install git+github.com/nitrain/nitrain.git

Dependencies

Nitrain is built on the powerful ANTsPy library, which is vital for efficiently reading, operating on, and visualizing medical images. Depending on what models you are creating, you may choose from Keras, TensorFlow, or PyTorch as your backend.

Resources for Learning More

To deepen your knowledge about Nitrain, you can explore the following resources:

  • Introduction tutorials: Link
  • Segmentation examples: Link
  • Classification examples: Link
  • Registration examples: Link

For additional information on handling medical images, refer to the ANTsPy repository.

Troubleshooting Tips

If you encounter any issues while using Nitrain, here are a few troubleshooting strategies:

  • Double-check that you have all necessary dependencies installed, particularly AnTsPy.
  • If the code fails during data loading, confirm that your image paths and transforms are accurately specified.
  • For model training problems, inspect the architecture and make sure your input dimensions match what is expected.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

In Conclusion

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox