{
  "id": 209814,
  "title": "Data augmentation by PyTorch",
  "url": "/competitions/cassava-leaf-disease-classification/discussion/209814",
  "author_name": "",
  "post_date": "2021-01-08T16:56:45.092626400Z",
  "votes": 6,
  "comment_count": 2,
  "views": 0,
  "content": "<p>Hi Kagglers<br>\nThere is some confusion with PyTorch data augmentation. I use the following code to create a training data loader:</p>\n<pre><code>def Get_train_transforms():\n    return Compose([\n            RandomResizedCrop(512,512),\n           HorizontalFlip(p=0.5),\n           VerticalFlip(p=0.5),\n            Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], \n            ToTensorV2(p=1.0),\n        ], p=1.)\n\ntrain_ds = CassavaDataset(train, '../input/cassava-leaf-disease-classification/train_images/', transforms=Get_train_transforms(), output_label=True)\n\ntrain_loader = torch.utils.data.DataLoader(\n        train_ds,\n        batch_size=16,\n        pin_memory=False,\n        drop_last=False,\n        shuffle=True,        \n        num_workers=4,)\n</code></pre>\n<p>I use data augmentation with three transformations:<br>\n            1-RandomResizedCrop(512,512),<br>\n           2-HorizontalFlip(p=0.5),<br>\n           3-VerticalFlip(p=0.5),<br>\nso, I would expect to obtain a total number of training samples to be 4 times the size of the original training set, but I get the same size!!!<br>\nWhere is the augmented data?<br>\nThank you in advance for your help!</p>",
  "messages": [
    {
      "id": "1144768",
      "postDate": "01/08/2021 16:56:45",
      "content": "<p>Hi Kagglers<br>\nThere is some confusion with PyTorch data augmentation. I use the following code to create a training data loader:</p>\n<pre><code>def Get_train_transforms():\n    return Compose([\n            RandomResizedCrop(512,512),\n           HorizontalFlip(p=0.5),\n           VerticalFlip(p=0.5),\n            Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], \n            ToTensorV2(p=1.0),\n        ], p=1.)\n\ntrain_ds = CassavaDataset(train, '../input/cassava-leaf-disease-classification/train_images/', transforms=Get_train_transforms(), output_label=True)\n\ntrain_loader = torch.utils.data.DataLoader(\n        train_ds,\n        batch_size=16,\n        pin_memory=False,\n        drop_last=False,\n        shuffle=True,        \n        num_workers=4,)\n</code></pre>\n<p>I use data augmentation with three transformations:<br>\n            1-RandomResizedCrop(512,512),<br>\n           2-HorizontalFlip(p=0.5),<br>\n           3-VerticalFlip(p=0.5),<br>\nso, I would expect to obtain a total number of training samples to be 4 times the size of the original training set, but I get the same size!!!<br>\nWhere is the augmented data?<br>\nThank you in advance for your help!</p>",
      "rawMarkdown": "Hi Kagglers\nThere is some confusion with PyTorch data augmentation. I use the following code to create a training data loader:\n\n```\ndef Get_train_transforms():\n    return Compose([\n            RandomResizedCrop(512,512),\n           HorizontalFlip(p=0.5),\n           VerticalFlip(p=0.5),\n            Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], \n            ToTensorV2(p=1.0),\n        ], p=1.)\n\ntrain_ds = CassavaDataset(train, '../input/cassava-leaf-disease-classification/train_images/', transforms=Get_train_transforms(), output_label=True)\n\ntrain_loader = torch.utils.data.DataLoader(\n        train_ds,\n        batch_size=16,\n        pin_memory=False,\n        drop_last=False,\n        shuffle=True,        \n        num_workers=4,)\n```\nI use data augmentation with three transformations:\n            1-RandomResizedCrop(512,512),\n           2-HorizontalFlip(p=0.5),\n           3-VerticalFlip(p=0.5),\nso, I would expect to obtain a total number of training samples to be 4 times the size of the original training set, but I get the same size!!!\nWhere is the augmented data?\nThank you in advance for your help!",
      "votes": null
    },
    {
      "id": "1144843",
      "postDate": "01/08/2021 17:49:51",
      "content": "<p>When you load the transforms into your DataLoader it creates the augmentations on the fly meaning that: everytime you iterate through your <code>train_loader</code> you will get your data augmentated with random different augmentations, it does not increase the size of your training samples directly. </p>\n<p>The idea behind it, is that each epoch your model will learn different transformed data and thus gets more robust and generalizes better.</p>\n<p>If you want to get safely 3 times the training size you will have to safe for each transformation the image locally and then use them for training. This needs way more storage and does not help the model to generalize as good as random augmentation on the fly.</p>",
      "rawMarkdown": "When you load the transforms into your DataLoader it creates the augmentations on the fly meaning that: everytime you iterate through your `train_loader` you will get your data augmentated with random different augmentations, it does not increase the size of your training samples directly. \n\nThe idea behind it, is that each epoch your model will learn different transformed data and thus gets more robust and generalizes better.\n\nIf you want to get safely 3 times the training size you will have to safe for each transformation the image locally and then use them for training. This needs way more storage and does not help the model to generalize as good as random augmentation on the fly.",
      "votes": null
    },
    {
      "id": "1145065",
      "postDate": "01/08/2021 21:12:08",
      "content": "<p>Thanks for a detailed explanation</p>",
      "rawMarkdown": "Thanks for a detailed explanation",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 1144843,
      "author_name": "aliabdin1",
      "author_url": "",
      "post_date": "01/08/2021 17:49:51",
      "content": "<p>When you load the transforms into your DataLoader it creates the augmentations on the fly meaning that: everytime you iterate through your <code>train_loader</code> you will get your data augmentated with random different augmentations, it does not increase the size of your training samples directly. </p>\n<p>The idea behind it, is that each epoch your model will learn different transformed data and thus gets more robust and generalizes better.</p>\n<p>If you want to get safely 3 times the training size you will have to safe for each transformation the image locally and then use them for training. This needs way more storage and does not help the model to generalize as good as random augmentation on the fly.</p>",
      "votes": null,
      "replies": [
        {
          "id": 1145065,
          "author_name": "ammarnassanalhajali",
          "author_url": "",
          "post_date": "01/08/2021 21:12:08",
          "content": "<p>Thanks for a detailed explanation</p>",
          "votes": null,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "1144768": "Hi Kagglers\nThere is some confusion with PyTorch data augmentation. I use the following code to create a training data loader:\n\n```\ndef Get_train_transforms():\n    return Compose([\n            RandomResizedCrop(512,512),\n           HorizontalFlip(p=0.5),\n           VerticalFlip(p=0.5),\n            Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], \n            ToTensorV2(p=1.0),\n        ], p=1.)\n\ntrain_ds = CassavaDataset(train, '../input/cassava-leaf-disease-classification/train_images/', transforms=Get_train_transforms(), output_label=True)\n\ntrain_loader = torch.utils.data.DataLoader(\n        train_ds,\n        batch_size=16,\n        pin_memory=False,\n        drop_last=False,\n        shuffle=True,        \n        num_workers=4,)\n```\nI use data augmentation with three transformations:\n            1-RandomResizedCrop(512,512),\n           2-HorizontalFlip(p=0.5),\n           3-VerticalFlip(p=0.5),\nso, I would expect to obtain a total number of training samples to be 4 times the size of the original training set, but I get the same size!!!\nWhere is the augmented data?\nThank you in advance for your help!",
    "1144843": "When you load the transforms into your DataLoader it creates the augmentations on the fly meaning that: everytime you iterate through your `train_loader` you will get your data augmentated with random different augmentations, it does not increase the size of your training samples directly. \n\nThe idea behind it, is that each epoch your model will learn different transformed data and thus gets more robust and generalizes better.\n\nIf you want to get safely 3 times the training size you will have to safe for each transformation the image locally and then use them for training. This needs way more storage and does not help the model to generalize as good as random augmentation on the fly.",
    "1145065": "Thanks for a detailed explanation"
  },
  "source": "meta"
}