Random horizontal translation

I want to perform data augmentation to my data specificaly I want to perform random horizontal and vertical translation
this is the class that I implemented

class HorizontalTranslation(object):
def --init–(self, max_translation=10):
self.max_translation = max_translation
def --call–(self, img):
# Randomly choose a horizontal translation value
translation = random.randint(-self.max_translation, self.max_translation
# Translate the image horizontally
img = img.transform(img.size, Image.AFFINE, (1, 0, translation, 0, 1, 0)
return img
def randomize_parameters(self):

that will be called in a main function
i didn’t get the result I wanted
is that the right way to implement random translation ?

I assume you are using PIL’s transformation? If so, it sounds like a valid approach. Could you describe why this approach doesn’t match your expected results?

at first i was only using center cropping and horizontall flip but my model overfit with 0.98 training accuracy and 0.45 vlidation accuracy after adding horizontal translation the accuracy values dropped for both training and validation (got 0.7 for training and 0.1 for validation )
this is the script that calls the functions in the main

def get_train_utils(opt, model_parameters):
spatial_transform =
if not opt.no_hflip:
spatial_transform = Compose(spatial_transform)

and after that it will be called by the dataloader
I don’t think I am calling it wrong !! @ptrblck

Did you check the transformed samples and compared them to an expected result?
Depending on your use case the augmentation might be too aggressive.