{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install -q tensorflow_io tensorflow-text","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-06-23T21:12:19.951109Z","iopub.execute_input":"2022-06-23T21:12:19.951603Z","iopub.status.idle":"2022-06-23T21:12:31.679833Z","shell.execute_reply.started":"2022-06-23T21:12:19.951501Z","shell.execute_reply":"2022-06-23T21:12:31.678107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nfrom PIL import Image\nimport matplotlib.pyplot as plt\n\nimport tensorflow as tf\nimport tensorflow_io as tfio\nimport tensorflow_text as tf_text","metadata":{"execution":{"iopub.status.busy":"2022-06-23T21:14:08.231933Z","iopub.execute_input":"2022-06-23T21:14:08.232553Z","iopub.status.idle":"2022-06-23T21:14:08.242211Z","shell.execute_reply.started":"2022-06-23T21:14:08.232505Z","shell.execute_reply":"2022-06-23T21:14:08.240275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataframe = pd.read_csv(\"../input/hubmap-organ-segmentation/train.csv\")\ntrain_dataframe.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-23T21:14:08.396841Z","iopub.execute_input":"2022-06-23T21:14:08.397416Z","iopub.status.idle":"2022-06-23T21:14:08.596571Z","shell.execute_reply.started":"2022-06-23T21:14:08.397367Z","shell.execute_reply":"2022-06-23T21:14:08.595174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_IMAGE_PATH = \"../input/hubmap-organ-segmentation/train_images\"\nTRAIN_ANNOTATION_PATH = \"../input/hubmap-organ-segmentation/train_annotations\"\nTEST_IMAGE_PATH = \"../input/hubmap-organ-segmentation/test_images\"","metadata":{"execution":{"iopub.status.busy":"2022-06-23T21:14:08.971347Z","iopub.execute_input":"2022-06-23T21:14:08.971888Z","iopub.status.idle":"2022-06-23T21:14:08.978219Z","shell.execute_reply.started":"2022-06-23T21:14:08.971839Z","shell.execute_reply":"2022-06-23T21:14:08.977069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image_files = [os.path.join(TRAIN_IMAGE_PATH, f\"{_id}.tiff\") for _id in train_dataframe.id]\ntrain_rles = list(train_dataframe.rle)","metadata":{"execution":{"iopub.status.busy":"2022-06-23T21:21:19.942345Z","iopub.execute_input":"2022-06-23T21:21:19.942796Z","iopub.status.idle":"2022-06-23T21:21:19.951962Z","shell.execute_reply.started":"2022-06-23T21:21:19.942761Z","shell.execute_reply":"2022-06-23T21:21:19.950230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"@tf.function\ndef preprocess(image, mask_rle):\n    image = tf.io.read_file(image)\n    image = tfio.experimental.image.decode_tiff(image)\n    image.set_shape([None, None, 4])\n    \n    def _preprocess(image, mask_rle):\n        \"\"\"Reference: https://www.kaggle.com/code/paulorzp/run-length-encode-and-decode/script\"\"\"\n        image = image.numpy()\n        mask_rle = mask_rle.numpy()\n        s = mask_rle.split()\n        shape = (image.shape[-3], image.shape[-2])\n        starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]\n        starts -= 1\n        ends = starts + lengths\n        mask = np.zeros(shape[0] * shape[1], dtype=np.uint8)\n        for lo, hi in zip(starts, ends):\n            mask[lo:hi] = 1\n        mask = mask.reshape(*shape, 1)\n        return image, mask\n    \n    return tf.py_function(_preprocess, [image, mask_rle], [tf.float32, tf.uint8])\n\n\ntrain_dataset = tf.data.Dataset.from_tensor_slices((train_image_files, train_rles))\ntrain_dataset = train_dataset.map(preprocess, num_parallel_calls=tf.data.AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2022-06-23T21:26:33.221754Z","iopub.execute_input":"2022-06-23T21:26:33.222314Z","iopub.status.idle":"2022-06-23T21:26:33.335464Z","shell.execute_reply.started":"2022-06-23T21:26:33.222266Z","shell.execute_reply":"2022-06-23T21:26:33.334188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x, y = next(iter(train_dataset))\nx.shape, y.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-23T21:25:44.958537Z","iopub.execute_input":"2022-06-23T21:25:44.958941Z","iopub.status.idle":"2022-06-23T21:25:46.130365Z","shell.execute_reply.started":"2022-06-23T21:25:44.958904Z","shell.execute_reply":"2022-06-23T21:25:46.128437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 15))\nplt.imshow(x.numpy().astype(np.uint8))\nplt.imshow(y, cmap='coolwarm', alpha=0.5)\nplt.axis(\"off\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-06-23T21:27:13.848625Z","iopub.execute_input":"2022-06-23T21:27:13.849403Z","iopub.status.idle":"2022-06-23T21:27:16.544162Z","shell.execute_reply.started":"2022-06-23T21:27:13.849336Z","shell.execute_reply":"2022-06-23T21:27:16.539624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Work In Progress","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}