{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"tpu1vmV38","dataSources":[{"sourceId":52254,"databundleVersionId":6863140,"sourceType":"competition"},{"sourceId":6523471,"sourceType":"datasetVersion","datasetId":3771357},{"sourceId":6524344,"sourceType":"datasetVersion","datasetId":3771912}],"dockerImageVersionId":30627,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install numpy pandas nibabel matplotlib tensorflow","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-01-06T06:36:02.07286Z","iopub.execute_input":"2024-01-06T06:36:02.073166Z","iopub.status.idle":"2024-01-06T06:36:06.188312Z","shell.execute_reply.started":"2024-01-06T06:36:02.073138Z","shell.execute_reply":"2024-01-06T06:36:06.187346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install numpy pandas tensorflow tensorflow-io","metadata":{"execution":{"iopub.status.busy":"2024-01-06T06:36:06.190276Z","iopub.execute_input":"2024-01-06T06:36:06.190543Z","iopub.status.idle":"2024-01-06T06:36:09.547636Z","shell.execute_reply.started":"2024-01-06T06:36:06.190514Z","shell.execute_reply":"2024-01-06T06:36:09.5467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport nibabel as nib\nimport tensorflow as tf\nfrom tensorflow.keras import layers\nimport tensorflow.keras as keras\nimport tensorflow_io as tfio\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report, roc_auc_score\n\n# Load your dataset (replace with your dataset loading code)\n# Assuming you have a DataFrame with columns: 'image_path', 'label_1', 'label_2', ..., 'label_14'\n# 'image_path' contains the file paths to your NIfTI files\n# 'label_1', ..., 'label_14' contain binary labels indicating the presence or absence of each class\n\n# Example loading code (replace with your actual code)\ndf = pd.read_csv('/kaggle/input/abdominal-trauma-nii-csv/abdominal_trauma_nii.csv')\nimages_paths = df['file_path'].tolist()\nlabels = df.iloc[:, 5:].values.astype(np.float32)\nprint(1)\n# Split the dataset into training and testing sets\nimages_paths_train, images_paths_test, labels_train, labels_test = train_test_split(images_paths, labels, test_size=0.2, random_state=42)\n\n# Load NIfTI data using XLA\ndef load_nifti(file_path):\n    img = nib.load(file_path).get_fdata()\n    return img\n\n# Load training data\nimages_train = np.array([load_nifti(file_path) for file_path in images_paths_train])\nimages_train = images_train / np.max(images_train)  # Normalize pixel values\n\n# Load testing data\nimages_test = np.array([load_nifti(file_path) for file_path in images_paths_test])\nimages_test = images_test / np.max(images_test)  # Normalize pixel values\nprint(2)\n\nimages_train = tf.convert_to_tensor(images_train, dtype=tf.float32)\nimages_test = tf.convert_to_tensor(images_test, dtype=tf.float32)\n\nimages_train = images_train[..., tf.newaxis]\nimages_test = images_test[..., tf.newaxis]\n\n# Define the 3D CNN model\nmodel = keras.Sequential([\n    layers.Conv3D(32, kernel_size=(3, 3, 3), activation='relu', input_shape=images_train.shape[1:]),\n    layers.MaxPooling3D(pool_size=(2, 2, 2)),\n    layers.Conv3D(64, kernel_size=(3, 3, 3), activation='relu'),\n    layers.MaxPooling3D(pool_size=(2, 2, 2)),\n    layers.Flatten(),\n    layers.Dense(128, activation='relu'),\n    layers.Dense(14, activation='sigmoid')  # 14 classes, sigmoid activation for multi-label classification\n])\n\n# Compile the model\nmodel.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\n\n# Train the model\nmodel.fit(images_train, labels_train, epochs=1, batch_size=32, validation_split=0.1)\nprint(3)\n\n\n# Evaluate the model on the test set\ntest_predictions = model.predict(images_test)\n\n# Threshold the predictions (adjust the threshold based on your requirements)\nthreshold = 0.5\nbinary_predictions = (test_predictions > threshold).astype(int)\n\n# Calculate metrics\ntest_loss, test_accuracy = model.evaluate(images_test, labels_test)\nprint(f'Test Accuracy: {test_accuracy}')\n\n# ROC-AUC Score\nroc_auc = roc_auc_score(labels_test, test_predictions)\nprint(f'ROC-AUC Score: {roc_auc}')\n\n\n# Make predictions on new data (replace 'new_data' with your actual new data)\n# new_data_predictions = model.predict(new_data)\n","metadata":{"execution":{"iopub.status.busy":"2024-01-06T06:36:09.549268Z","iopub.execute_input":"2024-01-06T06:36:09.54959Z","iopub.status.idle":"2024-01-06T06:36:54.340769Z","shell.execute_reply.started":"2024-01-06T06:36:09.549539Z","shell.execute_reply":"2024-01-06T06:36:54.33959Z"},"trusted":true},"execution_count":null,"outputs":[]}]}