Albumentations in Pytorch: Inconsistent Augmentation for multi-target datasets

Ruffybeo: <p>I’m using Pytorch and want to perform the data augmentation of my images with Albumentations. My dataset object has two different targets: ‘blurry’ and ‘sharp’. Each instance of both targets needs to have identical changes. When I try to perform the data augmentation with a Dataset object like this:</p>
<blockquote>
<p>class ApplyTransform(Dataset):<br>
def <strong>init</strong>(self, dataset, transformation):<br>
self.dataset = dataset<br>
self.aug = transformation</p>
<p>def <strong>len</strong>(self):<br>
return (len(self.dataset))</p>
<p>def <strong>getitem</strong>(self, idx):<br>
sample, target = self.dataset[idx][‘blurry’], self.dataset[idx][‘sharp’]<br>
transformedImgs = self.aug(image=sample, target_image=target)<br>
sample_aug, target_aug = transformedImgs[“image”], transformedImgs[“target_image”]<br>
return {‘blurry’: sample_aug, ‘sharp’: target_aug}</p>
</blockquote>
<p>Unfortunately, I receive two images with two different augmentations:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/d/5/d553a805e82542c5389db84cc0e6126ef8bd4028.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/d553a805e82542c5389db84cc0e6126ef8bd4028" title="Download"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/d/5/d553a805e82542c5389db84cc0e6126ef8bd4028_2_497x500.jpeg" alt="Download" data-base62-sha1="uraVUDgFY6jHMtyHusVsBl7zITK" width="497" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/d/5/d553a805e82542c5389db84cc0e6126ef8bd4028_2_497x500.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/d/5/d553a805e82542c5389db84cc0e6126ef8bd4028_2_745x750.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/d/5/d553a805e82542c5389db84cc0e6126ef8bd4028.jpeg 2x" data-dominant-color="868889"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Download</span><span class="informations">841×845 229 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>When I try the same without a Dataset object, I receive two images with the identical application of augmentations. Does anybody know how to make it work with a dataset object?</p>
<p>Here is my augmentation pipeline:</p>
<blockquote>
<p>augmentation_transform = A.Compose(<br>
[<br>
A.Resize(1024,1024, p=1),<br>
A.HorizontalFlip(p=0.25),<br>
A.Rotate(limit=(-45, 65)),<br>
A.VerticalFlip(p=0.24),<br>
A.RandomContrast(limit=0.3, p=0.15),<br>
A.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),<br>
A.pytorch.transforms.ToTensorV2(always_apply=True, p=1.0)<br>
],<br>
additional_targets={“target_image”: “image”}<br>
)</p>
</blockquote>

eqy: <p>I think a simple approach here, albeit more tedious, is to write your own transformation function directly via the functional versions of the transforms e.g.,</p>
<pre><code class="lang-auto">def buildmytransform(hflip_prob, vflip_prob, ...):
    def _transform(image):
        if torch.rand(1) &lt; hflip_prob:
            blur_tmp = albumentations.augmentations.functional.hflip(image)
            sharp_tmp = albumentations.augmentations.functional.hflip(image)
        blur_tmp = albumentations.augmentations.functional.rotate(blur_tmp, ...)
        sharp_tmp = albumentations.augmentations.functional.rotate(sharp_tmp, ...)
        if torch.rand(1) &lt; vflip_prob:
        ...
        return blur_tmp, sharp_tmp
    return _transform
</code></pre>