How shift an image vertically or horizontally?

If we want to shift an image in tensorflow we can do that using:

datagen = ImageDataGenerator(width_shift_range = width_shift_val,
height_shift_range= height_shift_val)

Is there a way to do the same in pytorch?

Using transform, we can do a RandomHorizontalFlip, but I want to shift an image either vertically or horizontally in Pytorch?

You can use imgaug with Pytorch and use its Fliplr and Flipud.
For a sample check this out

Its good, but it has the feature to flip the image not to shift the image like keras

Then use TranslateX and TranslateY.
imgaug is a decent library and it has all you need. just make sure to check the documentation.

You should be able to do this using torchvision.transforms.RandomAffine. You can set the degrees to 0 and set the translate parameter to what you need.
https://pytorch.org/vision/stable/transforms.html#torchvision.transforms.RandomAffine

# An example. Note that translate refers to the fraction of the length of that dimension to translate by.
shift = transforms.RandomAffine(degrees = 0, translate = (0.2, 0.2))
4 Likes

Thank you for the solution

1 Like