Image data augmentation in pytorch

cvprogrammer: <p>Hello Everyone,</p>
<p>How does data augmentation work on images in pytorch? i,e How does it work internally? For example. If my dataset has 8 images and i compose a transform as below</p>
<p>transforms.Compose([<br>
transforms.ToPILImage(),<br>
transforms.Resize((128,128)),<br>
transforms.RandomVerticalFlip(1),<br>
transforms.RandomHorizontalFlip(1),<br>
transforms.ColorJitter(brightness=(0.5,1.5),contrast=(1),saturation=(0.5,1.5),hue(-0.1,0.1))<br>
transforms.ToTensor()<br>
])</p>
<p>Will the pytorch dataloader have access to 8*4=32 number of images? If so, How to display(or save) these 32 images? Can we access these 32 images?</p>
<p>How to apply augmentation to image segmentation dataset? In segmentation, we use both image and mask. In some cases we dont want to apply augmentation to mask(eg. transforms.ColorJitter). If we pass both image and mask simultaneously to the pytorch augmentation function then augmentation will be applied to both image and mask. If we apply separately, then in case of random augmentations like transforms.RandomVerticalFlip(.5), the random augmentation may be applied to image and may not be applied to the mask. How to deal with this kind of situation?</p>
<p>Thank you.</p>

ptrblck: <aside class="quote no-group" data-username="cvprogrammer" data-post="1" data-topic="188307">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/cvprogrammer/48/41883_2.png" class="avatar"> cvprogrammer:</div>
<blockquote>
<p>Will the pytorch dataloader have access to 8*4=32 number of images?</p>
</blockquote>
</aside>
<p>The <code>DataLoader</code> doesn’t have access to the transformation in the standard use case as the samples are loaded and transformed in the <code>Dataset</code> separately.<br>
Here is a small example:</p>
<pre data-code-wrap="python"><code class="lang-python">transform = transforms.Compose([
    transforms.ToPILImage(),
    transforms.Resize((128,128)),
    transforms.RandomVerticalFlip(1),
    transforms.RandomHorizontalFlip(1),
    transforms.ColorJitter(brightness=(0.5,1.5),contrast=(1),saturation=(0.5,1.5),hue=(-0.1,0.1)),
    transforms.ToTensor(),
])

x = torch.randn(3, 224, 224)
out = transform(x)
</code></pre>
<aside class="quote no-group" data-username="cvprogrammer" data-post="1" data-topic="188307">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/cvprogrammer/48/41883_2.png" class="avatar"> cvprogrammer:</div>
<blockquote>
<p>How to apply augmentation to image segmentation dataset?</p>
</blockquote>
</aside>
<p>You can either use the functional API as described <a href="https://discuss.pytorch.org/t/torchvision-transfors-how-to-perform-identical-transform-on-both-image-and-target/10606/7">here</a>, <code>torchvision.transforms.v2</code> which allows to pass multiple objects as described <a href="https://pytorch.org/blog/extending-torchvisions-transforms-to-object-detection-segmentation-and-video-tasks/#the-new-transforms-api">here</a>, or any other library mentioned in the first link.</p>