{"cells":[{"metadata":{},"cell_type":"markdown","source":"# VinBigData Chest X-ray Abnormalities Detection\n\n* The aim of this notebook is to demonstrate how to create JPEG images from DICOM images using TensorFlow only. \n* This notebook may be useful to those who wish to use TensorFlow to handle DICOM images and build models using JPEG images (other alternatives include TFRecord).\n\n**Note** \n* It may take a long time to convert all images and the images occupy much space, even if they are resized.\n* You may want to use a GPU for this notebook to run successfully (I got errors without it :P). Using PyDICOM instead may be slightly faster."},{"metadata":{},"cell_type":"markdown","source":"## Install libraries"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"!pip install tensorflow_io","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import os\nimport tensorflow as tf\nimport tensorflow_io as tfio\nfrom tqdm.notebook import tqdm","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Read DICOM images"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Reading DICOM images\n\ndef read_dicom(path):\n    image_bytes = tf.io.read_file(path)\n    image = tfio.image.decode_dicom_image(\n        image_bytes, \n        dtype = tf.uint16\n    )\n    \n    image = tf.squeeze(image, axis = 0)\n    \n    image = tf.image.resize(\n        image, \n        (500, 500), \n        preserve_aspect_ratio = True\n    )\n    \n    image = image - tf.reduce_min(image)\n    image = image / tf.reduce_max(image)\n    image = tf.cast(image * 255, tf.uint8)\n    \n    return image","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Save JPEG images"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Destination folder\ndestination = \"train_jpeg\"\nos.makedirs(\"train_jpeg\", exist_ok = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Source folder\nsource = \"../input/vinbigdata-chest-xray-abnormalities-detection/train\"\n\nfor name in tqdm(sorted(os.listdir(source))[:100]):\n    image = read_dicom(os.path.join(source, name))\n    image = tf.io.encode_jpeg(\n        image, \n        quality = 100, \n        format = 'grayscale'\n    )\n    \n    name = name.replace(\".dicom\", \".jpeg\")\n    tf.io.write_file(os.path.join(destination, name), image)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}