{"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\nimport re\nimport gc\nimport glob\nimport imageio\nimport numpy as np\nimport pandas as pd \nfrom tqdm import tqdm\nimport pydicom as dicom\nfrom skimage.transform import resize\n\nimport tensorflow as tf\nfrom tensorflow.keras.layers import *\nfrom tensorflow.keras.models import *\n\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2021-08-14T17:44:57.898525Z","iopub.execute_input":"2021-08-14T17:44:57.898773Z","iopub.status.idle":"2021-08-14T17:44:57.909599Z","shell.execute_reply.started":"2021-08-14T17:44:57.898749Z","shell.execute_reply":"2021-08-14T17:44:57.908868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FF = 10\nBS = 12\nIS = 224\nLS = 50","metadata":{"execution":{"iopub.status.busy":"2021-08-14T17:44:57.910989Z","iopub.execute_input":"2021-08-14T17:44:57.911351Z","iopub.status.idle":"2021-08-14T17:44:57.919372Z","shell.execute_reply.started":"2021-08-14T17:44:57.911318Z","shell.execute_reply":"2021-08-14T17:44:57.918568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_patient_id(raw_ids):\n    list=[]\n    for patient_id in raw_ids:\n        patient_id = int(patient_id)\n        if patient_id < 10:\n            list.append('0000'+str(patient_id))\n        elif patient_id >= 10 and patient_id < 100:\n            list.append('000'+str(patient_id))\n        elif patient_id >= 100 and patient_id < 1000:\n            list.append('00'+str(patient_id))\n        else:\n            list.append('0'+str(patient_id))\n    return np.array(list)\n\n\ndef get_path(row, test):\n    nid = row.BraTS21ID\n    path = str(f'../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/{nid}/{test}')\n    return path","metadata":{"execution":{"iopub.status.busy":"2021-08-14T17:44:57.92239Z","iopub.execute_input":"2021-08-14T17:44:57.922657Z","iopub.status.idle":"2021-08-14T17:44:57.931953Z","shell.execute_reply.started":"2021-08-14T17:44:57.922635Z","shell.execute_reply":"2021-08-14T17:44:57.930965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train_labels.csv')\ndf = df.reset_index(drop=True)\ndf[\"BraTS21ID\"] = get_patient_id(df[\"BraTS21ID\"])\n\ndf_FLAIR = df.copy()\ndf_FLAIR[\"path\"] = df_FLAIR.apply(lambda row: get_path(row, 'FLAIR'), axis=1)\ndf_T1w = df.copy()\ndf_T1w[\"path\"] = df_T1w.apply(lambda row: get_path(row, 'T1w'), axis=1)\ndf_T1wCE = df.copy()\ndf_T1wCE[\"path\"] = df_T1wCE.apply(lambda row: get_path(row, 'T1wCE'), axis=1)\ndf_T2w = df.copy()\ndf_T2w[\"path\"] = df_T2w.apply(lambda row: get_path(row, 'T2w'), axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-08-14T17:44:57.93532Z","iopub.execute_input":"2021-08-14T17:44:57.93612Z","iopub.status.idle":"2021-08-14T17:44:58.009806Z","shell.execute_reply.started":"2021-08-14T17:44:57.936042Z","shell.execute_reply":"2021-08-14T17:44:58.009025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_FLAIR, valid_FLAIR = train_test_split(df_FLAIR, test_size=0.1, stratify=df_FLAIR.MGMT_value.values, random_state=42)\ntrain_T1w, valid_T1w = train_test_split(df_T1w, test_size=0.1, stratify=df_T1w.MGMT_value.values, random_state=42)\ntrain_T1wCE, valid_T1wCE = train_test_split(df_T1wCE, test_size=0.1, stratify=df_T1wCE.MGMT_value.values, random_state=42)\ntrain_T2w, valid_T2w = train_test_split(df_T2w, test_size=0.1, stratify=df_T2w.MGMT_value.values, random_state=42)\ntrain_label, valid_label = train_test_split(df_FLAIR.MGMT_value.values, test_size=0.1, stratify=df_FLAIR.MGMT_value.values, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2021-08-14T17:44:58.010911Z","iopub.execute_input":"2021-08-14T17:44:58.011247Z","iopub.status.idle":"2021-08-14T17:44:58.034398Z","shell.execute_reply.started":"2021-08-14T17:44:58.011215Z","shell.execute_reply":"2021-08-14T17:44:58.033467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def sorted_nicely(l): \n    \"\"\" Sort the given iterable in the way that humans expect.\"\"\" \n    convert = lambda text: int(text) if text.isdigit() else text \n    alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ] \n    return sorted(l, key = alphanum_key)\n\ndef decode_image(image):\n    image = dicom.dcmread(image)\n    image = image.pixel_array\n    return image\n\ndef parse_frames(dirname):\n    paths = glob.glob(dirname.decode('utf8')+'/*.dcm')\n    paths = sorted_nicely(paths)\n    if len(paths)<FF:\n        mri_images = np.zeros(shape=(FF, IS, IS, 3),dtype=np.float32)\n    else:\n        start = tf.random.uniform((1,), maxval=len(paths)-FF, dtype=tf.int32)\n        paths = tf.slice(paths, start, [FF])\n    \n        def get_frames(path):\n            path = str(path)\n            pathx_list = path.split(\"'\")\n            pathx = pathx_list[1]\n            image = decode_image(pathx)\n            image = resize(image, (IS, IS, 1), anti_aliasing=True)\n            image = image.repeat(3, axis=-1)\n            image = tf.convert_to_tensor(image, dtype=tf.float32)\n            return image\n\n        mri_images = tf.nest.map_structure(tf.stop_gradient, tf.map_fn(fn=get_frames, elems=paths, fn_output_signature=tf.float32))\n    return mri_images\n\ndef load_frame(df_dict):\n    dirname = df_dict['path']\n    paths = tf.numpy_function(parse_frames, [dirname], tf.float32)   \n    label = df_dict['MGMT_value']\n    label = tf.cast(label, tf.float32)\n    \n    return paths, label","metadata":{"execution":{"iopub.status.busy":"2021-08-14T17:46:31.533702Z","iopub.execute_input":"2021-08-14T17:46:31.534023Z","iopub.status.idle":"2021-08-14T17:46:31.548153Z","shell.execute_reply.started":"2021-08-14T17:46:31.533994Z","shell.execute_reply":"2021-08-14T17:46:31.547316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def test_load_frame(df_dict):\n    dirname = df_dict['path']\n    paths = tf.numpy_function(parse_frames, [dirname], tf.float32)  \n    \n    return paths","metadata":{"execution":{"iopub.status.busy":"2021-08-14T17:46:32.743988Z","iopub.execute_input":"2021-08-14T17:46:32.744354Z","iopub.status.idle":"2021-08-14T17:46:32.749476Z","shell.execute_reply.started":"2021-08-14T17:46:32.744324Z","shell.execute_reply":"2021-08-14T17:46:32.748568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FLAIR_t_loader = tf.data.Dataset.from_tensor_slices(dict(train_FLAIR))\nFLAIR_v_loader = tf.data.Dataset.from_tensor_slices(dict(valid_FLAIR))\n\nT1w_t_loader = tf.data.Dataset.from_tensor_slices(dict(train_T1w))\nT1w_v_loader = tf.data.Dataset.from_tensor_slices(dict(valid_T1w))\n\nT1wCE_t_loader = tf.data.Dataset.from_tensor_slices(dict(train_T1wCE))\nT1wCE_v_loader = tf.data.Dataset.from_tensor_slices(dict(valid_T1wCE))\n\nT2w_t_loader = tf.data.Dataset.from_tensor_slices(dict(train_T2w))\nT2w_v_loader = tf.data.Dataset.from_tensor_slices(dict(valid_T2w))","metadata":{"execution":{"iopub.status.busy":"2021-08-14T17:46:34.821318Z","iopub.execute_input":"2021-08-14T17:46:34.821625Z","iopub.status.idle":"2021-08-14T17:46:36.416716Z","shell.execute_reply.started":"2021-08-14T17:46:34.821598Z","shell.execute_reply":"2021-08-14T17:46:36.415932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"AUTOTUNE = tf.data.AUTOTUNE\n\nFLAIR_t_loader = (\n    FLAIR_t_loader\n    .shuffle(1024)\n    .map(load_frame, num_parallel_calls=AUTOTUNE)\n    .batch(BS)\n    .prefetch(AUTOTUNE)\n)\n\nFLAIR_v_loader = (\n    FLAIR_v_loader\n    .map(load_frame, num_parallel_calls=AUTOTUNE)\n    .batch(BS)\n    .prefetch(AUTOTUNE)\n)","metadata":{"execution":{"iopub.status.busy":"2021-08-14T17:46:36.418109Z","iopub.execute_input":"2021-08-14T17:46:36.418438Z","iopub.status.idle":"2021-08-14T17:46:36.654192Z","shell.execute_reply.started":"2021-08-14T17:46:36.418404Z","shell.execute_reply":"2021-08-14T17:46:36.653375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"T1w_t_loader = (\n    T1w_t_loader\n    .shuffle(1024)\n    .map(load_frame, num_parallel_calls=AUTOTUNE)\n    .batch(BS)\n    .prefetch(AUTOTUNE)\n)\n\nT1w_v_loader = (\n    T1w_v_loader\n    .map(load_frame, num_parallel_calls=AUTOTUNE)\n    .batch(BS)\n    .prefetch(AUTOTUNE)\n)","metadata":{"execution":{"iopub.status.busy":"2021-08-14T17:46:36.655729Z","iopub.execute_input":"2021-08-14T17:46:36.656055Z","iopub.status.idle":"2021-08-14T17:46:36.673654Z","shell.execute_reply.started":"2021-08-14T17:46:36.65602Z","shell.execute_reply":"2021-08-14T17:46:36.672925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"T1wCE_t_loader = (\n    T1wCE_t_loader\n    .shuffle(1024)\n    .map(load_frame, num_parallel_calls=AUTOTUNE)\n    .batch(BS)\n    .prefetch(AUTOTUNE)\n)\n\nT1wCE_v_loader = (\n    T1wCE_v_loader\n    .map(load_frame, num_parallel_calls=AUTOTUNE)\n    .batch(BS)\n    .prefetch(AUTOTUNE)\n)","metadata":{"execution":{"iopub.status.busy":"2021-08-14T17:46:36.73465Z","iopub.execute_input":"2021-08-14T17:46:36.734897Z","iopub.status.idle":"2021-08-14T17:46:36.751469Z","shell.execute_reply.started":"2021-08-14T17:46:36.734874Z","shell.execute_reply":"2021-08-14T17:46:36.750758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"T2w_t_loader = (\n    T2w_t_loader\n    .shuffle(1024)\n    .map(load_frame, num_parallel_calls=AUTOTUNE)\n    .batch(BS)\n    .prefetch(AUTOTUNE)\n)\n\nT2w_v_loader = (\n    T2w_v_loader\n    .map(load_frame, num_parallel_calls=AUTOTUNE)\n    .batch(BS)\n    .prefetch(AUTOTUNE)\n)","metadata":{"execution":{"iopub.status.busy":"2021-08-14T17:46:37.292734Z","iopub.execute_input":"2021-08-14T17:46:37.293028Z","iopub.status.idle":"2021-08-14T17:46:37.312254Z","shell.execute_reply.started":"2021-08-14T17:46:37.293Z","shell.execute_reply":"2021-08-14T17:46:37.311505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example = next(iter(T2w_t_loader))[0]\nexample.shape","metadata":{"execution":{"iopub.status.busy":"2021-08-14T17:46:37.895992Z","iopub.execute_input":"2021-08-14T17:46:37.8963Z","iopub.status.idle":"2021-08-14T17:46:41.490938Z","shell.execute_reply.started":"2021-08-14T17:46:37.896273Z","shell.execute_reply":"2021-08-14T17:46:41.489989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **MODEL and TRAINING**","metadata":{}},{"cell_type":"code","source":"import os\nos.system('pip install /kaggle/input/kerasapplications -q')\nos.system('pip install /kaggle/input/efficientnet-keras-source-code/ -q --no-deps')\n\nimport efficientnet.tfkeras as efn\n\ndef Eff():  \n    base_model = efn.EfficientNetB0(include_top=False, weights=None)\n    base_model.load_weights('../input/efficientnetb0b7-keras-weights/efficientnet-b0_weights_tf_dim_ordering_tf_kernels_autoaugment_notop.h5')\n    \n    base_model.trainable = True\n\n    inputs = Input((IS, IS, 3))\n    x = base_model(inputs, training=True)\n    flattened_output = GlobalAveragePooling2D()(x)\n    \n    return Model(inputs, flattened_output)","metadata":{"execution":{"iopub.status.busy":"2021-08-14T04:16:01.093375Z","iopub.execute_input":"2021-08-14T04:16:01.093807Z","iopub.status.idle":"2021-08-14T04:16:50.740952Z","shell.execute_reply.started":"2021-08-14T04:16:01.093762Z","shell.execute_reply":"2021-08-14T04:16:50.739723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def MainModel():\n    input = Input([FF, IS, IS, 3])\n    eff = Eff()\n    \n    time_wrapper = TimeDistributed(eff)(input)\n    \n    lstm = LSTM(LS, return_sequences=True, name=\"lstm\")(time_wrapper)\n    output = Dense(1, activation='sigmoid', name=\"lstm_sigmoid\")(lstm)\n    \n    return Model(input, output)","metadata":{"execution":{"iopub.status.busy":"2021-08-14T04:16:50.743035Z","iopub.execute_input":"2021-08-14T04:16:50.743472Z","iopub.status.idle":"2021-08-14T04:16:50.749911Z","shell.execute_reply.started":"2021-08-14T04:16:50.743431Z","shell.execute_reply":"2021-08-14T04:16:50.748548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.backend.clear_session() \nmodel_FLAIR = MainModel()\nmodel_T1w = MainModel()\nmodel_T1wCE = MainModel()\nmodel_T2w = MainModel()","metadata":{"execution":{"iopub.status.busy":"2021-08-14T04:16:50.752044Z","iopub.execute_input":"2021-08-14T04:16:50.752431Z","iopub.status.idle":"2021-08-14T04:17:05.519879Z","shell.execute_reply.started":"2021-08-14T04:16:50.752394Z","shell.execute_reply":"2021-08-14T04:17:05.519011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"earlystop = tf.keras.callbacks.EarlyStopping(monitor='val_loss', \n                                          patience=5, \n                                          restore_best_weights=True)\n\nsave_FLAIR = tf.keras.callbacks.ModelCheckpoint('./FLAIR', \n                                               monitor='val_loss', \n                                               save_best_only=True, \n                                               save_freq='epoch',)\nsave_T1w = tf.keras.callbacks.ModelCheckpoint('./T1w', \n                                               monitor='val_loss', \n                                               save_best_only=True, \n                                               save_freq='epoch',)\nsave_T1wCE = tf.keras.callbacks.ModelCheckpoint('./T1wCE', \n                                               monitor='val_loss', \n                                               save_best_only=True, \n                                               save_freq='epoch',)\nsave_T2w = tf.keras.callbacks.ModelCheckpoint('./T2w', \n                                               monitor='val_loss', \n                                               save_best_only=True, \n                                               save_freq='epoch',)\n\nlr = tf.keras.optimizers.schedules.ExponentialDecay(0.01, 2, 0.1)\noptimizer = tf.keras.optimizers.Adam(learning_rate=lr)\n\nmodel_FLAIR.compile(loss=\"binary_crossentropy\", optimizer=optimizer, metrics=\"acc\")\nmodel_T1w.compile(loss=\"binary_crossentropy\", optimizer=optimizer, metrics=\"acc\")\nmodel_T1wCE.compile(loss=\"binary_crossentropy\", optimizer=optimizer, metrics=\"acc\")\nmodel_T2w.compile(loss=\"binary_crossentropy\", optimizer=optimizer, metrics=\"acc\")","metadata":{"execution":{"iopub.status.busy":"2021-08-14T04:17:05.521543Z","iopub.execute_input":"2021-08-14T04:17:05.521858Z","iopub.status.idle":"2021-08-14T04:17:05.574143Z","shell.execute_reply.started":"2021-08-14T04:17:05.521824Z","shell.execute_reply":"2021-08-14T04:17:05.573366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_T1w.fit(\n    T1w_t_loader,\n    epochs=100, \n    callbacks=[save_T1w, earlystop], \n    validation_data=(T1w_v_loader), \n              )","metadata":{"execution":{"iopub.status.busy":"2021-08-14T04:17:05.575376Z","iopub.execute_input":"2021-08-14T04:17:05.575871Z","iopub.status.idle":"2021-08-14T04:24:28.235798Z","shell.execute_reply.started":"2021-08-14T04:17:05.575834Z","shell.execute_reply":"2021-08-14T04:24:28.234935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_FLAIR.fit(\n    FLAIR_t_loader,\n    epochs=100, \n    callbacks=[save_FLAIR, earlystop], \n    validation_data=(FLAIR_v_loader), \n              )","metadata":{"execution":{"iopub.status.busy":"2021-08-14T04:24:28.237166Z","iopub.execute_input":"2021-08-14T04:24:28.237546Z","iopub.status.idle":"2021-08-14T04:32:01.914349Z","shell.execute_reply.started":"2021-08-14T04:24:28.237505Z","shell.execute_reply":"2021-08-14T04:32:01.913532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_T1wCE.fit(\n    T1wCE_t_loader,\n    epochs=100, \n    callbacks=[save_T1wCE, earlystop], \n    validation_data=(T1wCE_v_loader), \n              )","metadata":{"execution":{"iopub.status.busy":"2021-08-14T04:32:01.915596Z","iopub.execute_input":"2021-08-14T04:32:01.915927Z","iopub.status.idle":"2021-08-14T04:39:28.094908Z","shell.execute_reply.started":"2021-08-14T04:32:01.915891Z","shell.execute_reply":"2021-08-14T04:39:28.094092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_T2w.fit(\n    T2w_t_loader,\n    epochs=100, \n    callbacks=[save_T2w, earlystop], \n    validation_data=(T2w_v_loader), \n              )","metadata":{"execution":{"iopub.status.busy":"2021-08-14T04:39:28.096246Z","iopub.execute_input":"2021-08-14T04:39:28.096612Z","iopub.status.idle":"2021-08-14T04:57:55.66779Z","shell.execute_reply.started":"2021-08-14T04:39:28.096572Z","shell.execute_reply":"2021-08-14T04:57:55.666749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# concate model","metadata":{}},{"cell_type":"markdown","source":"# **TEST**","metadata":{}},{"cell_type":"code","source":"test_array = [f.name for f in os.scandir('../input/rsna-miccai-brain-tumor-radiogenomic-classification/test') if f.is_dir()]\ntest_array = sorted_nicely(test_array)\ntest_df = pd.DataFrame({\"BraTS21ID\":test_array})\ntest_FLAIR = test_df.copy()\ntest_FLAIR[\"path\"] = test_df.apply(lambda row: get_path(row, 'FLAIR'), axis=1)\ntest_T1w = test_df.copy()\ntest_T1w[\"path\"] = test_T1w.apply(lambda row: get_path(row, 'T1w'), axis=1)\ntest_T1wCE = test_df.copy()\ntest_T1wCE[\"path\"] = test_T1wCE.apply(lambda row: get_path(row, 'T1wCE'), axis=1)\ntest_T2w = test_df.copy()\ntest_T2w[\"path\"] = test_T2w.apply(lambda row: get_path(row, 'T2w'), axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-08-14T17:46:50.086842Z","iopub.execute_input":"2021-08-14T17:46:50.087207Z","iopub.status.idle":"2021-08-14T17:46:50.118027Z","shell.execute_reply.started":"2021-08-14T17:46:50.087173Z","shell.execute_reply":"2021-08-14T17:46:50.117215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FLAIR_test_loader = tf.data.Dataset.from_tensor_slices(dict(test_FLAIR))\nT1w_test_loader = tf.data.Dataset.from_tensor_slices(dict(test_T1w))\nT1wCE_test_loader = tf.data.Dataset.from_tensor_slices(dict(test_T1wCE))\nT2w_test_loader = tf.data.Dataset.from_tensor_slices(dict(test_T2w))","metadata":{"execution":{"iopub.status.busy":"2021-08-14T17:46:50.949602Z","iopub.execute_input":"2021-08-14T17:46:50.949915Z","iopub.status.idle":"2021-08-14T17:46:50.96682Z","shell.execute_reply.started":"2021-08-14T17:46:50.949885Z","shell.execute_reply":"2021-08-14T17:46:50.965896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FLAIR_test_loader = (\n    FLAIR_test_loader\n    .map(test_load_frame, num_parallel_calls=AUTOTUNE)\n    .batch(BS)\n    .prefetch(AUTOTUNE)\n)\nT1wCE_test_loader = (\n    T1wCE_test_loader\n    .map(test_load_frame, num_parallel_calls=AUTOTUNE)\n    .batch(BS)\n    .prefetch(AUTOTUNE)\n)\nT1w_test_loader = (\n    T1w_test_loader\n    .map(test_load_frame, num_parallel_calls=AUTOTUNE)\n    .batch(BS)\n    .prefetch(AUTOTUNE)\n)\nT2w_test_loader = (\n    T2w_test_loader\n    .map(test_load_frame, num_parallel_calls=AUTOTUNE)\n    .batch(BS)\n    .prefetch(AUTOTUNE)\n)","metadata":{"execution":{"iopub.status.busy":"2021-08-14T17:46:52.514591Z","iopub.execute_input":"2021-08-14T17:46:52.514947Z","iopub.status.idle":"2021-08-14T17:46:52.573907Z","shell.execute_reply.started":"2021-08-14T17:46:52.514914Z","shell.execute_reply":"2021-08-14T17:46:52.573179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FLAIRpred = model_FLAIR.predict(FLAIR_test_loader)\nT1wpred = model_T1w.predict(T1w_test_loader)\nT1wCEpred = model_T1wCE.predict(T1wCE_test_loader)\nT2wpred = model_T2w.predict(T2w_test_loader)","metadata":{"execution":{"iopub.status.busy":"2021-08-14T17:46:53.814409Z","iopub.execute_input":"2021-08-14T17:46:53.814772Z","iopub.status.idle":"2021-08-14T17:46:54.14123Z","shell.execute_reply.started":"2021-08-14T17:46:53.814736Z","shell.execute_reply":"2021-08-14T17:46:54.139628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.save(\"./FLAIRpred.npy\", FLAIRpred)\nnp.save(\"./T1wpred.npy\", T1wpred)\nnp.save(\"./T1wCEpred.npy\", T1wCEpred)\nnp.save(\"./T2wpred.npy\", T2wpred)","metadata":{"execution":{"iopub.status.busy":"2021-08-14T04:58:12.683125Z","iopub.execute_input":"2021-08-14T04:58:12.683494Z","iopub.status.idle":"2021-08-14T04:58:12.690186Z","shell.execute_reply.started":"2021-08-14T04:58:12.683453Z","shell.execute_reply":"2021-08-14T04:58:12.689313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"fin_pred = np.concatenate([FLAIRpred,T1wpred,T1wCEpred,T2wpred], axis=-1)\nfin_pred = np.mean(fin_pred, axis = -1)\nfin_pred = np.mean(fin_pred, axis = 1)\"\"\"","metadata":{"execution":{"iopub.status.busy":"2021-08-14T04:58:12.691602Z","iopub.execute_input":"2021-08-14T04:58:12.692158Z","iopub.status.idle":"2021-08-14T04:58:12.701938Z","shell.execute_reply.started":"2021-08-14T04:58:12.692118Z","shell.execute_reply":"2021-08-14T04:58:12.701114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fin_pred = FLAIRpred+T1wpred+T1wCEpred+T2wpred\nfin_pred = fin_pred.sum(axis=1)\nfmax = fin_pred.max()\nfmin = fin_pred.min()\ndiff = fmax - fmin\nfmean = fin_pred.mean()\nfin_pred = ((fin_pred-fmean)/diff)+0.5\nfin_pred = np.reshape(fin_pred, (87))","metadata":{"execution":{"iopub.status.busy":"2021-08-14T04:58:12.703228Z","iopub.execute_input":"2021-08-14T04:58:12.703705Z","iopub.status.idle":"2021-08-14T04:58:12.710211Z","shell.execute_reply.started":"2021-08-14T04:58:12.703668Z","shell.execute_reply":"2021-08-14T04:58:12.709353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(len(fin_pred)):\n    if fin_pred[i]>1:\n        fin_pred[i] = 1\n    elif fin_pred[i]<0:\n        fin_pred[i] = 0","metadata":{"execution":{"iopub.status.busy":"2021-08-14T04:58:12.71157Z","iopub.execute_input":"2021-08-14T04:58:12.712131Z","iopub.status.idle":"2021-08-14T04:58:12.719535Z","shell.execute_reply.started":"2021-08-14T04:58:12.712093Z","shell.execute_reply":"2021-08-14T04:58:12.71872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({\"BraTS21ID\":test_array, \"MGMT_value\":fin_pred})\nsubmission","metadata":{"execution":{"iopub.status.busy":"2021-08-14T04:58:12.720899Z","iopub.execute_input":"2021-08-14T04:58:12.721484Z","iopub.status.idle":"2021-08-14T04:58:12.744944Z","shell.execute_reply.started":"2021-08-14T04:58:12.721448Z","shell.execute_reply":"2021-08-14T04:58:12.744152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2021-08-14T04:58:12.746148Z","iopub.execute_input":"2021-08-14T04:58:12.746496Z","iopub.status.idle":"2021-08-14T04:58:12.75453Z","shell.execute_reply.started":"2021-08-14T04:58:12.746463Z","shell.execute_reply":"2021-08-14T04:58:12.753417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}