Rotated MNIST for regression

Hi everyone,

I’d like to create a (random) rotated version of MNIST for regression, so that the target is the angle. Any ideas?

I have tried subclassing torchvision.datasets.MNIST and overriding getitem, but a new rotation would be applied every time I access the data.

Your approach of deriving from MNIST and applying the rotation in the __getitem__ method seems to be valid.
You could store the angle values in a list and apply the corresponding angle in __getitem__.
This would make sure that a particular sample for an index gets the same rotation using the index.

Thanks for your reply.

Yeah, storing the angles in a list would do the trick. I’m afraid this solution (overriding __getitem__) is not efficient though.

Do you have ideas of how I could rotate the input and save the corresponding target beforehand?
So that I could combine this pre-processing with other transforms.

For context, this is my current piece of code:

class RotatedMNIST(MNIST):
  def __getitem__(self, i):
    input, target = super(RotatedMNIST, self).__getitem__(i)
    target = random.normalvariate(0, 3.14/4)
    input = TF.rotate(input, target * (180 / 3.14))
    input = transforms.ToTensor()(input)
    return input, target

trainloader = DataLoader(RotatedMNIST('./data', train=True, download=True), batch_size=batch_size, shuffle=True)

Do you see a significant slowdown using this approach or why do you think it’s not efficient?
You could of course rotate all images beforehand and store these rotated tensors.
Then in your custom Dataset, you could load all tensors and preprocess them with the other augmentation methods.
I’m not sure how much time this would save.