{"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":"","metadata":{},"execution_count":null,"outputs":[]},{"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\n# import numpy as np # linear algebra\n# import 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\n# import os\n# # for dirname, _, filenames in os.walk('/kaggle/input'):\n# #     for filename in filenames:\n# #         print(os.path.join(dirname, filename\n                           \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":"2021-08-08T04:14:11.648321Z","iopub.execute_input":"2021-08-08T04:14:11.648606Z","iopub.status.idle":"2021-08-08T04:14:11.652543Z","shell.execute_reply.started":"2021-08-08T04:14:11.648578Z","shell.execute_reply":"2021-08-08T04:14:11.651652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import numpy as np \n# import pandas as pd \n# import tensorflow as tf \n# import matplotlib.pyplot as plt \n# import cv2 as cv\n# from path import Path\n# import os \n# import glob\n# import tensorflow_hub as hub","metadata":{"execution":{"iopub.status.busy":"2021-08-08T08:29:44.607819Z","iopub.execute_input":"2021-08-08T08:29:44.608417Z","iopub.status.idle":"2021-08-08T08:29:44.614348Z","shell.execute_reply.started":"2021-08-08T08:29:44.60837Z","shell.execute_reply":"2021-08-08T08:29:44.613485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Exploring dataset ","metadata":{}},{"cell_type":"code","source":"# training_labels= pd.read_csv('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train_labels.csv')\n# sample_submission = pd.read_csv('../input/rsna-miccai-brain-tumor-radiogenomic-classification/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-08-08T08:29:45.600628Z","iopub.execute_input":"2021-08-08T08:29:45.601715Z","iopub.status.idle":"2021-08-08T08:29:45.619076Z","shell.execute_reply.started":"2021-08-08T08:29:45.601643Z","shell.execute_reply":"2021-08-08T08:29:45.617637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# MGMT promoter : \n     MGMT promotor is the measure of the methylation ","metadata":{}},{"cell_type":"code","source":"# training_labels","metadata":{"execution":{"iopub.status.busy":"2021-08-08T08:29:46.632597Z","iopub.execute_input":"2021-08-08T08:29:46.633266Z","iopub.status.idle":"2021-08-08T08:29:46.653756Z","shell.execute_reply.started":"2021-08-08T08:29:46.63323Z","shell.execute_reply":"2021-08-08T08:29:46.652589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# training_labels.value_counts('MGMT_value')","metadata":{"execution":{"iopub.status.busy":"2021-08-08T08:29:47.118484Z","iopub.execute_input":"2021-08-08T08:29:47.118977Z","iopub.status.idle":"2021-08-08T08:29:47.128874Z","shell.execute_reply.started":"2021-08-08T08:29:47.118947Z","shell.execute_reply":"2021-08-08T08:29:47.127921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# sample_submission.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-08T08:29:47.397864Z","iopub.execute_input":"2021-08-08T08:29:47.398213Z","iopub.status.idle":"2021-08-08T08:29:47.410775Z","shell.execute_reply.started":"2021-08-08T08:29:47.398185Z","shell.execute_reply":"2021-08-08T08:29:47.409698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# training_labels = training_labels.drop(np.where(training_labels['BraTS21ID'].to_numpy()==109)[0],axis = 0 )","metadata":{"execution":{"iopub.status.busy":"2021-08-08T08:29:47.625488Z","iopub.execute_input":"2021-08-08T08:29:47.626172Z","iopub.status.idle":"2021-08-08T08:29:47.632248Z","shell.execute_reply.started":"2021-08-08T08:29:47.626133Z","shell.execute_reply":"2021-08-08T08:29:47.631591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# training_labels = training_labels.drop(index = 487)","metadata":{"execution":{"iopub.status.busy":"2021-08-08T08:29:48.075493Z","iopub.execute_input":"2021-08-08T08:29:48.076101Z","iopub.status.idle":"2021-08-08T08:29:48.080769Z","shell.execute_reply.started":"2021-08-08T08:29:48.076066Z","shell.execute_reply":"2021-08-08T08:29:48.079879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# np.where(training_labels['BraTS21ID'].to_numpy()==109)","metadata":{"execution":{"iopub.status.busy":"2021-08-08T08:29:48.486424Z","iopub.execute_input":"2021-08-08T08:29:48.486755Z","iopub.status.idle":"2021-08-08T08:29:48.490375Z","shell.execute_reply.started":"2021-08-08T08:29:48.486726Z","shell.execute_reply":"2021-08-08T08:29:48.489286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def get_image(img_path, size = (244,244)):\n#     image = cv.imread(img_path)\n\n# #     image = plt.imread(img_path)\n# #     resized_image = cv.resize(image, size)\n# #     print(image.shape)\n#     resized_image = tf.image.resize(image, size)\n# #     im_rgb = cv.cvtColor(resized_image, cv.COLOR_BGR2RGB)\n# #     print(f'resized_image.shape{resized_image.shape}')\n# #     print(resized_image)\n#     return resized_image","metadata":{"execution":{"iopub.status.busy":"2021-08-08T08:29:48.696576Z","iopub.execute_input":"2021-08-08T08:29:48.696948Z","iopub.status.idle":"2021-08-08T08:29:48.701501Z","shell.execute_reply.started":"2021-08-08T08:29:48.696918Z","shell.execute_reply":"2021-08-08T08:29:48.700645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# NUM_IMAGES = 12800\n# def load_3d_images(scan_id, base_path = '../input/rsna-miccai-png/train/', data = 'FLAIR', num_imgs = 128):\n#     try : \n#         scan_id = scan_id.numpy().decode('utf-8')\n#     except: \n#         pass \n#     files = sorted(glob.glob(base_path + scan_id + '/' + data + '/*'))\n# #     print(len(files))\n#     middle = len(files) // 2 \n#     num_imgs2 = num_imgs//2\n#     p1 = max(0, middle - num_imgs2)\n#     p2 = min(len(files), middle + num_imgs2)\n# #     print(f'p1 and p2 is {p1} and {p2}')\n#     image_3d = []\n#     for img_path in files[p1:p2]:\n# #         print(img_path)\n#         image_3d.append(get_image(img_path))\n#     image_3d_stack = np.array(image_3d)\n# #     print(image_3d_stack)\n# #     print(f'image3d stack shape {image_3d_stack.shape}')\n#     if image_3d_stack.shape[0] < num_imgs:\n#         n_zero = np.zeros(( num_imgs - image_3d_stack.shape[0],244, 244,3))\n# #         print(f'n_zsro shape {n_zero.shape}and this thing num_imgs - image_3d_stack.shape[-1] is {num_imgs - image_3d_stack.shape[0]}')\n# #         print(f'image_3d_stack shape {image_3d_stack.shape}')\n#         image_3d_stack = np.concatenate((image_3d_stack,  n_zero))\n# #         print(f'image shape after stacking {image_3d_stack.shape}')\n        \n#     if np.min(image_3d_stack) < np.max(image_3d_stack):\n#         image_3d_stack = image_3d_stack - np.min(image_3d_stack)\n#         image_3d_stack = image_3d_stack / np.max(image_3d_stack)\n            \n# #     return np.expand_dims(image_3d_stack, axis = 0 )\n#     return image_3d_stack\n\n        \n        ","metadata":{"execution":{"iopub.status.busy":"2021-08-08T08:29:49.352107Z","iopub.execute_input":"2021-08-08T08:29:49.352584Z","iopub.status.idle":"2021-08-08T08:29:49.362227Z","shell.execute_reply.started":"2021-08-08T08:29:49.352555Z","shell.execute_reply":"2021-08-08T08:29:49.361156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# minim_flair  = 10000000\n# minim_t2w = 10000000\n# lower_flair_in_patient = 0\n# lower_t2w_in_patient = 0 \n# flair_numbers = []\n# t2w_numbers = []\n# for i in range(len(patients)):\n#     try:\n        \n#         file_path_flair = f'../input/rsna-miccai-png/train/{patients[i]}/FLAIR/'\n#         file_path_t2w = f'../input/rsna-miccai-png/train/{patients[i]}/T2w/'\n#         flair_data = len(os.listdir(file_path_flair))\n#         t2w_data = len(os.listdir(file_path_t2w))\n#         flair_numbers.append(flair_data)\n#         t2w_numbers.append(t2w_data)\n#         if minim_flair > flair_data:\n#             lower_flair_in_patient = patients[i]\n#             minim_flair = min(minim_flair,flair_data )\n#         if minim_t2w > t2w_data:\n#             lower_t2w_in_patient = patients[i]\n#             minim_t2w = min(minim_t2w , t2w_data)\n#     except:\n#         print('flair / t2w missing')\n    \n# print(f'minimum number of data in flair images is {minim_flair} in {lower_flair_in_patient}')\n# print(f'minimum number of data in t2w images is {minim_t2w} in {lower_t2w_in_patient}')","metadata":{"execution":{"iopub.status.busy":"2021-08-08T08:29:49.562535Z","iopub.execute_input":"2021-08-08T08:29:49.563033Z","iopub.status.idle":"2021-08-08T08:29:49.595662Z","shell.execute_reply.started":"2021-08-08T08:29:49.563001Z","shell.execute_reply":"2021-08-08T08:29:49.594233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# lab = []\n# for x in training_labels['BraTS21ID'].to_numpy():\n#     y = str(str(x).zfill(5))\n#     lab.append(y)","metadata":{"execution":{"iopub.status.busy":"2021-08-08T08:29:50.321316Z","iopub.execute_input":"2021-08-08T08:29:50.321665Z","iopub.status.idle":"2021-08-08T08:29:50.329073Z","shell.execute_reply.started":"2021-08-08T08:29:50.321634Z","shell.execute_reply":"2021-08-08T08:29:50.327961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# lab[:10]","metadata":{"execution":{"iopub.status.busy":"2021-08-08T08:29:50.908428Z","iopub.execute_input":"2021-08-08T08:29:50.908775Z","iopub.status.idle":"2021-08-08T08:29:50.915063Z","shell.execute_reply.started":"2021-08-08T08:29:50.908745Z","shell.execute_reply":"2021-08-08T08:29:50.91405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # @tf.function()\n# def blocks(scan_id):\n#     print(scan_id)\n#     base_path = '../input/rsna-miccai-png/train/'\n#     data = 'FLAIR'\n#     num_imgs = 128\n#     try : \n#         scan_id = scan_id.numpy().decode('utf-8')\n#     except: \n#         pass \n#     return  sorted(glob.glob(base_path + str(scan_id) + '/' + data + '/*'))\n\n","metadata":{"execution":{"iopub.status.busy":"2021-08-08T08:30:30.608692Z","iopub.execute_input":"2021-08-08T08:30:30.609043Z","iopub.status.idle":"2021-08-08T08:30:30.61459Z","shell.execute_reply.started":"2021-08-08T08:30:30.609015Z","shell.execute_reply":"2021-08-08T08:30:30.613595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# blocks('00000')","metadata":{"execution":{"iopub.status.busy":"2021-08-08T08:30:31.035858Z","iopub.execute_input":"2021-08-08T08:30:31.036455Z","iopub.status.idle":"2021-08-08T08:30:31.050947Z","shell.execute_reply.started":"2021-08-08T08:30:31.036421Z","shell.execute_reply":"2021-08-08T08:30:31.05026Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # input_data = tf.data.Dataset.from_tensor_slices(\n# #     lab)\n\n# # input_data = tf.data.Dataset.from_tensor_slices(lab).map(lambda x: tf.py_function(load_3d_images, [x], tf.float32))\n\n# input_data = tf.data.Dataset.from_tensor_slices(lab).map(blocks)","metadata":{"execution":{"iopub.status.busy":"2021-08-08T08:30:31.53098Z","iopub.execute_input":"2021-08-08T08:30:31.531537Z","iopub.status.idle":"2021-08-08T08:30:31.642933Z","shell.execute_reply.started":"2021-08-08T08:30:31.531504Z","shell.execute_reply":"2021-08-08T08:30:31.641609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for x in input_data.take(1):\n#     print(x)","metadata":{"execution":{"iopub.status.busy":"2021-08-08T08:30:32.20347Z","iopub.execute_input":"2021-08-08T08:30:32.204052Z","iopub.status.idle":"2021-08-08T08:30:32.228351Z","shell.execute_reply.started":"2021-08-08T08:30:32.204018Z","shell.execute_reply":"2021-08-08T08:30:32.226973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# missing_index = []\n# count = 0\n# for x in lab:\n#     try:\n#         x = load_3d_images(x)\n#     except:\n#         missing_index.append(count)\n#     count +=1 \n    ","metadata":{"execution":{"iopub.status.busy":"2021-08-07T16:41:49.548351Z","iopub.execute_input":"2021-08-07T16:41:49.548666Z","iopub.status.idle":"2021-08-07T16:41:49.552501Z","shell.execute_reply.started":"2021-08-07T16:41:49.548638Z","shell.execute_reply":"2021-08-07T16:41:49.551324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# missing_index","metadata":{"execution":{"iopub.status.busy":"2021-08-07T16:41:50.053737Z","iopub.execute_input":"2021-08-07T16:41:50.054286Z","iopub.status.idle":"2021-08-07T16:41:50.060067Z","shell.execute_reply.started":"2021-08-07T16:41:50.054245Z","shell.execute_reply":"2021-08-07T16:41:50.059104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# training_outputs = tf.data.Dataset.from_tensor_slices(tf.expand_dims(training_labels['MGMT_value'].to_numpy(),axis = 1))","metadata":{"execution":{"iopub.status.busy":"2021-08-07T16:41:50.454629Z","iopub.execute_input":"2021-08-07T16:41:50.454976Z","iopub.status.idle":"2021-08-07T16:41:50.46065Z","shell.execute_reply.started":"2021-08-07T16:41:50.454948Z","shell.execute_reply":"2021-08-07T16:41:50.459603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# training_data = tf.data.Dataset.zip((input_data, training_outputs))","metadata":{"execution":{"iopub.status.busy":"2021-08-07T16:41:51.285024Z","iopub.execute_input":"2021-08-07T16:41:51.285356Z","iopub.status.idle":"2021-08-07T16:41:51.290354Z","shell.execute_reply.started":"2021-08-07T16:41:51.285326Z","shell.execute_reply":"2021-08-07T16:41:51.289341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# training_data.element_spec","metadata":{"execution":{"iopub.status.busy":"2021-08-07T17:07:42.460675Z","iopub.execute_input":"2021-08-07T17:07:42.461041Z","iopub.status.idle":"2021-08-07T17:07:42.466253Z","shell.execute_reply.started":"2021-08-07T17:07:42.461011Z","shell.execute_reply":"2021-08-07T17:07:42.465351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# base_layer = hub.KerasLayer('https://tfhub.dev/google/efficientnet/b5/classification/1',trainable = False)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T16:41:52.1522Z","iopub.execute_input":"2021-08-07T16:41:52.15251Z","iopub.status.idle":"2021-08-07T16:42:01.137758Z","shell.execute_reply.started":"2021-08-07T16:41:52.152483Z","shell.execute_reply":"2021-08-07T16:42:01.136894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# inputs = tf.keras.layers.Input(shape = (244, 244,3))\n# x = base_layer(inputs)\n# # x = tf.keras.layers.Conv2D(128,3,activation ='relu')(x)\n# # x = tf.keras.layers.GlobalMaxPooling2D()(x)\n# x = tf.keras.layers.Dense(128, activation ='relu')(x)\n\n\n# # input_2 = tf.keras.layers.Input(shape = [3,])\n# # print(input_2.shape)\n# # y = tf.keras.layers.Flatten(input_2)\n# # y = tf.keras.layers.Dense(128,activation ='relu')(input_2)\n\n# # concat = tf.keras.layers.concatenate([x,y])\n\n# # z = tf.keras.layers.Dense(64,activation= 'relu')(concat)\n\n# outputs = tf.keras.layers.Dense(1, activation = 'softmax')(x)\n\n# model =tf.keras.Model(inputs,outputs)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T16:42:01.139095Z","iopub.execute_input":"2021-08-07T16:42:01.139419Z","iopub.status.idle":"2021-08-07T16:42:01.424054Z","shell.execute_reply.started":"2021-08-07T16:42:01.139386Z","shell.execute_reply":"2021-08-07T16:42:01.423198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# tf.keras.utils.plot_model(model,show_shapes = True)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T16:42:01.425834Z","iopub.execute_input":"2021-08-07T16:42:01.426233Z","iopub.status.idle":"2021-08-07T16:42:01.610087Z","shell.execute_reply.started":"2021-08-07T16:42:01.426195Z","shell.execute_reply":"2021-08-07T16:42:01.609081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model.compile(loss = tf.keras.losses.binary_crossentropy,\n#              optimizer = tf.keras.optimizers.Adam(),\n#              metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-08-07T16:42:01.611887Z","iopub.execute_input":"2021-08-07T16:42:01.612257Z","iopub.status.idle":"2021-08-07T16:42:01.62677Z","shell.execute_reply.started":"2021-08-07T16:42:01.612218Z","shell.execute_reply":"2021-08-07T16:42:01.625975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model.fit(training_data,epochs = 5 )","metadata":{"execution":{"iopub.status.busy":"2021-08-07T16:42:01.628296Z","iopub.execute_input":"2021-08-07T16:42:01.628647Z","iopub.status.idle":"2021-08-07T16:51:59.642411Z","shell.execute_reply.started":"2021-08-07T16:42:01.628612Z","shell.execute_reply":"2021-08-07T16:51:59.63983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# working on total new approach ","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport tensorflow as tf \nimport matplotlib.pyplot as plt \nimport cv2 as cv\nfrom path import Path\nimport os \nimport glob\nimport tensorflow_hub as hub\nimport os ","metadata":{"execution":{"iopub.status.busy":"2021-08-10T13:23:36.099268Z","iopub.execute_input":"2021-08-10T13:23:36.099621Z","iopub.status.idle":"2021-08-10T13:23:40.914218Z","shell.execute_reply.started":"2021-08-10T13:23:36.099591Z","shell.execute_reply":"2021-08-10T13:23:40.913397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df= pd.read_csv('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train_labels.csv')\nsample_df = pd.read_csv('../input/rsna-miccai-brain-tumor-radiogenomic-classification/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-08-10T13:23:40.915710Z","iopub.execute_input":"2021-08-10T13:23:40.916032Z","iopub.status.idle":"2021-08-10T13:23:40.937576Z","shell.execute_reply.started":"2021-08-10T13:23:40.915997Z","shell.execute_reply":"2021-08-10T13:23:40.936801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-10T13:23:40.940968Z","iopub.execute_input":"2021-08-10T13:23:40.941219Z","iopub.status.idle":"2021-08-10T13:23:40.964188Z","shell.execute_reply.started":"2021-08-10T13:23:40.941195Z","shell.execute_reply":"2021-08-10T13:23:40.963463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['BraTS21ID'][-2:]","metadata":{"execution":{"iopub.status.busy":"2021-08-10T13:23:40.966772Z","iopub.execute_input":"2021-08-10T13:23:40.967013Z","iopub.status.idle":"2021-08-10T13:23:40.976083Z","shell.execute_reply.started":"2021-08-10T13:23:40.966990Z","shell.execute_reply":"2021-08-10T13:23:40.975197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files = glob.glob('../input/rsna-miccai-png/train/*/FLAIR/*.png')","metadata":{"execution":{"iopub.status.busy":"2021-08-10T13:23:40.977438Z","iopub.execute_input":"2021-08-10T13:23:40.977829Z","iopub.status.idle":"2021-08-10T13:23:49.662189Z","shell.execute_reply.started":"2021-08-10T13:23:40.977803Z","shell.execute_reply":"2021-08-10T13:23:49.661347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image","metadata":{"execution":{"iopub.status.busy":"2021-08-10T13:23:49.663544Z","iopub.execute_input":"2021-08-10T13:23:49.663884Z","iopub.status.idle":"2021-08-10T13:23:49.670822Z","shell.execute_reply.started":"2021-08-10T13:23:49.663850Z","shell.execute_reply":"2021-08-10T13:23:49.670056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def get_images(x):\n#     print(x)\n#     image = tf.io.read_file(x)\n#     image = tf.io.decode_image(image,expand_animations = False)\n# #     image = np.array(Image.open(x).convert('RGB'))\\\n#     image = tf.keras.preprocessing.image.img_to_array(image)\n\n\n#     return image \n\ndef get_images(x):\n    print(x)\n    image = tf.io.read_file(x)\n    image = tf.io.decode_image(image,expand_animations = False,channels = 1)\n    image = tf.image.resize(image , [128,128])\n    return image ","metadata":{"execution":{"iopub.status.busy":"2021-08-10T13:23:49.672728Z","iopub.execute_input":"2021-08-10T13:23:49.673068Z","iopub.status.idle":"2021-08-10T13:23:49.678922Z","shell.execute_reply.started":"2021-08-10T13:23:49.673033Z","shell.execute_reply":"2021-08-10T13:23:49.677846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(get_images(files[0]))\nget_images(files[0]).shape","metadata":{"execution":{"iopub.status.busy":"2021-08-10T13:23:49.680597Z","iopub.execute_input":"2021-08-10T13:23:49.681117Z","iopub.status.idle":"2021-08-10T13:23:51.935147Z","shell.execute_reply.started":"2021-08-10T13:23:49.681079Z","shell.execute_reply":"2021-08-10T13:23:51.934224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_inputs = tf.data.Dataset.from_tensor_slices(files).map(get_images)","metadata":{"execution":{"iopub.status.busy":"2021-08-10T13:23:51.936510Z","iopub.execute_input":"2021-08-10T13:23:51.936852Z","iopub.status.idle":"2021-08-10T13:23:52.207539Z","shell.execute_reply.started":"2021-08-10T13:23:51.936816Z","shell.execute_reply":"2021-08-10T13:23:52.206552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Path(files[0]).parts()[4]","metadata":{"execution":{"iopub.status.busy":"2021-08-10T13:23:52.208989Z","iopub.execute_input":"2021-08-10T13:23:52.209517Z","iopub.status.idle":"2021-08-10T13:23:52.216371Z","shell.execute_reply.started":"2021-08-10T13:23:52.209462Z","shell.execute_reply":"2021-08-10T13:23:52.215395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mgmt_values = []\n\nfor x in files:\n    brat_id = int(Path(x).parts()[4])\n    mgmt_values.append(train_df[train_df['BraTS21ID']==brat_id]['MGMT_value'].to_numpy()[0])\n    ","metadata":{"execution":{"iopub.status.busy":"2021-08-10T13:23:52.217695Z","iopub.execute_input":"2021-08-10T13:23:52.218057Z","iopub.status.idle":"2021-08-10T13:24:24.765436Z","shell.execute_reply.started":"2021-08-10T13:23:52.218021Z","shell.execute_reply":"2021-08-10T13:24:24.764603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_outputs = tf.data.Dataset.from_tensor_slices(mgmt_values)","metadata":{"execution":{"iopub.status.busy":"2021-08-10T13:24:24.766608Z","iopub.execute_input":"2021-08-10T13:24:24.766937Z","iopub.status.idle":"2021-08-10T13:24:24.958078Z","shell.execute_reply.started":"2021-08-10T13:24:24.766905Z","shell.execute_reply":"2021-08-10T13:24:24.957256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_outputs.element_spec","metadata":{"execution":{"iopub.status.busy":"2021-08-10T13:24:24.961118Z","iopub.execute_input":"2021-08-10T13:24:24.961621Z","iopub.status.idle":"2021-08-10T13:24:24.967016Z","shell.execute_reply.started":"2021-08-10T13:24:24.961582Z","shell.execute_reply":"2021-08-10T13:24:24.966087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = tf.data.Dataset.zip((train_inputs,train_outputs)).batch(20).prefetch(tf.data.AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2021-08-10T13:24:24.968506Z","iopub.execute_input":"2021-08-10T13:24:24.968895Z","iopub.status.idle":"2021-08-10T13:24:24.982233Z","shell.execute_reply.started":"2021-08-10T13:24:24.968852Z","shell.execute_reply":"2021-08-10T13:24:24.981370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# base_layer = hub.KerasLayer('https://tfhub.dev/google/efficientnet/b5/classification/1',trainable = False)","metadata":{"execution":{"iopub.status.busy":"2021-08-10T13:24:24.983504Z","iopub.execute_input":"2021-08-10T13:24:24.984011Z","iopub.status.idle":"2021-08-10T13:24:24.989510Z","shell.execute_reply.started":"2021-08-10T13:24:24.983969Z","shell.execute_reply":"2021-08-10T13:24:24.988383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ninputs = tf.keras.layers.Input(shape = (128,128,1))\n# x = tf.keras.layers.Lambda(lambda x : cv.merge((x,x,x)))(inputs)\n# x = base_layer(inputs)\nx = tf.keras.layers.Conv2D(128,3,activation ='relu')(inputs)\nx = tf.keras.layers.GlobalMaxPooling2D()(x)\nx = tf.keras.layers.Flatten()(x)\nx = tf.keras.layers.Dense(128, activation ='relu')(x)\nx = tf.keras.layers.Dense(64,activation = 'relu')(x)\nx = tf.keras.layers.Dense(32, activation = 'relu')(x)\n\n# input_2 = tf.keras.layers.Input(shape = [3,])\n# print(input_2.shape)\n# y = tf.keras.layers.Flatten(input_2)\n# y = tf.keras.layers.Dense(128,activation ='relu')(input_2)\n\n# concat = tf.keras.layers.concatenate([x,y])\n\n# z = tf.keras.layers.Dense(64,activation= 'relu')(concat)\n\noutputs = tf.keras.layers.Dense(1, activation = 'softmax')(x)\n\nmodel =tf.keras.Model(inputs,outputs)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-08-10T13:24:24.990843Z","iopub.execute_input":"2021-08-10T13:24:24.991420Z","iopub.status.idle":"2021-08-10T13:24:25.075718Z","shell.execute_reply.started":"2021-08-10T13:24:24.991384Z","shell.execute_reply":"2021-08-10T13:24:25.074994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss = tf.keras.losses.binary_crossentropy,\n             optimizer = tf.keras.optimizers.Adam(),\n             metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-08-10T13:24:25.078515Z","iopub.execute_input":"2021-08-10T13:24:25.078748Z","iopub.status.idle":"2021-08-10T13:24:25.092254Z","shell.execute_reply.started":"2021-08-10T13:24:25.078725Z","shell.execute_reply":"2021-08-10T13:24:25.091434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(train_dataset,\n         epochs = 15)","metadata":{"execution":{"iopub.status.busy":"2021-08-10T13:24:25.093624Z","iopub.execute_input":"2021-08-10T13:24:25.093995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_files = glob.glob('../input/rsna-miccai-png/test/*/FLAIR/*.png')","metadata":{"execution":{"iopub.status.busy":"2021-08-10T12:24:09.069194Z","iopub.execute_input":"2021-08-10T12:24:09.069566Z","iopub.status.idle":"2021-08-10T12:24:10.646809Z","shell.execute_reply.started":"2021-08-10T12:24:09.069529Z","shell.execute_reply":"2021-08-10T12:24:10.645631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = tf.data.Dataset.from_tensor_slices(test_files).map(get_images).batch(32)","metadata":{"execution":{"iopub.status.busy":"2021-08-10T12:27:19.859352Z","iopub.execute_input":"2021-08-10T12:27:19.859675Z","iopub.status.idle":"2021-08-10T12:27:19.900129Z","shell.execute_reply.started":"2021-08-10T12:27:19.859647Z","shell.execute_reply":"2021-08-10T12:27:19.899286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(test_dataset)","metadata":{"execution":{"iopub.status.busy":"2021-08-10T12:27:20.479227Z","iopub.execute_input":"2021-08-10T12:27:20.479566Z","iopub.status.idle":"2021-08-10T12:28:40.728488Z","shell.execute_reply.started":"2021-08-10T12:27:20.479539Z","shell.execute_reply":"2021-08-10T12:28:40.727605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(tf.squeeze(predictions).numpy()).value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-08-10T12:32:51.324702Z","iopub.execute_input":"2021-08-10T12:32:51.325086Z","iopub.status.idle":"2021-08-10T12:32:51.340091Z","shell.execute_reply.started":"2021-08-10T12:32:51.325050Z","shell.execute_reply":"2021-08-10T12:32:51.338958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(os.listdir('../input/rsna-miccai-png/test'))","metadata":{"execution":{"iopub.status.busy":"2021-08-10T12:33:56.630630Z","iopub.execute_input":"2021-08-10T12:33:56.630977Z","iopub.status.idle":"2021-08-10T12:33:56.638877Z","shell.execute_reply.started":"2021-08-10T12:33:56.630944Z","shell.execute_reply":"2021-08-10T12:33:56.637906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_df = sample_df.drop('MGMT_value',axis = 1 )","metadata":{"execution":{"iopub.status.busy":"2021-08-10T12:36:12.773956Z","iopub.execute_input":"2021-08-10T12:36:12.774334Z","iopub.status.idle":"2021-08-10T12:36:12.782157Z","shell.execute_reply.started":"2021-08-10T12:36:12.774303Z","shell.execute_reply":"2021-08-10T12:36:12.781097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_df","metadata":{"execution":{"iopub.status.busy":"2021-08-10T12:36:18.495939Z","iopub.execute_input":"2021-08-10T12:36:18.496363Z","iopub.status.idle":"2021-08-10T12:36:18.506517Z","shell.execute_reply.started":"2021-08-10T12:36:18.496330Z","shell.execute_reply":"2021-08-10T12:36:18.505607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = [1.0] * 87","metadata":{"execution":{"iopub.status.busy":"2021-08-10T12:35:14.109748Z","iopub.execute_input":"2021-08-10T12:35:14.110114Z","iopub.status.idle":"2021-08-10T12:35:14.115347Z","shell.execute_reply.started":"2021-08-10T12:35:14.110062Z","shell.execute_reply":"2021-08-10T12:35:14.114019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_df.insert(1,'MGMT_value',preds)","metadata":{"execution":{"iopub.status.busy":"2021-08-10T12:36:28.828859Z","iopub.execute_input":"2021-08-10T12:36:28.829233Z","iopub.status.idle":"2021-08-10T12:36:28.839857Z","shell.execute_reply.started":"2021-08-10T12:36:28.829192Z","shell.execute_reply":"2021-08-10T12:36:28.838276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_df.to_csv('submission.csv',index = False)","metadata":{"execution":{"iopub.status.busy":"2021-08-10T12:36:53.354280Z","iopub.execute_input":"2021-08-10T12:36:53.354595Z","iopub.status.idle":"2021-08-10T12:36:53.363309Z","shell.execute_reply.started":"2021-08-10T12:36:53.354567Z","shell.execute_reply":"2021-08-10T12:36:53.362410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}