{
  "id": 168371,
  "title": "I want to use similar function that is pytorch 'Colorjitter' in Keras",
  "url": "/competitions/siim-isic-melanoma-classification/discussion/168371",
  "author_name": "HyunjunLee",
  "post_date": "2020-07-20T10:32:05.812000",
  "votes": 2,
  "comment_count": 4,
  "views": 0,
  "content": "<p>I want to use similar function that is pytorch 'Colorjitter' in Keras.<br>\nIs there a similar function?<br>\nor <br>\nHow to use that function in keras?</p>",
  "messages": [
    {
      "id": 936629,
      "postDate": "2020-07-20T11:36:02.743Z",
      "content": "<p>```\nclass CustomAugment(object):\n    def <strong>call</strong>(self, sample): <br>\n        # Random flips\n        sample = self._random_apply(tf.image.flip_left_right, sample, p=0.5)</p>\n\n<pre><code>    # Randomly apply transformation (color distortions) with probability p.\n    sample = self._random_apply(self._color_jitter, sample, p=0.8)\n    sample = self._random_apply(self._color_drop, sample, p=0.2)\n\n    return sample\n\ndef _color_jitter(self, x, s=1):\n    # one can also shuffle the order of following augmentations\n    # each time they are applied.\n    x = tf.image.random_brightness(x, max_delta=0.8*s)\n    x = tf.image.random_contrast(x, lower=1-0.8*s, upper=1+0.8*s)\n    x = tf.image.random_saturation(x, lower=1-0.8*s, upper=1+0.8*s)\n    x = tf.image.random_hue(x, max_delta=0.2*s)\n    x = tf.clip_by_value(x, 0, 1)\n    return x\n\ndef _color_drop(self, x):\n    x = tf.image.rgb_to_grayscale(x)\n    x = tf.tile(x, [1, 1, 1, 3])\n    return x\n\ndef _random_apply(self, func, x, p):\n    return tf.cond(\n      tf.less(tf.random.uniform([], minval=0, maxval=1, dtype=tf.float32),\n              tf.cast(p, tf.float32)),\n      lambda: func(x),\n      lambda: x)\n</code></pre>\n\n<p>```</p>",
      "rawMarkdown": "```\nclass CustomAugment(object):\n    def __call__(self, sample):        \n        # Random flips\n        sample = self._random_apply(tf.image.flip_left_right, sample, p=0.5)\n        \n        # Randomly apply transformation (color distortions) with probability p.\n        sample = self._random_apply(self._color_jitter, sample, p=0.8)\n        sample = self._random_apply(self._color_drop, sample, p=0.2)\n\n        return sample\n\n    def _color_jitter(self, x, s=1):\n        # one can also shuffle the order of following augmentations\n        # each time they are applied.\n        x = tf.image.random_brightness(x, max_delta=0.8*s)\n        x = tf.image.random_contrast(x, lower=1-0.8*s, upper=1+0.8*s)\n        x = tf.image.random_saturation(x, lower=1-0.8*s, upper=1+0.8*s)\n        x = tf.image.random_hue(x, max_delta=0.2*s)\n        x = tf.clip_by_value(x, 0, 1)\n        return x\n    \n    def _color_drop(self, x):\n        x = tf.image.rgb_to_grayscale(x)\n        x = tf.tile(x, [1, 1, 1, 3])\n        return x\n    \n    def _random_apply(self, func, x, p):\n        return tf.cond(\n          tf.less(tf.random.uniform([], minval=0, maxval=1, dtype=tf.float32),\n                  tf.cast(p, tf.float32)),\n          lambda: func(x),\n          lambda: x)\n\n```",
      "votes": 8,
      "replies": [
        {
          "id": 937616,
          "postDate": "2020-07-21T05:28:20.050Z",
          "content": "<p>Thanks!!``</p>\n<pre><code>ImageDataGenerator( preprocessing_function = CustomAugment())\n</code></pre>\n<p>So this upper code right??</p>",
          "rawMarkdown": "Thanks!!``\n\n```\nImageDataGenerator( preprocessing_function = CustomAugment())\n\n```\nSo this upper code right??"
        },
        {
          "id": 937622,
          "postDate": "2020-07-21T05:31:54.933Z",
          "content": "<p>Great TF augmentation function. Thanks for sharing.</p>",
          "rawMarkdown": "Great TF augmentation function. Thanks for sharing.",
          "votes": 1
        },
        {
          "id": 939703,
          "postDate": "2020-07-22T11:45:38.560Z",
          "rawMarkdown": "",
          "isDeleted": true
        }
      ]
    },
    {
      "id": 936585,
      "postDate": "2020-07-20T10:32:05.813Z",
      "content": "<p>I want to use similar function that is pytorch 'Colorjitter' in Keras.<br>\nIs there a similar function?<br>\nor <br>\nHow to use that function in keras?</p>",
      "rawMarkdown": "I want to use similar function that is pytorch 'Colorjitter' in Keras.\nIs there a similar function?\nor \nHow to use that function in keras?",
      "votes": 2
    }
  ],
  "comments": [
    {
      "id": 936629,
      "author_name": "Sirish Somanchi",
      "author_url": "",
      "post_date": "2020-07-20T11:36:02.743000",
      "content": "<p>```\nclass CustomAugment(object):\n    def <strong>call</strong>(self, sample): <br>\n        # Random flips\n        sample = self._random_apply(tf.image.flip_left_right, sample, p=0.5)</p>\n\n<pre><code>    # Randomly apply transformation (color distortions) with probability p.\n    sample = self._random_apply(self._color_jitter, sample, p=0.8)\n    sample = self._random_apply(self._color_drop, sample, p=0.2)\n\n    return sample\n\ndef _color_jitter(self, x, s=1):\n    # one can also shuffle the order of following augmentations\n    # each time they are applied.\n    x = tf.image.random_brightness(x, max_delta=0.8*s)\n    x = tf.image.random_contrast(x, lower=1-0.8*s, upper=1+0.8*s)\n    x = tf.image.random_saturation(x, lower=1-0.8*s, upper=1+0.8*s)\n    x = tf.image.random_hue(x, max_delta=0.2*s)\n    x = tf.clip_by_value(x, 0, 1)\n    return x\n\ndef _color_drop(self, x):\n    x = tf.image.rgb_to_grayscale(x)\n    x = tf.tile(x, [1, 1, 1, 3])\n    return x\n\ndef _random_apply(self, func, x, p):\n    return tf.cond(\n      tf.less(tf.random.uniform([], minval=0, maxval=1, dtype=tf.float32),\n              tf.cast(p, tf.float32)),\n      lambda: func(x),\n      lambda: x)\n</code></pre>\n\n<p>```</p>",
      "votes": 8,
      "replies": [
        {
          "id": 937616,
          "author_name": "HyunjunLee",
          "author_url": "",
          "post_date": "2020-07-21T05:28:20.050000",
          "content": "<p>Thanks!!``</p>\n<pre><code>ImageDataGenerator( preprocessing_function = CustomAugment())\n</code></pre>\n<p>So this upper code right??</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 937622,
          "author_name": "Chris Deotte",
          "author_url": "",
          "post_date": "2020-07-21T05:31:54.933000",
          "content": "<p>Great TF augmentation function. Thanks for sharing.</p>",
          "votes": 1,
          "replies": []
        },
        {
          "id": 939703,
          "author_name": "",
          "author_url": "",
          "post_date": "2020-07-22T11:45:38.560000",
          "content": "",
          "votes": 0,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "936629": "```\nclass CustomAugment(object):\n    def __call__(self, sample):        \n        # Random flips\n        sample = self._random_apply(tf.image.flip_left_right, sample, p=0.5)\n        \n        # Randomly apply transformation (color distortions) with probability p.\n        sample = self._random_apply(self._color_jitter, sample, p=0.8)\n        sample = self._random_apply(self._color_drop, sample, p=0.2)\n\n        return sample\n\n    def _color_jitter(self, x, s=1):\n        # one can also shuffle the order of following augmentations\n        # each time they are applied.\n        x = tf.image.random_brightness(x, max_delta=0.8*s)\n        x = tf.image.random_contrast(x, lower=1-0.8*s, upper=1+0.8*s)\n        x = tf.image.random_saturation(x, lower=1-0.8*s, upper=1+0.8*s)\n        x = tf.image.random_hue(x, max_delta=0.2*s)\n        x = tf.clip_by_value(x, 0, 1)\n        return x\n    \n    def _color_drop(self, x):\n        x = tf.image.rgb_to_grayscale(x)\n        x = tf.tile(x, [1, 1, 1, 3])\n        return x\n    \n    def _random_apply(self, func, x, p):\n        return tf.cond(\n          tf.less(tf.random.uniform([], minval=0, maxval=1, dtype=tf.float32),\n                  tf.cast(p, tf.float32)),\n          lambda: func(x),\n          lambda: x)\n\n```",
    "936585": "I want to use similar function that is pytorch 'Colorjitter' in Keras.\nIs there a similar function?\nor \nHow to use that function in keras?"
  }
}