{"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":"import os, math, glob, re\nimport numpy as np\nimport pandas as pd\nimport cv2\n\nimport matplotlib.pyplot as plt\nimport pydicom\n\nfrom kaggle_datasets import KaggleDatasets\n\nfrom sklearn.model_selection import train_test_split\n\n\nfrom random import shuffle\nimport tensorflow as tf","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-10-14T22:46:59.695110Z","iopub.execute_input":"2021-10-14T22:46:59.695478Z","iopub.status.idle":"2021-10-14T22:46:59.700945Z","shell.execute_reply.started":"2021-10-14T22:46:59.695447Z","shell.execute_reply":"2021-10-14T22:46:59.699630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMAGE_SIZE  = 256\nIMAGE_DEPTH = 64\n\nmri_types = ['FLAIR','T1w','T1wCE','T2w']\nCHANNELS  = len(mri_types)\n\nlocal_directory = \"../input/rsna-miccai-brain-tumor-radiogenomic-classification\"\nlocal_label_path = local_directory+\"/train_labels.csv\"\nlocal_submission_path = local_directory+\"/sample_submission.csv\"","metadata":{"execution":{"iopub.status.busy":"2021-10-14T22:46:59.702809Z","iopub.execute_input":"2021-10-14T22:46:59.703311Z","iopub.status.idle":"2021-10-14T22:46:59.711090Z","shell.execute_reply.started":"2021-10-14T22:46:59.703269Z","shell.execute_reply":"2021-10-14T22:46:59.710222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load DICOM","metadata":{}},{"cell_type":"code","source":"# load 1 dicom img, e.g. image-1.dcm\ndef load_dicom_slice(path, img_size=256):\n\n    dicom = pydicom.read_file(path)\n    data = dicom.pixel_array\n    data = cv2.resize(data, (img_size, img_size))\n    \n    return data\n\n# load all dicoms in a modality folder, e.g. FLAIR/*.dcm\ndef load_dicom_modality(mri_type, scan_id, img_depth, img_size, split):\n\n    files = sorted(tf.io.gfile.glob(f\"{local_directory}/{split}/{scan_id}/{mri_type}/*.dcm\"), \n               key=lambda var:[int(x) if x.isdigit() else x for x in re.findall(r'[^0-9]|[0-9]+', var)])\n\n\n    \n    num_files = len(files)\n    num_files_middle = num_files//2\n    img_depth_middle = img_depth//2\n    \n    start_depth = max(0, num_files_middle - img_depth_middle)\n    end_depth   = min(num_files, num_files_middle + img_depth_middle)\n    img3d = np.stack([load_dicom_slice(dicom_img, img_size=img_size) \n                      for dicom_img in files[start_depth:end_depth]]).T\n    \n    if img3d.shape[-1] < img_depth:\n        n_zero = np.zeros((img_size, img_size, img_depth - img3d.shape[-1]))\n        img3d  = np.concatenate((img3d, n_zero), axis=-1)        \n\n    return img3d\n\n# load all modality for a single sample, e.g. 00010/*/*.dcm\ndef load_dicom_3D(scan_id, img_depth=128, img_size=256, split=\"test\"):\n    print(scan_id, end=\" \")\n    dicom_channels = [load_dicom_modality(scan_id=scan_id,\n                                         img_depth=img_depth, \n                                         img_size=img_size, \n                                         split=split,\n                                         mri_type=mtype) \n                      for mtype in mri_types]\n        \n    img = np.stack(dicom_channels, axis=-1)\n    # Normalize\n    if np.min(img) < np.max(img):\n        img = img - np.min(img)\n        img = img / np.max(img)\n    return img","metadata":{"execution":{"iopub.status.busy":"2021-10-14T22:46:59.713294Z","iopub.execute_input":"2021-10-14T22:46:59.713726Z","iopub.status.idle":"2021-10-14T22:46:59.727168Z","shell.execute_reply.started":"2021-10-14T22:46:59.713690Z","shell.execute_reply":"2021-10-14T22:46:59.726242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Convert to TFRecords","metadata":{}},{"cell_type":"code","source":"def _bytes_feature(value):\n    \"\"\"Returns a bytes_list from a string / byte.\"\"\"\n    if isinstance(value, type(tf.constant(0))):\n        value = value.numpy() \n    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))\n\ndef _float_feature(value):\n    \"\"\"Returns a float_list from a float / double.\"\"\"\n    return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))\n\ndef _int64_feature(value):\n    \"\"\"Returns an int64_list from a bool / enum / int / uint.\"\"\"\n    return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))","metadata":{"execution":{"iopub.status.busy":"2021-10-14T22:46:59.728975Z","iopub.execute_input":"2021-10-14T22:46:59.729358Z","iopub.status.idle":"2021-10-14T22:46:59.738344Z","shell.execute_reply.started":"2021-10-14T22:46:59.729324Z","shell.execute_reply":"2021-10-14T22:46:59.737562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def serialize_example(image):\n    feature = {\n        'image': _bytes_feature(image.tobytes()),\n    }\n    example_proto = tf.train.Example(features=tf.train.Features(feature=feature))\n    return example_proto.SerializeToString()","metadata":{"execution":{"iopub.status.busy":"2021-10-14T22:46:59.739598Z","iopub.execute_input":"2021-10-14T22:46:59.740054Z","iopub.status.idle":"2021-10-14T22:46:59.749756Z","shell.execute_reply.started":"2021-10-14T22:46:59.740019Z","shell.execute_reply":"2021-10-14T22:46:59.748868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def sorted_alphanumeric(data):  #\n    convert = lambda text: int(text) if text.isdigit() else text.lower()\n    alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]\n    return sorted(data, key=alphanum_key)\n\n#sorte the list ","metadata":{"execution":{"iopub.status.busy":"2021-10-14T22:46:59.752284Z","iopub.execute_input":"2021-10-14T22:46:59.752538Z","iopub.status.idle":"2021-10-14T22:46:59.759801Z","shell.execute_reply.started":"2021-10-14T22:46:59.752515Z","shell.execute_reply":"2021-10-14T22:46:59.759076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! mkdir -p ./tfrecords/test/\noutpath_train = \"./tfrecords/test\"\nwith tf.io.TFRecordWriter(str(outpath_train + os.sep + 'brain_test.tfrec'),\n                          options=tf.io.TFRecordOptions(compression_type=\"GZIP\")) as writer:\n    Path = \"../input/rsna-miccai-brain-tumor-radiogenomic-classification/test\"\n    train_dir = os.listdir(Path)\n    train_dir = sorted_alphanumeric(train_dir)\n    print(f\"traindir : {train_dir}\")\n    \n    for x in train_dir:\n        img = load_dicom_3D(x, img_size=IMAGE_SIZE, img_depth=IMAGE_DEPTH, split=\"test\")\n        example = serialize_example(img)\n        writer.write(example)\n\n        \n","metadata":{"execution":{"iopub.status.busy":"2021-10-14T22:46:59.761004Z","iopub.execute_input":"2021-10-14T22:46:59.761377Z","iopub.status.idle":"2021-10-14T22:52:54.986982Z","shell.execute_reply.started":"2021-10-14T22:46:59.761341Z","shell.execute_reply":"2021-10-14T22:52:54.986093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"AUTO = tf.data.AUTOTUNE\ndef deserialize_example(serialized_string):\n    image_feature_description = {\n        'image': tf.io.FixedLenFeature([], tf.string),\n    }\n    parsed_record = tf.io.parse_single_example(serialized_string, image_feature_description)\n    image = tf.io.decode_raw(parsed_record['image'], tf.float64)\n\n    image = tf.reshape(image,[256, 256, 64, 4])\n\n\n    return image\n\nBATCH_SIZE = 1 \ntrain_set = tf.data.TFRecordDataset(\"./tfrecords/test/brain_test.tfrec\",compression_type=\"GZIP\", num_parallel_reads=AUTO).map(deserialize_example).batch(BATCH_SIZE).prefetch(AUTO)\n","metadata":{"execution":{"iopub.status.busy":"2021-10-14T22:52:54.988533Z","iopub.execute_input":"2021-10-14T22:52:54.988914Z","iopub.status.idle":"2021-10-14T22:52:55.014048Z","shell.execute_reply.started":"2021-10-14T22:52:54.988872Z","shell.execute_reply":"2021-10-14T22:52:55.013347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.models.load_model('../input/test-2/Test2.h5',  custom_objects={'leaky_relu': tf.nn.leaky_relu})\npred = model.predict(train_set)","metadata":{"execution":{"iopub.status.busy":"2021-10-14T22:52:55.016104Z","iopub.execute_input":"2021-10-14T22:52:55.016454Z","iopub.status.idle":"2021-10-14T22:53:47.451153Z","shell.execute_reply.started":"2021-10-14T22:52:55.016419Z","shell.execute_reply":"2021-10-14T22:53:47.450024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(pred.shape)\nprint(\"dab\")\nsub = pd.read_csv(\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/sample_submission.csv\")\nprint(sub)\nsub['MGMT_value'] = pred\nsub.to_csv('submission.csv', index=False)\nprint(sub)","metadata":{"execution":{"iopub.status.busy":"2021-10-14T22:53:47.452998Z","iopub.execute_input":"2021-10-14T22:53:47.453388Z","iopub.status.idle":"2021-10-14T22:53:47.503027Z","shell.execute_reply.started":"2021-10-14T22:53:47.453346Z","shell.execute_reply":"2021-10-14T22:53:47.502241Z"},"trusted":true},"execution_count":null,"outputs":[]}]}