Rotate an image using torchvision.transform for a tensor?

I found that torchvision.transform only works for PIL image type of objects. However, I want to apply random rotations on torch tensor and I want to get the gradient of the input images, so I can’t convert it to PIL first and then apply the transform.

kornia might provide, what you are looking for.
CC @edgarriba


I want to pass an image into a network and perform gradient update on the image. But I also want to add random perturbation on the image such as rotations and jittering, but not the PIL type of image but on the tensor. However, all torchvision transformations of these types are on PIL type image not on tensor.

1 Like

I haven’t used kornia that much, but it should work on tensors as seen here.

1 Like

I would recommend searching for NumPy solutions to issues like these, as they are extremely easy to recreate in PyTorch. PyTorch has many of the same functions as NumPy, and you can also use indexing (which is the exact same in both libraries).

It looks like you could generate a random tensor, and then add it to the tensor you want to add jitter. If you want more of a random shifting type jitter, I wrote code for that a few days ago here: How to split a (C x H x W) tensor into tiles?

For the rotations, you could probably also use Torch.flip(), and Torch.diag() if the rotations are using 90 degree intervals.

1 Like

Thanks! That is also what I did!

take a look at the augmentation api we are developing. It’s all tensor based and tries to match torchvision interface:

More to come: