{
  "id": 40886,
  "title": "Data augmentation methods in image classification",
  "url": "/competitions/cdiscount-image-classification-challenge/discussion/40886",
  "author_name": "SCU_DM",
  "post_date": "2017-10-10T03:15:13.777000",
  "votes": 0,
  "comment_count": 0,
  "views": 0,
  "content": "<p>There are several data augmentation methods in image classification, such as random crop, random  resize, color augment, aspect ratio augmentation, random rotation. Below is an example about \"scale and aspect ratio augmentation\" written in python code:</p>\n\n<pre><code>  class GoogleNetResize(imgaug.ImageAugmentor):\n    \"\"\"\n    crop 8%~100% of the original image\n    See `Going Deeper with Convolutions` by Google.\n    \"\"\"\n    def __init__(self, crop_area_fraction=0.08,\n                 aspect_ratio_low=0.75, aspect_ratio_high=1.333):\n        self._init(locals())\n\n    def _augment(self, img, _):\n        h, w = img.shape[:2]\n        area = h * w\n        for _ in range(10):\n            targetArea = self.rng.uniform(self.crop_area_fraction, 1.0) * area\n            aspectR = self.rng.uniform(self.aspect_ratio_low, self.aspect_ratio_high)\n            ww = int(np.sqrt(targetArea * aspectR) + 0.5)\n            hh = int(np.sqrt(targetArea / aspectR) + 0.5)\n            if self.rng.uniform() &lt; 0.5:\n                ww, hh = hh, ww\n            if hh &lt;= h and ww &lt;= w:\n                x1 = 0 if w == ww else self.rng.randint(0, w - ww)\n                y1 = 0 if h == hh else self.rng.randint(0, h - hh)\n                out = img[y1:y1 + hh, x1:x1 + ww]\n                out = cv2.resize(out, (224, 224), interpolation=cv2.INTER_CUBIC)\n                return out\n        out = imgaug.ResizeShortestEdge(224, interp=cv2.INTER_CUBIC).augment(img)\n        out = imgaug.CenterCrop(224).augment(out)\n        return out\n</code></pre>\n\n<p><strong>I am confused about these codes. I wonder if there are any math descriptions about \"scale and aspect ratio augmentation\" method. And I also want to know if there are any book about data augmentation method in image classification</strong></p>",
  "messages": [
    {
      "id": 229604,
      "postDate": "2017-10-10T03:15:13.777Z",
      "content": "<p>There are several data augmentation methods in image classification, such as random crop, random  resize, color augment, aspect ratio augmentation, random rotation. Below is an example about \"scale and aspect ratio augmentation\" written in python code:</p>\n\n<pre><code>  class GoogleNetResize(imgaug.ImageAugmentor):\n    \"\"\"\n    crop 8%~100% of the original image\n    See `Going Deeper with Convolutions` by Google.\n    \"\"\"\n    def __init__(self, crop_area_fraction=0.08,\n                 aspect_ratio_low=0.75, aspect_ratio_high=1.333):\n        self._init(locals())\n\n    def _augment(self, img, _):\n        h, w = img.shape[:2]\n        area = h * w\n        for _ in range(10):\n            targetArea = self.rng.uniform(self.crop_area_fraction, 1.0) * area\n            aspectR = self.rng.uniform(self.aspect_ratio_low, self.aspect_ratio_high)\n            ww = int(np.sqrt(targetArea * aspectR) + 0.5)\n            hh = int(np.sqrt(targetArea / aspectR) + 0.5)\n            if self.rng.uniform() &lt; 0.5:\n                ww, hh = hh, ww\n            if hh &lt;= h and ww &lt;= w:\n                x1 = 0 if w == ww else self.rng.randint(0, w - ww)\n                y1 = 0 if h == hh else self.rng.randint(0, h - hh)\n                out = img[y1:y1 + hh, x1:x1 + ww]\n                out = cv2.resize(out, (224, 224), interpolation=cv2.INTER_CUBIC)\n                return out\n        out = imgaug.ResizeShortestEdge(224, interp=cv2.INTER_CUBIC).augment(img)\n        out = imgaug.CenterCrop(224).augment(out)\n        return out\n</code></pre>\n\n<p><strong>I am confused about these codes. I wonder if there are any math descriptions about \"scale and aspect ratio augmentation\" method. And I also want to know if there are any book about data augmentation method in image classification</strong></p>",
      "rawMarkdown": "There are several data augmentation methods in image classification, such as random crop, random  resize, color augment, aspect ratio augmentation, random rotation. Below is an example about \"scale and aspect ratio augmentation\" written in python code:\n\n      class GoogleNetResize(imgaug.ImageAugmentor):\n        \"\"\"\n        crop 8%~100% of the original image\n        See `Going Deeper with Convolutions` by Google.\n        \"\"\"\n        def __init__(self, crop_area_fraction=0.08,\n                     aspect_ratio_low=0.75, aspect_ratio_high=1.333):\n            self._init(locals())\n    \n        def _augment(self, img, _):\n            h, w = img.shape[:2]\n            area = h * w\n            for _ in range(10):\n                targetArea = self.rng.uniform(self.crop_area_fraction, 1.0) * area\n                aspectR = self.rng.uniform(self.aspect_ratio_low, self.aspect_ratio_high)\n                ww = int(np.sqrt(targetArea * aspectR) + 0.5)\n                hh = int(np.sqrt(targetArea / aspectR) + 0.5)\n                if self.rng.uniform() &lt; 0.5:\n                    ww, hh = hh, ww\n                if hh &lt;= h and ww &lt;= w:\n                    x1 = 0 if w == ww else self.rng.randint(0, w - ww)\n                    y1 = 0 if h == hh else self.rng.randint(0, h - hh)\n                    out = img[y1:y1 + hh, x1:x1 + ww]\n                    out = cv2.resize(out, (224, 224), interpolation=cv2.INTER_CUBIC)\n                    return out\n            out = imgaug.ResizeShortestEdge(224, interp=cv2.INTER_CUBIC).augment(img)\n            out = imgaug.CenterCrop(224).augment(out)\n            return out\n\n**I am confused about these codes. I wonder if there are any math descriptions about \"scale and aspect ratio augmentation\" method. And I also want to know if there are any book about data augmentation method in image classification**\n"
    }
  ],
  "comments": [],
  "raw_markdown_by_id": {
    "229604": "There are several data augmentation methods in image classification, such as random crop, random  resize, color augment, aspect ratio augmentation, random rotation. Below is an example about \"scale and aspect ratio augmentation\" written in python code:\n\n      class GoogleNetResize(imgaug.ImageAugmentor):\n        \"\"\"\n        crop 8%~100% of the original image\n        See `Going Deeper with Convolutions` by Google.\n        \"\"\"\n        def __init__(self, crop_area_fraction=0.08,\n                     aspect_ratio_low=0.75, aspect_ratio_high=1.333):\n            self._init(locals())\n    \n        def _augment(self, img, _):\n            h, w = img.shape[:2]\n            area = h * w\n            for _ in range(10):\n                targetArea = self.rng.uniform(self.crop_area_fraction, 1.0) * area\n                aspectR = self.rng.uniform(self.aspect_ratio_low, self.aspect_ratio_high)\n                ww = int(np.sqrt(targetArea * aspectR) + 0.5)\n                hh = int(np.sqrt(targetArea / aspectR) + 0.5)\n                if self.rng.uniform() &lt; 0.5:\n                    ww, hh = hh, ww\n                if hh &lt;= h and ww &lt;= w:\n                    x1 = 0 if w == ww else self.rng.randint(0, w - ww)\n                    y1 = 0 if h == hh else self.rng.randint(0, h - hh)\n                    out = img[y1:y1 + hh, x1:x1 + ww]\n                    out = cv2.resize(out, (224, 224), interpolation=cv2.INTER_CUBIC)\n                    return out\n            out = imgaug.ResizeShortestEdge(224, interp=cv2.INTER_CUBIC).augment(img)\n            out = imgaug.CenterCrop(224).augment(out)\n            return out\n\n**I am confused about these codes. I wonder if there are any math descriptions about \"scale and aspect ratio augmentation\" method. And I also want to know if there are any book about data augmentation method in image classification**\n"
  }
}