{
  "id": 215720,
  "title": "how to apply opencv's transformations in tensorflow.",
  "url": "/competitions/ranzcr-clip-catheter-line-classification/discussion/215720",
  "author_name": "Aishwary Shukla",
  "post_date": "2021-01-31T00:09:51.486000",
  "votes": 1,
  "comment_count": 7,
  "views": 0,
  "content": "<p>Suppose, I create a pipeline to load and resize an image in tensorflow like this:</p>\n<pre><code>def decode_image(file_name, label=None, IMAGE_SIZE=IMAGE_DIMENSIONS):\n    bits = tf.io.read_file(file_name)\n    image = tf.image.decode_jpeg(bits, channels=IMAGE_CHANNELS)\n    image = tf.cast(image, tf.float32)/255.0\n    image = tf.image.resize(image, size=IMAGE_SIZE)\n    if label is None:\n        return image\n    return image, label\n\ntrain_ds = (\n    tf.data.Dataset\n    .from_tensor_slices((train_df[\"StudyInstanceUID\"].values, train_df[labels].values))\n    .map(decode_image, num_parallel_calls=AUTO)\n    .map(data_augment, num_parallel_calls=AUTO)\n    .shuffle(512)\n    .repeat()\n    .batch(BATCH_SIZE)\n    .prefetch(AUTO)\n)\n</code></pre>\n<p>How can I apply functions like: <code>cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)</code> on an image (which is a tensor) inside the decode_image function mentioned above? Please let me know if I should elaborate more on the details.</p>",
  "messages": [
    {
      "id": 1179777,
      "postDate": "2021-01-31T20:08:14.230Z",
      "content": "<p>Note that tensorflow is not compatible with cv2 transformations/Albumentations when using TPU since it tensors cannot be executed eagerly when using TPU. However, for GPU you can wrap your transformations in a <code>tf.numpy_function</code>, refer to <a href=\"https://www.tensorflow.org/api_docs/python/tf/numpy_function\" target=\"_blank\">this</a></p>",
      "rawMarkdown": "Note that tensorflow is not compatible with cv2 transformations/Albumentations when using TPU since it tensors cannot be executed eagerly when using TPU. However, for GPU you can wrap your transformations in a `tf.numpy_function`, refer to [this](https://www.tensorflow.org/api_docs/python/tf/numpy_function)",
      "votes": 1
    },
    {
      "id": 1179245,
      "postDate": "2021-01-31T12:51:19.980Z",
      "content": "<p>I implemented it but with help of imagedatagenerator  and flow_from_dataframe or flow_from_directory there you can use custom processing function through setting processing_function property</p>",
      "rawMarkdown": "I implemented it but with help of imagedatagenerator  and flow_from_dataframe or flow_from_directory there you can use custom processing function through setting processing_function property",
      "votes": 1,
      "replies": [
        {
          "id": 1179640,
          "postDate": "2021-01-31T17:39:07.487Z",
          "content": "<p>Thank you for the information <a href=\"https://www.kaggle.com/dmitryleshchinskiy\" target=\"_blank\">@dmitryleshchinskiy</a>. I will try this technique.</p>",
          "rawMarkdown": "Thank you for the information @dmitryleshchinskiy. I will try this technique.",
          "votes": 1
        }
      ]
    },
    {
      "id": 1178522,
      "postDate": "2021-01-31T00:09:51.487Z",
      "content": "<p>Suppose, I create a pipeline to load and resize an image in tensorflow like this:</p>\n<pre><code>def decode_image(file_name, label=None, IMAGE_SIZE=IMAGE_DIMENSIONS):\n    bits = tf.io.read_file(file_name)\n    image = tf.image.decode_jpeg(bits, channels=IMAGE_CHANNELS)\n    image = tf.cast(image, tf.float32)/255.0\n    image = tf.image.resize(image, size=IMAGE_SIZE)\n    if label is None:\n        return image\n    return image, label\n\ntrain_ds = (\n    tf.data.Dataset\n    .from_tensor_slices((train_df[\"StudyInstanceUID\"].values, train_df[labels].values))\n    .map(decode_image, num_parallel_calls=AUTO)\n    .map(data_augment, num_parallel_calls=AUTO)\n    .shuffle(512)\n    .repeat()\n    .batch(BATCH_SIZE)\n    .prefetch(AUTO)\n)\n</code></pre>\n<p>How can I apply functions like: <code>cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)</code> on an image (which is a tensor) inside the decode_image function mentioned above? Please let me know if I should elaborate more on the details.</p>",
      "rawMarkdown": "Suppose, I create a pipeline to load and resize an image in tensorflow like this:\n\n```\ndef decode_image(file_name, label=None, IMAGE_SIZE=IMAGE_DIMENSIONS):\n    bits = tf.io.read_file(file_name)\n    image = tf.image.decode_jpeg(bits, channels=IMAGE_CHANNELS)\n    image = tf.cast(image, tf.float32)/255.0\n    image = tf.image.resize(image, size=IMAGE_SIZE)\n    if label is None:\n        return image\n    return image, label\n\ntrain_ds = (\n    tf.data.Dataset\n    .from_tensor_slices((train_df[\"StudyInstanceUID\"].values, train_df[labels].values))\n    .map(decode_image, num_parallel_calls=AUTO)\n    .map(data_augment, num_parallel_calls=AUTO)\n    .shuffle(512)\n    .repeat()\n    .batch(BATCH_SIZE)\n    .prefetch(AUTO)\n)\n```\nHow can I apply functions like: `cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)` on an image (which is a tensor) inside the decode_image function mentioned above? Please let me know if I should elaborate more on the details.",
      "votes": 1
    },
    {
      "id": 1179608,
      "postDate": "2021-01-31T17:10:59.230Z",
      "content": "<p>we always preprocess images before converting them into tfrecords. If you are using this as an augmentation, you can use <code>tf.image.rgb_to_grayscale</code> in your augmentation function.  But, the dataset already contains grayscale images, can you describe why you need to do this conversion?</p>",
      "rawMarkdown": "we always preprocess images before converting them into tfrecords. If you are using this as an augmentation, you can use ```tf.image.rgb_to_grayscale``` in your augmentation function.  But, the dataset already contains grayscale images, can you describe why you need to do this conversion?",
      "replies": [
        {
          "id": 1179635,
          "postDate": "2021-01-31T17:36:56.457Z",
          "content": "<p><a href=\"https://www.kaggle.com/kingofarmy\" target=\"_blank\">@kingofarmy</a> Thank you for the information. I am in the process of applying some opencv transformations to each image. Now, some of them are not available as a part of tensorflow e.g. contrast adaptive histogram equalization. So, to apply such functions via the pipeline I mentioned, I was searching for a solution. Hence, I gave a sample transformation to make my case more clear to the audience reading it.</p>",
          "rawMarkdown": "@kingofarmy Thank you for the information. I am in the process of applying some opencv transformations to each image. Now, some of them are not available as a part of tensorflow e.g. contrast adaptive histogram equalization. So, to apply such functions via the pipeline I mentioned, I was searching for a solution. Hence, I gave a sample transformation to make my case more clear to the audience reading it."
        },
        {
          "id": 1181038,
          "postDate": "2021-02-01T16:07:54.147Z",
          "content": "<p>Check this <a href=\"https://github.com/tensorflow/tensorflow/issues/24181\" target=\"_blank\">issue</a> in github.</p>\n<p>Check this implementation of <a href=\"https://github.com/VincentStimper/mclahe\" target=\"_blank\">MCLAHE</a> </p>\n<p>I think these infos might help you. But, also keep in mind you can't use such methods if you gonna train in TPU. Refer the previous comment by Andy.</p>",
          "rawMarkdown": "Check this [issue]  (https://github.com/tensorflow/tensorflow/issues/24181) in github.\n\nCheck this implementation of [MCLAHE](https://github.com/VincentStimper/mclahe) \n\n\nI think these infos might help you. But, also keep in mind you can't use such methods if you gonna train in TPU. Refer the previous comment by Andy.",
          "votes": 1
        }
      ]
    },
    {
      "id": 1179548,
      "postDate": "2021-01-31T16:10:18.607Z",
      "rawMarkdown": "",
      "isDeleted": true
    }
  ],
  "comments": [
    {
      "id": 1179777,
      "author_name": "Andy",
      "author_url": "",
      "post_date": "2021-01-31T20:08:14.230000",
      "content": "<p>Note that tensorflow is not compatible with cv2 transformations/Albumentations when using TPU since it tensors cannot be executed eagerly when using TPU. However, for GPU you can wrap your transformations in a <code>tf.numpy_function</code>, refer to <a href=\"https://www.tensorflow.org/api_docs/python/tf/numpy_function\" target=\"_blank\">this</a></p>",
      "votes": 1,
      "replies": []
    },
    {
      "id": 1179245,
      "author_name": "Dmitry Leshchinskiy",
      "author_url": "",
      "post_date": "2021-01-31T12:51:19.980000",
      "content": "<p>I implemented it but with help of imagedatagenerator  and flow_from_dataframe or flow_from_directory there you can use custom processing function through setting processing_function property</p>",
      "votes": 1,
      "replies": [
        {
          "id": 1179640,
          "author_name": "Aishwary Shukla",
          "author_url": "",
          "post_date": "2021-01-31T17:39:07.487000",
          "content": "<p>Thank you for the information <a href=\"https://www.kaggle.com/dmitryleshchinskiy\" target=\"_blank\">@dmitryleshchinskiy</a>. I will try this technique.</p>",
          "votes": 1,
          "replies": []
        }
      ]
    },
    {
      "id": 1179608,
      "author_name": "TEnsorSAge",
      "author_url": "",
      "post_date": "2021-01-31T17:10:59.230000",
      "content": "<p>we always preprocess images before converting them into tfrecords. If you are using this as an augmentation, you can use <code>tf.image.rgb_to_grayscale</code> in your augmentation function.  But, the dataset already contains grayscale images, can you describe why you need to do this conversion?</p>",
      "votes": 0,
      "replies": [
        {
          "id": 1179635,
          "author_name": "Aishwary Shukla",
          "author_url": "",
          "post_date": "2021-01-31T17:36:56.457000",
          "content": "<p><a href=\"https://www.kaggle.com/kingofarmy\" target=\"_blank\">@kingofarmy</a> Thank you for the information. I am in the process of applying some opencv transformations to each image. Now, some of them are not available as a part of tensorflow e.g. contrast adaptive histogram equalization. So, to apply such functions via the pipeline I mentioned, I was searching for a solution. Hence, I gave a sample transformation to make my case more clear to the audience reading it.</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 1181038,
          "author_name": "TEnsorSAge",
          "author_url": "",
          "post_date": "2021-02-01T16:07:54.147000",
          "content": "<p>Check this <a href=\"https://github.com/tensorflow/tensorflow/issues/24181\" target=\"_blank\">issue</a> in github.</p>\n<p>Check this implementation of <a href=\"https://github.com/VincentStimper/mclahe\" target=\"_blank\">MCLAHE</a> </p>\n<p>I think these infos might help you. But, also keep in mind you can't use such methods if you gonna train in TPU. Refer the previous comment by Andy.</p>",
          "votes": 1,
          "replies": []
        }
      ]
    },
    {
      "id": 1179548,
      "author_name": "",
      "author_url": "",
      "post_date": "2021-01-31T16:10:18.607000",
      "content": "",
      "votes": 0,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "1179777": "Note that tensorflow is not compatible with cv2 transformations/Albumentations when using TPU since it tensors cannot be executed eagerly when using TPU. However, for GPU you can wrap your transformations in a `tf.numpy_function`, refer to [this](https://www.tensorflow.org/api_docs/python/tf/numpy_function)",
    "1179245": "I implemented it but with help of imagedatagenerator  and flow_from_dataframe or flow_from_directory there you can use custom processing function through setting processing_function property",
    "1178522": "Suppose, I create a pipeline to load and resize an image in tensorflow like this:\n\n```\ndef decode_image(file_name, label=None, IMAGE_SIZE=IMAGE_DIMENSIONS):\n    bits = tf.io.read_file(file_name)\n    image = tf.image.decode_jpeg(bits, channels=IMAGE_CHANNELS)\n    image = tf.cast(image, tf.float32)/255.0\n    image = tf.image.resize(image, size=IMAGE_SIZE)\n    if label is None:\n        return image\n    return image, label\n\ntrain_ds = (\n    tf.data.Dataset\n    .from_tensor_slices((train_df[\"StudyInstanceUID\"].values, train_df[labels].values))\n    .map(decode_image, num_parallel_calls=AUTO)\n    .map(data_augment, num_parallel_calls=AUTO)\n    .shuffle(512)\n    .repeat()\n    .batch(BATCH_SIZE)\n    .prefetch(AUTO)\n)\n```\nHow can I apply functions like: `cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)` on an image (which is a tensor) inside the decode_image function mentioned above? Please let me know if I should elaborate more on the details.",
    "1179608": "we always preprocess images before converting them into tfrecords. If you are using this as an augmentation, you can use ```tf.image.rgb_to_grayscale``` in your augmentation function.  But, the dataset already contains grayscale images, can you describe why you need to do this conversion?",
    "1179548": ""
  }
}