{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib\nimport matplotlib.pyplot as plt\n#import seaborn as sns\n\nimport glob\nimport os\nimport random\nimport re\nimport time\n\nfrom tqdm import tqdm\nfrom matplotlib import colors\nfrom matplotlib.colors import LinearSegmentedColormap\n\nimport pydicom as dicom\nimport nibabel as nib\n\nimport joblib\n#import ipywidgets as widgets\n\nimport cv2","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-09-05T18:17:39.90538Z","iopub.execute_input":"2023-09-05T18:17:39.905924Z","iopub.status.idle":"2023-09-05T18:17:39.913961Z","shell.execute_reply.started":"2023-09-05T18:17:39.90589Z","shell.execute_reply":"2023-09-05T18:17:39.912829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2023-09-05T18:17:47.747235Z","iopub.execute_input":"2023-09-05T18:17:47.747577Z","iopub.status.idle":"2023-09-05T18:17:56.33631Z","shell.execute_reply.started":"2023-09-05T18:17:47.74755Z","shell.execute_reply":"2023-09-05T18:17:56.335327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2023-09-05T18:18:00.955983Z","iopub.execute_input":"2023-09-05T18:18:00.956949Z","iopub.status.idle":"2023-09-05T18:18:01.312902Z","shell.execute_reply.started":"2023-09-05T18:18:00.956915Z","shell.execute_reply":"2023-09-05T18:18:01.311899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def transform_to_hu(medical_image, image):\n    # medical_image = pydicom.read_file\n    # image = medical_image.pixel_array\n\n    intercept = medical_image.RescaleIntercept\n    slope = medical_image.RescaleSlope\n    hu_image = image * slope + intercept\n\n    return hu_image","metadata":{"execution":{"iopub.status.busy":"2023-09-05T18:18:03.989325Z","iopub.execute_input":"2023-09-05T18:18:03.989667Z","iopub.status.idle":"2023-09-05T18:18:03.995017Z","shell.execute_reply.started":"2023-09-05T18:18:03.989639Z","shell.execute_reply":"2023-09-05T18:18:03.994068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def apply_windowing(image, center, width):\n    min_window = center - (width / 2)\n    max_window = center + (width / 2)\n    windowed_image = image.copy()\n    windowed_image[windowed_image < min_window] = min_window\n    windowed_image[windowed_image > max_window] = max_window\n    \n    # Normalize the windowed image to [0, 255] scale for display\n    return ((windowed_image - min_window) / width * 255).astype('uint8')","metadata":{"execution":{"iopub.status.busy":"2023-09-05T18:18:08.902076Z","iopub.execute_input":"2023-09-05T18:18:08.902463Z","iopub.status.idle":"2023-09-05T18:18:08.908909Z","shell.execute_reply.started":"2023-09-05T18:18:08.902433Z","shell.execute_reply":"2023-09-05T18:18:08.907855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preprocessing(dicom_img_path):\n    img = dicom.dcmread(dicom_img_path)\n    img_thu = transform_to_hu(img,img.pixel_array)\n    img_win = apply_windowing(img_thu, img.WindowCenter,img.WindowWidth)\n    img_eqHist = cv2.equalizeHist(img_win)\n    new_img = cv2.resize(img_eqHist, (256,256))\n    new_img = cv2.cvtColor(new_img,cv2.COLOR_GRAY2BGR)#np.expand_dims(new_img,axis=-1)\n    return new_img","metadata":{"execution":{"iopub.status.busy":"2023-09-05T18:18:12.826817Z","iopub.execute_input":"2023-09-05T18:18:12.827574Z","iopub.status.idle":"2023-09-05T18:18:12.834899Z","shell.execute_reply.started":"2023-09-05T18:18:12.827529Z","shell.execute_reply":"2023-09-05T18:18:12.833727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dicom_tags = pd.read_parquet(\"/kaggle/input/rsna-2023-abdominal-trauma-detection/test_dicom_tags.parquet\")\ntest_dicom_tags.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-05T18:18:18.919703Z","iopub.execute_input":"2023-09-05T18:18:18.920156Z","iopub.status.idle":"2023-09-05T18:18:19.243592Z","shell.execute_reply.started":"2023-09-05T18:18:18.920119Z","shell.execute_reply":"2023-09-05T18:18:19.242647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test = []\nfor path in test_dicom_tags.path:\n    x_t = preprocessing(\"/kaggle/input/rsna-2023-abdominal-trauma-detection/\"+ path)\n    x_test.append(x_t)","metadata":{"execution":{"iopub.status.busy":"2023-09-05T18:18:24.135601Z","iopub.execute_input":"2023-09-05T18:18:24.135959Z","iopub.status.idle":"2023-09-05T18:18:24.289235Z","shell.execute_reply.started":"2023-09-05T18:18:24.13593Z","shell.execute_reply":"2023-09-05T18:18:24.288155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test[0].shape","metadata":{"execution":{"iopub.status.busy":"2023-09-05T18:18:29.737828Z","iopub.execute_input":"2023-09-05T18:18:29.738199Z","iopub.status.idle":"2023-09-05T18:18:29.744973Z","shell.execute_reply.started":"2023-09-05T18:18:29.73817Z","shell.execute_reply":"2023-09-05T18:18:29.743984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#import tensorflow as tf\nfrom tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D, Dropout, Flatten, InputLayer\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img\nfrom keras.applications.densenet import DenseNet201,DenseNet121\nfrom tensorflow.keras.optimizers import Adam\nfrom keras.callbacks import Callback, ModelCheckpoint, ReduceLROnPlateau, TensorBoard,EarlyStopping,  CSVLogger\nfrom keras import layers\nfrom keras.models import Sequential\nfrom keras.applications.mobilenet import MobileNet","metadata":{"execution":{"iopub.status.busy":"2023-09-05T18:21:47.83962Z","iopub.execute_input":"2023-09-05T18:21:47.839972Z","iopub.status.idle":"2023-09-05T18:21:47.849008Z","shell.execute_reply.started":"2023-09-05T18:21:47.839943Z","shell.execute_reply":"2023-09-05T18:21:47.847947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import backend as K\n\ndef recall_m(y_true, y_pred):\n    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n    possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))\n    recall = true_positives / (possible_positives + K.epsilon())\n    return recall\n\ndef precision_m(y_true, y_pred):\n    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))\n    predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))\n    precision = true_positives / (predicted_positives + K.epsilon())\n    return precision\n\ndef f1_m(y_true, y_pred):\n    precision = precision_m(y_true, y_pred)\n    recall = recall_m(y_true, y_pred)\n    return 2*((precision*recall)/(precision+recall+K.epsilon()))","metadata":{"execution":{"iopub.status.busy":"2023-09-05T18:18:43.373314Z","iopub.execute_input":"2023-09-05T18:18:43.373856Z","iopub.status.idle":"2023-09-05T18:18:43.382153Z","shell.execute_reply.started":"2023-09-05T18:18:43.373823Z","shell.execute_reply":"2023-09-05T18:18:43.381203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.applications.resnet import ResNet50\nfrom keras.applications.mobilenet import MobileNet","metadata":{"execution":{"iopub.status.busy":"2023-09-05T18:19:28.065453Z","iopub.execute_input":"2023-09-05T18:19:28.065807Z","iopub.status.idle":"2023-09-05T18:19:28.070877Z","shell.execute_reply.started":"2023-09-05T18:19:28.065778Z","shell.execute_reply":"2023-09-05T18:19:28.069829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def build_model1(backbone, lr):\n    model = Sequential()\n    model.add(backbone)\n    model.add(layers.GlobalAveragePooling2D())\n    model.add(layers.Dense(32,activation=\"relu\"))\n    model.add(layers.Dense(8,activation = \"relu\"))\n    model.add(layers.Dense(1,activation = \"sigmoid\"))\n    \n    model.compile(\n        loss='binary_focal_crossentropy',\n        optimizer=Adam(learning_rate = lr),\n        metrics=[precision_m,recall_m,f1_m,'accuracy']\n    )\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2023-09-05T18:19:42.611209Z","iopub.execute_input":"2023-09-05T18:19:42.611711Z","iopub.status.idle":"2023-09-05T18:19:42.619882Z","shell.execute_reply.started":"2023-09-05T18:19:42.611671Z","shell.execute_reply":"2023-09-05T18:19:42.618832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def build_model2(backbone, lr):\n    model = Sequential()\n    model.add(backbone)\n    model.add(layers.GlobalAveragePooling2D())\n    model.add(layers.Dense(32,activation=\"relu\"))\n    model.add(layers.Dense(8,activation = \"relu\"))\n    model.add(layers.Dense(3,activation = \"softmax\"))\n    \n    model.compile(\n        loss='binary_focal_crossentropy',\n        optimizer=Adam(learning_rate = lr),\n        metrics=[precision_m,recall_m,f1_m,'accuracy']\n    )\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2023-09-05T18:19:50.230318Z","iopub.execute_input":"2023-09-05T18:19:50.230668Z","iopub.status.idle":"2023-09-05T18:19:50.240126Z","shell.execute_reply.started":"2023-09-05T18:19:50.23064Z","shell.execute_reply":"2023-09-05T18:19:50.239129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"basemodel = MobileNet(weights=None,include_top=False,input_shape=(256,256,3))","metadata":{"execution":{"iopub.status.busy":"2023-09-05T18:20:36.406369Z","iopub.execute_input":"2023-09-05T18:20:36.407022Z","iopub.status.idle":"2023-09-05T18:20:40.734395Z","shell.execute_reply.started":"2023-09-05T18:20:36.406986Z","shell.execute_reply":"2023-09-05T18:20:40.733426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_bowel = build_model1(basemodel ,lr = 1e-4)  \nmodel_bowel.load_weights(\"/kaggle/input/rsna-atd-weights/MobileNet_0.0001_05092023_BCE_bowel.h5\")\nx_bowel = model_bowel.predict(np.array(x_test))","metadata":{"execution":{"iopub.status.busy":"2023-09-05T18:21:59.771233Z","iopub.execute_input":"2023-09-05T18:21:59.771585Z","iopub.status.idle":"2023-09-05T18:22:06.605151Z","shell.execute_reply.started":"2023-09-05T18:21:59.771558Z","shell.execute_reply":"2023-09-05T18:22:06.604209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_extra = build_model1(basemodel ,lr = 1e-4)  \nmodel_extra.load_weights(\"/kaggle/input/rsna-atd-weights/MobileNet_0.0001_05092023_BCE_extra.h5\")\nx_extra = model_extra.predict(np.array(x_test))","metadata":{"execution":{"iopub.status.busy":"2023-09-05T18:22:10.717491Z","iopub.execute_input":"2023-09-05T18:22:10.71784Z","iopub.status.idle":"2023-09-05T18:22:12.28218Z","shell.execute_reply.started":"2023-09-05T18:22:10.717812Z","shell.execute_reply":"2023-09-05T18:22:12.281326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_liver = build_model2(basemodel ,lr = 1e-4)  \nmodel_liver.load_weights(\"/kaggle/input/rsna-atd-weights/MobileNet_0.0001_05092023_CCE_liver.h5\")\nx_liver = model_liver.predict(np.array(x_test))","metadata":{"execution":{"iopub.status.busy":"2023-09-05T18:23:25.157516Z","iopub.execute_input":"2023-09-05T18:23:25.157866Z","iopub.status.idle":"2023-09-05T18:23:26.404716Z","shell.execute_reply.started":"2023-09-05T18:23:25.157838Z","shell.execute_reply":"2023-09-05T18:23:26.404028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_kidney = build_model2(basemodel ,lr = 1e-4)  \nmodel_kidney.load_weights(\"/kaggle/input/rsna-atd-weights/MobileNet_0.0001_05092023_CCE_kidney.h5\")\nx_kidney = model_kidney.predict(np.array(x_test))","metadata":{"execution":{"iopub.status.busy":"2023-09-05T18:24:44.247045Z","iopub.execute_input":"2023-09-05T18:24:44.24796Z","iopub.status.idle":"2023-09-05T18:24:45.567955Z","shell.execute_reply.started":"2023-09-05T18:24:44.247913Z","shell.execute_reply":"2023-09-05T18:24:45.567008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_spleen = build_model2(basemodel ,lr = 1e-4)  \nmodel_spleen.load_weights(\"/kaggle/input/rsna-atd-weights/MobileNet_0.0001_05092023_CCE_spleen.h5\")\nx_spleen = model_spleen.predict(np.array(x_test))","metadata":{"execution":{"iopub.status.busy":"2023-09-05T18:25:05.530623Z","iopub.execute_input":"2023-09-05T18:25:05.530974Z","iopub.status.idle":"2023-09-05T18:25:06.808821Z","shell.execute_reply.started":"2023-09-05T18:25:05.530945Z","shell.execute_reply":"2023-09-05T18:25:06.807879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model_anyI = build_model1(basemodel ,lr = 1e-4)  \n#model_anyI.load_weights(\"/kaggle/input/resnetweight/ResNet50Weights.h5\")\n#x_anyI = model_anyI.predict(np.array(x_test))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv(\"/kaggle/input/rsna-2023-abdominal-trauma-detection/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-09-05T18:25:23.289042Z","iopub.execute_input":"2023-09-05T18:25:23.289406Z","iopub.status.idle":"2023-09-05T18:25:23.304886Z","shell.execute_reply.started":"2023-09-05T18:25:23.289379Z","shell.execute_reply":"2023-09-05T18:25:23.303933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission[\"bowel_injury\"] = x_bowel\nsubmission['bowel_healthy'] = 1 - x_bowel\nsubmission['extravasation_injury'] = x_extra\nsubmission['extravasation_healthy'] = 1- x_extra\nsubmission[['kidney_healthy','kidney_low','kidney_high']] = x_kidney\nsubmission[['liver_healthy','liver_low','liver_high']] = x_liver\nsubmission[['spleen_healthy','spleen_low','spleen_high']] = x_spleen\n#submission[\"any_injury\"] = x_anyI","metadata":{"execution":{"iopub.status.busy":"2023-09-05T18:25:37.481953Z","iopub.execute_input":"2023-09-05T18:25:37.482917Z","iopub.status.idle":"2023-09-05T18:25:37.492268Z","shell.execute_reply.started":"2023-09-05T18:25:37.482882Z","shell.execute_reply":"2023-09-05T18:25:37.491064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2023-09-05T18:25:47.08225Z","iopub.execute_input":"2023-09-05T18:25:47.082607Z","iopub.status.idle":"2023-09-05T18:25:47.102598Z","shell.execute_reply.started":"2023-09-05T18:25:47.082579Z","shell.execute_reply":"2023-09-05T18:25:47.101327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# save file\nsubmission.to_csv(\"submission.csv\",index=False )","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}],"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"}}