How to do image data augmentation with k-fold cross validation

username: user_xyz
cooked: <p>Hello everyone, I was wondering if there was a solution on doing image data augmentation on just the training data. I have the k-fold setup</p>
<pre><code class="lang-auto">for fold,(train_idx,test_idx) in enumerate(kfold.split(dataset)):
  print('------------fold no---------{}----------------------'.format(fold))
  train_subsampler = torch.utils.data.SubsetRandomSampler(train_idx)
  test_subsampler = torch.utils.data.SubsetRandomSampler(test_idx)
 
  trainloader = torch.utils.data.DataLoader(
                      dataset, 
                      batch_size=batch_size, sampler=train_subsampler)
  testloader = torch.utils.data.DataLoader(
                      dataset,
                      batch_size=batch_size, sampler=test_subsampler)
 
  model.apply(reset_weights)
 
  for epoch in range(1, epochs + 1):
    train(fold, model, device, trainloader, optimizer, epoch)
    test(fold,model, device, testloader)
</code></pre>
<p>But during those folds, I am unable to find a way to augment just the training data just before the trainloader.</p>
<p>I see that other posts from years ago has not been answered. Has there been any update to this?</p>
<aside class="quote quote-modified" data-post="1" data-topic="128990">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/gopi0941/48/41038_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/how-to-augment-train-data-during-k-fold-cross-validation/128990">How to augment train data during k-Fold cross validation</a> <a class="badge-category__wrapper " href="/c/vision/5"><span data-category-id="5" data-drop-close="true" class="badge-category " title="Topics related to either pytorch/vision or vision research related topics"><span class="badge-category__name">vision</span></span></a>
  </div>
  <blockquote>
    I am trying to use data augmentation for each of the epoch on train set, but I also need the filenames of testloader for later. 
So, I used a custom ImageFolderWithPaths to generate tuple for image, label, path. 
But when combined with a wrapper dataset to build using augmentation have some issues. 
import os
import torch
from torch import nn
from torch.utils.data import DataLoader, ConcatDataset
from torchvision import transforms
import torchvision
from sklearn.model_selection import KFold
from…
  </blockquote>
</aside>

<aside class="quote" data-post="1" data-topic="127652">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sguerin/48/36682_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/correct-data-augmentation-with-k-fold-cross-validation/127652">Correct data augmentation with k-fold cross validation</a> <a class="badge-category__wrapper " href="/c/vision/5"><span data-category-id="5" data-drop-close="true" class="badge-category " title="Topics related to either pytorch/vision or vision research related topics"><span class="badge-category__name">vision</span></span></a>
  </div>
  <blockquote>
    Hi! 
I’m performing 10 k-fold cross validation on my neural network model. I’ve done the splits (k=10: 90% train, 10% validation), but It’s not clear for me if I should apply transforms (random horizontal flip, random rotation, etc) only on training set or both training and validation. What is the correct way to do it? 
Thanks!
  </blockquote>
</aside>

<p>Thank you!</p>

username: ptrblck
cooked: <p><a href="https://discuss.pytorch.org/t/split-dataset-into-training-and-validation-without-applying-training-transform/115429/2">This post</a> might be helpful.</p>