Cannot apply randomaffine to mask

username: NearsightedCV
cooked: <p>I am trying to augment my set with horizontal flips and random affines for a semantic segmentation task. The image seems to run fine but the mask fails.</p>
<p>How do I fix? I have tried setting fill=None and I still get the same error.</p>
<p><em><strong>my code</strong></em></p>
<pre><code class="lang-auto">if random.random() &gt; 0.5:
            random_affine = transforms.RandomAffine(
                degrees=5,translate=(0.1, 0.3),shear=0.1,fill=None )
            image = random_affine(image)
            mask = random_affine(mask)
</code></pre>
<p>The Error</p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_23244/1472552485.py in &lt;module&gt;
    116 
    117 if __name__ == '__main__':
--&gt; 118     main()
    119 

~\AppData\Local\Temp/ipykernel_23244/1472552485.py in main()
     62     print(f'Number of Training Samples: {len(train_loader)}')
     63 # Check Tensor shapes ======================================================
---&gt; 64     batch = next(iter(train_loader))
     65     images, labels = batch
     66 

~\anaconda3\envs\Pytorch\lib\site-packages\torch\utils\data\dataloader.py in __next__(self)
    519             if self._sampler_iter is None:
    520                 self._reset()
--&gt; 521             data = self._next_data()
    522             self._num_yielded += 1
    523             if self._dataset_kind == _DatasetKind.Iterable and \

~\anaconda3\envs\Pytorch\lib\site-packages\torch\utils\data\dataloader.py in _next_data(self)
    559     def _next_data(self):
    560         index = self._next_index()  # may raise StopIteration
--&gt; 561         data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
    562         if self._pin_memory:
    563             data = _utils.pin_memory.pin_memory(data)

~\anaconda3\envs\Pytorch\lib\site-packages\torch\utils\data\_utils\fetch.py in fetch(self, possibly_batched_index)
     47     def fetch(self, possibly_batched_index):
     48         if self.auto_collation:
---&gt; 49             data = [self.dataset[idx] for idx in possibly_batched_index]
     50         else:
     51             data = self.dataset[possibly_batched_index]

~\anaconda3\envs\Pytorch\lib\site-packages\torch\utils\data\_utils\fetch.py in &lt;listcomp&gt;(.0)
     47     def fetch(self, possibly_batched_index):
     48         if self.auto_collation:
---&gt; 49             data = [self.dataset[idx] for idx in possibly_batched_index]
     50         else:
     51             data = self.dataset[possibly_batched_index]

~\AppData\Local\Temp/ipykernel_23244/3743379561.py in __getitem__(self, index)
     77         image = Image.open(img_path).convert('RGB')
     78         mask = Image.open(mask_path)#.convert('L')
---&gt; 79         x, y = self.transform(image, mask)
     80         return x, y
     81 

~\AppData\Local\Temp/ipykernel_23244/3743379561.py in transform(self, image, mask)
     68                 degrees=5,translate=(0.1, 0.3),shear=0.1,fill=None )
     69             image = random_affine(image)
---&gt; 70             mask = random_affine(mask)
     71         return image, mask
     72 

~\anaconda3\envs\Pytorch\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
   1100         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1101                 or _global_forward_hooks or _global_forward_pre_hooks):
-&gt; 1102             return forward_call(*input, **kwargs)
   1103         # Do not call functions when jit is used
   1104         full_backward_hooks, non_full_backward_hooks = [], []

~\anaconda3\envs\Pytorch\lib\site-packages\torchvision\transforms\transforms.py in forward(self, img)
   1464         ret = self.get_params(self.degrees, self.translate, self.scale, self.shear, img_size)
   1465 
-&gt; 1466         return F.affine(img, *ret, interpolation=self.interpolation, fill=fill)
   1467 
   1468     def __repr__(self):

~\anaconda3\envs\Pytorch\lib\site-packages\torchvision\transforms\functional.py in affine(img, angle, translate, scale, shear, interpolation, fill, resample, fillcolor)
   1123     translate_f = [1.0 * t for t in translate]
   1124     matrix = _get_inverse_affine_matrix([0.0, 0.0], angle, translate_f, scale, shear)
-&gt; 1125     return F_t.affine(img, matrix=matrix, interpolation=interpolation.value, fill=fill)
   1126 
   1127 

~\anaconda3\envs\Pytorch\lib\site-packages\torchvision\transforms\functional_tensor.py in affine(img, matrix, interpolation, fill)
    696     # grid will be generated on the same device as theta and img
    697     grid = _gen_affine_grid(theta, w=shape[-1], h=shape[-2], ow=shape[-1], oh=shape[-2])
--&gt; 698     return _apply_grid_transform(img, grid, interpolation, fill=fill)
    699 
    700 

~\anaconda3\envs\Pytorch\lib\site-packages\torchvision\transforms\functional_tensor.py in _apply_grid_transform(img, grid, mode, fill)
    642     # Append a dummy mask for customized fill colors, should be faster than grid_sample() twice
    643     if fill is not None:
--&gt; 644         dummy = torch.ones((img.shape[0], 1, img.shape[2], img.shape[3]), dtype=img.dtype, device=img.device)
    645         img = torch.cat((img, dummy), dim=1)
    646 

IndexError: tuple index out of range
</code></pre>