{"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":"markdown","source":"This is my first Release Code.\n\nI learn from this [code](https://www.kaggle.com/josepc/rsna-effnet) so much,Thank you.\n\nPlease Run with My this code[[Train_with_TFRecord_for_RSNA-Radiogenomic](https://www.kaggle.com/hazigin/train-with-tfrecord-for-rsna-radiogenomic)]","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"import numpy as np\nfrom numpy.lib.function_base import append\nimport pandas as pd\nimport os\nimport sys\nimport tensorflow as tf\nfrom pathlib import Path\nimport cv2\nimport pydicom\nfrom sklearn.model_selection import train_test_split\nfrom tqdm import tqdm","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# limit the GPU memory growth\ngpu = tf.config.list_physical_devices('GPU')\nprint(\"Num GPUs Available: \", len(gpu))\nif len(gpu) > 0:\n    tf.config.experimental.set_memory_growth(gpu[0], True)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# modelの設定\nheight = 512\nwidth = 512\nchannel = 4\nseed = 26\ninput_depth = 4","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputdatapath = \"../input/rsna-miccai-brain-tumor-radiogenomic-classification\"\ntraindatapaht=\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/test\"\ntestdatapaht=\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train\"\noutpath = \"./\"","metadata":{},"execution_count":null,"outputs":[]},{"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() # BytesList won't unpack a string from an EagerTensor.\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_count":null,"outputs":[]},{"cell_type":"code","source":"def serialize_example(feature0, feature2):\n  feature = {\n      'image': _bytes_feature(feature0.tobytes()),\n      'MGMT_value': _float_feature(feature2)\n  }\n  example_proto = tf.train.Example(features=tf.train.Features(feature=feature))\n  return example_proto.SerializeToString()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dim = (height,width)\ndef data_generation(ID):\n    'Generates data containing batch_size samples' # X : (n_samples, *dim, n_channels)\n\n    # Store sample\n    idx = str(ID).zfill(5)\n    imgs = load_imgs(idx, ignore_zeros=False, train=True)\n    new_imgs = []\n\n    for ii in range(4):\n        img_ = imgs[views[ii]].mean(axis=0)\n        img_ = cv2.resize(img_, dsize=dim, interpolation=cv2.INTER_LINEAR)               \n        new_imgs.append(img_)\n    new_imgs = np.array(new_imgs).transpose(1,2,0)\n    return new_imgs","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"views = ['FLAIR', 'T1w', 'T1wCE', 'T2w']\ndef load_imgs(idx, ignore_zeros=True, train=True):\n    imgs = {}\n    for view in views:\n        save_ds = []\n        if train:\n            dir_path = os.walk(os.path.join(\n            traindatapaht, idx, view\n        ))\n        else:\n            dir_path = os.walk(os.path.join(\n            testdatapaht, idx, view\n        ))\n        for path, subdirs, files in dir_path:\n            for name in files:\n                image_path = os.path.join(path, name) \n                pyds = pydicom.filereader.dcmread(image_path)\n                save_ds.append(np.array(pyds.pixel_array))\n        if len(save_ds) == 0:\n            save_ds = np.zeros((1,height,width))\n        imgs[view] = np.array(save_ds)\n    return imgs","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv(os.path.join(inputdatapath,\"train_labels.csv\"))\nX_train, X_val, y_train, y_val = train_test_split(df_train.BraTS21ID, df_train.MGMT_value,\n                                                 test_size=0.2, random_state=42,stratify=df_train.MGMT_value)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with tf.io.TFRecordWriter(str(outpath + os.sep + 'brain_train.tfrec'),options=tf.io.TFRecordOptions(compression_type=\"GZIP\")) as writer:\n    for x,y in tqdm(zip(X_train,y_train)):\n        img = data_generation(x)\n        example = serialize_example(\n            img, y)\n        writer.write(example)\n\n\nwith tf.io.TFRecordWriter(str(outpath + os.sep + 'brain_val.tfrec'),options=tf.io.TFRecordOptions(compression_type=\"GZIP\")) as writer:\n    for x,y in tqdm(zip(X_val,y_val)):\n        img = data_generation(x)\n        example = serialize_example(\n            img, y)\n        writer.write(example)\n","metadata":{},"execution_count":null,"outputs":[]}]}