{"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":"# RSNA 2022 Cerical Spine Fracture Detection\n**CSCI217 Project**","metadata":{}},{"cell_type":"markdown","source":"## Import Libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nimport tensorflow as tf\nimport tensorflow.keras.layers as tfl\nfrom tensorflow.keras import backend as K\nfrom sklearn.model_selection import StratifiedKFold\nfrom tensorflow.keras.preprocessing.image import load_img, img_to_array\nfrom tensorflow.keras.applications import EfficientNetB0\n\nimport os\nimport cv2\nimport glob\nimport pydicom as dicom\nimport nibabel as nib\nimport sys","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-01-06T18:16:20.68347Z","iopub.execute_input":"2023-01-06T18:16:20.684329Z","iopub.status.idle":"2023-01-06T18:16:27.650287Z","shell.execute_reply.started":"2023-01-06T18:16:20.684203Z","shell.execute_reply":"2023-01-06T18:16:27.649324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load Data","metadata":{}},{"cell_type":"markdown","source":"#### Load dataframes","metadata":{}},{"cell_type":"code","source":"df_train = pd.read_csv(\"/kaggle/input/rsna-2022-cervical-spine-fracture-detection/train.csv\")\ndf_test = pd.DataFrame({\"row_id\": ['1.2.826.0.1.3680043.22327_C1', '1.2.826.0.1.3680043.25399_C1', '1.2.826.0.1.3680043.5876_C1'], \n                        \"StudyInstanceUID\": ['1.2.826.0.1.3680043.22327', '1.2.826.0.1.3680043.25399', '1.2.826.0.1.3680043.5876'], \n                        \"prediction_type\": [\"C1\", \"C1\", \"C1\"]})  \n\ntrain_images_dir = '/kaggle/input/rsna-2022-cervical-spine-fracture-detection/train_images'\ntest_images_dir = '/kaggle/input/rsna-2022-cervical-spine-fracture-detection/test_images'\n\nnew_submission = []\nmeans = dict(zip(df_train.columns[1:], np.average(df_train.iloc[:,1:], axis=0, weights=df_train[\"patient_overall\"] + 1)))\nprediction_type = df_test['prediction_type'].tolist()\nsubmission = pd.read_csv('/kaggle/input/rsna-2022-cervical-spine-fracture-detection/sample_submission.csv')\nfor i in range(len(submission)):        \n    new_submission.append(means[prediction_type[i]])\nsubmission['fractured'] = new_submission\n\nprediction_type_mapping = df_test['prediction_type'].map({'C1': 0, 'C2': 1, 'C3': 2, 'C4': 3, 'C5': 4, 'C6': 5, 'C7': 6}).values\n\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-06T18:16:27.652156Z","iopub.execute_input":"2023-01-06T18:16:27.652958Z","iopub.status.idle":"2023-01-06T18:16:27.709369Z","shell.execute_reply.started":"2023-01-06T18:16:27.65292Z","shell.execute_reply":"2023-01-06T18:16:27.708511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Load Dicom Helper Function","metadata":{}},{"cell_type":"code","source":"def load_dicom(path, size = 64):\n    img=dicom.dcmread(path)\n    img.PhotometricInterpretation = 'YBR_FULL'\n    data=img.pixel_array\n    data=data-np.min(data)\n    if np.max(data) != 0:\n        data=data/np.max(data)\n    data=(data*255).astype(np.uint8)        \n    return cv2.cvtColor(data.reshape(512, 512), cv2.COLOR_GRAY2RGB)\n\n    \npatients = sorted(os.listdir(train_images_dir))","metadata":{"execution":{"iopub.status.busy":"2023-01-06T18:16:27.711216Z","iopub.execute_input":"2023-01-06T18:16:27.711811Z","iopub.status.idle":"2023-01-06T18:16:27.812013Z","shell.execute_reply.started":"2023-01-06T18:16:27.711776Z","shell.execute_reply":"2023-01-06T18:16:27.811164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### visualize Images","metadata":{}},{"cell_type":"code","source":"image_file = glob.glob(\"/kaggle/input/rsna-2022-cervical-spine-fracture-detection/train_images/1.2.826.0.1.3680043.10001/*.dcm\")\nplt.figure(figsize=(20, 10))\n\nfor i in range(16):\n    ax = plt.subplot(4, 4, i + 1)\n    image_path = image_file[i]\n    image = load_dicom(image_path)\n    plt.axis('off')   \n    plt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2023-01-06T18:16:27.815207Z","iopub.execute_input":"2023-01-06T18:16:27.815513Z","iopub.status.idle":"2023-01-06T18:16:29.445191Z","shell.execute_reply.started":"2023-01-06T18:16:27.815485Z","shell.execute_reply":"2023-01-06T18:16:29.444288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Create Data Generator\n(As data is so large that it can't fit in memory)","metadata":{}},{"cell_type":"code","source":"def RSNATrainGenerator(train_df, batch_size, infinite = True, base_path = train_images_dir):\n    while True:\n        trainset = []\n        trainidt = []\n        trainlabel = []\n        for i in (range(len(train_df))):\n            idt = train_df.loc[i, 'StudyInstanceUID']\n            path = os.path.join(base_path, idt)\n            for im in os.listdir(path):\n                dc = dicom.read_file(os.path.join(path,im))\n                if dc.file_meta.TransferSyntaxUID.name =='JPEG Lossless, Non-Hierarchical, First-Order Prediction (Process 14 [Selection Value 1])':\n                    continue\n                img = load_dicom(os.path.join(path , im))\n                img = cv2.resize(img, (128 , 128))\n                image = img_to_array(img)\n                image = image / 255.0\n                trainset += [image]\n                cur_label = [train_df.loc[i,f'C{j}'] for j in range(1,8)]\n                trainlabel += [cur_label]\n                trainidt += [idt]\n                if len(trainidt) == batch_size:                    \n                    yield np.array(trainset), np.array(trainlabel)\n                    trainset, trainlabel, trainidt = [], [], []\n            i+=1","metadata":{"execution":{"iopub.status.busy":"2023-01-06T18:16:29.446386Z","iopub.execute_input":"2023-01-06T18:16:29.44681Z","iopub.status.idle":"2023-01-06T18:16:29.458941Z","shell.execute_reply.started":"2023-01-06T18:16:29.446763Z","shell.execute_reply":"2023-01-06T18:16:29.457977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def RSNATestGenerator(test_df, batch_size, infinite = True, base_path = test_images_dir):\n    while 1:        \n        testset=[]\n        testidt=[]\n        for i in (range(len(test_df))):        \n            if type(test_df) is list: idt = test_df[i]\n            else: idt = test_df['StudyInstanceUID'].iloc[i]\n            path = os.path.join(base_path, idt)\n            if os.path.exists(path):\n                for im in os.listdir(path):\n                    dc = dicom.read_file(os.path.join(path,im))\n                    if dc.file_meta.TransferSyntaxUID.name =='JPEG Lossless, Non-Hierarchical, First-Order Prediction (Process 14 [Selection Value 1])':\n                        continue\n                    img=load_dicom(os.path.join(path,im))\n                    img=cv2.resize(img,(128, 128))\n                    image=img_to_array(img)\n                    image=image/255.0\n                    testset+=[image]\n                    testidt+=[idt]\n                    if len(testset) == batch_size:                        \n                        yield np.array(testset)\n                        testset = []\n        if len(testset) > 0: yield np.array(testset)\n        if not infinite: break","metadata":{"execution":{"iopub.status.busy":"2023-01-06T18:16:29.460666Z","iopub.execute_input":"2023-01-06T18:16:29.461481Z","iopub.status.idle":"2023-01-06T18:16:29.47184Z","shell.execute_reply.started":"2023-01-06T18:16:29.4614Z","shell.execute_reply":"2023-01-06T18:16:29.470768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = RSNATrainGenerator(df_train, 64)\nsample = next(train_data)\nprint(\"input_shape:\", sample[0].shape)\nprint(\"target_shape:\", sample[1].shape)","metadata":{"execution":{"iopub.status.busy":"2023-01-06T18:16:29.474416Z","iopub.execute_input":"2023-01-06T18:16:29.47553Z","iopub.status.idle":"2023-01-06T18:16:32.291965Z","shell.execute_reply.started":"2023-01-06T18:16:29.47539Z","shell.execute_reply":"2023-01-06T18:16:32.290954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Create Model","metadata":{"execution":{"iopub.status.busy":"2022-12-24T08:45:28.342629Z","iopub.execute_input":"2022-12-24T08:45:28.342996Z","iopub.status.idle":"2022-12-24T08:45:28.347812Z","shell.execute_reply.started":"2022-12-24T08:45:28.342963Z","shell.execute_reply":"2022-12-24T08:45:28.346634Z"}}},{"cell_type":"code","source":"def get_model_A1():       \n    eff_model = tf.keras.applications.EfficientNetB0(\n                    include_top=False,\n                    weights=\"imagenet\",\n                    pooling=\"max\")\n    \n    for layer in eff_model.layers[:-10]:\n        layer.trainable = False\n        \n        \n    inp = tfl.Input((128, 128 ,3))\n    x = eff_model(inp)\n    x = tfl.Dense(128, 'relu')(x)\n    x = tfl.Dropout(0.5)(x)\n    out = tfl.Dense(7, 'sigmoid')(x)\n    model = tf.keras.models.Model(inp, out)\n    model.compile(loss=\"binary_crossentropy\", optimizer = tf.keras.optimizers.Adam(learning_rate = 1e-4),\n                 metrics=[tf.keras.metrics.BinaryAccuracy()])\n    model.summary()\n    return model\n\nget_model_A1()","metadata":{"execution":{"iopub.status.busy":"2023-01-06T18:17:25.428161Z","iopub.execute_input":"2023-01-06T18:17:25.428863Z","iopub.status.idle":"2023-01-06T18:17:30.73234Z","shell.execute_reply.started":"2023-01-06T18:17:25.428826Z","shell.execute_reply":"2023-01-06T18:17:30.731165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_model_A2():\n    inp = tfl.Input((128, 128 ,3))\n    x = tfl.Conv2D(32, (3, 3), activation='relu')(inp)\n    x = tfl.MaxPooling2D((2, 2))(x)\n    x = tfl.Conv2D(64, (3, 3), activation='relu')(inp)\n    x = tfl.MaxPooling2D((2, 2))(x)\n    x = tfl.Conv2D(128, (3, 3), activation='relu')(inp)\n    x = tfl.MaxPooling2D((2, 2))(x)\n    x = tfl.Flatten()(x)\n    x = tfl.Dense(128, 'relu')(x)\n    x = tfl.Dropout(0.5)(x)\n    out = tfl.Dense(7, 'sigmoid')(x)\n    \n    model = tf.keras.models.Model(inp, out)\n    \n    model.compile(loss=\"binary_crossentropy\",\n                  optimizer = tf.keras.optimizers.Adam(learning_rate = 1e-4),\n                  metrics=[tf.keras.metrics.BinaryAccuracy()])\n    model.summary()\n    \n    return model\n\nget_model_A2()","metadata":{"execution":{"iopub.status.busy":"2023-01-06T18:17:30.734774Z","iopub.execute_input":"2023-01-06T18:17:30.735703Z","iopub.status.idle":"2023-01-06T18:17:30.80336Z","shell.execute_reply.started":"2023-01-06T18:17:30.735674Z","shell.execute_reply":"2023-01-06T18:17:30.802425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_model_M1():       \n    MobileNet_model = tf.keras.applications.MobileNet(\n    include_top=False,\n    weights='imagenet',\n    pooling=\"max\",\n)\n    \n    for layer in MobileNet_model.layers[:-10]:\n        layer.trainable = False\n        \n        \n    inp = tfl.Input((128, 128 ,3))\n    x = MobileNet_model(inp)\n    out = tfl.Dense(7, 'sigmoid')(x)\n    model = tf.keras.models.Model(inp, out)\n    model.compile(loss=\"binary_crossentropy\", optimizer = tf.keras.optimizers.Adam(learning_rate = 0.0001),\n                 metrics=[tf.keras.metrics.BinaryAccuracy()])\n    model.summary()\n    return model","metadata":{"execution":{"iopub.status.busy":"2023-01-06T18:17:30.804895Z","iopub.execute_input":"2023-01-06T18:17:30.80525Z","iopub.status.idle":"2023-01-06T18:17:30.812325Z","shell.execute_reply.started":"2023-01-06T18:17:30.805216Z","shell.execute_reply":"2023-01-06T18:17:30.811117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_model_M2():\n    DenseNet_model = tf.keras.applications.DenseNet121(\n    include_top=False,\n    weights='imagenet',\n    pooling=\"max\",\n)\n    \n    for layer in DenseNet_model.layers[:-10]:\n        layer.trainable = False\n        \n        \n    inp = tfl.Input((128, 128 ,3))\n    x = DenseNet_model(inp)\n    out = tfl.Dense(7, 'sigmoid')(x)\n    model = tf.keras.models.Model(inp, out)\n    model.compile(loss=\"binary_crossentropy\", optimizer = tf.keras.optimizers.Adam(learning_rate = 0.0001),\n                 metrics=[tf.keras.metrics.BinaryAccuracy()])\n    model.summary()\n    return model","metadata":{"execution":{"iopub.status.busy":"2023-01-06T18:17:31.050355Z","iopub.execute_input":"2023-01-06T18:17:31.05098Z","iopub.status.idle":"2023-01-06T18:17:31.059024Z","shell.execute_reply.started":"2023-01-06T18:17:31.050936Z","shell.execute_reply":"2023-01-06T18:17:31.057967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_model_O1():       \n    eff_model = tf.keras.applications.Xception(\n                    include_top=False,\n                    weights=\"imagenet\",\n                    pooling=\"max\"\n                    )\n    \n    for layer in eff_model.layers[:-10]:\n        layer.trainable = False\n        \n        \n    inp = tfl.Input((128, 128 ,3))\n    x = eff_model(inp)\n    out = tfl.Dense(7, 'sigmoid')(x)\n    model = tf.keras.models.Model(inp, out)\n    model.compile(loss=\"binary_crossentropy\", optimizer = tf.keras.optimizers.Adam(learning_rate = 0.0001),\n                 metrics=[tf.keras.metrics.BinaryAccuracy()])\n    model.summary()\n    return model","metadata":{"execution":{"iopub.status.busy":"2023-01-06T18:17:31.656382Z","iopub.execute_input":"2023-01-06T18:17:31.65705Z","iopub.status.idle":"2023-01-06T18:17:31.66426Z","shell.execute_reply.started":"2023-01-06T18:17:31.657016Z","shell.execute_reply":"2023-01-06T18:17:31.663074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras\nfrom tensorflow.keras import layers\ndef get_model_O2():\n    model = keras.Sequential([\n        layers.Dense(128, activation='relu', input_shape=[128,128,3]),\n        layers.Dropout(0.4),\n        layers.Dense(128, activation='relu'),\n        layers.Dropout(0.6),\n        layers.Dense(64, activation='relu'),\n        layers.Flatten(),\n        layers.Dense(7, activation='sigmoid'),\n    ])\n    \n    model.compile(loss=\"binary_crossentropy\", \n                  optimizer = tf.keras.optimizers.Adam(learning_rate = 1e-4),\n                  metrics=[tf.keras.metrics.BinaryAccuracy()])\n    model.summary()\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2023-01-06T18:17:32.192895Z","iopub.execute_input":"2023-01-06T18:17:32.193386Z","iopub.status.idle":"2023-01-06T18:17:32.20885Z","shell.execute_reply.started":"2023-01-06T18:17:32.193344Z","shell.execute_reply":"2023-01-06T18:17:32.207923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_model_K1():       \n    eff_model = tf.keras.applications.InceptionV3(\n                    include_top=False,\n                    weights=\"imagenet\",\n                    pooling=\"max\"\n                    )\n    \n    for layer in eff_model.layers[:-10]:\n        layer.trainable = False\n        \n        \n    inp = tfl.Input((128, 128 ,3))\n    x = eff_model(inp)\n    # x = tfl.Conv2D(3, 3, padding = 'SAME')(x)\n    out = tfl.Dense(7, 'sigmoid')(x)\n    model = tf.keras.models.Model(inp, out)\n    model.layers[2].trainable = False\n    model.compile(loss=\"binary_crossentropy\", optimizer = tf.keras.optimizers.Adam(learning_rate = 0.0001),\n                 metrics=[tf.keras.metrics.BinaryAccuracy()])\n    model.summary()\n    return model","metadata":{"execution":{"iopub.status.busy":"2023-01-06T18:27:56.331723Z","iopub.execute_input":"2023-01-06T18:27:56.332096Z","iopub.status.idle":"2023-01-06T18:27:56.341708Z","shell.execute_reply.started":"2023-01-06T18:27:56.332057Z","shell.execute_reply":"2023-01-06T18:27:56.340743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_model_K2():\n    num_classes = 7\n    image_size = 128\n    model = keras.Sequential([\n                    layers.Input((128, 128 ,3)),\n                    layers.Conv2D(16, 3, padding='same', activation='relu'),\n                    layers.MaxPooling2D(),\n                    layers.Conv2D(32, 3, padding='same', activation='relu'),\n                    layers.MaxPooling2D(),\n                    layers.Conv2D(64, 3, padding='same', activation='relu'),\n                    layers.MaxPooling2D(),\n                    layers.Flatten(),\n                    layers.Dense(128, activation='relu'),\n                    layers.Dense(64, activation='relu'),\n                    layers.Dropout(0.5),\n                    layers.Dense(num_classes)])\n    model.compile(loss=\"binary_crossentropy\", optimizer = tf.keras.optimizers.Adam(learning_rate = 1e-4),\n                 metrics=[tf.keras.metrics.BinaryAccuracy()])\n    model.summary()\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2023-01-06T20:44:00.616811Z","iopub.execute_input":"2023-01-06T20:44:00.617211Z","iopub.status.idle":"2023-01-06T20:44:00.625188Z","shell.execute_reply.started":"2023-01-06T20:44:00.61718Z","shell.execute_reply":"2023-01-06T20:44:00.624182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_model_K3():       \n    eff_model = tf.keras.applications.InceptionV3(\n                    include_top=False,\n                    weights=\"imagenet\",\n                    pooling=\"max\"\n                    )\n    \n    for layer in eff_model.layers[:-20]:\n        layer.trainable = False\n        \n        \n    inp = tfl.Input((128, 128 ,3))\n    x = eff_model(inp)\n    x = tfl.Dense(128, 'relu')(x)\n    x = tfl.Dropout(0.5)(x)\n    out = tfl.Dense(7, 'sigmoid')(x)\n    \n    model = tf.keras.models.Model(inp, out)\n    model.layers[2].trainable = False\n    model.compile(loss=\"binary_crossentropy\", optimizer = tf.keras.optimizers.Adam(learning_rate = 0.0001),\n                 metrics=[tf.keras.metrics.BinaryAccuracy()])\n    model.summary()\n    return model","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_model_N1():\n    base_model = keras.applications.ResNet50(\n    weights='imagenet',  # Load weights pre-trained on ImageNet.\n    input_shape=(128, 128, 3),\n    include_top=False)  # include the ImageNet classifier at the top.\n    base_model.trainable = False\n\n    model = tf.keras.models.Sequential()\n    model.add(base_model)\n    model.add(tf.keras.layers.Flatten())\n#     model.add(tf.keras.layers.Dropout(0.5))\n    model.add(tf.keras.layers.Dense(128, activation='relu'))\n    model.add(tf.keras.layers.Dense(64, activation='relu'))\n    model.add(tf.keras.layers.Dense(32, activation='relu'))\n    model.add(tf.keras.layers.Dense(7, activation='sigmoid'))\n\n#     model.layers[0].trainable = False\n    \n    model.compile(\n        loss='binary_crossentropy',\n        optimizer=tf.keras.optimizers.Adam(learning_rate = 1e-4),\n        metrics=[tf.keras.metrics.BinaryAccuracy()]\n    )\n    return model","metadata":{"execution":{"iopub.status.busy":"2023-01-06T21:26:42.163242Z","iopub.execute_input":"2023-01-06T21:26:42.164368Z","iopub.status.idle":"2023-01-06T21:26:42.174246Z","shell.execute_reply.started":"2023-01-06T21:26:42.16431Z","shell.execute_reply":"2023-01-06T21:26:42.172987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_model_N2():\n    base_model = keras.applications.VGG16(\n    weights='imagenet',  # Load weights pre-trained on ImageNet.\n    input_shape=(128, 128, 3),\n    include_top=False)  # Do not include the ImageNet classifier at the top.\n    base_model.trainable = False\n\n    model = tf.keras.models.Sequential()\n    model.add(base_model)\n    model.add(tf.keras.layers.Flatten())\n#     model.add(tf.keras.layers.Dropout(0.5))\n    model.add(tf.keras.layers.Dense(128, activation='relu'))\n    model.add(tf.keras.layers.Dense(64, activation='relu'))\n    model.add(tf.keras.layers.Dense(32, activation='relu'))\n    model.add(tf.keras.layers.Dense(7, activation='sigmoid'))\n\n#     model.layers[0].trainable = False\n    \n    model.compile(\n        loss='binary_crossentropy',\n        optimizer=tf.keras.optimizers.Adam(learning_rate = 1e-4),\n        metrics=[tf.keras.metrics.BinaryAccuracy()]\n    )\n    return model","metadata":{"execution":{"iopub.status.busy":"2023-01-06T21:26:42.501959Z","iopub.execute_input":"2023-01-06T21:26:42.502294Z","iopub.status.idle":"2023-01-06T21:26:42.510576Z","shell.execute_reply.started":"2023-01-06T21:26:42.502246Z","shell.execute_reply":"2023-01-06T21:26:42.509472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"functions = [('Abdallah', get_model_A1, get_model_A2), ('Mohammad', get_model_M1, get_model_M2), ('Omar Ahmed', get_model_O1, get_model_O2), ('Omar Khaled', get_model_K1, get_model_K2), ('Nadeen', get_model_N1, get_model_N2)]","metadata":{"execution":{"iopub.status.busy":"2023-01-06T21:27:09.831545Z","iopub.execute_input":"2023-01-06T21:27:09.831927Z","iopub.status.idle":"2023-01-06T21:27:09.838107Z","shell.execute_reply.started":"2023-01-06T21:27:09.831894Z","shell.execute_reply":"2023-01-06T21:27:09.836975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracies = []\nfor person in functions:\n    print(person[0])\n    for train_idx, val_idx in StratifiedKFold(5).split(df_train, df_train['patient_overall']):    \n        K.clear_session()\n        x_train = df_train.iloc[train_idx].reset_index()\n        x_val = df_train.iloc[val_idx].reset_index()\n\n        train_gen = RSNATrainGenerator(x_train, min(len(x_train), 64), infinite = False, base_path = train_images_dir)\n        val_gen = RSNATrainGenerator(x_val, min(len(x_val), 64), infinite = False, base_path = train_images_dir)\n\n        model1 = person[1]()\n        model2 = person[2]()\n\n\n        hist1 = model1.fit_generator(                            \n            train_gen,\n            epochs = 5,\n            callbacks = [tf.keras.callbacks.EarlyStopping(monitor = 'val_loss', patience = 2, restore_best_weights = True)],\n            validation_steps = max((len(x_val) // 64), 1),\n            steps_per_epoch = max((len(x_train) // 64), 1),\n            validation_data = val_gen,\n          )\n\n        hist2 = model2.fit_generator(                            \n            train_gen,\n            epochs = 5,\n            callbacks = [tf.keras.callbacks.EarlyStopping(monitor = 'val_loss', patience = 2, restore_best_weights = True)],\n            validation_steps = max((len(x_val) // 64), 1),\n            steps_per_epoch = max((len(x_train) // 64), 1),\n            validation_data = val_gen,\n          )\n        accuracies.append(model1.evaluate(val_gen, steps = max((len(x_val) // 64), 1))[1])\n        accuracies.append(model2.evaluate(val_gen, steps = max((len(x_val) // 64), 1))[1])\n        try: # the best we can do at the moment..\n            preds1 = model1.predict_generator(RSNATestGenerator(df_test, min(len(df_test), 64), infinite = False, base_path = test_images_dir), steps = max((len(df_test) // 64), 1))\n            preds2 = model2.predict_generator(RSNATestGenerator(df_test, min(len(df_test), 64), infinite = False, base_path = test_images_dir), steps = max((len(df_test) // 64), 1))\n\n            new_preds = []\n            for pred_idx in range(len(preds1)):\n                new_preds.append(preds1[pred_idx][prediction_type_mapping[pred_idx]])\n            submission['fractured'] += np.array(new_preds) / 55\n\n            new_preds = []\n            for pred_idx in range(len(preds2)):\n                new_preds.append(preds2[pred_idx][prediction_type_mapping[pred_idx]])\n            submission['fractured'] += np.array(new_preds) / 55\n\n        except: traceback.print_exc()    ","metadata":{"execution":{"iopub.status.busy":"2023-01-06T21:27:13.680226Z","iopub.execute_input":"2023-01-06T21:27:13.680699Z","iopub.status.idle":"2023-01-06T21:29:19.034192Z","shell.execute_reply.started":"2023-01-06T21:27:13.680665Z","shell.execute_reply":"2023-01-06T21:29:19.031597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfor train_idx, val_idx in StratifiedKFold(5).split(df_train, df_train['patient_overall']):    \n    K.clear_session()\n    x_train = df_train.iloc[train_idx].reset_index()\n    x_val = df_train.iloc[val_idx].reset_index()\n\n    train_gen = RSNATrainGenerator(x_train, min(len(x_train), 64), infinite = False, base_path = train_images_dir)\n    val_gen = RSNATrainGenerator(x_val, min(len(x_val), 64), infinite = False, base_path = train_images_dir)\n\n    model1 = get_model_K3()\n\n\n    hist1 = model1.fit_generator(                            \n        train_gen,\n        epochs = 5,\n        callbacks = [tf.keras.callbacks.EarlyStopping(monitor = 'val_loss', patience = 2, restore_best_weights = True)],\n        validation_steps = max((len(x_val) // 64), 1),\n        steps_per_epoch = max((len(x_train) // 64), 1),\n        validation_data = val_gen,\n      )\n\n    #hist2 = model2.fit_generator(                            \n    #    train_gen,\n    #    epochs = 5,\n    #    callbacks = [tf.keras.callbacks.EarlyStopping(monitor = 'val_loss', patience = 2, restore_best_weights = True)],\n    #    validation_steps = max((len(x_val) // 64), 1),\n    #    steps_per_epoch = max((len(x_train) // 64), 1),\n    #    validation_data = val_gen,\n    #  )\n    accuracies.append(model1.evaluate(val_gen, steps = max((len(x_val) // 64), 1))[1])\n    #accuracies.append(model2.evaluate(val_gen, steps = max((len(x_val) // 64), 1))[1])\n    try: # the best we can do at the moment..\n        preds1 = model1.predict_generator(RSNATestGenerator(df_test, min(len(df_test), 64), infinite = False, base_path = test_images_dir), steps = max((len(df_test) // 64), 1))\n    #    preds2 = model2.predict_generator(RSNATestGenerator(df_test, min(len(df_test), 64), infinite = False, base_path = test_images_dir), steps = max((len(df_test) // 64), 1))\n\n        new_preds = []\n        for pred_idx in range(len(preds1)):\n            new_preds.append(preds1[pred_idx][prediction_type_mapping[pred_idx]])\n        submission['fractured'] += np.array(new_preds) / 55\n\n     #   new_preds = []\n      #  for pred_idx in range(len(preds2)):\n       #     new_preds.append(preds2[pred_idx][prediction_type_mapping[pred_idx]])\n        #submission['fractured'] += np.array(new_preds) / 50\n\n    except: traceback.print_exc()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.array(accuracies).mean()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2023-01-05T04:57:02.25679Z","iopub.status.idle":"2023-01-05T04:57:02.257457Z","shell.execute_reply.started":"2023-01-05T04:57:02.257198Z","shell.execute_reply":"2023-01-05T04:57:02.257221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index = 0)","metadata":{"execution":{"iopub.status.busy":"2023-01-05T04:57:02.258671Z","iopub.status.idle":"2023-01-05T04:57:02.25931Z","shell.execute_reply.started":"2023-01-05T04:57:02.259062Z","shell.execute_reply":"2023-01-05T04:57:02.259085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_preds","metadata":{"execution":{"iopub.status.busy":"2023-01-05T04:57:02.260906Z","iopub.status.idle":"2023-01-05T04:57:02.26155Z","shell.execute_reply.started":"2023-01-05T04:57:02.26126Z","shell.execute_reply":"2023-01-05T04:57:02.261294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2023-01-05T04:57:02.263143Z","iopub.status.idle":"2023-01-05T04:57:02.263723Z","shell.execute_reply.started":"2023-01-05T04:57:02.263451Z","shell.execute_reply":"2023-01-05T04:57:02.263498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.array(new_preds) / 5","metadata":{"execution":{"iopub.status.busy":"2023-01-05T04:57:02.265288Z","iopub.status.idle":"2023-01-05T04:57:02.267832Z","shell.execute_reply.started":"2023-01-05T04:57:02.267579Z","shell.execute_reply":"2023-01-05T04:57:02.267604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}