{
  "id": 171448,
  "title": "i wrote a helper function to load DICOM with tf datasets",
  "url": "/competitions/osic-pulmonary-fibrosis-progression/discussion/171448",
  "author_name": "Dron Dronych",
  "post_date": "2020-07-31T21:10:32.335000",
  "votes": 5,
  "comment_count": 0,
  "views": 0,
  "content": "<p>I struggled finding a good way to load DICOM images into a tensorflow dataset and wrote a little helper function for starters w/ Tensorflow to do that. Who knows, maybe it'll be useful for you as well 🤓 \n1. Install the tensorflow_io package:\n<code>pip install -q tensorflow-io</code></p>\n\n<ol>\n<li>the function itself</li>\n</ol>\n\n<p>```\nimport tensorflow_io as tfio</p>\n\n<p>def parse_image(filename):</p>\n\n<pre><code>image_bytes = tf.io.read_file(filename)\n\n# if bad decoding - raise an error\nimage = tfio.image.decode_dicom_image(image_bytes, on_error='strict',\n    dtype=tf.uint32)   \nimg = tf.image.resize(image, IMG_RESIZE) # optional\n\nreturn img\n</code></pre>\n\n<p>```</p>\n\n<p>You can then use this function as a regular dataset mapper function, e.g. like so:\n<code>\ndataset = tf.data.Dataset.from_tensor_slices(filenames)\ndataset = dataset.map(parse_image, num_parallel_calls=4)\n</code>\nGood luck in chasing you dreams 😎 </p>",
  "messages": [
    {
      "id": 953521,
      "postDate": "2020-07-31T21:10:32.337Z",
      "content": "<p>I struggled finding a good way to load DICOM images into a tensorflow dataset and wrote a little helper function for starters w/ Tensorflow to do that. Who knows, maybe it'll be useful for you as well 🤓 \n1. Install the tensorflow_io package:\n<code>pip install -q tensorflow-io</code></p>\n\n<ol>\n<li>the function itself</li>\n</ol>\n\n<p>```\nimport tensorflow_io as tfio</p>\n\n<p>def parse_image(filename):</p>\n\n<pre><code>image_bytes = tf.io.read_file(filename)\n\n# if bad decoding - raise an error\nimage = tfio.image.decode_dicom_image(image_bytes, on_error='strict',\n    dtype=tf.uint32)   \nimg = tf.image.resize(image, IMG_RESIZE) # optional\n\nreturn img\n</code></pre>\n\n<p>```</p>\n\n<p>You can then use this function as a regular dataset mapper function, e.g. like so:\n<code>\ndataset = tf.data.Dataset.from_tensor_slices(filenames)\ndataset = dataset.map(parse_image, num_parallel_calls=4)\n</code>\nGood luck in chasing you dreams 😎 </p>",
      "rawMarkdown": "I struggled finding a good way to load DICOM images into a tensorflow dataset and wrote a little helper function for starters w/ Tensorflow to do that. Who knows, maybe it'll be useful for you as well 🤓 \n1. Install the tensorflow_io package:\n```pip install -q tensorflow-io```\n\n2. the function itself\n\n```\nimport tensorflow_io as tfio\n\ndef parse_image(filename):\n    \n    image_bytes = tf.io.read_file(filename)\n    \n    # if bad decoding - raise an error\n    image = tfio.image.decode_dicom_image(image_bytes, on_error='strict',\n        dtype=tf.uint32)   \n    img = tf.image.resize(image, IMG_RESIZE) # optional\n    \n    return img\n```\n\nYou can then use this function as a regular dataset mapper function, e.g. like so:\n```\ndataset = tf.data.Dataset.from_tensor_slices(filenames)\ndataset = dataset.map(parse_image, num_parallel_calls=4)\n```\nGood luck in chasing you dreams 😎 ",
      "votes": 5
    }
  ],
  "comments": [],
  "raw_markdown_by_id": {
    "953521": "I struggled finding a good way to load DICOM images into a tensorflow dataset and wrote a little helper function for starters w/ Tensorflow to do that. Who knows, maybe it'll be useful for you as well 🤓 \n1. Install the tensorflow_io package:\n```pip install -q tensorflow-io```\n\n2. the function itself\n\n```\nimport tensorflow_io as tfio\n\ndef parse_image(filename):\n    \n    image_bytes = tf.io.read_file(filename)\n    \n    # if bad decoding - raise an error\n    image = tfio.image.decode_dicom_image(image_bytes, on_error='strict',\n        dtype=tf.uint32)   \n    img = tf.image.resize(image, IMG_RESIZE) # optional\n    \n    return img\n```\n\nYou can then use this function as a regular dataset mapper function, e.g. like so:\n```\ndataset = tf.data.Dataset.from_tensor_slices(filenames)\ndataset = dataset.map(parse_image, num_parallel_calls=4)\n```\nGood luck in chasing you dreams 😎 "
  }
}