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

sleepyo: <p>Hi, I am currently learning PyTorch. Following some tutorials, I have learned to do some little augmentation using PyTorch and albumentation.<br>
But somehow the augmentation that I’ve done only augments each image once.<br>
If I want to randomly augment to add two more data for each image (maybe rotate then crop randomly), How to do that?<br>
Here’s my code.<br>
My input data is already in npy form with shape [no_of_images,height,width]</p>
<p>Thanks!</p>
<pre><code class="lang-auto">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
</code></pre>

sleepyo: <p>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.</p>

ptrblck: <p>Based on your code each image will be randomly augmented in the epoch.<br>
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 <code>self.aug</code> multiple times and return all augmented images.</p>

sleepyo: <p>Thank you,</p>
<p>I managed to do it like this just as you said by multiplying the <code>len</code> and then do another augmentation.</p>

monster: <p>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.</p>
<p>Thanks in advance</p>

ptrblck: <p>That’s a tricky use case, as your training data doesn’t fit the expected test data domain.<br>
I assume that at least your test set contains the expected images taken from different distances etc.<br>
Otherwise your training would be “blind” and you would only get the real performance after deploying the model.</p>
<p>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.</p>

Bhavya_Soni: <p>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 .</p>
<pre><code class="lang-auto">test_loader = data['test_loader']
train_loader = data['train_loader']
train_dataset = data['train_dataset']
test_dataset = data['test_dataset']
</code></pre>
<p>I want to augment directly the train_dataset . can anyone tell me how to do that? <a class="mention" href="/u/ptrblck">@ptrblck</a> sir can you help me here?</p>