Semantic Segmentation Data Augmentation for both images and Masks

kenneth_maanika: <p>Hi i tried implementing code to get hflip and vflip of both my images and masks … but i keep getting this error … any advice is much appreciated</p>
<p>My masks are numpy arrays where each pixel is represented by the class index<br>
My images are jpg files</p>
<p><span class="hashtag">#Dataset</span> class for BiopsyData (used by the data loader)<br>
class BiopsyDataset(Dataset):</p>
<pre><code>def __init__(self, root_data_dir, ids):
  
    files = []
    files2 = []
    
    #Get all file names
    for file in os.listdir(root_data_dir+'/CroppedImages'):
        if file.endswith('.jpg'):
            files.append( file )
    files = np.sort(files).tolist()
    
    
    #Get all file names masks
    for file in os.listdir(root_data_dir+'/Masks'):
        if file.endswith('.npy'):
            files2.append( file )
    files2 = np.sort(files2).tolist()
    
    
    
    #Create dataset from specified ids
    self.files = [files[i] for i in ids]
    
    self.files2 = [files2[i] for i in ids]
    
    #Transforms
    
    self.to_tensor = torchvision.transforms.ToTensor()
    
    
    self.root_data_dir = root_data_dir   
    

def __len__(self):
    return len(self.files)
  
  
def transform(self, image, mask):
  
  image = TF.to_pil_image(image)
  
  # Random horizontal flipping
  if random.random() &gt; 0.5:
    image = TF.hflip(image)
    mask = np.flip(mask, 1)
    
    
  # Random vertical flipping
  if random.random() &gt; 0.5:
    image = TF.vflip(image)
    mask = np.flip(mask, 0)

    # Transform to tensor
    image = TF.to_tensor(image)
    image = TF.normalize(image, mean, std)
    
    return image, mask

#Returns a single image and label pair
def __getitem__(self, index):
  
  #Read image and labels
  image = Image.open(self.root_data_dir + '/CroppedImages/' + self.files[index])
  
  Mask = np.load(self.root_data_dir + '/Masks/' + self.files2[index])
  
  image = self.to_tensor(image)
  
  image, Mask = self.transform(image, Mask)
  
  #Cross entropy loss needs labels as LongTensor type

  Mask = self.to_tensor(Mask).type(torch.LongTensor)
    
  return image, Mask</code></pre>

kenneth_maanika: <aside class="quote no-group" data-username="kenneth_maanika" data-post="1" data-topic="58379">
<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/kenneth_maanika/48/16033_2.png" class="avatar"> kenneth_maanika:</div>
<blockquote>
<p>c</p>
</blockquote>
</aside>
<p>Error is…</p>
<hr>
<p>ValueError                                Traceback (most recent call last)<br>
 in ()<br>
4<br>
5     <span class="hashtag-raw">#sample</span> from the itterable object<br>
----&gt; 6     image, mask = next(dataloader_it)<br>
7<br>
8     plt.subplot(1,2,1)</p>
<p>5 frames<br>
/usr/local/lib/python3.6/dist-packages/torchvision/transforms/functional.py in to_tensor(pic)<br>
62             pic = pic[:, :, None]<br>
63<br>
—&gt; 64         img = torch.from_numpy(pic.transpose((2, 0, 1)))<br>
65         # backward compatibility<br>
66         if isinstance(img, torch.ByteTensor):</p>
<p>ValueError: some of the strides of a given numpy array are negative. This is currently not supported, but will be added in future releases.</p>

ptrblck: <p><code>np.flip</code> creates negative strides, which are not supported at the moment in PyTorch.<br>
Could you transform the <code>mask</code> to a tensor first and use <code>torch.flip</code>?</p>

kenneth_maanika: <p>Thanks <a class="mention" href="/u/ptrblck">@ptrblck</a></p>
<p>I tried flipping both horizontally and vertically but end up getting the same thing.<br>
Am i doing something wrong here</p>
<p>x = ([[[ 0,  1],[ 2,  3]],[[ 4,  5],[ 6,  7]]])</p>
<p>x = torch.tensor(x)</p>
<p>print(‘Before Fliiping’)<br>
print(x)</p>
<p>a = torch.flip(x, [0, 1])</p>
<p>print(’\n\nAfter Fliiping 1’)<br>
print(a)</p>
<p>b = torch.flip(x, [1, 0])</p>
<p>print(’\n\nAfter Fliiping 2’)<br>
print(b)</p>
<p>-----OUTPUT--------------------------<br>
Before Fliiping<br>
tensor([[[0, 1],<br>
[2, 3]],</p>
<pre><code>    [[4, 5],
     [6, 7]]])
</code></pre>
<p>After Fliiping 1<br>
tensor([[[6, 7],<br>
[4, 5]],</p>
<pre><code>    [[2, 3],
     [0, 1]]])
</code></pre>
<p>After Fliiping 2<br>
tensor([[[6, 7],<br>
[4, 5]],</p>
<pre><code>    [[2, 3],
     [0, 1]]])</code></pre>

ptrblck: <p>Oh sorry, I missed the <code>np.transpose</code> call. Could you first convert to a tensor and then call <code>torch.permute</code> with the same arguments?</p>