{
  "id": 240790,
  "title": "How can i implement Mixup In Tensorflow",
  "url": "/competitions/seti-breakthrough-listen/discussion/240790",
  "author_name": "Mithil Salunkhe",
  "post_date": "2021-05-21T11:45:08.662000",
  "votes": 3,
  "comment_count": 6,
  "views": 0,
  "content": "<p>All the notebook That implement Mixup are from Pytorch. Can someone Help me out In implementing Mixup in Tensor flow ? Any help would be greatly appreciated </p>",
  "messages": [
    {
      "id": 1317442,
      "postDate": "2021-05-21T12:16:30.617Z",
      "content": "<p>Hello. It's so nice to meet you.<br>\nI'm not sure about TensorFlow, but we need to apply the steps below.</p>\n<ol>\n<li><p>define mixup function with usual python language.<br>\nx &lt;- original inputs (have items as many as batch size)<br>\nindex &lt;- random permutation of x index<br>\ny = x[index]  &lt;- permutation of x.<br>\nz = alpha * x + (1-alpha) * y &lt;- some input between (original inputs, permutation of x)<br>\nnew_loss = alpha<em>loss(predict_x) + (1-alpha)</em>loss(predict_y)</p></li>\n<li><p>define tensorflow dataset with batchsize</p></li>\n<li><p>mapping dataset to new dataset using mixup function</p></li>\n</ol>",
      "rawMarkdown": "Hello. It's so nice to meet you.\nI'm not sure about TensorFlow, but we need to apply the steps below.\n\n1. define mixup function with usual python language.\nx <- original inputs (have items as many as batch size)\nindex <- random permutation of x index\ny = x[index]  <- permutation of x.\nz = alpha * x + (1-alpha) * y <- some input between (original inputs, permutation of x)\nnew_loss = alpha*loss(predict_x) + (1-alpha)*loss(predict_y)\n\n2. define tensorflow dataset with batchsize\n\n3. mapping dataset to new dataset using mixup function",
      "votes": 3
    },
    {
      "id": 1317403,
      "postDate": "2021-05-21T11:45:08.663Z",
      "content": "<p>All the notebook That implement Mixup are from Pytorch. Can someone Help me out In implementing Mixup in Tensor flow ? Any help would be greatly appreciated </p>",
      "rawMarkdown": "All the notebook That implement Mixup are from Pytorch. Can someone Help me out In implementing Mixup in Tensor flow ? Any help would be greatly appreciated ",
      "votes": 3
    },
    {
      "id": 1317713,
      "postDate": "2021-05-21T16:21:44.503Z",
      "content": "<p>You can also look in <a href=\"https://keras.io/examples/vision/mixup/\" target=\"_blank\">MixUp augmentation for image classification</a> in Keras documentation.</p>",
      "rawMarkdown": "You can also look in [MixUp augmentation for image classification](https://keras.io/examples/vision/mixup/) in Keras documentation.",
      "votes": 1
    },
    {
      "id": 1318150,
      "postDate": "2021-05-22T04:01:22.890Z",
      "content": "<p>For those who are using  the notebook <a href=\"https://www.kaggle.com/micheomaano\" target=\"_blank\">@micheomaano</a> shared  make sure to change your metrics from keras.metrics.AUC() to tf.keras.metrics.BinaryAccuracy</p>",
      "rawMarkdown": "For those who are using  the notebook @micheomaano shared  make sure to change your metrics from keras.metrics.AUC() to tf.keras.metrics.BinaryAccuracy\n",
      "votes": 2
    },
    {
      "id": 1317428,
      "postDate": "2021-05-21T12:06:25.950Z",
      "content": "<p>Here you go. <a href=\"https://www.kaggle.com/cdeotte/cutmix-and-mixup-on-gpu-tpu\" target=\"_blank\">https://www.kaggle.com/cdeotte/cutmix-and-mixup-on-gpu-tpu</a></p>",
      "rawMarkdown": "Here you go. [https://www.kaggle.com/cdeotte/cutmix-and-mixup-on-gpu-tpu](https://www.kaggle.com/cdeotte/cutmix-and-mixup-on-gpu-tpu)",
      "votes": 2,
      "replies": [
        {
          "id": 1318149,
          "postDate": "2021-05-22T03:59:37.547Z",
          "content": "<p>Thank You very much</p>",
          "rawMarkdown": "Thank You very much\n"
        }
      ]
    },
    {
      "id": 1318557,
      "postDate": "2021-05-22T12:02:34.137Z",
      "content": "<p>Here's a blog post that I have written: <a href=\"https://wandb.ai/authors/tfaugmentation/reports/Modern-Data-Augmentation-Techniques-for-Computer-Vision--VmlldzoxNzU3NTU\" target=\"_blank\">https://wandb.ai/authors/tfaugmentation/reports/Modern-Data-Augmentation-Techniques-for-Computer-Vision--VmlldzoxNzU3NTU</a></p>\n<p>Code snippet:</p>\n<pre><code>def mixup(a, b):\n\n  alpha = [0.2]\n  beta = [0.2]\n\n  # unpack (image, label) pairs\n  (image1, label1), (image2, label2) = a, b\n\n  # define beta distribution\n  dist = tfd.Beta(alpha, beta)\n  # sample from this distribution\n  l = dist.sample(1)[0][0]\n\n  # mixup augmentation\n  img = l*image1+(1-l)*image2\n  lab = l*label1+(1-l)*label2\n\n  return img, lab\n\n\ntrainloader1 = tf.data.Dataset.from_tensor_slices((x_train, y_train)).shuffle(1024).map(preprocess_image, num_parallel_calls=AUTO)\ntrainloader2 = tf.data.Dataset.from_tensor_slices((x_train, y_train)).shuffle(1024).map(preprocess_image, num_parallel_calls=AUTO)\n\ntrainloader = tf.data.Dataset.zip((trainloader1, trainloader2))\ntrainloader = (\n    trainloader\n    .shuffle(1024)\n    .map(mixup, num_parallel_calls=AUTO)\n    .batch(BATCH_SIZE)\n    .prefetch(AUTO)\n)\n</code></pre>",
      "rawMarkdown": "Here's a blog post that I have written: https://wandb.ai/authors/tfaugmentation/reports/Modern-Data-Augmentation-Techniques-for-Computer-Vision--VmlldzoxNzU3NTU\n\nCode snippet:\n\n```\ndef mixup(a, b):\n \n  alpha = [0.2]\n  beta = [0.2]\n \n  # unpack (image, label) pairs\n  (image1, label1), (image2, label2) = a, b\n\n  # define beta distribution\n  dist = tfd.Beta(alpha, beta)\n  # sample from this distribution\n  l = dist.sample(1)[0][0]\n  \n  # mixup augmentation\n  img = l*image1+(1-l)*image2\n  lab = l*label1+(1-l)*label2\n\n  return img, lab\n\n\ntrainloader1 = tf.data.Dataset.from_tensor_slices((x_train, y_train)).shuffle(1024).map(preprocess_image, num_parallel_calls=AUTO)\ntrainloader2 = tf.data.Dataset.from_tensor_slices((x_train, y_train)).shuffle(1024).map(preprocess_image, num_parallel_calls=AUTO)\n\ntrainloader = tf.data.Dataset.zip((trainloader1, trainloader2))\ntrainloader = (\n    trainloader\n    .shuffle(1024)\n    .map(mixup, num_parallel_calls=AUTO)\n    .batch(BATCH_SIZE)\n    .prefetch(AUTO)\n)\n```"
    }
  ],
  "comments": [
    {
      "id": 1317442,
      "author_name": "WOOSUNG YOON",
      "author_url": "",
      "post_date": "2021-05-21T12:16:30.617000",
      "content": "<p>Hello. It's so nice to meet you.<br>\nI'm not sure about TensorFlow, but we need to apply the steps below.</p>\n<ol>\n<li><p>define mixup function with usual python language.<br>\nx &lt;- original inputs (have items as many as batch size)<br>\nindex &lt;- random permutation of x index<br>\ny = x[index]  &lt;- permutation of x.<br>\nz = alpha * x + (1-alpha) * y &lt;- some input between (original inputs, permutation of x)<br>\nnew_loss = alpha<em>loss(predict_x) + (1-alpha)</em>loss(predict_y)</p></li>\n<li><p>define tensorflow dataset with batchsize</p></li>\n<li><p>mapping dataset to new dataset using mixup function</p></li>\n</ol>",
      "votes": 3,
      "replies": []
    },
    {
      "id": 1317713,
      "author_name": "Geir Drange",
      "author_url": "",
      "post_date": "2021-05-21T16:21:44.503000",
      "content": "<p>You can also look in <a href=\"https://keras.io/examples/vision/mixup/\" target=\"_blank\">MixUp augmentation for image classification</a> in Keras documentation.</p>",
      "votes": 1,
      "replies": []
    },
    {
      "id": 1318150,
      "author_name": "Mithil Salunkhe",
      "author_url": "",
      "post_date": "2021-05-22T04:01:22.890000",
      "content": "<p>For those who are using  the notebook <a href=\"https://www.kaggle.com/micheomaano\" target=\"_blank\">@micheomaano</a> shared  make sure to change your metrics from keras.metrics.AUC() to tf.keras.metrics.BinaryAccuracy</p>",
      "votes": 2,
      "replies": []
    },
    {
      "id": 1317428,
      "author_name": "Salman",
      "author_url": "",
      "post_date": "2021-05-21T12:06:25.950000",
      "content": "<p>Here you go. <a href=\"https://www.kaggle.com/cdeotte/cutmix-and-mixup-on-gpu-tpu\" target=\"_blank\">https://www.kaggle.com/cdeotte/cutmix-and-mixup-on-gpu-tpu</a></p>",
      "votes": 2,
      "replies": [
        {
          "id": 1318149,
          "author_name": "Mithil Salunkhe",
          "author_url": "",
          "post_date": "2021-05-22T03:59:37.547000",
          "content": "<p>Thank You very much</p>",
          "votes": 0,
          "replies": []
        }
      ]
    },
    {
      "id": 1318557,
      "author_name": "Ayush Thakur",
      "author_url": "",
      "post_date": "2021-05-22T12:02:34.137000",
      "content": "<p>Here's a blog post that I have written: <a href=\"https://wandb.ai/authors/tfaugmentation/reports/Modern-Data-Augmentation-Techniques-for-Computer-Vision--VmlldzoxNzU3NTU\" target=\"_blank\">https://wandb.ai/authors/tfaugmentation/reports/Modern-Data-Augmentation-Techniques-for-Computer-Vision--VmlldzoxNzU3NTU</a></p>\n<p>Code snippet:</p>\n<pre><code>def mixup(a, b):\n\n  alpha = [0.2]\n  beta = [0.2]\n\n  # unpack (image, label) pairs\n  (image1, label1), (image2, label2) = a, b\n\n  # define beta distribution\n  dist = tfd.Beta(alpha, beta)\n  # sample from this distribution\n  l = dist.sample(1)[0][0]\n\n  # mixup augmentation\n  img = l*image1+(1-l)*image2\n  lab = l*label1+(1-l)*label2\n\n  return img, lab\n\n\ntrainloader1 = tf.data.Dataset.from_tensor_slices((x_train, y_train)).shuffle(1024).map(preprocess_image, num_parallel_calls=AUTO)\ntrainloader2 = tf.data.Dataset.from_tensor_slices((x_train, y_train)).shuffle(1024).map(preprocess_image, num_parallel_calls=AUTO)\n\ntrainloader = tf.data.Dataset.zip((trainloader1, trainloader2))\ntrainloader = (\n    trainloader\n    .shuffle(1024)\n    .map(mixup, num_parallel_calls=AUTO)\n    .batch(BATCH_SIZE)\n    .prefetch(AUTO)\n)\n</code></pre>",
      "votes": 0,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "1317442": "Hello. It's so nice to meet you.\nI'm not sure about TensorFlow, but we need to apply the steps below.\n\n1. define mixup function with usual python language.\nx <- original inputs (have items as many as batch size)\nindex <- random permutation of x index\ny = x[index]  <- permutation of x.\nz = alpha * x + (1-alpha) * y <- some input between (original inputs, permutation of x)\nnew_loss = alpha*loss(predict_x) + (1-alpha)*loss(predict_y)\n\n2. define tensorflow dataset with batchsize\n\n3. mapping dataset to new dataset using mixup function",
    "1317403": "All the notebook That implement Mixup are from Pytorch. Can someone Help me out In implementing Mixup in Tensor flow ? Any help would be greatly appreciated ",
    "1317713": "You can also look in [MixUp augmentation for image classification](https://keras.io/examples/vision/mixup/) in Keras documentation.",
    "1318150": "For those who are using  the notebook @micheomaano shared  make sure to change your metrics from keras.metrics.AUC() to tf.keras.metrics.BinaryAccuracy\n",
    "1317428": "Here you go. [https://www.kaggle.com/cdeotte/cutmix-and-mixup-on-gpu-tpu](https://www.kaggle.com/cdeotte/cutmix-and-mixup-on-gpu-tpu)",
    "1318557": "Here's a blog post that I have written: https://wandb.ai/authors/tfaugmentation/reports/Modern-Data-Augmentation-Techniques-for-Computer-Vision--VmlldzoxNzU3NTU\n\nCode snippet:\n\n```\ndef mixup(a, b):\n \n  alpha = [0.2]\n  beta = [0.2]\n \n  # unpack (image, label) pairs\n  (image1, label1), (image2, label2) = a, b\n\n  # define beta distribution\n  dist = tfd.Beta(alpha, beta)\n  # sample from this distribution\n  l = dist.sample(1)[0][0]\n  \n  # mixup augmentation\n  img = l*image1+(1-l)*image2\n  lab = l*label1+(1-l)*label2\n\n  return img, lab\n\n\ntrainloader1 = tf.data.Dataset.from_tensor_slices((x_train, y_train)).shuffle(1024).map(preprocess_image, num_parallel_calls=AUTO)\ntrainloader2 = tf.data.Dataset.from_tensor_slices((x_train, y_train)).shuffle(1024).map(preprocess_image, num_parallel_calls=AUTO)\n\ntrainloader = tf.data.Dataset.zip((trainloader1, trainloader2))\ntrainloader = (\n    trainloader\n    .shuffle(1024)\n    .map(mixup, num_parallel_calls=AUTO)\n    .batch(BATCH_SIZE)\n    .prefetch(AUTO)\n)\n```"
  }
}