Data augmentation in PyTorch

f10w: <p>Hi,<br>
There is something with PyTorch data augmentation that I would like to understand. I used the following code to create a training data loader:</p>
<pre><code>rgb_mean = (0.4914, 0.4822, 0.4465)
rgb_std = (0.2023, 0.1994, 0.2010)

transform_train = transforms.Compose([
    transforms.RandomCrop(32, padding=4),
    transforms.RandomHorizontalFlip(),
    transforms.ToTensor(),
    transforms.Normalize(rgb_mean, rgb_std),
])

kwargs = {'num_workers': 2, 'pin_memory': True} if args.cuda else {}
train_loader = torch.utils.data.DataLoader(datasets.CIFAR10(root=data_root, train=True, transform=transform_train,
    transforms.Normalize(rgb_mean, rgb_std),
]), download=False), batch_size=128, **kwargs)

print('len(train_loader)=',len(train_loader))
</code></pre>
<p>I guess that data augmentation was used with <strong>two transformations</strong>: random crop and random horizontal flip. Thus, I would expect the obtained total number of training samples to be 3 times the size of the training set of Cifar-10, i.e. 3*50000 = 150000. However, the output of the above code is:</p>
<p><code>len(train_loader) = 391</code></p>
<p>which means there are approximately 391*128 ~= 50000 samples. <strong>Where are the augmented data?</strong></p>
<p>Thank you in advance for your help!</p>

tom: <p>Hello,</p>
<p>in any epoch the dataloader will apply a fresh set of random operations “on the fly”. So instead of showing the exact same items at every epoch, you are showing a variant that has been changed in a different way. So after three epochs, you would have seen three random variants of each item in a dataset.</p>
<p>That said: I don’t think your counting method works for estimating the number of samples in the augmented set: The flip will double the number of pictures, but the crop has many potential outcomes. Also you would need to multiply the relative increases. (One might also question whether the augmented samples fully count, but that is a different discussion.)</p>
<p>Best regards</p>
<p>Thomas</p>

f10w: <p>Thanks, Thomas! That totally makes sense. So the augmentation happens inside of this line:</p>
<pre><code>for (data, target) in dataloader:
</code></pre>
<p>Very cool.</p>

oneTaken: <p>The idea is that, according to your words, <a class="mention" href="/u/tom">@tom</a> , every epoch the data is augmetated. I’m still puzzled with this, does this mean that data was transformed in every epoch? So, how to do make 3x times data in every specific epoch? To make 50000x3=150000 data in one certain epoch?</p>

smth: <p><a class="mention" href="/u/onetaken">@oneTaken</a> it just means that the data is changed on the fly. The epoch size does not change, you just get randomly transformed samples every epoch. So the concept of a static dataset becomes a bit more dynamic.</p>

oneTaken: <p>Yeah, I use the some transorms to do this, such as <code>torchvision.transforms.RandomCrop</code>,<br>
but how to double or many times the dataset? such as five_crop to 5x bigger the dataset?<br>
I’m so confused. <a class="mention" href="/u/smth">@smth</a> .</p>

f10w: <p>Well, you can see it this way: The new “ONE epoch” is in fact FIVE consecutive old “one epoch” stacked together.</p>

oneTaken: <p>Can you give an experiment on that? It’s just hard to get this.</p>

f10w: <p>Let n denote the size of the original dataset. In ordinary augmentation (i.e. precomputed and static, not on the fly), if we have 5 transformations then the size of the augmented data is 5n, which means at each epoch the number of iterations is also 5n. Now, if we augment the data on the fly (with random transformations) using PyTorch, then each epoch has the same number of iterations n. If we concatenate 5 epochs consécutive to create a large epoch (or call it whatever you want), then the total number of iterations in this large epoch is 5n. Thus it is roughly equivalent to static augmentation. (Note that this large epoch is a valid epoch because there’s no duplicate in the 5n iteration since the transformations are random.)</p>

SpandanMadan: <p>It’s important to understand that the data_loader only goes through the files and indexes files and their target labels. The actual loading of the images happens when the get_item() function is called, which is basically when you enumerate the DataLoader. This is usually the line in the program which looks like this -</p>
<pre><code> for data in train_loader:
</code></pre>
<p>It is at this time that the transformations are randomly done.</p>

Kornel_Dylski: <p>For clarification:</p>
<p>If you have random transformations, after each epoch, you will receive a new set of randomly transformed samples (e.g. rotated random num of deg).<br>
In this case, it’s enough to multiply the number of epochs to get more samples.</p>
<p>But you may want to concat all of generated samples if your transformations<br>
performs determined operations. (e.g. add padding)<br>
Then just use ConcatDataset like here: <a href="https://discuss.pytorch.org/t/concatenation-while-using-data-loader/10772">Concatenation while using Data Loader</a></p>
<p>In my opinion, the first approach is slightly better to avoid overfitting.<br>
After all, you can create a transformation that will be randomly applied or not.</p>

Josiane_Rodrigues: <p>Hi,<br>
Is there any way to implement data augmentation only for some specific CIFAR-10 classes?</p>

ptrblck: <p>You can create your own <code>Dataset</code> and internally load CIFAR10.<br>
In the <code>__getitem__</code> method you could check what the current target is and apply the transformation based on this condition.</p>

Josiane_Rodrigues: <p>Could you give me a example?</p>

ptrblck: <p>Sure! You could start with the following code:</p>
<pre><code class="lang-auto">class MyDataset(Dataset):
    def __init__(self, train=True, transforms=None):
        self.cifar10 = datasets.CIFAR10(root='YOUR_PATH',
                                        train=train,
                                        download=False)
        self.transforms = transforms

    def __getitem__(self, index):
        x, y = self.cifar10[index]
        
        if self.transforms:
            print('Chosing transform ', y)
            x = self.transforms[y](x) # Chose class transform based on y
        
        return x, y

    def __len__(self):
        return len(self.cifar10)


class_transforms = []
for _ in range(10):
    transform = transforms.Compose([
        transforms.ColorJitter(
            brightness=0.1*torch.randn(1),
            contrast=0.1*torch.randn(1),
            saturation=0.1*torch.randn(1),
            hue=0.1*torch.randn(1)),
        transforms.ToTensor()
    ])
    class_transforms.append(transform)


dataset = MyDataset(transforms=class_transforms)
loader = DataLoader(dataset, batch_size=5, shuffle=True)
data, target = iter(loader).next()
&gt; ('Chosing transform ', 9)
('Chosing transform ', 2)
('Chosing transform ', 5)
('Chosing transform ', 0)
('Chosing transform ', 2)
print(target)
&gt; tensor([ 9,  2,  5,  0,  2])
</code></pre>
<p>I have just applied random transformations to each class. Your <code>class_transforms</code> will probably look a bit more complicated. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=5" title=":wink:" class="emoji" alt=":wink:"></p>

Josiane_Rodrigues: <p>Thanks, <a class="mention" href="/u/ptrblck">@ptrblck</a>  <img src="https://discuss.pytorch.org/images/emoji/apple/smile.png?v=5" title=":smile:" class="emoji" alt=":smile:"></p>

Yicong_Li: <p>Hi,<br>
Does that means  will get different randomly transformed samples every epoch, and unlikely to get same transformed sample twice?</p>

KyrieHe: <p>Maybe imgaug is the best choice.</p>

MariosOreo: <p>Yes, I think so. I think the more transform options we use, the more likely we get different samples at every iteration.</p>

falmasri: <p>There is a new movement right now in data augmentation I found it used in most of the super-resolution problem. for example these lines in “Residual Dense Network CVPR 2018”</p>
<blockquote>
<p>we randomly extract 16 LR RGB patches with the size of 32 × 32 as inputs. We randomly augment<br>
the patches by flipping horizontally or vertically and rotating 90◦ . 1,000 iterations of back-propagation constitute an epoch</p>
</blockquote>
<p>which I guess either they are doing nested-looping or they are augmenting the dataset offline before training.<br>
They said training on DIV2K they have 1000 iteration per epoch for this patche size while you should have 50. it means 20 times larger training per epoch.</p>