{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfrom glob import glob\nimport pydicom\nimport tensorflow as tf\nimport tqdm as tqdm\n\"\"\"\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\"\"\"\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-01T23:22:48.013922Z","iopub.execute_input":"2022-08-01T23:22:48.014447Z","iopub.status.idle":"2022-08-01T23:22:55.201491Z","shell.execute_reply.started":"2022-08-01T23:22:48.014325Z","shell.execute_reply":"2022-08-01T23:22:55.200217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Creating the Dataset #","metadata":{}},{"cell_type":"code","source":"class cl_CreatingDataset:\n    def __init__(self, imageHeight, imageWidth, batch_size):\n        self.imageHeight = imageHeight\n        self.imageWidth = imageWidth\n        self.batch_size = batch_size\n\n    \n    def creatingPathList(self, trainImagesPath, trainDfPath):\n        trainDf = pd.read_csv(trainDfPath)\n        trainImagesPathList = []\n        labels = []\n        # reading the tain CSV\n        trainDf = pd.read_csv(trainDfPath)\n        for i in tqdm.tqdm(range(len(trainDf))):\n            folderName = trainDf[\"StudyInstanceUID\"].iloc[i]\n            imageFolderPath = os.path.join(trainImagesPath, folderName)\n\n            for file in glob(os.path.join(imageFolderPath, \"*.dcm\")):\n                # taking the imageName\n                trainImagesPathList.append(file)\n                # creating the labels\n                label = np.array([trainDf[\"C1\"].iloc[i],trainDf[\"C2\"].iloc[i], trainDf[\"C3\"].iloc[i], trainDf[\"C4\"].iloc[i], trainDf[\"C5\"].iloc[i], \n                                 trainDf[\"C6\"].iloc[i], trainDf[\"C7\"].iloc[i], trainDf[\"patient_overall\"].iloc[i]])\n                labels.append(label)\n        \n        return trainImagesPathList, labels\n    \n    \n    def parse_function(self, filename, label):\n        \n        image_bytes = tf.io.read_file(filename)\n\n        image = tfio.image.decode_dicom_image(image_bytes, dtype=tf.uint16)\n\n        image = tf.image.convert_image_dtype(image, tf.float32)\n        \n        resized_image = tf.image.resize(image, [self.imageHeight, self.imageWidth])\n\n        return resized_image, label\n    \n    def train_preprocess(self, image, label):\n        return image, label\n\n\n    def creatingDataset(self, filenames, labels):\n        dataset = tf.data.Dataset.from_tensor_slices((filenames, labels))\n        dataset = dataset.shuffle(len(filenames))\n        dataset = dataset.map(self.parse_function, num_parallel_calls=4)\n        dataset = dataset.map(self.train_preprocess, num_parallel_calls=4)\n        dataset = dataset.batch(self.batch_size)\n        dataset = dataset.prefetch(1)\n\n        return dataset","metadata":{"execution":{"iopub.status.busy":"2022-08-01T23:24:13.562642Z","iopub.execute_input":"2022-08-01T23:24:13.563837Z","iopub.status.idle":"2022-08-01T23:24:13.58832Z","shell.execute_reply.started":"2022-08-01T23:24:13.563559Z","shell.execute_reply":"2022-08-01T23:24:13.586636Z"},"trusted":true},"execution_count":null,"outputs":[]}]}