Data Augmentation after creating Dataset

username: Bhavya_Soni
cooked: <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>Here , I’ve used previously saved tensor data of images and loaded it in train_dataset,test_dataset …</p>
<p>I want to augment directly the train_dataset . can anyone tell me how to do that?</p>

username: ariG23498
cooked: <p>You can create a <code>Compose</code> of augmentations and then use it in the training loop itslelf.</p>
<pre><code class="lang-python">aug = Compose(&lt;the list of augmentations&gt;)

for x,y in dataloader:
    x_aug = aug(x)
</code></pre>
<p>I think this might do the trick.</p>

username: Bhavya_Soni
cooked: <p>But it will overwrite x_aug everytime , at the end of loop only last batch will be augmented , I guess.</p>

username: Bhavya_Soni
cooked: <pre><code class="lang-auto">aug = transforms.Compose([transforms.RandomHorizontalFlip(1),
               transforms.RandomAffine(20)])
temp_list = []
for x,y in train_loader:
    temp_list.append(aug(x))
final_tensor = torch.cat(temp_list)
</code></pre>
<p>This way we can do that , is there ant efficient way? without using list .<br>
Thank you for your suggestion.</p>

username: ariG23498
cooked: <p>Hey <a class="mention" href="/u/bhavya_soni">@Bhavya_Soni</a><br>
Why would you concatenate the augmented tensors?<br>
It would be efficient to augment and send the augmented tensors through a model and learn the weights.</p>
<p>The only problem that I see here is that the <code>Random_</code> augmentation will augment each image in a batch the same way, which is not an ideal setup.</p>

username: ejguan
cooked: <p>First, you can set <code>batch_size</code> for the DataLoader to load multiple data into a batch and do <code>transform</code> or <code>augmentation</code> for each batch.<br>
Secondly, I am not sure why you have this <code>tmp_list</code>. You can directly use the augmented data to train your model.</p>
<pre><code class="lang-auto">for x, y in train_loader:
    aug_x = aug(x)
    ...
    output = model(aug_x)
    loss = ...
    loss.backward()
    ...
``</code></pre>

username: Bhavya_Soni
cooked: <p>Oh yes. I did augmentation first, of whole dataset randomly and then started training , I get this now . Thank you very much.</p>

username: Bhavya_Soni
cooked: <p>Yes sir, I got it now , thank you very much.</p>
<p>I’m beginner <img src="https://discuss.pytorch.org/images/emoji/apple/slightly_smiling_face.png?v=9" title=":slightly_smiling_face:" class="emoji" alt=":slightly_smiling_face:"></p>

username: saeid_rasouli
cooked: <p>For anyone who faced this problem, this is the solution: <a href="https://stackoverflow.com/questions/75010445/pytorch-apply-different-transform-to-dataset-after-creation/75010446#75010446" rel="noopener nofollow ugc">Pytorch, apply different transform to dataset after creation</a></p>

username: karar
cooked: <p>Thank you for the helpful link: <a href="https://stackoverflow.com/questions/75010445/pytorch-apply-different-transform-to-dataset-after-creation/75010446#75010446" rel="noopener nofollow ugc">Pytorch, apply different transform to dataset after </a><a href="https://www.xn--mlltonnenbox24-gsb.de/" rel="noopener nofollow ugc">creation</a></p>