Semantic Segmentation Data Augmentation for both images and Masks

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

My masks are numpy arrays where each pixel is represented by the class index
My images are jpg files

#Dataset class for BiopsyData (used by the data loader)
class BiopsyDataset(Dataset):

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() > 0.5:
    image = TF.hflip(image)
    mask = np.flip(mask, 1)
    
    
  # Random vertical flipping
  if random.random() > 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

Error is…


ValueError Traceback (most recent call last)
in ()
4
5 #sample from the itterable object
----> 6 image, mask = next(dataloader_it)
7
8 plt.subplot(1,2,1)

5 frames
/usr/local/lib/python3.6/dist-packages/torchvision/transforms/functional.py in to_tensor(pic)
62 pic = pic[:, :, None]
63
—> 64 img = torch.from_numpy(pic.transpose((2, 0, 1)))
65 # backward compatibility
66 if isinstance(img, torch.ByteTensor):

ValueError: some of the strides of a given numpy array are negative. This is currently not supported, but will be added in future releases.

np.flip creates negative strides, which are not supported at the moment in PyTorch.
Could you transform the mask to a tensor first and use torch.flip?

1 Like

Thanks @ptrblck

I tried flipping both horizontally and vertically but end up getting the same thing.
Am i doing something wrong here

x = ([[[ 0, 1],[ 2, 3]],[[ 4, 5],[ 6, 7]]])

x = torch.tensor(x)

print(‘Before Fliiping’)
print(x)

a = torch.flip(x, [0, 1])

print(’\n\nAfter Fliiping 1’)
print(a)

b = torch.flip(x, [1, 0])

print(’\n\nAfter Fliiping 2’)
print(b)

-----OUTPUT--------------------------
Before Fliiping
tensor([[[0, 1],
[2, 3]],

    [[4, 5],
     [6, 7]]])

After Fliiping 1
tensor([[[6, 7],
[4, 5]],

    [[2, 3],
     [0, 1]]])

After Fliiping 2
tensor([[[6, 7],
[4, 5]],

    [[2, 3],
     [0, 1]]])

Oh sorry, I missed the np.transpose call. Could you first convert to a tensor and then call torch.permute with the same arguments?

1 Like