{"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":"## Training notebook - https://www.kaggle.com/hijest/rsna-miccai-2dcnn-training","metadata":{}},{"cell_type":"code","source":"import os\nimport json\nimport glob\nimport random\nimport collections\n\nimport numpy as np\nimport pandas as pd\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nimport cv2\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport random\nfrom tqdm.notebook import tqdm\n\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom sklearn.metrics import roc_auc_score\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras import layers\n\n\n\nTYPES = [\"FLAIR\", \"T1w\", \"T2w\", \"T1wCE\"]\nWHITE_THRESHOLD = 10 # out of 255\nEXCLUDE = [109, 123, 709]\n\n\ntrain_df = pd.read_csv(\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train_labels.csv\")\ntest_df = pd.read_csv('../input/rsna-miccai-brain-tumor-radiogenomic-classification/sample_submission.csv')\ntrain_df = train_df[~train_df.BraTS21ID.isin(EXCLUDE)]\ndef load_dicom(path, size = 224):\n    ''' \n    Reads a DICOM image, standardizes so that the pixel values are between 0 and 1, then rescales to 0 and 255\n    \n    Note super sure if this kind of scaling is appropriate, but everyone seems to do it. \n    '''\n    dicom = pydicom.read_file(path)\n    data = dicom.pixel_array\n    if np.max(data) != 0:\n        data = data / np.max(data)\n    data = (data * 255).astype(np.uint8)\n    return cv2.resize(data, (size, size))\n\ndef get_all_image_paths(brats21id, image_type, folder='train'): \n    '''\n    Returns an arry of all the images of a particular type for a particular patient ID\n    '''\n    assert(image_type in TYPES)\n    \n    patient_path = os.path.join(\n        \"../input/rsna-miccai-brain-tumor-radiogenomic-classification/%s/\" % folder, \n        str(brats21id).zfill(5),\n    )\n\n    paths = sorted(\n        glob.glob(os.path.join(patient_path, image_type, \"*\")), \n        key=lambda x: int(x[:-4].split(\"-\")[-1]),\n    )\n    \n    num_images = len(paths)\n    \n    start = int(num_images * 0.25)\n    end = int(num_images * 0.75)\n\n    interval = 3\n    \n    if num_images < 10: \n        interval = 1\n    \n    return np.array(paths[start:end:interval])\n\ndef get_all_images(brats21id, image_type, folder='train', size=225):\n    return [load_dicom(path, size) for path in get_all_image_paths(brats21id, image_type, folder)]\nIMAGE_SIZE = 128\n\ndef get_all_data_for_train(image_type):\n    global train_df\n    \n    X = []\n    y = []\n    train_ids = []\n\n    for i in tqdm(train_df.index):\n        x = train_df.loc[i]\n        images = get_all_images(int(x['BraTS21ID']), image_type, 'train', IMAGE_SIZE)\n        label = x['MGMT_value']\n\n        X += images\n        y += [label] * len(images)\n        train_ids += [int(x['BraTS21ID'])] * len(images)\n        assert(len(X) == len(y))\n    return np.array(X), np.array(y), np.array(train_ids)\n\ndef get_all_data_for_test(image_type):\n    global test_df\n    \n    X = []\n    test_ids = []\n\n    for i in tqdm(test_df.index):\n        x = test_df.loc[i]\n        images = get_all_images(int(x['BraTS21ID']), image_type, 'test', IMAGE_SIZE)\n        X += images\n        test_ids += [int(x['BraTS21ID'])] * len(images)\n\n    return np.array(X), np.array(test_ids)\n\nX_test, testidt = get_all_data_for_test('T1wCE')","metadata":{"execution":{"iopub.status.busy":"2021-10-16T00:43:47.501871Z","iopub.execute_input":"2021-10-16T00:43:47.502426Z","iopub.status.idle":"2021-10-16T00:43:53.632887Z","shell.execute_reply.started":"2021-10-16T00:43:47.502372Z","shell.execute_reply":"2021-10-16T00:43:53.632216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#X_test = tf.expand_dims(X_test, -1)","metadata":{"execution":{"iopub.status.busy":"2021-10-16T00:43:30.358399Z","iopub.execute_input":"2021-10-16T00:43:30.358752Z","iopub.status.idle":"2021-10-16T00:43:30.363313Z","shell.execute_reply.started":"2021-10-16T00:43:30.358717Z","shell.execute_reply":"2021-10-16T00:43:30.36267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#X_test.shape","metadata":{"execution":{"iopub.status.busy":"2021-10-16T00:43:53.634603Z","iopub.execute_input":"2021-10-16T00:43:53.634885Z","iopub.status.idle":"2021-10-16T00:43:53.638578Z","shell.execute_reply.started":"2021-10-16T00:43:53.634848Z","shell.execute_reply":"2021-10-16T00:43:53.637812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# X_test_ = tf.concat([X_test,X_test,X_test],axis=-1)\n# X_test_.shape","metadata":{"execution":{"iopub.status.busy":"2021-10-16T00:43:11.923862Z","iopub.execute_input":"2021-10-16T00:43:11.924107Z","iopub.status.idle":"2021-10-16T00:43:11.939707Z","shell.execute_reply.started":"2021-10-16T00:43:11.924075Z","shell.execute_reply":"2021-10-16T00:43:11.939096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_path = '../input/fork-of-rsna-miccai-2dcnn-training/best_model_inception.h5'","metadata":{"execution":{"iopub.status.busy":"2021-10-16T00:43:11.942939Z","iopub.execute_input":"2021-10-16T00:43:11.945793Z","iopub.status.idle":"2021-10-16T00:43:11.950516Z","shell.execute_reply.started":"2021-10-16T00:43:11.945756Z","shell.execute_reply":"2021-10-16T00:43:11.949893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def loss(y_true, y_pred):\n    return - (1 - theta(y_true - margin) * theta(y_pred - margin)\n                - theta(1 - margin - y_true) * theta(1 - margin - y_pred)\n             ) * (y_true * K.log(y_pred + 1e-8) + (1 - y_true) * K.log(1 - y_pred + 1e-8))\n\nimport tensorflow as tf\n\nmargin = 0.6\ntheta = lambda t: (K.sign(t)+1.)/2.\n\ndef mish(inputs):\n    x = tf.nn.softplus(inputs)\n    x = tf.nn.tanh(x)\n    x = tf.multiply(x, inputs)\n    return x\n\n#model_best = tf.keras.models.load_model(filepath=file_path,custom_objects={\"loss\":loss,'leaky_relu': tf.nn.leaky_relu,\"mish\":mish})\n# model_best2 = tf.keras.models.load_model('../input/k/juliojuse/k/juliojuse/rsna-miccai-2dcnn-training/best_model.h5')\n# model_best3 = tf.keras.models.load_model('../input/d/juliojuse/2dcnn/best_model.h5')","metadata":{"execution":{"iopub.status.busy":"2021-10-16T00:43:11.95215Z","iopub.execute_input":"2021-10-16T00:43:11.953642Z","iopub.status.idle":"2021-10-16T00:43:11.969362Z","shell.execute_reply.started":"2021-10-16T00:43:11.953602Z","shell.execute_reply":"2021-10-16T00:43:11.968652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model_path =[\n#  '../input/cnn2d-model/best_model0.747.h5',\n#  '../input/cnn2d-model/best_model0.741.h5',\n#  '../input/cnn2d-model/best_model0.739.h5',\n# '../input/cnn2d-model/best_model0.725.h5',]\nmodel_path = [\n '../input/cnn-model-0747/best_model0.747.h5',\n]\npred_list = []\nfor path in model_path:\n    model_best = tf.keras.models.load_model(filepath=path,custom_objects={\"loss\":loss,'leaky_relu': tf.nn.leaky_relu,'mish':mish})\n    sample = pd.read_csv('../input/rsna-miccai-brain-tumor-radiogenomic-classification/sample_submission.csv')\n    y_pred = model_best.predict(X_test)\n    #pred = np.argmax(y_pred, axis=1)\n    pred_list.append(y_pred)","metadata":{"execution":{"iopub.status.busy":"2021-10-16T00:43:57.533218Z","iopub.execute_input":"2021-10-16T00:43:57.533502Z","iopub.status.idle":"2021-10-16T00:44:00.84062Z","shell.execute_reply.started":"2021-10-16T00:43:57.533472Z","shell.execute_reply":"2021-10-16T00:44:00.839647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_best.summary()","metadata":{"execution":{"iopub.status.busy":"2021-10-16T00:44:11.951527Z","iopub.execute_input":"2021-10-16T00:44:11.951809Z","iopub.status.idle":"2021-10-16T00:44:11.973706Z","shell.execute_reply.started":"2021-10-16T00:44:11.951781Z","shell.execute_reply":"2021-10-16T00:44:11.972906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# a = (pred_list[0]+ pred_list[1]+ pred_list[2] + pred_list[3]+ pred_list[4])\n# #a = np.argmax(a, axis=1)\n\n# b = (pred_list[0]+ pred_list[1]+ pred_list[2] + pred_list[3])\n# #b = np.argmax(b, axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-10-16T00:44:17.896434Z","iopub.execute_input":"2021-10-16T00:44:17.896755Z","iopub.status.idle":"2021-10-16T00:44:17.904352Z","shell.execute_reply.started":"2021-10-16T00:44:17.896716Z","shell.execute_reply":"2021-10-16T00:44:17.903523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = pd.read_csv('../input/rsna-miccai-brain-tumor-radiogenomic-classification/sample_submission.csv')\n\ny_pred = sum(pred_list)/len(pred_list)#model_best.predict(X_test)\n# y_pred2 = model_best2.predict(X_test)\n# y_pred3 = model_best3.predict(X_test)\n\npred = np.argmax(y_pred, axis=1)\n# pred2 = np.argmax(y_pred2, axis=1)\n# pred3 = np.argmax(y_pred3, axis=1)\n\nresult=pd.DataFrame(testidt)\nresult[1]=pred #(pred+pred2+pred3)/3\n\nresult.columns=['BraTS21ID','MGMT_value']\nresult2 = result.groupby('BraTS21ID',as_index=False).mean()\nresult2['BraTS21ID'] = sample['BraTS21ID']\n# result2['MGMT_value'] = result2['MGMT_value'].apply(lambda x:round(x*10)/10)\n# result2['MGMT_value'][result2['MGMT_value']>0.8] = 1\n# result2['MGMT_value'][result2['MGMT_value']<0.2] = 0\nresult2.to_csv('submission.csv',index=False)\nresult2","metadata":{"execution":{"iopub.status.busy":"2021-10-16T00:43:21.492226Z","iopub.status.idle":"2021-10-16T00:43:21.492686Z","shell.execute_reply.started":"2021-10-16T00:43:21.492445Z","shell.execute_reply":"2021-10-16T00:43:21.492467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}