{"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 pydicom\nimport pandas as pd\nimport numpy as np\nimport cv2\nimport math\ndata_dir = \"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/\"\npatients = os.listdir(data_dir)","metadata":{"execution":{"iopub.status.busy":"2021-10-02T13:00:02.5957Z","iopub.execute_input":"2021-10-02T13:00:02.596502Z","iopub.status.idle":"2021-10-02T13:00:03.023293Z","shell.execute_reply.started":"2021-10-02T13:00:02.596415Z","shell.execute_reply":"2021-10-02T13:00:03.021965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def average(l):\n    return sum(l)/len(l)\n    \ndef layers(l,n):\n    for i in range(0, len(l), n):\n        yield l[i:i + n]\n\nm2 = [22, 23, 24, 36, 37, 38, 39, 40, 50, 51, 52, 53, 54, 55, 56, 65, 66, 67, 68, 69, 70, 81, 82, 83, 84, 97, 98]\nm3 = [19, 20, 21, 25, 26, 33, 34, 35, 41, 42, 49]\nm4 = [18, 27, 28]\nm5 = [17]\n\ndef adjuster(file):\n    if len(file) in m5:\n        n = 5\n    elif len(file) in m4:\n        n = 4\n    elif len(file) in m3:\n        n = 3\n    elif len(file) in m2:\n        n = 2\n    else:\n        n = 1\n    new_file = []\n    for i in range(len(file)):\n        for j in range(n):\n            new_file.append(file[i])\n    return new_file       \n\ndef decider(length, layer_number):\n    return math.ceil(length/layer_number)\n   \ndef adjuster2(layers, layer_number):\n    if len(layers) == layer_number - 1:\n        layers.append(layers[-1])","metadata":{"execution":{"iopub.status.busy":"2021-10-02T13:00:05.384175Z","iopub.execute_input":"2021-10-02T13:00:05.384572Z","iopub.status.idle":"2021-10-02T13:00:05.407956Z","shell.execute_reply.started":"2021-10-02T13:00:05.384527Z","shell.execute_reply":"2021-10-02T13:00:05.405754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"layer_number=16\nc=1\nInput_Values = []\nfor p in patients[:]:\n    if p != '00109' and p != '00123' and p != '00709':\n        scan=[]\n\n        flair = [\n            cv2.resize(pydicom.read_file(data_dir + p + '/FLAIR/'+ layer).pixel_array,(64,64)) \n            for layer in os.listdir(data_dir + p + '/FLAIR/')]\n\n        new_flair=[]\n        flair = adjuster(flair)\n        layer_size = decider(len(flair) , layer_number)\n        for layer_chunk in layers(flair, layer_size):\n            layer_chunk = list(map(average, zip(*layer_chunk)))\n            new_flair.append(layer_chunk)\n        adjuster2(new_flair, layer_number)\n\n        T1w = [\n            cv2.resize(pydicom.read_file(data_dir + p + '/T1w/'+ layer).pixel_array,(64,64)) \n            for layer in os.listdir(data_dir + p + '/T1w/')]\n\n        new_T1w = []\n        T1w=adjuster(T1w)\n        layer_size = decider(len(T1w) , layer_number)\n        for layer_chunk in layers(T1w, layer_size):\n            layer_chunk = list(map(average, zip(*layer_chunk)))\n            new_T1w.append(layer_chunk)\n\n        adjuster2(new_T1w, layer_number)\n\n        T1wCE = [\n            cv2.resize(pydicom.read_file(data_dir + p + '/T1wCE/'+ layer).pixel_array,(64,64)) \n            for layer in os.listdir(data_dir + p + '/T1wCE/')]\n\n        new_T1wCE = []\n        T1wCE=adjuster(T1wCE)\n        layer_size = decider(len(T1wCE) , layer_number)\n        for layer_chunk in layers(T1wCE, layer_size):\n            layer_chunk = list(map(average, zip(*layer_chunk)))\n            new_T1wCE.append(layer_chunk)\n\n        adjuster2(new_T1wCE, layer_number)\n\n        T2w = [\n            cv2.resize(pydicom.read_file(data_dir + p + '/T2w/'+ layer).pixel_array,(64,64)) \n            for layer in os.listdir(data_dir + p + '/T2w/')]\n\n        new_T2w=[]\n        T2w=adjuster(T2w)\n        layer_size = decider(len(T2w) , layer_number)\n        for layer_chunk in layers(T2w, layer_size):\n            layer_chunk = list(map(average, zip(*layer_chunk)))\n            new_T2w.append(layer_chunk)\n\n        adjuster2(new_T2w, layer_number)\n\n        for i in range(0,layer_number):\n            x=[]\n            for j in range(64):\n                y =[]\n                for k in range(64):\n                    channel=[]\n                    channel.append(new_flair[i][j][k])\n                    channel.append(new_T1w[i][j][k])\n                    channel.append(new_T1wCE[i][j][k])\n                    channel.append(new_T2w[i][j][k])\n                    y.append(channel)\n                x.append(y)\n            scan.append(np.array(x))\n        Input_Values.append(scan)\n        print(str(c) +' Done')\n        c+=1\n\nnp.shape(Input_Values)","metadata":{"execution":{"iopub.status.busy":"2021-10-02T13:00:13.348718Z","iopub.execute_input":"2021-10-02T13:00:13.349066Z","iopub.status.idle":"2021-10-02T13:57:03.772022Z","shell.execute_reply.started":"2021-10-02T13:00:13.349029Z","shell.execute_reply":"2021-10-02T13:57:03.77102Z"},"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.drop([71,81,488])\nOutput = df['MGMT_value']\nnp.shape(Output)","metadata":{"execution":{"iopub.status.busy":"2021-10-02T13:57:53.793676Z","iopub.execute_input":"2021-10-02T13:57:53.794021Z","iopub.status.idle":"2021-10-02T13:57:53.837339Z","shell.execute_reply.started":"2021-10-02T13:57:53.793991Z","shell.execute_reply":"2021-10-02T13:57:53.836234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Output_Values = []\nfor i in range(585):\n    if i != 71 and i != 81 and i != 488:\n        Output_Values.append(Output[i])\nnp.array(Output_Values)","metadata":{"execution":{"iopub.status.busy":"2021-10-02T13:57:59.894083Z","iopub.execute_input":"2021-10-02T13:57:59.894881Z","iopub.status.idle":"2021-10-02T13:57:59.912781Z","shell.execute_reply.started":"2021-10-02T13:57:59.894832Z","shell.execute_reply":"2021-10-02T13:57:59.911731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Output_Values = np.reshape(Output_Values,(582,1))\nInput_Values = np.array(Input_Values)\nOutput_Values = np.array(Output_Values)\nnp.save('./Input_Values_64x64',Input_Values)\nprint(np.shape(Input_Values))\nprint(np.shape(Output_Values))","metadata":{"execution":{"iopub.status.busy":"2021-10-02T14:00:46.04458Z","iopub.execute_input":"2021-10-02T14:00:46.0452Z","iopub.status.idle":"2021-10-02T14:00:48.141221Z","shell.execute_reply.started":"2021-10-02T14:00:46.045112Z","shell.execute_reply":"2021-10-02T14:00:48.138997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.layers import Conv3D, MaxPooling3D, BatchNormalization, Activation, Dropout, Flatten, Dense\nfrom tensorflow.keras.models import Sequential, load_model\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2021-10-09T17:08:52.732851Z","iopub.execute_input":"2021-10-09T17:08:52.733538Z","iopub.status.idle":"2021-10-09T17:08:57.194727Z","shell.execute_reply.started":"2021-10-09T17:08:52.733498Z","shell.execute_reply":"2021-10-09T17:08:57.194012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv3D(32, (3, 3, 3), activation = \"relu\", input_shape=(16, 64, 64, 4)))\nmodel.add(MaxPooling3D(pool_size=2))\nmodel.add(BatchNormalization())\nmodel.add(Conv3D(64, (3, 3, 3), activation = \"relu\"))\nmodel.add(MaxPooling3D(pool_size=(1, 2, 2)))\nmodel.add(BatchNormalization())\nmodel.add(Conv3D(128, (3, 3, 3), activation = \"relu\"))\nmodel.add(MaxPooling3D(pool_size=(1, 2, 2)))\nmodel.add(BatchNormalization())\nmodel.add(Conv3D(256, (3, 3, 3), activation = \"relu\"))\nmodel.add(MaxPooling3D(pool_size=(1, 2, 2)))\nmodel.add(BatchNormalization())\nmodel.add(Flatten())\nmodel.add(Dense(256, activation = \"relu\" ))\nmodel.add(Dropout(0.3))\nmodel.add(Dense(1, activation = \"sigmoid\" ))\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-10-09T17:11:32.150101Z","iopub.execute_input":"2021-10-09T17:11:32.15039Z","iopub.status.idle":"2021-10-09T17:11:32.275046Z","shell.execute_reply.started":"2021-10-09T17:11:32.15035Z","shell.execute_reply":"2021-10-09T17:11:32.274418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"opt = tf.keras.optimizers.SGD(learning_rate=0.0003)\nmodel.compile( loss='binary_crossentropy' , metrics=['accuracy'], optimizer=opt)","metadata":{"execution":{"iopub.status.busy":"2021-10-02T14:01:22.914569Z","iopub.execute_input":"2021-10-02T14:01:22.914955Z","iopub.status.idle":"2021-10-02T14:01:22.935203Z","shell.execute_reply.started":"2021-10-02T14:01:22.91491Z","shell.execute_reply":"2021-10-02T14:01:22.934264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(Input_Values,Output_Values,validation_split=0.1, epochs=500, batch_size=20)\n\nprint(history.history.keys())\n\nplt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('Model Accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['Training set', 'Validation set'], loc='upper left')\nplt.show()\n\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('Model Loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['Training set', 'Validation set'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-02T14:07:45.707823Z","iopub.execute_input":"2021-10-02T14:07:45.708219Z","iopub.status.idle":"2021-10-02T14:07:45.878526Z","shell.execute_reply.started":"2021-10-02T14:07:45.708099Z","shell.execute_reply":"2021-10-02T14:07:45.87689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('./Model5')","metadata":{},"execution_count":null,"outputs":[]}]}