{
  "id": 130187,
  "title": "How to do cutmix or mixup ?",
  "url": "/competitions/flower-classification-with-tpus/discussion/130187",
  "author_name": "",
  "post_date": "2020-02-12T16:11:26.897714500Z",
  "votes": 4,
  "comment_count": 7,
  "views": 0,
  "content": "",
  "messages": [
    {
      "id": "744160",
      "postDate": "02/12/2020 16:11:26",
      "content": "",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "744307",
      "postDate": "02/12/2020 18:09:20",
      "content": "<p>Good question. The starter code uses a TensorFlow dataset, API <a href=\"https://www.tensorflow.org/api_docs/python/tf/data/Dataset\">here</a>. You can call the map function on your dataset and write routines to perform augmentation. However I think the map function gets called on each image individually so it wouldn't work for CutMix where you need to create new Images/Labels from 2 Images/Labels.</p>\n\n<p>I'm reading about this now and will report if I figure an efficient way to do CutMix on TensorFlow dataset.</p>",
      "rawMarkdown": "Good question. The starter code uses a TensorFlow dataset, API [here][1]. You can call the map function on your dataset and write routines to perform augmentation. However I think the map function gets called on each image individually so it wouldn't work for CutMix where you need to create new Images/Labels from 2 Images/Labels.\n\nI'm reading about this now and will report if I figure an efficient way to do CutMix on TensorFlow dataset.\n\n[1]: https://www.tensorflow.org/api_docs/python/tf/data/Dataset",
      "votes": null
    },
    {
      "id": "744308",
      "postDate": "02/12/2020 18:10:32",
      "content": "<p>If you want other simple augmentation, it's easy. Just add TensorFlow calls inside the starter codes function <code>data_augment</code> which gets called with <code>dataset.map()</code> already.</p>",
      "rawMarkdown": "If you want other simple augmentation, it's easy. Just add TensorFlow calls inside the starter codes function `data_augment` which gets called with `dataset.map()` already.",
      "votes": null
    },
    {
      "id": "744342",
      "postDate": "02/12/2020 18:52:00",
      "content": "<p>The map function is mapped on either single or multiple elements if you have already batched the dataset.</p>",
      "rawMarkdown": "The map function is mapped on either single or multiple elements if you have already batched the dataset.",
      "votes": null
    },
    {
      "id": "744344",
      "postDate": "02/12/2020 18:54:27",
      "content": "<p>This is a great idea by the way! Let us know how it works.</p>",
      "rawMarkdown": "This is a great idea by the way! Let us know how it works.",
      "votes": null
    },
    {
      "id": "744555",
      "postDate": "02/12/2020 23:40:31",
      "content": "<p>Ah yes, i forgot that, thanks for pointing it out Martin. What Martin is saying is as follows. If you call <code>map()</code> before <code>batch()</code> then your map function receives a single image and if you call <code>map()</code> after <code>batch()</code> then your function receives a batch of images.</p>\n\n<pre><code># EXAMPLE OF MAP BEFORE BATCH\ndataset = dataset.map(data_augment, num_parallel_calls=AUTO)\ndataset = dataset.batch(BATCH_SIZE)\n</code></pre>\n\n<p>Then inside your augment function, you can check whether you received a single image or batch of images by checking the input's dimensions:</p>\n\n<pre><code>@tf.function\ndef transformation(data):\n    if image.shape.__len__() ==4:\n        # WE HAVE BATCH\n    if image.shape.__len__() ==3:\n        # WE HAVE SINGLE IMAGES\n    return data\n</code></pre>",
      "rawMarkdown": "Ah yes, i forgot that, thanks for pointing it out Martin. What Martin is saying is as follows. If you call `map()` before `batch()` then your map function receives a single image and if you call `map()` after `batch()` then your function receives a batch of images.\n\n    # EXAMPLE OF MAP BEFORE BATCH\n    dataset = dataset.map(data_augment, num_parallel_calls=AUTO)\n    dataset = dataset.batch(BATCH_SIZE)\n\nThen inside your augment function, you can check whether you received a single image or batch of images by checking the input's dimensions:\n\n    @tf.function\n    def transformation(data):\n        if image.shape.__len__() ==4:\n            # WE HAVE BATCH\n        if image.shape.__len__() ==3:\n            # WE HAVE SINGLE IMAGES\n        return data",
      "votes": null
    },
    {
      "id": "744602",
      "postDate": "02/13/2020 01:10:55",
      "content": "<p>Yes exactly and if you want to mix multiple images into one you can do something like this:\n<code>\ndataset = dataset.batch(2)\ndataset = dataset.map(my_mix) # your own mixing function, returns individual images again\ndataset = dataset.batch(BATCH_SIZE)\n</code></p>",
      "rawMarkdown": "Yes exactly and if you want to mix multiple images into one you can do something like this:\n```\ndataset = dataset.batch(2)\ndataset = dataset.map(my_mix) # your own mixing function, returns individual images again\ndataset = dataset.batch(BATCH_SIZE)\n```",
      "votes": null
    },
    {
      "id": "861639",
      "postDate": "05/26/2020 06:22:12",
      "content": "<p>I am using your method.It can work.Suddenly it throw an exception <code>tensorflow.python.framework.errors_impl.InvalidArgumentError: Index out of range using input dim 0; input has only 0 dims [Op:StridedSlice] name: strided_slice/</code> and <code>distributed_function -&gt; distributed_function</code>\nI need 4 picture to merge 1 image.I think maybe i have 15 picture, when I getting the last 3 picture, program can't split.So How do you solve?</p>",
      "rawMarkdown": "I am using your method.It can work.Suddenly it throw an exception `tensorflow.python.framework.errors_impl.InvalidArgumentError: Index out of range using input dim 0; input has only 0 dims [Op:StridedSlice] name: strided_slice/` and `distributed_function -&gt; distributed_function`\nI need 4 picture to merge 1 image.I think maybe i have 15 picture, when I getting the last 3 picture, program can't split.So How do you solve?",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 744307,
      "author_name": "cdeotte",
      "author_url": "",
      "post_date": "02/12/2020 18:09:20",
      "content": "<p>Good question. The starter code uses a TensorFlow dataset, API <a href=\"https://www.tensorflow.org/api_docs/python/tf/data/Dataset\">here</a>. You can call the map function on your dataset and write routines to perform augmentation. However I think the map function gets called on each image individually so it wouldn't work for CutMix where you need to create new Images/Labels from 2 Images/Labels.</p>\n\n<p>I'm reading about this now and will report if I figure an efficient way to do CutMix on TensorFlow dataset.</p>",
      "votes": null,
      "replies": [
        {
          "id": 744308,
          "author_name": "cdeotte",
          "author_url": "",
          "post_date": "02/12/2020 18:10:32",
          "content": "<p>If you want other simple augmentation, it's easy. Just add TensorFlow calls inside the starter codes function <code>data_augment</code> which gets called with <code>dataset.map()</code> already.</p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 744342,
          "author_name": "mgorner",
          "author_url": "",
          "post_date": "02/12/2020 18:52:00",
          "content": "<p>The map function is mapped on either single or multiple elements if you have already batched the dataset.</p>",
          "votes": null,
          "replies": []
        },
        {
          "id": 744555,
          "author_name": "cdeotte",
          "author_url": "",
          "post_date": "02/12/2020 23:40:31",
          "content": "<p>Ah yes, i forgot that, thanks for pointing it out Martin. What Martin is saying is as follows. If you call <code>map()</code> before <code>batch()</code> then your map function receives a single image and if you call <code>map()</code> after <code>batch()</code> then your function receives a batch of images.</p>\n\n<pre><code># EXAMPLE OF MAP BEFORE BATCH\ndataset = dataset.map(data_augment, num_parallel_calls=AUTO)\ndataset = dataset.batch(BATCH_SIZE)\n</code></pre>\n\n<p>Then inside your augment function, you can check whether you received a single image or batch of images by checking the input's dimensions:</p>\n\n<pre><code>@tf.function\ndef transformation(data):\n    if image.shape.__len__() ==4:\n        # WE HAVE BATCH\n    if image.shape.__len__() ==3:\n        # WE HAVE SINGLE IMAGES\n    return data\n</code></pre>",
          "votes": null,
          "replies": []
        },
        {
          "id": 744602,
          "author_name": "mgorner",
          "author_url": "",
          "post_date": "02/13/2020 01:10:55",
          "content": "<p>Yes exactly and if you want to mix multiple images into one you can do something like this:\n<code>\ndataset = dataset.batch(2)\ndataset = dataset.map(my_mix) # your own mixing function, returns individual images again\ndataset = dataset.batch(BATCH_SIZE)\n</code></p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 744344,
      "author_name": "mgorner",
      "author_url": "",
      "post_date": "02/12/2020 18:54:27",
      "content": "<p>This is a great idea by the way! Let us know how it works.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 861639,
      "author_name": "footrunist",
      "author_url": "",
      "post_date": "05/26/2020 06:22:12",
      "content": "<p>I am using your method.It can work.Suddenly it throw an exception <code>tensorflow.python.framework.errors_impl.InvalidArgumentError: Index out of range using input dim 0; input has only 0 dims [Op:StridedSlice] name: strided_slice/</code> and <code>distributed_function -&gt; distributed_function</code>\nI need 4 picture to merge 1 image.I think maybe i have 15 picture, when I getting the last 3 picture, program can't split.So How do you solve?</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "744160": "",
    "744307": "Good question. The starter code uses a TensorFlow dataset, API [here][1]. You can call the map function on your dataset and write routines to perform augmentation. However I think the map function gets called on each image individually so it wouldn't work for CutMix where you need to create new Images/Labels from 2 Images/Labels.\n\nI'm reading about this now and will report if I figure an efficient way to do CutMix on TensorFlow dataset.\n\n[1]: https://www.tensorflow.org/api_docs/python/tf/data/Dataset",
    "744308": "If you want other simple augmentation, it's easy. Just add TensorFlow calls inside the starter codes function `data_augment` which gets called with `dataset.map()` already.",
    "744342": "The map function is mapped on either single or multiple elements if you have already batched the dataset.",
    "744344": "This is a great idea by the way! Let us know how it works.",
    "744555": "Ah yes, i forgot that, thanks for pointing it out Martin. What Martin is saying is as follows. If you call `map()` before `batch()` then your map function receives a single image and if you call `map()` after `batch()` then your function receives a batch of images.\n\n    # EXAMPLE OF MAP BEFORE BATCH\n    dataset = dataset.map(data_augment, num_parallel_calls=AUTO)\n    dataset = dataset.batch(BATCH_SIZE)\n\nThen inside your augment function, you can check whether you received a single image or batch of images by checking the input's dimensions:\n\n    @tf.function\n    def transformation(data):\n        if image.shape.__len__() ==4:\n            # WE HAVE BATCH\n        if image.shape.__len__() ==3:\n            # WE HAVE SINGLE IMAGES\n        return data",
    "744602": "Yes exactly and if you want to mix multiple images into one you can do something like this:\n```\ndataset = dataset.batch(2)\ndataset = dataset.map(my_mix) # your own mixing function, returns individual images again\ndataset = dataset.batch(BATCH_SIZE)\n```",
    "861639": "I am using your method.It can work.Suddenly it throw an exception `tensorflow.python.framework.errors_impl.InvalidArgumentError: Index out of range using input dim 0; input has only 0 dims [Op:StridedSlice] name: strided_slice/` and `distributed_function -&gt; distributed_function`\nI need 4 picture to merge 1 image.I think maybe i have 15 picture, when I getting the last 3 picture, program can't split.So How do you solve?"
  },
  "source": "meta"
}