dfdx icon indicating copy to clipboard operation
dfdx copied to clipboard

Rotate and Mirror Operations

Open Cifram opened this issue 3 years ago • 2 comments

For data augmentation, it's very useful to have operations to mirror and rotate a tensor. See PyTorch's flip and rot90 methods.

Cifram avatar Mar 18 '23 23:03 Cifram

Note that this isn't super urgent, as I've figured out I can achieve all combinations of flips and rotates with gathers and permutes.

Specifically, this will mirror the first dimension:

let flip_gather: Tensor<Rank1<Dim0>, usize, _, _> = dev.tensor((0..Dim0).rev().collect::<Vec<usize>>());
let tensor = tensor.gather(flip_gather);

And this will rotate 90 degrees and mirror the second dimension:

let tensor = tensor.permute::<_, Axes2<1, 0>>()

And if you alternate between these two operations, you will cycle through all 8 possible combinations of rotates and mirrors on the first two axes. And you can use permutes to make sure the relevant axes are the first two, and then permute back afterwards.

But in the long run, I think having explicit mirror and rotate operations would be much more intuitive.

Cifram avatar Mar 19 '23 09:03 Cifram

Ooh nice work arounds! rotate is super useful for augmentations in image classification so definitely want to add these

chelsea0x3b avatar Mar 21 '23 14:03 chelsea0x3b