{
  "id": 77186,
  "title": "Image Augmentation",
  "url": "/competitions/humpback-whale-identification/discussion/77186",
  "author_name": "",
  "post_date": "2019-01-10T12:12:33.886211700Z",
  "votes": null,
  "comment_count": 2,
  "views": 0,
  "content": "<p>Currently I’m working on Humpback Whale Identification — where there is a class imbalance and hence thought of implementing oversampling using “albumentation” library.</p>\n\n<p>I want to apply below augmentation logic for almost 4000+ images with less than 10 images per class and when trying to do oversampling (creating 5 more images for each such class) — Kernel gets killed as RAM is totally filled up (i.e., 14GB — GPU).</p>\n\n<p>I'm using below code for augmentation</p>\n\n<p>import albumentations\nfrom albumentations import torch as AT</p>\n\n<p>train_data_transform = albumentations.Compose([\n    albumentations.Resize(256, 256),\n    albumentations.HorizontalFlip(),\n    albumentations.OneOf([\n        albumentations.RandomContrast(),\n        albumentations.RandomBrightness(),\n    ]),\n    albumentations.ShiftScaleRotate(rotate_limit=10, scale_limit=0.15),\n    albumentations.JpegCompression(80),\n    albumentations.HueSaturationValue(),\n    albumentations.Normalize(),\n    AT.ToTensor()\n    ])</p>\n\n<p>Creating augmented images for just 5000 random images here, but RAM memory (14GB - GPU) totally occupies and kernel dies</p>\n\n<p>files = os.listdir(\"../input/train\")</p>\n\n<p>augmented_images_list = []\nfor num,filename in enumerate(np.random.choice(files, 500)):\n    if(filename.endswith(\".jpg\")):\n        img = cv2.imread(\"../input/train/\"+ filename)\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n        image = train_data_transform(image=img)['image']\n        augmented_images_list.append(image)</p>\n\n<p>I'm I doing anything wrong here, request to help</p>\n\n<p>Thank you</p>",
  "messages": [
    {
      "id": "453577",
      "postDate": "01/10/2019 12:12:33",
      "content": "<p>Currently I’m working on Humpback Whale Identification — where there is a class imbalance and hence thought of implementing oversampling using “albumentation” library.</p>\n\n<p>I want to apply below augmentation logic for almost 4000+ images with less than 10 images per class and when trying to do oversampling (creating 5 more images for each such class) — Kernel gets killed as RAM is totally filled up (i.e., 14GB — GPU).</p>\n\n<p>I'm using below code for augmentation</p>\n\n<p>import albumentations\nfrom albumentations import torch as AT</p>\n\n<p>train_data_transform = albumentations.Compose([\n    albumentations.Resize(256, 256),\n    albumentations.HorizontalFlip(),\n    albumentations.OneOf([\n        albumentations.RandomContrast(),\n        albumentations.RandomBrightness(),\n    ]),\n    albumentations.ShiftScaleRotate(rotate_limit=10, scale_limit=0.15),\n    albumentations.JpegCompression(80),\n    albumentations.HueSaturationValue(),\n    albumentations.Normalize(),\n    AT.ToTensor()\n    ])</p>\n\n<p>Creating augmented images for just 5000 random images here, but RAM memory (14GB - GPU) totally occupies and kernel dies</p>\n\n<p>files = os.listdir(\"../input/train\")</p>\n\n<p>augmented_images_list = []\nfor num,filename in enumerate(np.random.choice(files, 500)):\n    if(filename.endswith(\".jpg\")):\n        img = cv2.imread(\"../input/train/\"+ filename)\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n        image = train_data_transform(image=img)['image']\n        augmented_images_list.append(image)</p>\n\n<p>I'm I doing anything wrong here, request to help</p>\n\n<p>Thank you</p>",
      "rawMarkdown": "Currently I’m working on Humpback Whale Identification — where there is a class imbalance and hence thought of implementing oversampling using “albumentation” library.\n\nI want to apply below augmentation logic for almost 4000+ images with less than 10 images per class and when trying to do oversampling (creating 5 more images for each such class) — Kernel gets killed as RAM is totally filled up (i.e., 14GB — GPU).\n\nI'm using below code for augmentation\n\nimport albumentations\nfrom albumentations import torch as AT\n\ntrain_data_transform = albumentations.Compose([\n    albumentations.Resize(256, 256),\n    albumentations.HorizontalFlip(),\n    albumentations.OneOf([\n        albumentations.RandomContrast(),\n        albumentations.RandomBrightness(),\n    ]),\n    albumentations.ShiftScaleRotate(rotate_limit=10, scale_limit=0.15),\n    albumentations.JpegCompression(80),\n    albumentations.HueSaturationValue(),\n    albumentations.Normalize(),\n    AT.ToTensor()\n    ])\n\nCreating augmented images for just 5000 random images here, but RAM memory (14GB - GPU) totally occupies and kernel dies\n\nfiles = os.listdir(\"../input/train\")\n\naugmented_images_list = []\nfor num,filename in enumerate(np.random.choice(files, 500)):\n    if(filename.endswith(\".jpg\")):\n        img = cv2.imread(\"../input/train/\"+ filename)\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n        image = train_data_transform(image=img)['image']\n        augmented_images_list.append(image)\n\nI'm I doing anything wrong here, request to help\n\nThank you",
      "votes": null
    },
    {
      "id": "453582",
      "postDate": "01/10/2019 12:26:04",
      "content": "<p>How are you fitting your model? I mean, what function are you using. (It would be better if you could share the notebook)</p>",
      "rawMarkdown": "How are you fitting your model? I mean, what function are you using. (It would be better if you could share the notebook)",
      "votes": null
    },
    {
      "id": "454147",
      "postDate": "01/11/2019 07:08:30",
      "content": "<p>Without augmentation I'm using Pytorch custom data loader to load images from Kaggle input train path. Loader is as below without augmentation. But after augmentation not sure where to store these images, so thought of using List to hold augmented images, and corresponding labels ... so that can iterate over these in this custom data loader ... but can't do this as it takes more memory. I'm I doing anything wrong by loading images into List? Is there any other way to efficiently use memory?</p>\n\n<p>class WhaleDataset(Dataset):\n    def <strong>init</strong>(self, datafolder, datatype='train', df=None, transform=None, y=None):\n        self.datafolder = datafolder\n        self.datatype = datatype\n        self.y = y\n        if self.datatype == 'train':\n            self.df = df.values\n        self.image_files_list = [s for s in os.listdir(datafolder)]\n        self.transform = transform</p>\n\n<pre><code>def __len__(self):\n    return len(self.image_files_list)\n\ndef __getitem__(self, idx):\n    if self.datatype == 'train':\n        img_name = os.path.join(self.datafolder, self.df[idx][0])\n        label = self.y[idx]\n\n    elif self.datatype == 'test':\n        img_name = os.path.join(self.datafolder, self.image_files_list[idx])\n        label = np.zeros((NUM_CLASSES,))\n\n    img = cv2.imread(img_name)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    image = self.transform(image=img)['image']\n    if self.datatype == 'train':\n        return image, label\n    elif self.datatype == 'test':\n        # so that the images will be in a correct order\n        return image, label, self.image_files_list[idx]\n</code></pre>\n\n<p>train_dataset = WhaleDataset(\n    datafolder='../input/train/', \n    datatype='train', \n    df=train_df, \n    transform=data_transforms, \n    y=y\n)</p>\n\n<p>test_set = WhaleDataset(\n    datafolder='../input/test/', \n    datatype='test', \n    transform=data_transforms_test\n)       </p>",
      "rawMarkdown": "Without augmentation I'm using Pytorch custom data loader to load images from Kaggle input train path. Loader is as below without augmentation. But after augmentation not sure where to store these images, so thought of using List to hold augmented images, and corresponding labels ... so that can iterate over these in this custom data loader ... but can't do this as it takes more memory. I'm I doing anything wrong by loading images into List? Is there any other way to efficiently use memory?\n\nclass WhaleDataset(Dataset):\n    def __init__(self, datafolder, datatype='train', df=None, transform=None, y=None):\n        self.datafolder = datafolder\n        self.datatype = datatype\n        self.y = y\n        if self.datatype == 'train':\n            self.df = df.values\n        self.image_files_list = [s for s in os.listdir(datafolder)]\n        self.transform = transform\n\n\n    def __len__(self):\n        return len(self.image_files_list)\n    \n    def __getitem__(self, idx):\n        if self.datatype == 'train':\n            img_name = os.path.join(self.datafolder, self.df[idx][0])\n            label = self.y[idx]\n            \n        elif self.datatype == 'test':\n            img_name = os.path.join(self.datafolder, self.image_files_list[idx])\n            label = np.zeros((NUM_CLASSES,))\n\n        img = cv2.imread(img_name)\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n        image = self.transform(image=img)['image']\n        if self.datatype == 'train':\n            return image, label\n        elif self.datatype == 'test':\n            # so that the images will be in a correct order\n            return image, label, self.image_files_list[idx]\n        \ntrain_dataset = WhaleDataset(\n    datafolder='../input/train/', \n    datatype='train', \n    df=train_df, \n    transform=data_transforms, \n    y=y\n)\n\ntest_set = WhaleDataset(\n    datafolder='../input/test/', \n    datatype='test', \n    transform=data_transforms_test\n)",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 453582,
      "author_name": "cayala",
      "author_url": "",
      "post_date": "01/10/2019 12:26:04",
      "content": "<p>How are you fitting your model? I mean, what function are you using. (It would be better if you could share the notebook)</p>",
      "votes": null,
      "replies": [
        {
          "id": 454147,
          "author_name": "raghavendramp",
          "author_url": "",
          "post_date": "01/11/2019 07:08:30",
          "content": "<p>Without augmentation I'm using Pytorch custom data loader to load images from Kaggle input train path. Loader is as below without augmentation. But after augmentation not sure where to store these images, so thought of using List to hold augmented images, and corresponding labels ... so that can iterate over these in this custom data loader ... but can't do this as it takes more memory. I'm I doing anything wrong by loading images into List? Is there any other way to efficiently use memory?</p>\n\n<p>class WhaleDataset(Dataset):\n    def <strong>init</strong>(self, datafolder, datatype='train', df=None, transform=None, y=None):\n        self.datafolder = datafolder\n        self.datatype = datatype\n        self.y = y\n        if self.datatype == 'train':\n            self.df = df.values\n        self.image_files_list = [s for s in os.listdir(datafolder)]\n        self.transform = transform</p>\n\n<pre><code>def __len__(self):\n    return len(self.image_files_list)\n\ndef __getitem__(self, idx):\n    if self.datatype == 'train':\n        img_name = os.path.join(self.datafolder, self.df[idx][0])\n        label = self.y[idx]\n\n    elif self.datatype == 'test':\n        img_name = os.path.join(self.datafolder, self.image_files_list[idx])\n        label = np.zeros((NUM_CLASSES,))\n\n    img = cv2.imread(img_name)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    image = self.transform(image=img)['image']\n    if self.datatype == 'train':\n        return image, label\n    elif self.datatype == 'test':\n        # so that the images will be in a correct order\n        return image, label, self.image_files_list[idx]\n</code></pre>\n\n<p>train_dataset = WhaleDataset(\n    datafolder='../input/train/', \n    datatype='train', \n    df=train_df, \n    transform=data_transforms, \n    y=y\n)</p>\n\n<p>test_set = WhaleDataset(\n    datafolder='../input/test/', \n    datatype='test', \n    transform=data_transforms_test\n)       </p>",
          "votes": null,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "453577": "Currently I’m working on Humpback Whale Identification — where there is a class imbalance and hence thought of implementing oversampling using “albumentation” library.\n\nI want to apply below augmentation logic for almost 4000+ images with less than 10 images per class and when trying to do oversampling (creating 5 more images for each such class) — Kernel gets killed as RAM is totally filled up (i.e., 14GB — GPU).\n\nI'm using below code for augmentation\n\nimport albumentations\nfrom albumentations import torch as AT\n\ntrain_data_transform = albumentations.Compose([\n    albumentations.Resize(256, 256),\n    albumentations.HorizontalFlip(),\n    albumentations.OneOf([\n        albumentations.RandomContrast(),\n        albumentations.RandomBrightness(),\n    ]),\n    albumentations.ShiftScaleRotate(rotate_limit=10, scale_limit=0.15),\n    albumentations.JpegCompression(80),\n    albumentations.HueSaturationValue(),\n    albumentations.Normalize(),\n    AT.ToTensor()\n    ])\n\nCreating augmented images for just 5000 random images here, but RAM memory (14GB - GPU) totally occupies and kernel dies\n\nfiles = os.listdir(\"../input/train\")\n\naugmented_images_list = []\nfor num,filename in enumerate(np.random.choice(files, 500)):\n    if(filename.endswith(\".jpg\")):\n        img = cv2.imread(\"../input/train/\"+ filename)\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n        image = train_data_transform(image=img)['image']\n        augmented_images_list.append(image)\n\nI'm I doing anything wrong here, request to help\n\nThank you",
    "453582": "How are you fitting your model? I mean, what function are you using. (It would be better if you could share the notebook)",
    "454147": "Without augmentation I'm using Pytorch custom data loader to load images from Kaggle input train path. Loader is as below without augmentation. But after augmentation not sure where to store these images, so thought of using List to hold augmented images, and corresponding labels ... so that can iterate over these in this custom data loader ... but can't do this as it takes more memory. I'm I doing anything wrong by loading images into List? Is there any other way to efficiently use memory?\n\nclass WhaleDataset(Dataset):\n    def __init__(self, datafolder, datatype='train', df=None, transform=None, y=None):\n        self.datafolder = datafolder\n        self.datatype = datatype\n        self.y = y\n        if self.datatype == 'train':\n            self.df = df.values\n        self.image_files_list = [s for s in os.listdir(datafolder)]\n        self.transform = transform\n\n\n    def __len__(self):\n        return len(self.image_files_list)\n    \n    def __getitem__(self, idx):\n        if self.datatype == 'train':\n            img_name = os.path.join(self.datafolder, self.df[idx][0])\n            label = self.y[idx]\n            \n        elif self.datatype == 'test':\n            img_name = os.path.join(self.datafolder, self.image_files_list[idx])\n            label = np.zeros((NUM_CLASSES,))\n\n        img = cv2.imread(img_name)\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n        image = self.transform(image=img)['image']\n        if self.datatype == 'train':\n            return image, label\n        elif self.datatype == 'test':\n            # so that the images will be in a correct order\n            return image, label, self.image_files_list[idx]\n        \ntrain_dataset = WhaleDataset(\n    datafolder='../input/train/', \n    datatype='train', \n    df=train_df, \n    transform=data_transforms, \n    y=y\n)\n\ntest_set = WhaleDataset(\n    datafolder='../input/test/', \n    datatype='test', \n    transform=data_transforms_test\n)"
  },
  "source": "meta"
}