How to augment more than once on each image (extra data)

Hi, I am currently learning PyTorch. Following some tutorials, I have learned to do some little augmentation using PyTorch and albumentation.
But somehow the augmentation that I’ve done only augments each image once.
If I want to randomly augment to add two more data for each image (maybe rotate then crop randomly), How to do that?
Here’s my code.
My input data is already in npy form with shape [no_of_images,height,width]

Thanks!

class DataGenerator(Dataset):
    def __init__(self, x_set, y_set):
        self.x, self.y = x_set, y_set
        self.aug = A.Compose([
            A.Resize(p=1, height=512, width=256) 
        ])        

    def __len__(self):
        return len(self.x)
    
    def __getitem__(self, index):
        batch_x = self.x[index]
        batch_y = self.y[index]
        # print(batch_x.shape)
        # batch_x = batch_x[None, :, :]
        augmented = self.aug(image=batch_x, mask=batch_y)
        image, mask = augmented['image'], augmented['mask']
        #return np.expand_dims(batch_x,axis=0), batch_y
        return image[None,:,:], mask

just for clarification what I’m asking is not the code to augment, but to do augment we want more than once on each image.

Based on your code each image will be randomly augmented in the epoch.
If you want to return the same image using different augmentations (and thus increase the number of samples in the epoch) you could just call self.aug multiple times and return all augmented images.

Thank you,

I managed to do it like this just as you said by multiplying the len and then do another augmentation.

Hi… My question is quite different but related to augmentation, I am working on a project in which I have to detect some products from different distance ,angle , lighting etc. but for training dataset I have images from same distance so how can I robust my model so that it can detect products from different distances, kindly let me know which augmentation I should try for this use case.

Thanks in advance

That’s a tricky use case, as your training data doesn’t fit the expected test data domain.
I assume that at least your test set contains the expected images taken from different distances etc.
Otherwise your training would be “blind” and you would only get the real performance after deploying the model.

That being said, you could certainly try to use an aggressive data augmentation and hope to capture the expected transformations already during the training. Based on your description you could try to use resizing, rotation, and generally a combination of affine transformations.

Hello there , I’m new to PyTorch, I’ve created a dataset that is having x-ray images and it is transformed but after creating the dataset I’m not getting good test accuracy so i have decided to do augmentation but I don’t know how to do augmentation on already created dataset .

test_loader = data['test_loader']
train_loader = data['train_loader']
train_dataset = data['train_dataset']
test_dataset = data['test_dataset']

I want to augment directly the train_dataset . can anyone tell me how to do that? @ptrblck sir can you help me here?