{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":36363,"databundleVersionId":4050810,"sourceType":"competition"}],"dockerImageVersionId":30804,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-12-08T01:22:03.669952Z","iopub.execute_input":"2024-12-08T01:22:03.670372Z","iopub.status.idle":"2024-12-08T01:22:04.115622Z","shell.execute_reply.started":"2024-12-08T01:22:03.670338Z","shell.execute_reply":"2024-12-08T01:22:04.114384Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport cv2\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.applications.vgg16 import preprocess_input\nfrom tensorflow.keras.preprocessing.image import load_img, img_to_array","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T17:40:44.375488Z","iopub.execute_input":"2024-12-08T17:40:44.375915Z","iopub.status.idle":"2024-12-08T17:40:44.635588Z","shell.execute_reply.started":"2024-12-08T17:40:44.375877Z","shell.execute_reply":"2024-12-08T17:40:44.634448Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.applications import VGG16\nfrom tensorflow.keras.layers import Conv2D, MaxPool2D, Dropout, Flatten, Dense\nfrom tensorflow.keras.models import Model\nfrom sklearn.model_selection import train_test_split\n\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T17:40:48.699072Z","iopub.execute_input":"2024-12-08T17:40:48.699471Z","iopub.status.idle":"2024-12-08T17:40:48.705565Z","shell.execute_reply.started":"2024-12-08T17:40:48.699438Z","shell.execute_reply":"2024-12-08T17:40:48.704152Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df = pd.read_csv(\"/kaggle/input/rsna-2022-cervical-spine-fracture-detection/train.csv\")\ntrain_df[\"image_path\"] = train_df[\"StudyInstanceUID\"].apply(lambda x: f\"train_images/{x}.png\") \ntrain_df = train_df[[\"image_path\", \"patient_overall\"]]\n\ntrain_data, val_data = train_test_split(train_df, test_size=0.2, stratify=train_df[\"patient_overall\"], random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T17:40:52.817705Z","iopub.execute_input":"2024-12-08T17:40:52.818233Z","iopub.status.idle":"2024-12-08T17:40:52.861397Z","shell.execute_reply.started":"2024-12-08T17:40:52.818184Z","shell.execute_reply":"2024-12-08T17:40:52.860293Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Image Augmentation**","metadata":{}},{"cell_type":"code","source":"datagen = ImageDataGenerator(\n    rescale=1.0 / 255,\n    rotation_range=20,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    validation_split=0.2\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T17:40:58.369361Z","iopub.execute_input":"2024-12-08T17:40:58.369741Z","iopub.status.idle":"2024-12-08T17:40:58.375603Z","shell.execute_reply.started":"2024-12-08T17:40:58.369702Z","shell.execute_reply":"2024-12-08T17:40:58.374445Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_generator = datagen.flow_from_dataframe(\n    train_data,\n    x_col=\"StudyInstanceUID\",\n    y_col=\"patient_overall\",\n    target_size=(224, 224),\n    batch_size=32,\n    class_mode=\"binary\"\n)\n\nval_generator = datagen.flow_from_dataframe(\n    val_data,\n    x_col=\"image_path\",\n    y_col=\"patient_overall_label\",\n    target_size=(224, 224),\n    batch_size=32,\n    class_mode=\"binary\"\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-08T17:41:04.64405Z","iopub.execute_input":"2024-12-08T17:41:04.644422Z","iopub.status.idle":"2024-12-08T17:41:05.908937Z","shell.execute_reply.started":"2024-12-08T17:41:04.64439Z","shell.execute_reply":"2024-12-08T17:41:05.907312Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class VGGModel(tf.keras.Model):\n    def __init__(self):\n        super(VGGModel, self).__init__()\n\n        # TASK 3\n        # TODO: Select an optimizer for your network (see the documentation\n        #       for tf.keras.optimizers)\n\n        self.optimizer = self.optimizer = tf.keras.optimizers.Adam(learning_rate= 1e-4)\n\n        # Don't change the below:\n\n        self.vgg16 = [\n            # Block 1\n            Conv2D(64, 3, 1, padding=\"same\",\n                   activation=\"relu\", name=\"block1_conv1\"),\n            Conv2D(64, 3, 1, padding=\"same\",\n                   activation=\"relu\", name=\"block1_conv2\"),\n            MaxPool2D(2, name=\"block1_pool\"),\n            # Block 2\n            Conv2D(128, 3, 1, padding=\"same\",\n                   activation=\"relu\", name=\"block2_conv1\"),\n            Conv2D(128, 3, 1, padding=\"same\",\n                   activation=\"relu\", name=\"block2_conv2\"),\n            MaxPool2D(2, name=\"block2_pool\"),\n            # Block 3\n            Conv2D(256, 3, 1, padding=\"same\",\n                   activation=\"relu\", name=\"block3_conv1\"),\n            Conv2D(256, 3, 1, padding=\"same\",\n                   activation=\"relu\", name=\"block3_conv2\"),\n            Conv2D(256, 3, 1, padding=\"same\",\n                   activation=\"relu\", name=\"block3_conv3\"),\n            MaxPool2D(2, name=\"block3_pool\"),\n            # Block 4\n            Conv2D(512, 3, 1, padding=\"same\",\n                   activation=\"relu\", name=\"block4_conv1\"),\n            Conv2D(512, 3, 1, padding=\"same\",\n                   activation=\"relu\", name=\"block4_conv2\"),\n            Conv2D(512, 3, 1, padding=\"same\",\n                   activation=\"relu\", name=\"block4_conv3\"),\n            MaxPool2D(2, name=\"block4_pool\"),\n            # Block 5\n            Conv2D(512, 3, 1, padding=\"same\",\n                   activation=\"relu\", name=\"block5_conv1\"),\n            Conv2D(512, 3, 1, padding=\"same\",\n                   activation=\"relu\", name=\"block5_conv2\"),\n            Conv2D(512, 3, 1, padding=\"same\",\n                   activation=\"relu\", name=\"block5_conv3\"),\n            MaxPool2D(2, name=\"block5_pool\")\n        ]\n\n        # self.vgg16 = tf.keras.Sequential(vgg16_layers, name=\"vgg_base\")\n        \n        # for layer in self.vgg16.layers:\n        #     layer.trainable = False\n\n        # TASK 3\n        # TODO: Make all layers in self.vgg16 non-trainable. This will freeze the\n        #       pretrained VGG16 weights into place so that only the classificaiton\n        #       head is trained.\n\n        # TODO: Write a classification head for our 15-scene classification task.\n\n        self.head = [\n              Flatten(),\n              Dense(256, activation='relu'),\n              Dropout(0.5),\n              Dense(128, activation='relu'),\n              Dropout(0.3),\n              Dense(1, activation='sigmoid')]\n\n        # Don't change the below:\n        self.vgg16 = tf.keras.Sequential(self.vgg16, name=\"vgg_base\")\n        self.head = tf.keras.Sequential(self.head, name=\"vgg_head\")\n\n        for layer in self.vgg16.layers:\n            layer.trainable = False\n\n    def call(self, x):\n        \"\"\" Passes the image through the network. \"\"\"\n\n        x = self.vgg16(x)\n        x = self.head(x)\n\n        return x\n\n    @staticmethod\n    def loss_fn(labels, predictions):\n        \"\"\" Loss function for model. \"\"\"\n\n        # TASK 3\n        # TODO: Select a loss function for your network (see the documentation\n        #       for tf.keras.losses)\n        #       Read the documentation carefully, some might not work with our \n        #       model!\n        return tf.keras.losses.BinaryCrossentropy()(labels, predictions)\n\n        pass","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"weights_path = '/kaggle/input/vgg16weights2/vgg16_imagenet.h5'","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = VGGModel()\nmodel(tf.keras.Input(shape=(224, 224, 3)))\nmodel.vgg16.load_weights(weights_path)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\n# Define paths to training and validation directories\ntrain_dir = '/kaggle/input/spine-fracture-prediction-from-xrays/cervical fracture/train'\nval_dir = '/kaggle/input/spine-fracture-prediction-from-xrays/cervical fracture/val'\n\n# Load and preprocess datasets\ntrain_dataset = tf.keras.utils.image_dataset_from_directory(\n    train_dir,\n    image_size=(224, 224),  # Resize images to match your model's input size\n    batch_size=32          # Adjust based on your hardware\n)\n\nval_dataset = tf.keras.utils.image_dataset_from_directory(\n    val_dir,\n    image_size=(224, 224),\n    batch_size=32\n)\n\n# Optional: Improve performance with caching and prefetching\n# train_dataset = train_dataset.prefetch(buffer_size=tf.data.AUTOTUNE)\n# val_dataset = val_dataset.prefetch(buffer_size=tf.data.AUTOTUNE)\n\n# Compile your model (assuming `model` is already defined)\nmodel.compile(\n    optimizer='adam',\n    loss='binary_crossentropy',  # Adjust based on your task\n    metrics=['accuracy']\n)\n\n# Train the model\nhistory = model.fit(\n    train_dataset,\n    validation_data=val_dataset,\n    epochs=10  # Adjust as needed\n)\n","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}