Using data augmentation to generate new data for deep learning

Hi everyone,
what is the best module to create new data of MRI heads using affine transformation, the volumes and their segmentation (binary labelmap) saved in NIFTI format ?

detailed steps are really appreciated…

Thanks

hi - You could try TorchIO. Let us know if how it works for you.

2 Likes

Since affine transformation is so simple, you shouldn’t even need to create new data but augment your input data during learning. If you use monai then you can enable random transformations during training.

@pieper Thanks for that, I’ll try it

Thanks Dr @lassoan, the supervisor of our project is a big fan of MATLAB and actually we have achieved good results so far but we need to improve the accuracy.
So is there any module that can accept an image and its segmentation in NIFTI format and apply augmentation on these images?

thanks again for your help

Yes, you can load images and segmentations in nifti format to Slicer.

2 Likes

Sorry, can you elaborate on this:
(TorchIO returned the error: Traceback (most recent call last):
File “C:/Users/z5049553/AppData/Roaming/NA-MIC/Extensions-29402/TorchIO/lib/Slicer-4.11/qt-scripted-modules/TorchIOTransforms.py”, line 201, in onApplyButton
outputImage = self.currentTransform(inputVolumeNode, outputVolumeNode)
File “C:\Users\z5049553\AppData\Roaming\NA-MIC\Extensions-29402\TorchIO\lib\Slicer-4.11\qt-scripted-modules\TorchIOTransformsLib\Transform.py”, line 119, in call
tensor = torch.from_numpy(data.astype(np.float32)) # why do I need this? Open a TorchIO issue?
AttributeError: ‘Tensor’ object has no attribute ‘astype’

Transform kwargs:
{‘scales’: (0.5, 1.5), ‘degrees’: (0.0, 45.0), ‘translation’: (0.0, 1.0), ‘isotropic’: True, ‘image_interpolation’: ‘linear’, ‘default_pad_value’: ‘minimum’})

I’m not familiar with Torch environment

Maybe you are trying to process a color (or multi-channel grayscale) image. Maybe you saved the image to a consumer file format (jpg, png, tiff, …) and the file writer converted automatically. Convert the image to single-component and see if it works well. If not, then report the error to the extension’s bugtracker.

I exported an image as a volume (.nii) and volume label map as segmentation but doesn’t work.
also I exported the image without segmentation as a single input but still the same error shows up

If @Fernando does not respond here within a few days then I would recommend to submit a question/bug report to Torchio.

1 Like

Hi, @Abdulrahman. Thanks @pieper and @lassoan for pinging me.

The error that you got was fixed yesterday: 'Tensor' object has no attribute 'astype' · Issue #475 · fepegar/torchio · GitHub. It should work in today’s version (latest Preview).

You can use the Slicer TorchIO extension for experimenting with the transforms parameters but, as Andras, I don’t recommend using it for the actual augmentation. I also think it’s better to augment the data during training, instead of beforehand. This means that training will be more computationally expensive, but you’ll need less storage and, more importantly, your data will be more diverse. An example of augmenting your MRI and corresponding segmentation simultaneously:

import torchio as tio
subject = tio.Subject(
    mri=tio.ScalarImage('path_mri.nii.gz'),
    seg=tio.LabelMap('path_seg.nrrd'),
)
transform = tio.RandomAffine()
transformed = transform(subject)

There are many examples in the documentation. If you have any questions, you can use the Discussions tab in the GitHub repository.

I also work very closely with the MONAI team. In my opinion, if you choose to use MONAI, you’ll need to do less coding but have less control over what’s going on.

2 Likes

Hi, @Fernando
Thanks for your reply. I’ve deleted and installed TorchIO again but the same error still existing.
Meanwhile, I’m gonna try to augment during training as you advised.

Thanks again

What version of Slicer are you using (Help → About 3D Slicer)? Can you please install the Preview release and try again?

It is (4.11.20200930 r29402 / 002be18). I’ve installed the latest release and it works.

Thanks

1 Like

Hi, @Fernando
Hoping you’re well,

After almost 3 weeks of hard attempts to deal with MATLAB, I found it is the time to move to PYTHON.

As you suggest using MONAI, Do you recommend any step by step materials for beginners?

My project simply is implement 3D Unet to segment healthy brains. Datasets have been segmented using Slicer and saved in NIFTI format (images + labelmap).

Thanks again :smiley:

Here’s a tutorial to segment brains from healthy subjects using a 3D U-Net: TorchIO tutorial on Google Colab.

If you decide to use MONAI instead, there are some tutorials here.

1 Like

The SimpleITK Notebooks have an example on using SimpleITK to do data augmentation that might be of interest.

2 Likes

Thanks, @dave3d. Although TorchIO’s API is very similar to torchvision's, it relies heavily on SimpleITK for preprocessing and augmentation.

Hi, @Fernando
sorry for keep asking.

Do know any tutorial for 3D multi-class semantic segmentation of brain (classes are CSF, white matter and grey matter)?

Thanks

This discussion is not really related to Slicer. You can post on the TorchIO Discussions tab, if you have more unrelated questions.

I haven’t written any tutorial for multiclass segmentation, but it’s really very similar to the one I shared, so I recommend you take a look.

1 Like