Albumentations for image augmentation

PytorchLearning: <p>Hi all,</p>
<p>I would like to use albumentations for image augmentation. I would like to transform from “transforms.Compose” to “A.Compose” but I don’t know how to do it for this simple example bellow.</p>
<p>Can someone please show me with this simple example bellow how to use albumentations.</p>
<p>Thank you for your help.</p>
<p>t_transforms = transforms.Compose([transforms.Grayscale(num_output_channels = 1),<br>
transforms.Resize((224, 224)),<br>
transforms.RandomAffine(degrees = 50, translate = (0.3, 0.3), scale = (0.45, 1), shear = (-45, 45, -45, 45))<br>
transforms.RandomCrop(224),<br>
transforms.RandomHorizontalFlip(),	<br>
transforms.ToTensor(),<br>
transforms.Normalize((MEAN,), (STANDARD_DEVIATION,))<br>
])</p>

ksz: <p>Here you can find example: <a href="https://albumentations.ai/docs/examples/pytorch_classification/" class="inline-onebox" rel="noopener nofollow ugc">PyTorch and Albumentations for image classification - Albumentations Documentation</a></p>
<p>The fragment of code:</p>
<pre><code class="lang-auto">train_transform = A.Compose(
    [
        A.SmallestMaxSize(max_size=160),
        A.ShiftScaleRotate(shift_limit=0.05, scale_limit=0.05, rotate_limit=15, p=0.5),
        A.RandomCrop(height=128, width=128),
        A.RGBShift(r_shift_limit=15, g_shift_limit=15, b_shift_limit=15, p=0.5),
        A.RandomBrightnessContrast(p=0.5),
        A.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
        ToTensorV2(),
    ]
)
train_dataset = CatsVsDogsDataset(images_filepaths=train_images_filepaths, transform=train_transform)
</code></pre>

PytorchLearning: <p>Thanks a lot <a class="mention" href="/u/ksz">@ksz</a> for your answer and your precious help.</p>
<p>I have three questions:</p>
<ol>
<li>
<p>Is there an instruction in albumentations that looks like “transforms.Grayscale(num_output_channels = 1)”</p>
</li>
<li>
<p>Is it interesting to use albumentations instead of transforms proposed by pytorch?</p>
</li>
<li>
<p>Is it possible to use at the same time albumentations  and transforms or only one should be used?</p>
</li>
</ol>

ksz: <p>To be honest I never used Albumentations.</p>
<blockquote>
<ol>
<li>Is there an instruction in albumentations that looks like “transforms.Grayscale(num_output_channels = 1)</li>
</ol>
</blockquote>
<p><code>ToGray(always_apply=False, p=1.0)</code>. Here is a demo with code: <a href="https://albumentations-demo.herokuapp.com/" rel="noopener nofollow ugc">https://albumentations-demo.herokuapp.com/</a></p>
<blockquote>
<ol start="2">
<li>Is it interesting to use albumentations instead of transforms proposed by pytorch?</li>
</ol>
</blockquote>
<p>In my opinion it depends on the problem. In classic approaches usually not, but if you need extreme augmentation that this library can help.</p>
<blockquote>
<p>3 .Is it possible to use at the same time albumentations and transforms or only one should be used?</p>
</blockquote>
<p>I don’t know, but probably you can use both, one after another.  But why do you need to combine them?</p>

PytorchLearning: <p><a class="mention" href="/u/ksz">@ksz</a> Thanks a lot for your precious help.</p>
<p>I try every day to learn more and more about pytorch, and recently I have discovered albumentations. From what I have read it contains some advantages when we want to do image augmentation for image segmentation for example, if I am not mistaken, as described here:</p>
<aside class="onebox allowlistedgeneric">
  <header class="source">
      <img src="https://albumentations.ai/docs/getting_started/mask_augmentation/../../images/logo.png" class="site-icon" width="190" height="182">
      <a href="https://albumentations.ai/docs/getting_started/mask_augmentation/" target="_blank" rel="noopener nofollow ugc">albumentations.ai</a>
  </header>
  <article class="onebox-body">
    <div class="aspect-image" style="--aspect-ratio:690/309;"><img src="https://albumentations.ai/docs/images/albumentations_card.png" class="thumbnail" width="690" height="309"></div>

<h3><a href="https://albumentations.ai/docs/getting_started/mask_augmentation/" target="_blank" rel="noopener nofollow ugc">Albumentations Documentation - Mask augmentation for segmentation</a></h3>

<p>Albumentations: fast and flexible image augmentations</p>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>

<p>I want to combine albumentations and transforms because I wanted to know if it was possible and whether there are any benefits to combining them.</p>
<p>Once again thanks a lot for your answers.</p>