{"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":"none","dataSources":[{"sourceId":52254,"databundleVersionId":6863140,"sourceType":"competition"},{"sourceId":7293504,"sourceType":"datasetVersion","datasetId":4225503}],"dockerImageVersionId":30626,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport os\ndirname = '/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images'","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-28T00:51:21.062309Z","iopub.execute_input":"2023-12-28T00:51:21.062715Z","iopub.status.idle":"2023-12-28T00:51:21.46466Z","shell.execute_reply.started":"2023-12-28T00:51:21.062684Z","shell.execute_reply":"2023-12-28T00:51:21.463699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install nibabel\n!pip install imageio","metadata":{"execution":{"iopub.status.busy":"2023-12-28T00:51:21.466309Z","iopub.execute_input":"2023-12-28T00:51:21.46671Z","iopub.status.idle":"2023-12-28T00:51:30.121121Z","shell.execute_reply.started":"2023-12-28T00:51:21.46668Z","shell.execute_reply":"2023-12-28T00:51:30.120107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np \nimport os \nimport nibabel as nib \nimport imageio\nimport matplotlib\ndef create_pngs_from_liver_nii(file_name,output):\n    img = nib.load(file_name) #read nii '/kaggle/input/livermask/a1000-livermask.nii'\n    img_fdata = img.get_fdata()\n    fname = file_name.replace('.nii','') \n    isExist = os.path.exists(output)\n    if not isExist:\n        os.makedirs(output)          \n    (x,y,z) = img.shape\n    for i in range(z):\n        slice = img_fdata[:, :, i]\n        matplotlib.image.imsave(os.path.join(output,'{}.png'.format(i)), slice)\n    print(\"Created\",z,\"images in\",output)","metadata":{"execution":{"iopub.status.busy":"2023-12-28T00:51:30.122554Z","iopub.execute_input":"2023-12-28T00:51:30.122862Z","iopub.status.idle":"2023-12-28T00:51:30.318347Z","shell.execute_reply.started":"2023-12-28T00:51:30.122832Z","shell.execute_reply":"2023-12-28T00:51:30.317704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\n\ndef show_image(image_path):\n    img = mpimg.imread(image_path) #'/kaggle/working/88.png'\n    plt.imshow(img)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-28T00:51:30.319971Z","iopub.execute_input":"2023-12-28T00:51:30.320279Z","iopub.status.idle":"2023-12-28T00:51:31.116243Z","shell.execute_reply.started":"2023-12-28T00:51:30.320253Z","shell.execute_reply":"2023-12-28T00:51:31.115425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\ndef remove_empty_images(folder_path):\n    img = None\n    for file in os.listdir(folder_path):\n        file_path = os.path.join(folder_path, file)\n        img = mpimg.imread(file_path)\n        if np.all(img == img[0][0]):\n            break\n    files = os.listdir(folder_path)\n    count =0\n    for file in files:\n        if '.png' not in file:\n            continue\n        file_path = os.path.join(folder_path, file)\n        img1 = mpimg.imread(file_path)\n        if (img1 ==  img).all():\n            try:\n                if os.path.isfile(file_path):\n                    os.unlink(file_path)\n                    count += 1\n            except Exception as e:\n                print(f\"Error deleting {file_path}: {e}\")\n    print(count, \"Images Deleted\")\n","metadata":{"execution":{"iopub.status.busy":"2023-12-28T00:51:31.117222Z","iopub.execute_input":"2023-12-28T00:51:31.117621Z","iopub.status.idle":"2023-12-28T00:51:31.124767Z","shell.execute_reply.started":"2023-12-28T00:51:31.117593Z","shell.execute_reply":"2023-12-28T00:51:31.124162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nimport numpy as np\n\n\ndef get_organ_label(image_array):\n    yellow_rgb = [253,231,36,255]\n    yellow_mask = np.all(image_array == yellow_rgb, axis=2)\n    percentage_yellow = (np.sum(yellow_mask) / yellow_mask.size) \n    return percentage_yellow\n\ndef get_images_from_folder(folder_path):\n    images = []\n    files = os.listdir(folder_path)\n    for file in files:\n        if '.png' not in file:\n            continue\n        file_path = os.path.join(folder_path, file)\n        img1 = Image.open(file_path)\n        img1 = np.array(img1)\n        if img1 is not None:\n                images.append(img1)\n    return np.array(images)\n        ","metadata":{"execution":{"iopub.status.busy":"2023-12-28T00:51:31.125717Z","iopub.execute_input":"2023-12-28T00:51:31.125941Z","iopub.status.idle":"2023-12-28T00:51:31.135955Z","shell.execute_reply.started":"2023-12-28T00:51:31.125917Z","shell.execute_reply":"2023-12-28T00:51:31.135307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def process_images_array(images):\n    result_list = []\n    for image in images:\n        result = get_organ_label(image)\n        result_list.append(result)\n    return np.array(result_list)","metadata":{"execution":{"iopub.status.busy":"2023-12-28T00:51:31.136757Z","iopub.execute_input":"2023-12-28T00:51:31.136966Z","iopub.status.idle":"2023-12-28T00:51:31.145569Z","shell.execute_reply.started":"2023-12-28T00:51:31.136945Z","shell.execute_reply":"2023-12-28T00:51:31.144969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def list_files_in_directory(directory_path):\n    files = [f for f in os.listdir(directory_path) if os.path.isfile(os.path.join(directory_path, f))]\n    return files\n\ndirectory_path = '/kaggle/input/liver-seg'\nfiles = list_files_in_directory(directory_path)\nprint(files)","metadata":{"execution":{"iopub.status.busy":"2023-12-28T00:51:31.146466Z","iopub.execute_input":"2023-12-28T00:51:31.146728Z","iopub.status.idle":"2023-12-28T00:51:31.162801Z","shell.execute_reply.started":"2023-12-28T00:51:31.146703Z","shell.execute_reply":"2023-12-28T00:51:31.162067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for file in files:\n    file_path = os.path.join('/kaggle/input/liver-seg',file)\n    folder_path = os.path.join('/kaggle/working',file[:-4])\n    create_pngs_from_liver_nii(file_path,folder_path)\n#     remove_empty_images(folder_path)\n    print('working on',file_path)","metadata":{"execution":{"iopub.status.busy":"2023-12-28T00:51:31.163693Z","iopub.execute_input":"2023-12-28T00:51:31.163906Z","iopub.status.idle":"2023-12-28T00:55:38.39902Z","shell.execute_reply.started":"2023-12-28T00:51:31.163884Z","shell.execute_reply":"2023-12-28T00:55:38.397714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_data():\n    filename = '/kaggle/input/rsna-2023-abdominal-trauma-detection/train.csv'\n    columns_to_read = ['patient_id', 'liver_healthy', 'liver_low','liver_high']\n    df = pd.read_csv(filename, usecols=columns_to_read)\n    patients = []\n    for file in files:\n        parts = file.split('_') if '_' in file else file.split('-')\n        result = parts[0]\n        patients.append(int(result))\n    \n    filtered_patients = df[df['patient_id'].isin(patients)]\n    return filtered_patients\ndf = get_data()\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-28T00:55:38.402277Z","iopub.execute_input":"2023-12-28T00:55:38.402621Z","iopub.status.idle":"2023-12-28T00:55:38.433642Z","shell.execute_reply.started":"2023-12-28T00:55:38.40259Z","shell.execute_reply":"2023-12-28T00:55:38.432734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_labels(patient_id,array):\n    result = df['patient_id'] == patient_id\n    result = df[result]\n    injury = int(result['liver_low'].iloc[0])\n    injury_high = int(result['liver_high'].iloc[0])\n    label = 1.0 if (injury or injury_high) else 0.0\n    return array * label\n\nprint(get_labels(10004,np.array([1.0,2.0,3.0,4.0])))","metadata":{"execution":{"iopub.status.busy":"2023-12-28T00:55:38.435862Z","iopub.execute_input":"2023-12-28T00:55:38.436382Z","iopub.status.idle":"2023-12-28T00:55:38.44464Z","shell.execute_reply.started":"2023-12-28T00:55:38.436322Z","shell.execute_reply":"2023-12-28T00:55:38.443747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result_array = np.array([])\nresult_labels = np.array([])\nfor file in files:\n    file_path = os.path.join('/kaggle/working',file[:-4])\n    print('working on',file_path)\n    images = np.array(get_images_from_folder(file_path))\n    if images.shape[1] != 512 or images.shape[2] !=  512:\n        continue\n    if len(result_array) == 0:\n        result_array = images\n    else:\n        result_array = np.concatenate((result_array, images), axis=0)\n    image_labels = process_images_array(images)\n    max_label = np.max(image_labels)\n    image_labels = image_labels/max_label\n    parts = file.split('_') if '_' in file else file.split('-')\n    patient_id = int(parts[0])\n    image_labels = get_labels(patient_id,image_labels)\n    result_labels = np.concatenate((image_labels, result_labels))","metadata":{"execution":{"iopub.status.busy":"2023-12-28T00:55:38.445796Z","iopub.execute_input":"2023-12-28T00:55:38.4461Z","iopub.status.idle":"2023-12-28T00:59:31.735927Z","shell.execute_reply.started":"2023-12-28T00:55:38.446068Z","shell.execute_reply":"2023-12-28T00:59:31.735112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(result_array.shape)\nprint(result_labels.shape)","metadata":{"execution":{"iopub.status.busy":"2023-12-28T00:59:31.737027Z","iopub.execute_input":"2023-12-28T00:59:31.737571Z","iopub.status.idle":"2023-12-28T00:59:31.741229Z","shell.execute_reply.started":"2023-12-28T00:59:31.737524Z","shell.execute_reply":"2023-12-28T00:59:31.740668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nx_train, x_test, y_train, y_test = train_test_split(result_array, result_labels, test_size=0.2, random_state=42)\nprint(\"x_train shape:\", x_train.shape)\nprint(\"y_train shape:\", y_train.shape)\nprint(\"x_test shape:\", x_test.shape)\nprint(\"y_test shape:\", y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-12-28T00:59:31.742105Z","iopub.execute_input":"2023-12-28T00:59:31.742364Z","iopub.status.idle":"2023-12-28T00:59:36.188206Z","shell.execute_reply.started":"2023-12-28T00:59:31.74231Z","shell.execute_reply":"2023-12-28T00:59:36.187488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = x_train.astype('float32') / 255.0 \nx_test = x_test.astype('float32') / 255.0","metadata":{"execution":{"iopub.status.busy":"2023-12-28T00:59:36.189157Z","iopub.execute_input":"2023-12-28T00:59:36.189566Z","iopub.status.idle":"2023-12-28T01:00:04.254718Z","shell.execute_reply.started":"2023-12-28T00:59:36.189525Z","shell.execute_reply":"2023-12-28T01:00:04.253965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python3 -m pip install tensorflow[and-cuda]\n# Verify the installation:\n!python3 -c \"import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))\"","metadata":{"execution":{"iopub.status.busy":"2023-12-28T01:00:04.255762Z","iopub.execute_input":"2023-12-28T01:00:04.256048Z","iopub.status.idle":"2023-12-28T01:02:59.705093Z","shell.execute_reply.started":"2023-12-28T01:00:04.256018Z","shell.execute_reply":"2023-12-28T01:02:59.703988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import datasets, layers, models\nimport matplotlib.pyplot as plt\n\n# detect and init the TPU\ntpu = tf.distribute.cluster_resolver.TPUClusterResolver()\n\n# instantiate a distribution strategy\ntf.tpu.experimental.initialize_tpu_system(tpu)\ntpu_strategy = tf.distribute.TPUStrategy(tpu)","metadata":{"execution":{"iopub.status.busy":"2023-12-28T01:02:59.706651Z","iopub.execute_input":"2023-12-28T01:02:59.706949Z","iopub.status.idle":"2023-12-28T01:03:11.041239Z","shell.execute_reply.started":"2023-12-28T01:02:59.706923Z","shell.execute_reply":"2023-12-28T01:03:11.040475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_height, img_width = 512, 512\nwith tpu_strategy.scope():\n    model = models.Sequential()\n    model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(img_height, img_width, 4)))\n    model.add(layers.MaxPooling2D((2, 2)))\n    model.add(layers.Conv2D(64, (3, 3), activation='relu'))\n    model.add(layers.MaxPooling2D((2, 2)))\n    model.add(layers.Conv2D(128, (3, 3), activation='relu'))\n    model.add(layers.MaxPooling2D((2, 2)))\n    model.add(layers.Flatten())\n    model.add(layers.Dense(128, activation='relu'))\n    model.add(layers.Dense(1, activation='linear'))  # Sigmoid activation for regression in the [0, 1] range\n\n# Compile the model\n    model.compile(optimizer='adam',loss='mean_squared_error',  # Use mean_squared_error for regression\n              metrics=['mae'])  # Use mean absolute error (mae) as an additional metric\n\nhistory = model.fit(x_train, y_train, epochs=10, validation_data=(x_test, y_test))\n\n# Evaluate the model\ntest_loss, test_mae = model.evaluate(x_test, y_test)\nprint(f'Test Mean Absolute Error: {test_mae:.2f}')","metadata":{"execution":{"iopub.status.busy":"2023-12-28T01:06:12.41214Z","iopub.execute_input":"2023-12-28T01:06:12.412584Z","iopub.status.idle":"2023-12-28T01:15:02.962518Z","shell.execute_reply.started":"2023-12-28T01:06:12.412528Z","shell.execute_reply":"2023-12-28T01:15:02.96153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"arr = model.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2023-12-28T01:16:52.292097Z","iopub.execute_input":"2023-12-28T01:16:52.292604Z","iopub.status.idle":"2023-12-28T01:17:33.959603Z","shell.execute_reply.started":"2023-12-28T01:16:52.292561Z","shell.execute_reply":"2023-12-28T01:17:33.9586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(arr)","metadata":{"execution":{"iopub.status.busy":"2023-12-28T01:17:51.903809Z","iopub.execute_input":"2023-12-28T01:17:51.904246Z","iopub.status.idle":"2023-12-28T01:17:51.910287Z","shell.execute_reply.started":"2023-12-28T01:17:51.904211Z","shell.execute_reply":"2023-12-28T01:17:51.909299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(1,10):\n    print(abs(y_test[i]-arr[i]))","metadata":{"execution":{"iopub.status.busy":"2023-12-28T01:18:30.521693Z","iopub.execute_input":"2023-12-28T01:18:30.522113Z","iopub.status.idle":"2023-12-28T01:18:30.527987Z","shell.execute_reply.started":"2023-12-28T01:18:30.522079Z","shell.execute_reply":"2023-12-28T01:18:30.527208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport shutil\n\ndef zip_folder(folder_path, zip_filename):\n    # Zip the entire folder and its contents\n    shutil.make_archive(zip_filename, 'zip', folder_path)\n\n# Example usage:\nfolder_to_zip = '/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/12600'\nzip_file_name = '/kaggle/working/12600'\n\nzip_folder(folder_to_zip, zip_file_name)","metadata":{"execution":{"iopub.status.busy":"2023-12-28T01:04:48.481281Z","iopub.status.idle":"2023-12-28T01:04:48.481631Z","shell.execute_reply.started":"2023-12-28T01:04:48.48144Z","shell.execute_reply":"2023-12-28T01:04:48.481457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import nibabel as nib\nimport matplotlib.pyplot as plt\n\ndef show_middle_slice(nii_file_path):\n    # Load the NIfTI file\n    nii_img = nib.load(nii_file_path)\n\n    # Get the data array from the loaded image\n    data = nii_img.get_fdata()\n    print(data.shape)\n    # Calculate the middle slice index\n    middle_slice_index =33\n\n    # Get the middle slice\n    middle_slice = data[:, :, middle_slice_index]\n\n    # Display the middle slice using imshow\n    plt.imshow(middle_slice, cmap='gray')\n    plt.title('Middle Slice')\n    plt.axis('off')  # Turn off axis labels\n    plt.show()\n\n# Example usage\nnii_file_path = '/kaggle/input/liver-seg/10005_18667-livermask.nii'\nshow_middle_slice(nii_file_path)","metadata":{"execution":{"iopub.status.busy":"2023-12-28T01:18:52.680912Z","iopub.execute_input":"2023-12-28T01:18:52.681328Z","iopub.status.idle":"2023-12-28T01:18:52.983852Z","shell.execute_reply.started":"2023-12-28T01:18:52.681297Z","shell.execute_reply":"2023-12-28T01:18:52.982886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import shutil\n# def empty_dir(folder_path):\n#     files = os.listdir(folder_path)\n#     for file in files:\n#         file_path = os.path.join(folder_path, file)\n#         try:\n#             if os.path.isfile(file_path):\n#                 os.unlink(file_path)\n#             if os.path.isdir(file_path):\n#                 shutil.rmtree(file_path)\n#         except Exception as e:\n#             print(f\"Error deleting {file_path}: {e}\")\n            \n            \n# empty_dir('/kaggle/working/')","metadata":{"execution":{"iopub.status.busy":"2023-12-28T01:04:48.484002Z","iopub.status.idle":"2023-12-28T01:04:48.484278Z","shell.execute_reply.started":"2023-12-28T01:04:48.48414Z","shell.execute_reply":"2023-12-28T01:04:48.484154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_image('/kaggle/working/10065_46839-livermask/50.png')","metadata":{"execution":{"iopub.status.busy":"2023-12-28T01:20:40.354849Z","iopub.execute_input":"2023-12-28T01:20:40.355231Z","iopub.status.idle":"2023-12-28T01:20:40.509609Z","shell.execute_reply.started":"2023-12-28T01:20:40.355197Z","shell.execute_reply":"2023-12-28T01:20:40.508591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}