Is there any built-in way in PyTorch to augment the dataset?

username: ShaidaMuhammad
cooked: <p>I have a training dataset of 1000 images and 5 classes, which is indeed very fewer for a deep learning model. Is there any built-in way in PyTorch to augment this dataset? i.e. cropping the images randomly <strong>or</strong> changing their orientation <strong>or</strong> doing some other transformations to those training images and then add them to dataset to increase dataset size?</p>

username: KFrank
cooked: <p>Hi Shaida!</p>
<aside class="quote no-group" data-username="ShaidaMuhammad" data-post="1" data-topic="74256" data-full="true">
<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/shaidamuhammad/48/21928_2.png" class="avatar"> ShaidaMuhammad:</div>
<blockquote>
<p>Is there any built-in way in PyTorch to augment this dataset? i.e. cropping the images randomly <strong>or</strong> changing their orientation <strong>or</strong> doing some other transformations to those training images</p>
</blockquote>
</aside>
<p>Yes, but you do have to write just a little bit of code.</p>
<p>Take a look at <a href="https://pytorch.org/docs/stable/torchvision/transforms.html" rel="noopener nofollow ugc">torchvision.transforms</a>.  You might also work through<br>
parts of the <a href="https://pytorch.org/tutorials/beginner/data_loading_tutorial.html" rel="noopener nofollow ugc">Writing Custom Datasets</a> tutorial.</p>
<p>The basic idea is that before you pass a batch of images to your<br>
model you apply various random <code>transforms</code> to your images.<br>
Every time you run an epoch (iterate through your dataset) you<br>
will get a different set of random <code>transforms</code> (applied, of course,<br>
to the same original images in your dataset).</p>
<p>You could, of course, build in advance a larger, augmented dataset<br>
that includes your original images as well as transformed images,<br>
and then train on that normally,  But it seems like less of a bother<br>
just to apply the <code>transforms</code> on the fly.</p>
<p>Best.</p>
<p>K. Frank</p>

username: TinfoilHat0
cooked: <p>Perhaps the following snippets might help.</p>
<pre><code class="lang-auto">transform_train = transforms.Compose([
    transforms.RandomCrop(32, padding=4),
    transforms.RandomHorizontalFlip(),
    transforms.ToTensor(),
    transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])

transform_test = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
</code></pre>

username: ShaidaMuhammad
cooked: <p>I applied</p>
<p><strong>transforms.RandomHorizontalFlip()</strong><br>
to data, but the data points remain the same in train data when I <strong>print(dataset_train)</strong>.<br>
take a look to the code.</p>
<blockquote>
<p>transform_in = transforms.Compose([transforms.Grayscale(num_output_channels=1),<br>
transforms.Resize([256, 256]),<br>
transforms.RandomHorizontalFlip(),<br>
transforms.ToTensor(),<br>
transforms.Normalize(mean=[0.5], std=[0.5])])</p>
<p>dataset_train = dsets.ImageFolder(train_dir, transform=transform_in)<br>
dataset_test = dsets.ImageFolder(test_dir, transform=transform_in)</p>
<p>print(dataset_train)</p>
</blockquote>
<p>The output is the same as before <strong>transforms.RandomHorizontalFlip()</strong></p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/2/f/2f71c5a6c96d0b431cd3a656cefc1890a9e4851e.png" alt="image" data-base62-sha1="6LIcbXDiJZqcPFOM0DtNkPpzAoC" width="605" height="197"></p>

username: wolterlw
cooked: <p>I’d recommend you try <a href="https://github.com/kornia/kornia" rel="nofollow noopener">kornia</a>, it performs augmentations on GPU (which is beneficial in your case since your don’t have much data so it’s possible to achieve high throughput, possibly even load it into memory if you have enough RAM), plus they have a lot more augments and decent docs.</p>