# Using data augmentation to generate new data for deep learning

**URL:** https://discourse.slicer.org/t/using-data-augmentation-to-generate-new-data-for-deep-learning/16421
**Category:** Support
**Tags:** segmentation
**Created:** [March 8, 2021, 6:31am UTC](https://discourse.slicer.org/t/using-data-augmentation-to-generate-new-data-for-deep-learning/16421 "2021-03-08T06:31:29Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Abdulrahman](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@Abdulrahman](https://discourse.slicer.org/u/Abdulrahman)
#### Post date: [March 8, 2021, 6:31am UTC](https://discourse.slicer.org/t/using-data-augmentation-to-generate-new-data-for-deep-learning/16421/1 "2021-03-08T06:31:29Z")

</div>

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

---

<div class="post-metadata">

### Author: ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)
#### Post date: [March 8, 2021, 4:15pm UTC](https://discourse.slicer.org/t/using-data-augmentation-to-generate-new-data-for-deep-learning/16421/2 "2021-03-08T16:15:47Z")

</div>

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

> **[fepegar/SlicerTorchIO](https://github.com/fepegar/SlicerTorchIO)**
>
> 3D Slicer module for TorchIO. Contribute to fepegar/SlicerTorchIO development by creating an account on GitHub.

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [March 8, 2021, 4:39pm UTC](https://discourse.slicer.org/t/using-data-augmentation-to-generate-new-data-for-deep-learning/16421/3 "2021-03-08T16:39:34Z")

</div>

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](https://docs.monai.io/en/latest/highlights.html#medical-image-data-i-o-processing-and-augmentation).

---

<div class="post-metadata">

### Author: ![Abdulrahman](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@Abdulrahman](https://discourse.slicer.org/u/Abdulrahman)
#### Post date: [March 8, 2021, 11:10pm UTC](https://discourse.slicer.org/t/using-data-augmentation-to-generate-new-data-for-deep-learning/16421/4 "2021-03-08T23:10:32Z")

</div>

@pieper Thanks for that, I’ll try it

---

<div class="post-metadata">

### Author: ![Abdulrahman](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@Abdulrahman](https://discourse.slicer.org/u/Abdulrahman)
#### Post date: [March 8, 2021, 11:25pm UTC](https://discourse.slicer.org/t/using-data-augmentation-to-generate-new-data-for-deep-learning/16421/5 "2021-03-08T23:25:43Z")

</div>

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

---

<div class="post-metadata">

### Author: ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)
#### Post date: [March 8, 2021, 11:31pm UTC](https://discourse.slicer.org/t/using-data-augmentation-to-generate-new-data-for-deep-learning/16421/6 "2021-03-08T23:31:56Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Abdulrahman](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@Abdulrahman](https://discourse.slicer.org/u/Abdulrahman)
#### Post date: [March 9, 2021, 12:49am UTC](https://discourse.slicer.org/t/using-data-augmentation-to-generate-new-data-for-deep-learning/16421/7 "2021-03-09T00:49:56Z")

</div>

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

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [March 9, 2021, 12:57am UTC](https://discourse.slicer.org/t/using-data-augmentation-to-generate-new-data-for-deep-learning/16421/8 "2021-03-09T00:57:43Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Abdulrahman](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@Abdulrahman](https://discourse.slicer.org/u/Abdulrahman)
#### Post date: [March 9, 2021, 1:06am UTC](https://discourse.slicer.org/t/using-data-augmentation-to-generate-new-data-for-deep-learning/16421/9 "2021-03-09T01:06:09Z")

</div>

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

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [March 9, 2021, 1:54am UTC](https://discourse.slicer.org/t/using-data-augmentation-to-generate-new-data-for-deep-learning/16421/10 "2021-03-09T01:54:59Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Fernando](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fernando/32/5640_2.png) [@Fernando](https://discourse.slicer.org/u/Fernando)
#### Post date: [March 9, 2021, 8:37am UTC](https://discourse.slicer.org/t/using-data-augmentation-to-generate-new-data-for-deep-learning/16421/11 "2021-03-09T08:37:41Z")

</div>

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](https://github.com/fepegar/torchio/issues/475). 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:

```python
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](https://torchio.readthedocs.io/). If you have any questions, you can use the [Discussions](https://github.com/fepegar/torchio/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.

---

<div class="post-metadata">

### Author: ![Abdulrahman](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@Abdulrahman](https://discourse.slicer.org/u/Abdulrahman)
#### Post date: [March 9, 2021, 11:49pm UTC](https://discourse.slicer.org/t/using-data-augmentation-to-generate-new-data-for-deep-learning/16421/12 "2021-03-09T23:49:26Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Fernando](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fernando/32/5640_2.png) [@Fernando](https://discourse.slicer.org/u/Fernando)
#### Post date: [March 10, 2021, 11:12am UTC](https://discourse.slicer.org/t/using-data-augmentation-to-generate-new-data-for-deep-learning/16421/13 "2021-03-10T11:12:45Z")

</div>

What version of Slicer are you using (Help → About 3D Slicer)? Can you please install the [Preview release](https://download.slicer.org/) and try again?

---

<div class="post-metadata">

### Author: ![Abdulrahman](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@Abdulrahman](https://discourse.slicer.org/u/Abdulrahman)
#### Post date: [March 10, 2021, 11:36pm UTC](https://discourse.slicer.org/t/using-data-augmentation-to-generate-new-data-for-deep-learning/16421/14 "2021-03-10T23:36:06Z")

</div>

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

Thanks

---

<div class="post-metadata">

### Author: ![Abdulrahman](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@Abdulrahman](https://discourse.slicer.org/u/Abdulrahman)
#### Post date: [March 29, 2021, 7:02am UTC](https://discourse.slicer.org/t/using-data-augmentation-to-generate-new-data-for-deep-learning/16421/15 "2021-03-29T07:02:15Z")

</div>

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 😃

---

<div class="post-metadata">

### Author: ![Fernando](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fernando/32/5640_2.png) [@Fernando](https://discourse.slicer.org/u/Fernando)
#### Post date: [March 29, 2021, 9:18am UTC](https://discourse.slicer.org/t/using-data-augmentation-to-generate-new-data-for-deep-learning/16421/16 "2021-03-29T09:18:57Z")

</div>

Here’s a tutorial to segment brains from healthy subjects using a 3D U-Net: [TorchIO tutorial on Google Colab](https://github.com/fepegar/torchio/tree/master/examples#general).

If you decide to use MONAI instead, there are some tutorials [here](https://github.com/Project-MONAI/tutorials).

---

<div class="post-metadata">

### Author: ![dave3d](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dave3d/32/5003_2.png) [@dave3d](https://discourse.slicer.org/u/dave3d)
#### Post date: [March 29, 2021, 1:39pm UTC](https://discourse.slicer.org/t/using-data-augmentation-to-generate-new-data-for-deep-learning/16421/17 "2021-03-29T13:39:07Z")

</div>

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

> <https://github.com/InsightSoftwareConsortium/SimpleITK-Notebooks/blob/master/Python/70_Data_Augmentation.ipynb>

---

<div class="post-metadata">

### Author: ![Fernando](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fernando/32/5640_2.png) [@Fernando](https://discourse.slicer.org/u/Fernando)
#### Post date: [March 29, 2021, 1:55pm UTC](https://discourse.slicer.org/t/using-data-augmentation-to-generate-new-data-for-deep-learning/16421/18 "2021-03-29T13:55:53Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Abdulrahman](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@Abdulrahman](https://discourse.slicer.org/u/Abdulrahman)
#### Post date: [March 30, 2021, 6:25am UTC](https://discourse.slicer.org/t/using-data-augmentation-to-generate-new-data-for-deep-learning/16421/19 "2021-03-30T06:25:09Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Fernando](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fernando/32/5640_2.png) [@Fernando](https://discourse.slicer.org/u/Fernando)
#### Post date: [March 30, 2021, 10:25am UTC](https://discourse.slicer.org/t/using-data-augmentation-to-generate-new-data-for-deep-learning/16421/20 "2021-03-30T10:25:04Z")

</div>

This discussion is not really related to Slicer. You can post on the TorchIO [Discussions](https://github.com/fepegar/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.
