Thank you for your reply. However correct me if I’m wrong but this code will permute all the pixels within the same image. This means that pixels in the corner of the image which are typically always dark could be swapped for pixels in the middle which have much more variation. What I am trying to achieve is for instance to randomly replace the top left corner pixel of image 1 with the top left corner pixel of another image within the same batch and so on for all pixels so as to preserve the distribution of each pixel but remove the dependencies between pixels of the same image.
The best implementation I could come up with is below but I am wondering if there is a more computationally efficient solution which avoids iterating over every pixel.
from torchvision import datasets, transforms
import matplotlib.pyplot as plt
import torch

dataset = datasets.MNIST(root='./data',
                                 download=True,
                                 transform=transforms.ToTensor())
n = 10
data = torch.stack([dataset[i][0] for i in range(n)], dim=0)
target = torch.stack([dataset[i][1] for i in range(n)], dim=0)

# Show first sample
plt.figure()
plt.imshow(data[0][0])

# Permute the pixels
data_pixelshuffled = torch.stack([x[torch.randperm(n)] for x in data.view(n,-1).t()],
                   dim=0).t().view(-1,28,28)

# Show after permutation
plt.figure()
plt.imshow(data_pixelshuffled[0])

plt.show()