{"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":"train_path = '/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/10004/21057/*.dcm'","metadata":{"execution":{"iopub.status.busy":"2023-08-04T09:43:10.186067Z","iopub.execute_input":"2023-08-04T09:43:10.186503Z","iopub.status.idle":"2023-08-04T09:43:10.191668Z","shell.execute_reply.started":"2023-08-04T09:43:10.186469Z","shell.execute_reply":"2023-08-04T09:43:10.190458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Import necessary libraries\nimport glob                   # For file path matching\nimport numpy as np            # For numerical operations\nimport pandas as pd           # For data manipulation\nimport matplotlib.pyplot as plt  # For data visualization\nimport os                     # For interacting with the operating system\nimport pydicom as dicom       # For working with DICOM files (medical imaging)\nimport random                 # For random number generation\nrandom.seed(42)              # Set a random seed for reproducibility\n\nimport torch                  # For PyTorch-based deep learning\nimport torch.nn as nn         # For defining neural network modules\nimport torch.optim as optim   # For defining optimization algorithms\n\nimport tensorflow as tf       # For TensorFlow-based deep learning\nfrom tensorflow.keras import layers, models  # For defining Keras models and layers","metadata":{"execution":{"iopub.status.busy":"2023-08-04T09:43:10.193529Z","iopub.execute_input":"2023-08-04T09:43:10.194476Z","iopub.status.idle":"2023-08-04T09:43:10.207465Z","shell.execute_reply.started":"2023-08-04T09:43:10.194444Z","shell.execute_reply":"2023-08-04T09:43:10.206129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_train_examples = len(train_path)\nprint(num_train_examples)","metadata":{"execution":{"iopub.status.busy":"2023-08-04T09:43:10.209593Z","iopub.execute_input":"2023-08-04T09:43:10.210216Z","iopub.status.idle":"2023-08-04T09:43:10.219036Z","shell.execute_reply.started":"2023-08-04T09:43:10.210183Z","shell.execute_reply":"2023-08-04T09:43:10.217488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pixel_arries = []\npixel_number = []\n\nfor _ in range(min(6, num_train_examples)):\n    randomly_selected_image = random.choice(os.listdir('/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/10004/21057'))\n    pixel_arries.append(dicom.dcmread('/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/10004/21057/' + randomly_selected_image).pixel_array)\n    pixel_number.append(randomly_selected_image)","metadata":{"execution":{"iopub.status.busy":"2023-08-04T09:43:10.221438Z","iopub.execute_input":"2023-08-04T09:43:10.221729Z","iopub.status.idle":"2023-08-04T09:43:10.572735Z","shell.execute_reply.started":"2023-08-04T09:43:10.221705Z","shell.execute_reply":"2023-08-04T09:43:10.571588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# randomly show some figures for checking\nplt.figure(figsize=(10, 8))\nfor x in range(min(6, num_train_examples)):\n    plt.subplot(2, 3, x + 1)\n    plt.imshow(pixel_arries[x], cmap='bone')\n    plt.title(pixel_number[x])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-04T09:43:10.575803Z","iopub.execute_input":"2023-08-04T09:43:10.576246Z","iopub.status.idle":"2023-08-04T09:43:11.730862Z","shell.execute_reply.started":"2023-08-04T09:43:10.57621Z","shell.execute_reply":"2023-08-04T09:43:11.730003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv('/kaggle/input/rsna-2023-abdominal-trauma-detection/train.csv')","metadata":{"execution":{"iopub.status.busy":"2023-08-04T09:43:11.732121Z","iopub.execute_input":"2023-08-04T09:43:11.73323Z","iopub.status.idle":"2023-08-04T09:43:11.748983Z","shell.execute_reply.started":"2023-08-04T09:43:11.733195Z","shell.execute_reply":"2023-08-04T09:43:11.748084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head().style.set_properties(**{'background-color': 'black',\n                                                'color': 'lawngreen',\n                                                'border': '1.5px  white'})","metadata":{"execution":{"iopub.status.busy":"2023-08-04T09:43:11.750605Z","iopub.execute_input":"2023-08-04T09:43:11.750969Z","iopub.status.idle":"2023-08-04T09:43:11.765809Z","shell.execute_reply.started":"2023-08-04T09:43:11.750936Z","shell.execute_reply":"2023-08-04T09:43:11.764481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert patient_id column from DataFrame to a numpy array and cast it to string data type\nid = df_train.patient_id.to_numpy().astype(str)\n\n# Initialize empty lists for storing image data (X) and corresponding labels (y)\nX, y = [], []\n\n# Loop over the first 3 patient IDs\nfor x, p_id in enumerate(id[:3]):\n    # Define the directory path for the current patient's images\n    dir = '/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/' + p_id + '/'\n    \n    # Extract the features (labels) for the current patient using their index\n    features = df_train.iloc[x].to_numpy()[1:]\n    \n    # Loop through each file in the patient's directory\n    for file in glob.glob(dir + '*'):\n        # Loop through each image file in the current directory\n        for image_path in glob.glob(file + '/*'):\n            # Read the DICOM image and extract the pixel array\n            X.append(dicom.dcmread(image_path).pixel_array)\n            \n            # Append the features (labels) for this image to the y list\n            y.append(features)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-04T09:43:11.767327Z","iopub.execute_input":"2023-08-04T09:43:11.767735Z","iopub.status.idle":"2023-08-04T09:43:51.425159Z","shell.execute_reply.started":"2023-08-04T09:43:11.767704Z","shell.execute_reply":"2023-08-04T09:43:51.424121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(X), len(y)","metadata":{"execution":{"iopub.status.busy":"2023-08-04T09:43:51.426533Z","iopub.execute_input":"2023-08-04T09:43:51.426876Z","iopub.status.idle":"2023-08-04T09:43:51.43484Z","shell.execute_reply.started":"2023-08-04T09:43:51.426843Z","shell.execute_reply":"2023-08-04T09:43:51.433727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = np.array(X)\ny = np.array(y)\n\nX.shape, y.shape","metadata":{"execution":{"iopub.status.busy":"2023-08-04T09:43:51.436695Z","iopub.execute_input":"2023-08-04T09:43:51.437334Z","iopub.status.idle":"2023-08-04T09:43:52.185333Z","shell.execute_reply.started":"2023-08-04T09:43:51.4373Z","shell.execute_reply":"2023-08-04T09:43:52.184367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 8))\nplt.imshow(X[1], cmap='bone')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-04T09:43:52.188824Z","iopub.execute_input":"2023-08-04T09:43:52.189172Z","iopub.status.idle":"2023-08-04T09:43:52.628317Z","shell.execute_reply.started":"2023-08-04T09:43:52.189143Z","shell.execute_reply":"2023-08-04T09:43:52.627473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the model architecture using the functional API\n\n# Define the input layer with the shape of each input image (X.shape[1], X.shape[2], 1)\ninputs = tf.keras.Input(shape=(X.shape[1], X.shape[2], 1))\n\n# First set of convolutional layers\nx = tf.keras.layers.Conv2D(2, (3, 3), activation='relu')(inputs)\nx = tf.keras.layers.Conv2D(2, (3, 3), activation='relu')(x)\nx = tf.keras.layers.BatchNormalization()(x)\nx = tf.keras.layers.MaxPool2D(2)(x)\n\n# Second set of convolutional layers\nx = tf.keras.layers.Conv2D(4, (3, 3), activation='relu')(x)\nx = tf.keras.layers.Conv2D(4, (3, 3), activation='relu')(x)\nx = tf.keras.layers.BatchNormalization()(x)\nx = tf.keras.layers.MaxPool2D(2)(x)\n\n# Third set of convolutional layers\nx = tf.keras.layers.Conv2D(4, (3, 3), activation='relu')(x)\nx = tf.keras.layers.Conv2D(4, (3, 3), activation='relu')(x)\nx = tf.keras.layers.BatchNormalization()(x)\nx = tf.keras.layers.MaxPool2D(2)(x)\n\n# Fourth set of convolutional layers\nx = tf.keras.layers.Conv2D(8, (3, 3), activation='relu')(x)\nx = tf.keras.layers.Conv2D(8, (3, 3), activation='relu')(x)\nx = tf.keras.layers.BatchNormalization()(x)\nx = tf.keras.layers.MaxPool2D(2)(x)\n\n# Fifth set of convolutional layers\nx = tf.keras.layers.Conv2D(8, (3, 3), activation='relu')(x)\nx = tf.keras.layers.Conv2D(8, (3, 3), activation='relu')(x)\nx = tf.keras.layers.BatchNormalization()(x)\nx = tf.keras.layers.MaxPool2D(2)(x)\n\n# Flatten the output and connect to a Dense (fully connected) layer\nx = tf.keras.layers.Flatten()(x)\noutputs = tf.keras.layers.Dense(y.shape[1], activation='sigmoid')(x)\n\n# Create the model with the defined input and output layers\nmodel = tf.keras.Model(inputs=inputs, outputs=outputs)\n\n# Compile the model with an Adam optimizer and MSE loss function, and print model summary\nopt = tf.keras.optimizers.Adam(learning_rate=0.0008, beta_1=0.9, beta_2=0.999, epsilon=1e-07, amsgrad=False)\nmodel.compile(optimizer=opt, loss='mse', metrics='mae')\nmodel.summary()\n\n# Train the model with the specified data (X, y)\nhistory = model.fit(X, y, epochs=10, batch_size=512, validation_split=0.3, verbose=1, shuffle=True)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-04T09:43:52.631026Z","iopub.execute_input":"2023-08-04T09:43:52.631369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# This notebook is inspired by https://www.kaggle.com/code/jigarbhanderi/eda-baseline-tf-model-reg","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# to do: add torch training example","metadata":{},"execution_count":null,"outputs":[]}]}