{
  "id": 221188,
  "title": "Image Augmentation:  Help to use random_transform method from ImageDataGenerator",
  "url": "/competitions/tpu-getting-started/discussion/221188",
  "author_name": "Sau Kha",
  "post_date": "2021-02-21T18:42:01.559000",
  "votes": 4,
  "comment_count": 1,
  "views": null,
  "content": "<p>I need help to use random_transform method from ImageDataGenerator for image augmentation for training images to train my image classification model.  Here is <a href=\"https://www.kaggle.com/saukha/petals-to-the-metals-flower-classification\" target=\"_blank\">my notebook</a>  Help and comments will be very much appreciated!</p>\n<p>I have created an image generator <strong>img_gen</strong> and tried to use the random_transform method to transform single images as inputs.  (See A below)  But I ran into errors in both situations:</p>\n<p>a) use the random_transform method in the <strong>data_augment(image,  label)</strong> function where I also use tf.image(…) and tfa.image(…) to augment single images, (See B below) or </p>\n<p>b) create the <strong>img_gen_random_transform(image, label)</strong> function with the <strong>img_gen.random_transform</strong> method <code>image = img_gen.random_transform(image)</code> and then use the function in the <strong>get_training_dataset(filenames, augmentation=False)</strong> function in this line.  <br>\n<code>dataset = dataset.map(img_gen_random_transform, num_parallel_calls=AUTO)</code> <br>\n (See C below)   </p>\n<h1>A</h1>\n<pre><code>#### create an ImageDataGenerator for random transformation\nimg_gen = tf.keras.preprocessing.image.ImageDataGenerator(\n    rotation_range=54, width_shift_range=0.15, height_shift_range=0.15,\n    brightness_range=None, zoom_range=[1.0, 1.25], fill_mode='constant', \n    horizontal_flip=True, preprocessing_function=None)\n\n#### random_transform method in a function\ndef img_gen_random_transform(image, label):\n    # apply random_transform method to single image\n    image = img_gen.random_transform(image)\n    return image, label\n</code></pre>\n<h1>B</h1>\n<pre><code>#### define data augmentation function,                  \ndef data_augment(image,  label):  # one image at a time \n\n    ######### using tf.image on one single image\n    image = tf.image.random_contrast(image, 0.90, 0.99)\n    image = tf.image.random_brightness(image, 0.05) \n    #image = tf.image.random_saturation(image, 0.90, 0.99)\n    ######### using ImageDataGenerator random_transform method on one single image    \n    #mage = img_gen.random_transform(image)  # didn't work\n    ######### using tfa.image on one single image\n    rdn = tf.random.normal([1], mean=0, stddev=1, dtype=tf.float32) \n    if rdn &gt; 2.0:  # blur 2.5% of the images (1 tail, 2 stddev above mean)\n        image = tfa.image.mean_filter2d(image, filter_shape = 3,\n                                   padding='constant')    \nreturn image, label\n</code></pre>\n<h1>C</h1>\n<pre><code>#### get training datatset with augmentation option\ndef get_training_dataset(filenames, augmentation=False):\n    dataset = load_dataset(filenames, labeled=True, ordered=False)\n    if augmentation:\n        # map the data_augment function across images of dthe ataset \n        dataset = dataset.map(data_augment, num_parallel_calls=AUTO)\n        # map the img_gen_random_transform function across images of the dataset\n        dataset = dataset.map(img_gen_random_transform, num_parallel_calls=AUTO)  # didn't work\n\n    dataset = dataset.repeat() # the training dataset must repeat for several epochs\n    dataset = dataset.shuffle(buffer_size=1920)\n    dataset = dataset.batch(BATCH_SIZE)\n\n    if augmentation:\n        # apply data augmentation preprocessing layers in batch of images\n        dataset = dataset.map(lambda image, y: (data_aug_layers(image, training=True), y))\n\n    dataset = dataset.prefetch(AUTO)  # prefetch next batch while training\n    return dataset\n</code></pre>",
  "messages": [
    {
      "id": 1212983,
      "postDate": "2021-02-21T18:42:01.560Z",
      "content": "<p>I need help to use random_transform method from ImageDataGenerator for image augmentation for training images to train my image classification model.  Here is <a href=\"https://www.kaggle.com/saukha/petals-to-the-metals-flower-classification\" target=\"_blank\">my notebook</a>  Help and comments will be very much appreciated!</p>\n<p>I have created an image generator <strong>img_gen</strong> and tried to use the random_transform method to transform single images as inputs.  (See A below)  But I ran into errors in both situations:</p>\n<p>a) use the random_transform method in the <strong>data_augment(image,  label)</strong> function where I also use tf.image(…) and tfa.image(…) to augment single images, (See B below) or </p>\n<p>b) create the <strong>img_gen_random_transform(image, label)</strong> function with the <strong>img_gen.random_transform</strong> method <code>image = img_gen.random_transform(image)</code> and then use the function in the <strong>get_training_dataset(filenames, augmentation=False)</strong> function in this line.  <br>\n<code>dataset = dataset.map(img_gen_random_transform, num_parallel_calls=AUTO)</code> <br>\n (See C below)   </p>\n<h1>A</h1>\n<pre><code>#### create an ImageDataGenerator for random transformation\nimg_gen = tf.keras.preprocessing.image.ImageDataGenerator(\n    rotation_range=54, width_shift_range=0.15, height_shift_range=0.15,\n    brightness_range=None, zoom_range=[1.0, 1.25], fill_mode='constant', \n    horizontal_flip=True, preprocessing_function=None)\n\n#### random_transform method in a function\ndef img_gen_random_transform(image, label):\n    # apply random_transform method to single image\n    image = img_gen.random_transform(image)\n    return image, label\n</code></pre>\n<h1>B</h1>\n<pre><code>#### define data augmentation function,                  \ndef data_augment(image,  label):  # one image at a time \n\n    ######### using tf.image on one single image\n    image = tf.image.random_contrast(image, 0.90, 0.99)\n    image = tf.image.random_brightness(image, 0.05) \n    #image = tf.image.random_saturation(image, 0.90, 0.99)\n    ######### using ImageDataGenerator random_transform method on one single image    \n    #mage = img_gen.random_transform(image)  # didn't work\n    ######### using tfa.image on one single image\n    rdn = tf.random.normal([1], mean=0, stddev=1, dtype=tf.float32) \n    if rdn &gt; 2.0:  # blur 2.5% of the images (1 tail, 2 stddev above mean)\n        image = tfa.image.mean_filter2d(image, filter_shape = 3,\n                                   padding='constant')    \nreturn image, label\n</code></pre>\n<h1>C</h1>\n<pre><code>#### get training datatset with augmentation option\ndef get_training_dataset(filenames, augmentation=False):\n    dataset = load_dataset(filenames, labeled=True, ordered=False)\n    if augmentation:\n        # map the data_augment function across images of dthe ataset \n        dataset = dataset.map(data_augment, num_parallel_calls=AUTO)\n        # map the img_gen_random_transform function across images of the dataset\n        dataset = dataset.map(img_gen_random_transform, num_parallel_calls=AUTO)  # didn't work\n\n    dataset = dataset.repeat() # the training dataset must repeat for several epochs\n    dataset = dataset.shuffle(buffer_size=1920)\n    dataset = dataset.batch(BATCH_SIZE)\n\n    if augmentation:\n        # apply data augmentation preprocessing layers in batch of images\n        dataset = dataset.map(lambda image, y: (data_aug_layers(image, training=True), y))\n\n    dataset = dataset.prefetch(AUTO)  # prefetch next batch while training\n    return dataset\n</code></pre>",
      "rawMarkdown": "I need help to use random_transform method from ImageDataGenerator for image augmentation for training images to train my image classification model.  Here is [my notebook](https://www.kaggle.com/saukha/petals-to-the-metals-flower-classification)  Help and comments will be very much appreciated!\n\nI have created an image generator **img_gen** and tried to use the random_transform method to transform single images as inputs.  (See A below)  But I ran into errors in both situations:\n\na) use the random_transform method in the **data_augment(image,  label)** function where I also use tf.image(...) and tfa.image(...) to augment single images, (See B below) or \n\nb) create the **img_gen_random_transform(image, label)** function with the **img_gen.random_transform** method `image = img_gen.random_transform(image)` and then use the function in the **get_training_dataset(filenames, augmentation=False)** function in this line.  \n`dataset = dataset.map(img_gen_random_transform, num_parallel_calls=AUTO)` \n (See C below)   \n\n\n#  A   \n```\n#### create an ImageDataGenerator for random transformation\nimg_gen = tf.keras.preprocessing.image.ImageDataGenerator(\n    rotation_range=54, width_shift_range=0.15, height_shift_range=0.15,\n    brightness_range=None, zoom_range=[1.0, 1.25], fill_mode='constant', \n    horizontal_flip=True, preprocessing_function=None)\n\n#### random_transform method in a function\ndef img_gen_random_transform(image, label):\n    # apply random_transform method to single image\n    image = img_gen.random_transform(image)\n    return image, label\n```\n\n#   B    \n    #### define data augmentation function,                  \n    def data_augment(image,  label):  # one image at a time \n \n        ######### using tf.image on one single image\n        image = tf.image.random_contrast(image, 0.90, 0.99)\n        image = tf.image.random_brightness(image, 0.05) \n        #image = tf.image.random_saturation(image, 0.90, 0.99)\n        ######### using ImageDataGenerator random_transform method on one single image    \n        #mage = img_gen.random_transform(image)  # didn't work\n        ######### using tfa.image on one single image\n        rdn = tf.random.normal([1], mean=0, stddev=1, dtype=tf.float32) \n        if rdn > 2.0:  # blur 2.5% of the images (1 tail, 2 stddev above mean)\n            image = tfa.image.mean_filter2d(image, filter_shape = 3,\n                                       padding='constant')    \n    return image, label\n\n#  C   \n```\n#### get training datatset with augmentation option\ndef get_training_dataset(filenames, augmentation=False):\n    dataset = load_dataset(filenames, labeled=True, ordered=False)\n    if augmentation:\n        # map the data_augment function across images of dthe ataset \n        dataset = dataset.map(data_augment, num_parallel_calls=AUTO)\n        # map the img_gen_random_transform function across images of the dataset\n        dataset = dataset.map(img_gen_random_transform, num_parallel_calls=AUTO)  # didn't work\n    \n    dataset = dataset.repeat() # the training dataset must repeat for several epochs\n    dataset = dataset.shuffle(buffer_size=1920)\n    dataset = dataset.batch(BATCH_SIZE)\n    \n    if augmentation:\n        # apply data augmentation preprocessing layers in batch of images\n        dataset = dataset.map(lambda image, y: (data_aug_layers(image, training=True), y))\n        \n    dataset = dataset.prefetch(AUTO)  # prefetch next batch while training\n    return dataset\n```",
      "votes": 4
    },
    {
      "id": 1501112,
      "postDate": "2021-09-03T00:03:45.743Z",
      "content": "<p>Try making the augmentation part of the model. <br>\nIt should be able to work well. </p>",
      "rawMarkdown": "Try making the augmentation part of the model. \nIt should be able to work well. "
    }
  ],
  "comments": [
    {
      "id": 1501112,
      "author_name": "samu2505",
      "author_url": "",
      "post_date": "2021-09-03T00:03:45.743000",
      "content": "<p>Try making the augmentation part of the model. <br>\nIt should be able to work well. </p>",
      "votes": 0,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "1212983": "I need help to use random_transform method from ImageDataGenerator for image augmentation for training images to train my image classification model.  Here is [my notebook](https://www.kaggle.com/saukha/petals-to-the-metals-flower-classification)  Help and comments will be very much appreciated!\n\nI have created an image generator **img_gen** and tried to use the random_transform method to transform single images as inputs.  (See A below)  But I ran into errors in both situations:\n\na) use the random_transform method in the **data_augment(image,  label)** function where I also use tf.image(...) and tfa.image(...) to augment single images, (See B below) or \n\nb) create the **img_gen_random_transform(image, label)** function with the **img_gen.random_transform** method `image = img_gen.random_transform(image)` and then use the function in the **get_training_dataset(filenames, augmentation=False)** function in this line.  \n`dataset = dataset.map(img_gen_random_transform, num_parallel_calls=AUTO)` \n (See C below)   \n\n\n#  A   \n```\n#### create an ImageDataGenerator for random transformation\nimg_gen = tf.keras.preprocessing.image.ImageDataGenerator(\n    rotation_range=54, width_shift_range=0.15, height_shift_range=0.15,\n    brightness_range=None, zoom_range=[1.0, 1.25], fill_mode='constant', \n    horizontal_flip=True, preprocessing_function=None)\n\n#### random_transform method in a function\ndef img_gen_random_transform(image, label):\n    # apply random_transform method to single image\n    image = img_gen.random_transform(image)\n    return image, label\n```\n\n#   B    \n    #### define data augmentation function,                  \n    def data_augment(image,  label):  # one image at a time \n \n        ######### using tf.image on one single image\n        image = tf.image.random_contrast(image, 0.90, 0.99)\n        image = tf.image.random_brightness(image, 0.05) \n        #image = tf.image.random_saturation(image, 0.90, 0.99)\n        ######### using ImageDataGenerator random_transform method on one single image    \n        #mage = img_gen.random_transform(image)  # didn't work\n        ######### using tfa.image on one single image\n        rdn = tf.random.normal([1], mean=0, stddev=1, dtype=tf.float32) \n        if rdn > 2.0:  # blur 2.5% of the images (1 tail, 2 stddev above mean)\n            image = tfa.image.mean_filter2d(image, filter_shape = 3,\n                                       padding='constant')    \n    return image, label\n\n#  C   \n```\n#### get training datatset with augmentation option\ndef get_training_dataset(filenames, augmentation=False):\n    dataset = load_dataset(filenames, labeled=True, ordered=False)\n    if augmentation:\n        # map the data_augment function across images of dthe ataset \n        dataset = dataset.map(data_augment, num_parallel_calls=AUTO)\n        # map the img_gen_random_transform function across images of the dataset\n        dataset = dataset.map(img_gen_random_transform, num_parallel_calls=AUTO)  # didn't work\n    \n    dataset = dataset.repeat() # the training dataset must repeat for several epochs\n    dataset = dataset.shuffle(buffer_size=1920)\n    dataset = dataset.batch(BATCH_SIZE)\n    \n    if augmentation:\n        # apply data augmentation preprocessing layers in batch of images\n        dataset = dataset.map(lambda image, y: (data_aug_layers(image, training=True), y))\n        \n    dataset = dataset.prefetch(AUTO)  # prefetch next batch while training\n    return dataset\n```",
    "1501112": "Try making the augmentation part of the model. \nIt should be able to work well. "
  }
}