{
  "id": 221362,
  "title": "SnapMix with Tensorflow: 1st Implementation",
  "url": "/competitions/cassava-leaf-disease-classification/discussion/221362",
  "author_name": "",
  "post_date": "2021-02-22T12:32:55.178960800Z",
  "votes": 12,
  "comment_count": 1,
  "views": 0,
  "content": "<p>Hello guys,<br>\nAs I have noticed in some discussions like <a href=\"https://www.kaggle.com/c/cassava-leaf-disease-classification/discussion/204631\" target=\"_blank\">1</a> <a href=\"https://www.kaggle.com/c/cassava-leaf-disease-classification/discussion/202627\" target=\"_blank\">2</a> <a href=\"https://www.kaggle.com/c/cassava-leaf-disease-classification/discussion/205630\" target=\"_blank\">3</a> and Kernels like <a href=\"https://www.kaggle.com/shaolihuang/training-with-snapmix/notebook\" target=\"_blank\">4</a> that <strong>SnapMix</strong> Augmentation has a nice improvement in the performance, so I have decided to apply it with Tensorflow, as all the implementations I have <a href=\"https://github.com/Shaoli-Huang/SnapMix\" target=\"_blank\">found</a> were in Pytorch.<br>\nYou can find the notebook <a href=\"https://colab.research.google.com/drive/14_Mhw3ATNyrXvwhDK8PdLSGbOgvJ9qzx?usp=sharing\" target=\"_blank\">here</a></p>\n<p>I wrote 2 versions of it</p>\n<ol>\n<li><strong>GPU</strong> version: works with the normal pipeline of <em>loading images</em> then use augmentations (like Keras ones)</li>\n<li><strong>GPU/TPU</strong> version: works with <strong>TFRecords</strong> <em>data type</em>, and I built it to be <em>easy to use</em> like the <strong>CutMix/MixUp</strong> augmentations in this <a href=\"https://www.kaggle.com/itsuki9180/efficientnet-and-cutmixup-with-tpu-train-phase#CutMix-and-MixUp-Augmentation\" target=\"_blank\">kernel</a>, so you can simply add it in the <code>def transform(image, label)</code> function and use it like other augmentations there.</li>\n</ol>\n<p>I will share a notebook that demonstrates using the TPU version so soon.</p>\n<hr>\n<p>One problem I faced with the <strong>TPU</strong> version, is that when apply <code>dataset.map(snapmix)</code>, it runs in \"<em>disabled eager execution mode</em>\" and <code>[conv_outputs, predictions] = get_output(x)</code> raise an <em>error</em>, as the <code>map()</code> method runs in \"<em>graph mode</em>\" and then I can't return <strong>Numpy</strong> values.<br>\nI tried deifferent solutions like using <code>tf.py_function(func=snapmix, inp=[image, label, model, ...], Tout=[tf.float32,tf.float32])</code> but couldn't reach to a solution that work.<br>\nSo if you can edit the <strong>TPU</strong> version and make it run, that would be interesting😃.</p>",
  "messages": [
    {
      "id": "1213907",
      "postDate": "02/22/2021 12:32:55",
      "content": "<p>Hello guys,<br>\nAs I have noticed in some discussions like <a href=\"https://www.kaggle.com/c/cassava-leaf-disease-classification/discussion/204631\" target=\"_blank\">1</a> <a href=\"https://www.kaggle.com/c/cassava-leaf-disease-classification/discussion/202627\" target=\"_blank\">2</a> <a href=\"https://www.kaggle.com/c/cassava-leaf-disease-classification/discussion/205630\" target=\"_blank\">3</a> and Kernels like <a href=\"https://www.kaggle.com/shaolihuang/training-with-snapmix/notebook\" target=\"_blank\">4</a> that <strong>SnapMix</strong> Augmentation has a nice improvement in the performance, so I have decided to apply it with Tensorflow, as all the implementations I have <a href=\"https://github.com/Shaoli-Huang/SnapMix\" target=\"_blank\">found</a> were in Pytorch.<br>\nYou can find the notebook <a href=\"https://colab.research.google.com/drive/14_Mhw3ATNyrXvwhDK8PdLSGbOgvJ9qzx?usp=sharing\" target=\"_blank\">here</a></p>\n<p>I wrote 2 versions of it</p>\n<ol>\n<li><strong>GPU</strong> version: works with the normal pipeline of <em>loading images</em> then use augmentations (like Keras ones)</li>\n<li><strong>GPU/TPU</strong> version: works with <strong>TFRecords</strong> <em>data type</em>, and I built it to be <em>easy to use</em> like the <strong>CutMix/MixUp</strong> augmentations in this <a href=\"https://www.kaggle.com/itsuki9180/efficientnet-and-cutmixup-with-tpu-train-phase#CutMix-and-MixUp-Augmentation\" target=\"_blank\">kernel</a>, so you can simply add it in the <code>def transform(image, label)</code> function and use it like other augmentations there.</li>\n</ol>\n<p>I will share a notebook that demonstrates using the TPU version so soon.</p>\n<hr>\n<p>One problem I faced with the <strong>TPU</strong> version, is that when apply <code>dataset.map(snapmix)</code>, it runs in \"<em>disabled eager execution mode</em>\" and <code>[conv_outputs, predictions] = get_output(x)</code> raise an <em>error</em>, as the <code>map()</code> method runs in \"<em>graph mode</em>\" and then I can't return <strong>Numpy</strong> values.<br>\nI tried deifferent solutions like using <code>tf.py_function(func=snapmix, inp=[image, label, model, ...], Tout=[tf.float32,tf.float32])</code> but couldn't reach to a solution that work.<br>\nSo if you can edit the <strong>TPU</strong> version and make it run, that would be interesting😃.</p>",
      "rawMarkdown": "Hello guys,\nAs I have noticed in some discussions like [1](https://www.kaggle.com/c/cassava-leaf-disease-classification/discussion/204631) [2](https://www.kaggle.com/c/cassava-leaf-disease-classification/discussion/202627) [3](https://www.kaggle.com/c/cassava-leaf-disease-classification/discussion/205630) and Kernels like [4](https://www.kaggle.com/shaolihuang/training-with-snapmix/notebook) that **SnapMix** Augmentation has a nice improvement in the performance, so I have decided to apply it with Tensorflow, as all the implementations I have [found](https://github.com/Shaoli-Huang/SnapMix) were in Pytorch.\nYou can find the notebook [here](https://colab.research.google.com/drive/14_Mhw3ATNyrXvwhDK8PdLSGbOgvJ9qzx?usp=sharing)\n\nI wrote 2 versions of it\n1. **GPU** version: works with the normal pipeline of *loading images* then use augmentations (like Keras ones)\n2. **GPU/TPU** version: works with **TFRecords** *data type*, and I built it to be *easy to use* like the **CutMix/MixUp** augmentations in this [kernel](https://www.kaggle.com/itsuki9180/efficientnet-and-cutmixup-with-tpu-train-phase#CutMix-and-MixUp-Augmentation), so you can simply add it in the `def transform(image, label)` function and use it like other augmentations there.\n\nI will share a notebook that demonstrates using the TPU version so soon.\n\n---\nOne problem I faced with the **TPU** version, is that when apply `dataset.map(snapmix)`, it runs in \"*disabled eager execution mode*\" and `[conv_outputs, predictions] = get_output(x)` raise an *error*, as the `map()` method runs in \"*graph mode*\" and then I can't return **Numpy** values.\nI tried deifferent solutions like using `tf.py_function(func=snapmix, inp=[image, label, model, ...], Tout=[tf.float32,tf.float32])` but couldn't reach to a solution that work.\nSo if you can edit the **TPU** version and make it run, that would be interesting😃.",
      "votes": null
    },
    {
      "id": "1213915",
      "postDate": "02/22/2021 12:49:33",
      "content": "<p><strong>Note</strong>: when I started (3 months ago) building the snapmix augmentation , I have not found any tensorflow implementation of it, all were in pytorch, so I decided to build it, and finished it early but I was busy to reorganize the notebook and share it, so while I was writing this discussion I found that someone was working (nearly the same period) on SnapMix too and have uploaded it a weeks ago in github, so feel free to check <a href=\"https://github.com/wangermeng2021/SnapMix-tensorflow2\" target=\"_blank\">it</a></p>",
      "rawMarkdown": "**Note**: when I started (3 months ago) building the snapmix augmentation , I have not found any tensorflow implementation of it, all were in pytorch, so I decided to build it, and finished it early but I was busy to reorganize the notebook and share it, so while I was writing this discussion I found that someone was working (nearly the same period) on SnapMix too and have uploaded it a weeks ago in github, so feel free to check [it](https://github.com/wangermeng2021/SnapMix-tensorflow2)",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 1213915,
      "author_name": "omarzaghlol",
      "author_url": "",
      "post_date": "02/22/2021 12:49:33",
      "content": "<p><strong>Note</strong>: when I started (3 months ago) building the snapmix augmentation , I have not found any tensorflow implementation of it, all were in pytorch, so I decided to build it, and finished it early but I was busy to reorganize the notebook and share it, so while I was writing this discussion I found that someone was working (nearly the same period) on SnapMix too and have uploaded it a weeks ago in github, so feel free to check <a href=\"https://github.com/wangermeng2021/SnapMix-tensorflow2\" target=\"_blank\">it</a></p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "1213907": "Hello guys,\nAs I have noticed in some discussions like [1](https://www.kaggle.com/c/cassava-leaf-disease-classification/discussion/204631) [2](https://www.kaggle.com/c/cassava-leaf-disease-classification/discussion/202627) [3](https://www.kaggle.com/c/cassava-leaf-disease-classification/discussion/205630) and Kernels like [4](https://www.kaggle.com/shaolihuang/training-with-snapmix/notebook) that **SnapMix** Augmentation has a nice improvement in the performance, so I have decided to apply it with Tensorflow, as all the implementations I have [found](https://github.com/Shaoli-Huang/SnapMix) were in Pytorch.\nYou can find the notebook [here](https://colab.research.google.com/drive/14_Mhw3ATNyrXvwhDK8PdLSGbOgvJ9qzx?usp=sharing)\n\nI wrote 2 versions of it\n1. **GPU** version: works with the normal pipeline of *loading images* then use augmentations (like Keras ones)\n2. **GPU/TPU** version: works with **TFRecords** *data type*, and I built it to be *easy to use* like the **CutMix/MixUp** augmentations in this [kernel](https://www.kaggle.com/itsuki9180/efficientnet-and-cutmixup-with-tpu-train-phase#CutMix-and-MixUp-Augmentation), so you can simply add it in the `def transform(image, label)` function and use it like other augmentations there.\n\nI will share a notebook that demonstrates using the TPU version so soon.\n\n---\nOne problem I faced with the **TPU** version, is that when apply `dataset.map(snapmix)`, it runs in \"*disabled eager execution mode*\" and `[conv_outputs, predictions] = get_output(x)` raise an *error*, as the `map()` method runs in \"*graph mode*\" and then I can't return **Numpy** values.\nI tried deifferent solutions like using `tf.py_function(func=snapmix, inp=[image, label, model, ...], Tout=[tf.float32,tf.float32])` but couldn't reach to a solution that work.\nSo if you can edit the **TPU** version and make it run, that would be interesting😃.",
    "1213915": "**Note**: when I started (3 months ago) building the snapmix augmentation , I have not found any tensorflow implementation of it, all were in pytorch, so I decided to build it, and finished it early but I was busy to reorganize the notebook and share it, so while I was writing this discussion I found that someone was working (nearly the same period) on SnapMix too and have uploaded it a weeks ago in github, so feel free to check [it](https://github.com/wangermeng2021/SnapMix-tensorflow2)"
  },
  "source": "meta"
}