{"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"}],"dockerImageVersionId":30627,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## ","metadata":{}},{"cell_type":"markdown","source":"# Data Mount from Kaggle competition","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport os\n!pip install pydicom\n!pip install lime\n!pip install tensorflow-addons\nimport os\nimport numpy as np\nfrom PIL import Image\nimport pydicom\nimport cv2\nimport matplotlib.pyplot as plt\n","metadata":{"execution":{"iopub.status.busy":"2024-04-11T21:19:24.365178Z","iopub.execute_input":"2024-04-11T21:19:24.366023Z","iopub.status.idle":"2024-04-11T21:19:44.118589Z","shell.execute_reply.started":"2024-04-11T21:19:24.365984Z","shell.execute_reply":"2024-04-11T21:19:44.117795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Preparition for CNN","metadata":{}},{"cell_type":"code","source":"def get_data(filename,injury_name):\n    df = pd.read_csv(filename)\n    df.head()\n    df_ext = df.loc[df['injury_name'] == injury_name]\n    return df_ext","metadata":{"execution":{"iopub.status.busy":"2024-04-11T21:19:44.119925Z","iopub.execute_input":"2024-04-11T21:19:44.120242Z","iopub.status.idle":"2024-04-11T21:19:44.124159Z","shell.execute_reply.started":"2024-04-11T21:19:44.12021Z","shell.execute_reply":"2024-04-11T21:19:44.123582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_data_for_cnn(dirname,df_ext,length):\n    data = {\n    'patient_id': [],\n    'series_id': [],\n    'instance_number': [],\n    }\n    df_all = pd.DataFrame(data)\n    for dir in os.listdir(dirname):\n        for series in os.listdir(dirname+'/'+dir):\n            for img in os.listdir(dirname+'/'+dir+'/'+series):\n                new_row = {'patient_id': int(dir) ,'series_id': int(series),'instance_number': int(img.split('.')[0])}\n                df_all.loc[len(df_all)] = new_row\n        if len(df_all) > length:\n            break\n    \n    df_all.sort_values(['patient_id','series_id','instance_number'],inplace=True)\n    merged_df = pd.merge(df_all, df_ext[['patient_id','series_id','instance_number','injury_name']], on=['patient_id','series_id','instance_number'], how='outer')\n    merged_df = merged_df.fillna(\"Healthy\")\n    merged_df = merged_df.sample(frac=1).reset_index(drop=True)\n    merged_df.head()\n    return merged_df","metadata":{"execution":{"iopub.status.busy":"2024-04-11T21:19:44.124931Z","iopub.execute_input":"2024-04-11T21:19:44.125138Z","iopub.status.idle":"2024-04-11T21:19:44.136819Z","shell.execute_reply.started":"2024-04-11T21:19:44.125115Z","shell.execute_reply":"2024-04-11T21:19:44.136182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_data_for_cnn2(dirname, df_ext, length):\n    data = {\n        'patient_id': [],\n        'series_id': [],\n        'instance_number': [],\n    }\n    df_all = pd.DataFrame(data)\n    df_all['patient_id'] = df_all['patient_id'].astype(int)\n    df_all['series_id'] = df_all['series_id'].astype(int)\n    df_all['instance_number'] = df_all['instance_number'].astype(int)\n    \n    for dir in os.listdir(dirname):\n        for series in os.listdir(os.path.join(dirname, dir)):\n            slices = os.listdir(os.path.join(dirname, dir, series))\n            temp_df = pd.DataFrame({\n                'patient_id': [int(dir)] * len(slices),\n                'series_id': [int(series)] * len(slices),\n                'instance_number': [int(slc.replace('.dcm', '')) for slc in slices]\n            })\n            df_all = pd.concat([df_all, temp_df], ignore_index=True)\n        if len(df_all) > length:\n            break\n    \n    df_all.sort_values(['patient_id', 'series_id', 'instance_number'], inplace=True)\n    \n    merged_df = pd.merge(df_all, df_ext[['patient_id', 'series_id', 'instance_number', 'injury_name']], \n                        on=['patient_id', 'series_id', 'instance_number'], how='outer')\n    merged_df = merged_df.fillna(\"Healthy\")\n    merged_df = merged_df.sample(frac=1).reset_index(drop=True)\n    return merged_df","metadata":{"execution":{"iopub.status.busy":"2024-04-12T00:16:36.89908Z","iopub.execute_input":"2024-04-12T00:16:36.899542Z","iopub.status.idle":"2024-04-12T00:16:36.909187Z","shell.execute_reply.started":"2024-04-12T00:16:36.899495Z","shell.execute_reply":"2024-04-12T00:16:36.908154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data copying to working dir","metadata":{}},{"cell_type":"code","source":"def read_data_for_cnn(merged_df,dicom_directory):\n    dicom_files = merged_df.apply(lambda row: f\"{row['patient_id']}/{row['series_id']}/{row['instance_number']}.dcm\", axis=1)\n    \n    first_dicom_path = os.path.join(dicom_directory, dicom_files[0])\n    first_dicom = pydicom.dcmread(first_dicom_path)\n    image_size = (int(first_dicom.Rows), int(first_dicom.Columns))\n    print(image_size,first_dicom)\n    images = [None] * len(dicom_files)\n    labels = np.array(merged_df['injury_name'])\n\n    for dicom_file in dicom_files:\n        dicom_path = os.path.join(dicom_directory, dicom_file)\n        dicom_data = pydicom.dcmread(dicom_path)\n        pixel_array = dicom_data.pixel_array.astype(np.float32)\n    \n        if len(pixel_array.shape) == 2:\n        # Convert grayscale to three channels using OpenCV\n            pixel_array = cv2.cvtColor(pixel_array, cv2.COLOR_GRAY2RGB)\n\n    # Preprocess pixel_array as needed (e.g., resize, normalize)\n    # Assuming preprocessing involves normalizing to [0, 255]\n        pixel_array = (pixel_array - np.min(pixel_array)) / (np.max(pixel_array) - np.min(pixel_array)) * 255\n        pixel_array = pixel_array.astype(np.uint8)\n    \n        image = Image.fromarray(pixel_array)\n        image = image.resize((299,299),Image.BICUBIC)\n        image_array = np.array(image)\n        images.append(image_array)\n\n    images = np.array(images)\n    return images","metadata":{"execution":{"iopub.status.busy":"2024-04-12T00:15:19.854667Z","iopub.execute_input":"2024-04-12T00:15:19.855623Z","iopub.status.idle":"2024-04-12T00:15:19.863439Z","shell.execute_reply.started":"2024-04-12T00:15:19.855581Z","shell.execute_reply":"2024-04-12T00:15:19.862475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from joblib import Parallel, delayed\n\ndef preprocess_dicom(dicom_file_path):\n    dicom_data = pydicom.dcmread(dicom_file_path)\n    pixel_array = dicom_data.pixel_array.astype(np.float32)\n\n    if len(pixel_array.shape) == 2:\n        # Convert grayscale to three channels using OpenCV\n        pixel_array = cv2.cvtColor(pixel_array, cv2.COLOR_GRAY2RGB)\n\n    # Preprocess pixel_array as needed (e.g., resize, normalize)\n    # Assuming preprocessing involves normalizing to [0, 255]\n    pixel_array = (pixel_array - np.min(pixel_array)) / (np.max(pixel_array) - np.min(pixel_array)) * 255\n    pixel_array = pixel_array.astype(np.uint8)\n\n    image = Image.fromarray(pixel_array)\n    image = image.resize((299, 299), Image.BICUBIC)\n    image_array = np.array(image)\n\n    return image_array\n\ndef read_data_for_cnn_parallel(merged_df, dicom_directory):\n    dicom_files = merged_df.apply(lambda row: os.path.join(dicom_directory, f\"{row['patient_id']}/{row['series_id']}/{row['instance_number']}.dcm\"), axis=1)\n\n    # Define the function to preprocess each DICOM file\n    def preprocess_file(dicom_file):\n        return preprocess_dicom(dicom_file)\n\n    # Use joblib to parallelize the preprocessing across multiple cores\n    images = Parallel(n_jobs=-1)(delayed(preprocess_file)(dicom_file) for dicom_file in dicom_files)\n\n    images = np.array(images)\n    return images","metadata":{"execution":{"iopub.status.busy":"2024-04-12T00:22:37.503508Z","iopub.execute_input":"2024-04-12T00:22:37.504029Z","iopub.status.idle":"2024-04-12T00:22:37.513187Z","shell.execute_reply.started":"2024-04-12T00:22:37.503987Z","shell.execute_reply":"2024-04-12T00:22:37.512309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"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        except Exception as e:\n            print(f\"Error deleting {file_path}: {e}\")\n","metadata":{"execution":{"iopub.status.busy":"2024-04-12T00:15:20.519425Z","iopub.execute_input":"2024-04-12T00:15:20.519852Z","iopub.status.idle":"2024-04-12T00:15:20.525064Z","shell.execute_reply.started":"2024-04-12T00:15:20.519801Z","shell.execute_reply":"2024-04-12T00:15:20.524222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_and_preprocess_dicom(folder_path):\n    dicom_images = []\n    for filename in os.listdir(folder_path):\n        if filename.endswith(\".dcm\"):\n            filepath = os.path.join(folder_path, filename)\n            dicom_data = pydicom.dcmread(filepath)\n            pixel_array = dicom_data.pixel_array.astype(np.float32)\n            # Check if the image is grayscale\n            if len(pixel_array.shape) == 2:\n                # Convert grayscale to three channels using OpenCV\n                pixel_array = cv2.cvtColor(pixel_array, cv2.COLOR_GRAY2RGB)\n            \n            # Preprocess pixel_array as needed (e.g., resize, normalize)\n            preprocessed_image = preprocess_input(pixel_array)\n            dicom_images.append(preprocessed_image)\n    return np.array(dicom_images)","metadata":{"execution":{"iopub.status.busy":"2024-04-12T00:15:20.612342Z","iopub.execute_input":"2024-04-12T00:15:20.612701Z","iopub.status.idle":"2024-04-12T00:15:20.618752Z","shell.execute_reply.started":"2024-04-12T00:15:20.612669Z","shell.execute_reply":"2024-04-12T00:15:20.617851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Image visualization","metadata":{}},{"cell_type":"code","source":"def img_show(image_array):\n    plt.imshow(image_array)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-12T00:15:21.398356Z","iopub.execute_input":"2024-04-12T00:15:21.398767Z","iopub.status.idle":"2024-04-12T00:15:21.403361Z","shell.execute_reply.started":"2024-04-12T00:15:21.398732Z","shell.execute_reply":"2024-04-12T00:15:21.402214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Extravasation:","metadata":{}},{"cell_type":"code","source":"dirname = '/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images'\nfilename = '/kaggle/input/rsna-2023-abdominal-trauma-detection/image_level_labels.csv'","metadata":{"execution":{"iopub.status.busy":"2024-04-11T21:19:44.189911Z","iopub.execute_input":"2024-04-11T21:19:44.190163Z","iopub.status.idle":"2024-04-11T21:19:44.200703Z","shell.execute_reply.started":"2024-04-11T21:19:44.190139Z","shell.execute_reply":"2024-04-11T21:19:44.200107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_ext = get_data(filename,'Active_Extravasation')\nmerged_df = create_data_for_cnn2(dirname, df_ext, 10000)","metadata":{"execution":{"iopub.status.busy":"2024-04-12T00:16:41.552481Z","iopub.execute_input":"2024-04-12T00:16:41.55311Z","iopub.status.idle":"2024-04-12T00:16:41.651454Z","shell.execute_reply.started":"2024-04-12T00:16:41.55304Z","shell.execute_reply":"2024-04-12T00:16:41.650102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"merged_df","metadata":{"execution":{"iopub.status.busy":"2024-04-12T00:16:43.485689Z","iopub.execute_input":"2024-04-12T00:16:43.486501Z","iopub.status.idle":"2024-04-12T00:16:43.497335Z","shell.execute_reply.started":"2024-04-12T00:16:43.486462Z","shell.execute_reply":"2024-04-12T00:16:43.496486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = read_data_for_cnn_parallel(merged_df,'/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images')","metadata":{"execution":{"iopub.status.busy":"2024-04-12T00:22:47.952591Z","iopub.execute_input":"2024-04-12T00:22:47.953038Z","iopub.status.idle":"2024-04-12T00:25:58.991379Z","shell.execute_reply.started":"2024-04-12T00:22:47.952998Z","shell.execute_reply":"2024-04-12T00:25:58.989468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = np.array(merged_df['injury_name'])\nlabels","metadata":{"execution":{"iopub.status.busy":"2024-04-12T00:25:58.994092Z","iopub.execute_input":"2024-04-12T00:25:58.994421Z","iopub.status.idle":"2024-04-12T00:25:59.0018Z","shell.execute_reply.started":"2024-04-12T00:25:58.994383Z","shell.execute_reply":"2024-04-12T00:25:59.000948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images[0].shape","metadata":{"execution":{"iopub.status.busy":"2024-04-12T00:25:59.002877Z","iopub.execute_input":"2024-04-12T00:25:59.003162Z","iopub.status.idle":"2024-04-12T00:25:59.012921Z","shell.execute_reply.started":"2024-04-12T00:25:59.003134Z","shell.execute_reply":"2024-04-12T00:25:59.012073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_show(images[0])","metadata":{"execution":{"iopub.status.busy":"2024-04-12T00:25:59.014742Z","iopub.execute_input":"2024-04-12T00:25:59.015022Z","iopub.status.idle":"2024-04-12T00:25:59.313453Z","shell.execute_reply.started":"2024-04-12T00:25:59.014996Z","shell.execute_reply":"2024-04-12T00:25:59.312341Z"},"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(images, labels, test_size=0.2, random_state=42)\n\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":"2024-04-12T00:25:59.314424Z","iopub.execute_input":"2024-04-12T00:25:59.314668Z","iopub.status.idle":"2024-04-12T00:26:00.732811Z","shell.execute_reply.started":"2024-04-12T00:25:59.314642Z","shell.execute_reply":"2024-04-12T00:26:00.731589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-04-11T21:27:46.813055Z","iopub.execute_input":"2024-04-11T21:27:46.813531Z","iopub.status.idle":"2024-04-11T21:27:46.81894Z","shell.execute_reply.started":"2024-04-11T21:27:46.8135Z","shell.execute_reply":"2024-04-11T21:27:46.818167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# x_train = np.reshape(x_train, (x_train.shape[0], 512, 512, 3))\n# x_test = np.reshape(x_test,(x_test.shape[0], 512, 512,3))","metadata":{"execution":{"iopub.status.busy":"2024-04-11T21:27:46.819974Z","iopub.execute_input":"2024-04-11T21:27:46.820239Z","iopub.status.idle":"2024-04-11T21:27:46.829846Z","shell.execute_reply.started":"2024-04-11T21:27:46.82021Z","shell.execute_reply":"2024-04-11T21:27:46.829095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nlabel_encoder = LabelEncoder()\ny_train = label_encoder.fit_transform(y_train)\ny_test = label_encoder.transform(y_test)","metadata":{"execution":{"iopub.status.busy":"2024-04-11T21:27:46.830784Z","iopub.execute_input":"2024-04-11T21:27:46.831047Z","iopub.status.idle":"2024-04-11T21:27:46.842854Z","shell.execute_reply.started":"2024-04-11T21:27:46.831023Z","shell.execute_reply":"2024-04-11T21:27:46.842125Z"},"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":"2024-04-11T21:27:46.843897Z","iopub.execute_input":"2024-04-11T21:27:46.844172Z","iopub.status.idle":"2024-04-11T21:27:56.747649Z","shell.execute_reply.started":"2024-04-11T21:27:46.844146Z","shell.execute_reply":"2024-04-11T21:27:56.746591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## CNN","metadata":{}},{"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":{"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\nfrom tensorflow.keras.callbacks import ModelCheckpoint\nfrom tensorflow.keras.optimizers import AdamW\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":"2024-04-11T21:27:56.750968Z","iopub.execute_input":"2024-04-11T21:27:56.751315Z","iopub.status.idle":"2024-04-11T21:28:18.921318Z","shell.execute_reply.started":"2024-04-11T21:27:56.751282Z","shell.execute_reply":"2024-04-11T21:28:18.920214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\ncheckpoint_filepath = '/kaggle/working/model_weights/weights_epoch_{epoch:02d}.hdf5'\nmodel_checkpoint_callback = ModelCheckpoint(\n    filepath=checkpoint_filepath,\n    save_weights_only=True,\n    save_freq='epoch'\n)\n\nwith tpu_strategy.scope():\n    model = models.Sequential()\n    model.add(layers.Conv2D(1024, (3, 3), activation='relu', input_shape=(299, 299,3)))\n    model.add(layers.MaxPooling2D((2, 2)))\n    model.add(layers.Dropout(0.23))\n    model.add(layers.Conv2D(512, (3, 3), activation='relu'))\n    model.add(layers.MaxPooling2D((2, 2)))\n    model.add(layers.Dropout(0.2))\n    model.add(layers.Conv2D(512, (3, 3), activation='relu'))\n    model.add(layers.MaxPooling2D((2, 2)))\n    model.add(layers.Conv2D(256, (3, 3), activation='relu'))\n    model.add(layers.MaxPooling2D((2, 2)))\n    model.add(layers.Conv2D(256, (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.Conv2D(128, (3, 3), activation='relu'))\n#     model.add(layers.MaxPooling2D((2, 2)))\n    model.add(layers.Conv2D(64, (3, 3), activation='relu'))\n\n    model.add(layers.Flatten())\n    model.add(layers.Dense(64, activation='relu'))\n    model.add(layers.Dense(2))\n\n    model.compile(optimizer=AdamW(learning_rate = 0.01),\n                  loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n                  metrics=['accuracy'])\n\n# history = model.fit(x_train, y_train, epochs=3,\n#                     validation_data=(x_test, y_test),\n# #                     callbacks=[model_checkpoint_callback]\n#                    )\n\n# plt.plot(history.history['accuracy'], label='accuracy')\n# plt.plot(history.history['val_accuracy'], label = 'val_accuracy')\n# plt.xlabel('Epoch')\n# plt.ylabel('Accuracy')\n# plt.ylim([0.5, 1])\n# plt.legend(loc='lower right')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(x_train, y_train, epochs=3,\n                    validation_data=(x_test, y_test),\n                    callbacks=[model_checkpoint_callback]\n                   )","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss,test_acc = model.evaluate(x_test, y_test)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(test_loss,test_acc)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Bowel","metadata":{}},{"cell_type":"code","source":"images = read_data_for_cnn(merged_df,'/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images')\nlabels = np.array(merged_df['injury_name'])\nlabels","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_ext = get_data(filename,'Bowel')\nmerged_df = create_data_for_cnn(dirname, df_ext, 10000)\nmerged_df\n","metadata":{"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":{"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(images, labels, test_size=0.2, random_state=42)\n\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)\n\nfrom sklearn.preprocessing import LabelEncoder\nlabel_encoder = LabelEncoder()\ny_train = label_encoder.fit_transform(y_train)\ny_test = label_encoder.transform(y_test)\n\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(x_train, y_train, epochs=5,\n                    validation_data=(x_test, y_test),\n                    callbacks=[model_checkpoint_callback]\n                   )","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### LIME\n","metadata":{}},{"cell_type":"code","source":"import os\nimport keras\nfrom keras.applications import inception_v3 as inc_net\nfrom keras.applications.imagenet_utils import decode_predictions\nfrom skimage.io import imread\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport numpy as np\nprint('Notebook run using keras:', keras.__version__)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inet_model = inc_net.InceptionV3()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef transform_img_fn(path_list):\n    out = []\n    for img in path_list:\n        # Resize the image to 299x299 using bicubic interpolation\n        x = cv2.resize(img, (299, 299), interpolation=cv2.INTER_LANCZOS4)\n        x = np.expand_dims(x, axis=0)\n        x = inc_net.preprocess_input(x)\n        out.append(x)\n    return np.vstack(out)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"selectedImgs[0].shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = transform_img_fn(x_train[:10])\nselectedImgs = images[:3]\nimg_show(selectedImgs[2] / 2 + 0.5)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = transform_img_fn(x_train[:10])\n# I'm dividing by 2 and adding 0.5 because of how this Inception represents images\nselectedImgs = images[:10]\n\nplt.imshow(images[0] / 2 + 0.5)\npreds = inet_model.predict(images)\nfor x in decode_predictions(preds)[0]:\n    print(x)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images[0].shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Bukhar main likha tha or mujhy bhi nahi pata kia chal rha hai yahan\n\n# image_size = inception.inception_v3.default_image_size\n# def transform_img_fn(path_list):\n#     out = []\n#     for f in path_list:\n#         image_raw = tf.image.decode_jpeg(open(f).read(), channels=3)\n#         image = inception_preprocessing.preprocess_image(image_raw, image_size, image_size, is_training=False)\n#         out.append(image)\n#     return session.run([out])[0]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# images = transform_img_fn([os.path.join('data','cat_mouse.jpg')])\n# # I'm dividing by 2 and adding 0.5 because of how this Inception represents images\n# plt.imshow(images[0] / 2 + 0.5)\n# preds = inet_model.predict(images)\n# for x in decode_predictions(preds)[0]:\n#     print(x)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from skimage.segmentation import mark_boundaries\nfrom lime import lime_image\nwith tpu_strategy.scope():\n    explainer = lime_image.LimeImageExplainer()","metadata":{"execution":{"iopub.status.busy":"2024-04-11T22:44:30.93982Z","iopub.execute_input":"2024-04-11T22:44:30.941322Z","iopub.status.idle":"2024-04-11T22:44:30.94812Z","shell.execute_reply.started":"2024-04-11T22:44:30.941263Z","shell.execute_reply":"2024-04-11T22:44:30.946535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def generateExplaination(img,classifierFn, numSamples):\n    with tpu_strategy.scope():\n        return explainer.explain_instance(img.astype('double'), classifierFn, labels=[0,1],  num_samples=numSamples)\n    \ndef showExplainationTopLabel(explaination):\n    temp, mask = explaination.get_image_and_mask(explaination.top_labels[0], positive_only=True, hide_rest=False)\n    plt.imshow(mark_boundaries(temp, mask))\n    plt.show()\n    \ndef showExplainationPosNeg(explaination):\n    temp, mask = explaination.get_image_and_mask(explaination.top_labels[0], positive_only=False, hide_rest=False)\n    plt.imshow(mark_boundaries(temp, mask))\n    plt.show()\n    \ndef predictInjury(img, classificationFn, numSamples):\n    if (len(img.shape) == 3):\n        img = np.expand_dims(img, axis=0)\n    with tpu_strategy.scope():\n        print(f'label: {np.argmax(classificationFn(img))}')\n        explaination = generateExplaination(img[0], classificationFn, numSamples)\n        showExplainationTopLabel(explaination)\n        showExplainationPosNeg(explaination)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-11T22:44:32.137007Z","iopub.execute_input":"2024-04-11T22:44:32.137374Z","iopub.status.idle":"2024-04-11T22:44:32.144755Z","shell.execute_reply.started":"2024-04-11T22:44:32.137343Z","shell.execute_reply":"2024-04-11T22:44:32.14389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for slc in x_test[:5]:\n    predictInjury(slc, model.predict, 3000)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(x_test)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"explanation10.top_labels","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions[12]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test[12]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_show(x_test[12])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ResNet","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.layers import Flatten, Dense\nfrom tensorflow.keras.losses import SparseCategoricalCrossentropy\nfrom tensorflow.keras.callbacks import ModelCheckpoint\nfrom tensorflow.keras.optimizers import AdamW\nfrom tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input, decode_predictions\nfrom keras.utils import to_categorical","metadata":{"execution":{"iopub.status.busy":"2024-04-11T21:31:55.641565Z","iopub.execute_input":"2024-04-11T21:31:55.642459Z","iopub.status.idle":"2024-04-11T21:31:55.64742Z","shell.execute_reply.started":"2024-04-11T21:31:55.642417Z","shell.execute_reply":"2024-04-11T21:31:55.646522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpoint_filepath = '/kaggle/working/resnet_model_weights/weights_epoch_{epoch:02d}.hdf5'\nmodel_checkpoint_callback = ModelCheckpoint(\n    filepath=checkpoint_filepath,\n    save_weights_only=True,\n    save_freq='epoch'\n)","metadata":{"execution":{"iopub.status.busy":"2024-04-11T21:31:59.453444Z","iopub.execute_input":"2024-04-11T21:31:59.45377Z","iopub.status.idle":"2024-04-11T21:31:59.457895Z","shell.execute_reply.started":"2024-04-11T21:31:59.453742Z","shell.execute_reply":"2024-04-11T21:31:59.457111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with tpu_strategy.scope():\n    resnet_model = models.Sequential()\n    \n    base_model = ResNet50(include_top=False, \n                          input_shape=(299, 299, 3),\n                          pooling='avg',\n                          classes=2,\n                          weights='imagenet'\n                         )\n        \n    for layer in base_model.layers:\n        layer.trainable = False\n        \n    resnet_model.add(base_model)\n    resnet_model.add(Flatten())\n    resnet_model.add(Dense(1024, activation='relu'))\n#     resnet_model.add(Dense(256, activation='relu'))\n    resnet_model.add(Dense(2, activation='softmax'))\n\n    resnet_model.compile(optimizer=AdamW(learning_rate = 0.0001), loss='categorical_crossentropy', metrics=['accuracy'])\n    \n\nresnet_model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-04-11T21:32:04.00876Z","iopub.execute_input":"2024-04-11T21:32:04.009291Z","iopub.status.idle":"2024-04-11T21:32:16.491865Z","shell.execute_reply.started":"2024-04-11T21:32:04.009253Z","shell.execute_reply":"2024-04-11T21:32:16.49104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\n# Assuming y_train and y_test are your original target values \ny_train_encoded = to_categorical(y_train, num_classes=2) #converts 1 or 0 in y to [1,0]\ny_test_encoded = to_categorical(y_test, num_classes=2)","metadata":{"execution":{"iopub.status.busy":"2024-04-11T21:32:16.493186Z","iopub.execute_input":"2024-04-11T21:32:16.493485Z","iopub.status.idle":"2024-04-11T21:32:16.497584Z","shell.execute_reply.started":"2024-04-11T21:32:16.493454Z","shell.execute_reply":"2024-04-11T21:32:16.496814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Then, use y_train_encoded and y_test_encoded in your model.fit\nhistory = resnet_model.fit(x_train, y_train_encoded, epochs=20,\n                           validation_data=(x_test, y_test_encoded),\n                           callbacks=[model_checkpoint_callback])\n\nplt.plot(history.history['accuracy'], label='accuracy')\nplt.plot(history.history['val_accuracy'], label = 'val_accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.ylim([0.5, 1])\nplt.legend(loc='lower right')","metadata":{"execution":{"iopub.status.busy":"2024-04-11T22:15:04.285938Z","iopub.execute_input":"2024-04-11T22:15:04.286419Z","iopub.status.idle":"2024-04-11T22:21:55.400808Z","shell.execute_reply.started":"2024-04-11T22:15:04.286383Z","shell.execute_reply":"2024-04-11T22:21:55.399499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = resnet_model.predict(x_test[:100])\n\nfor index, slc in enumerate(x_test[:10]):\n    predictInjury(slc, resnet_model.predict, 5000)\n    print(f'Predicted Label: {np.argmax(predictions[index])}, True Label: {y_test[index]}')","metadata":{"execution":{"iopub.status.busy":"2024-04-11T22:46:17.471514Z","iopub.execute_input":"2024-04-11T22:46:17.471853Z","iopub.status.idle":"2024-04-11T23:34:26.307564Z","shell.execute_reply.started":"2024-04-11T22:46:17.471824Z","shell.execute_reply":"2024-04-11T23:34:26.306188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Gradient Based Method:","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n# Choose an image from your dataset\nimage = x_test[0]  # Assuming x_test contains your test images\n\n# Get the model's prediction for the image\nprediction = resnet_model.predict(np.expand_dims(image, axis=0))\nimage_tensor = tf.convert_to_tensor(image)\n\n# Compute gradients of the predicted class score with respect to the input image\nwith tf.GradientTape() as tape:\n    tape.watch(image_tensor)\n    prediction = resnet_model(image_tensor[np.newaxis, ...])\n    predicted_class = tf.argmax(prediction[0])\n    loss = prediction[:, predicted_class]\n\ngradients = tape.gradient(loss, image_tensor)\n\n# Normalize gradients\ngradients /= tf.reduce_max(tf.abs(gradients))\n\n# Convert gradients tensor to NumPy array\ngradients_np = gradients.numpy()\n\n# Plot the original image\nplt.subplot(1, 2, 1)\nplt.imshow(image)\nplt.title('Original Image')\n\n# Plot the gradient image\nplt.subplot(1, 2, 2)\nplt.imshow(gradients_np)\nplt.title('Gradient Image')\n\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-04-11T23:59:49.625721Z","iopub.execute_input":"2024-04-11T23:59:49.626528Z","iopub.status.idle":"2024-04-11T23:59:52.209849Z","shell.execute_reply.started":"2024-04-11T23:59:49.626489Z","shell.execute_reply":"2024-04-11T23:59:52.208706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Saliency Maps:","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\nimport tensorflow as tf\n\n# Choose an image from your dataset\nimage = x_test[0]  # Assuming x_test contains your test images\n\n# Get the model's prediction for the image\nprediction = resnet_model.predict(np.expand_dims(image, axis=0))\n\nimage_tensor = tf.convert_to_tensor(image)\n\n# Compute gradients of the predicted class score with respect to the input image\nwith tf.GradientTape() as tape:\n    tape.watch(image_tensor)\n    prediction = resnet_model(image_tensor[np.newaxis, ...])\n    predicted_class = tf.argmax(prediction[0])\n    loss = prediction[:, predicted_class]\n\ngradients = tape.gradient(loss, image_tensor)\n\n# Compute the absolute values of gradients to emphasize the salient regions\nsaliency = tf.abs(gradients)\n\n# Normalize the saliency map\ndgrad_max_ = np.max(saliency, axis=-1)\narr_min, arr_max  = np.min(dgrad_max_), np.max(dgrad_max_)\ngrad_eval = (dgrad_max_ - arr_min) / (arr_max - arr_min + 1e-18)\n\n# Plotting\nfig, axes = plt.subplots(1, 2, figsize=(14, 5))\naxes[0].imshow(image)\nim = axes[1].imshow(grad_eval, cmap=\"jet\", alpha=0.8, vmin=0, vmax=0.1)  # Adjust vmin and vmax as needed\nfig.colorbar(im)\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-04-11T23:57:54.067189Z","iopub.execute_input":"2024-04-11T23:57:54.06765Z","iopub.status.idle":"2024-04-11T23:57:56.768682Z","shell.execute_reply.started":"2024-04-11T23:57:54.06761Z","shell.execute_reply":"2024-04-11T23:57:56.767427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Image shape:\", image.shape)\nprint(\"Saliency map shape:\", saliency.shape)","metadata":{"execution":{"iopub.status.busy":"2024-04-11T23:44:14.102138Z","iopub.execute_input":"2024-04-11T23:44:14.102521Z","iopub.status.idle":"2024-04-11T23:44:14.107107Z","shell.execute_reply.started":"2024-04-11T23:44:14.102487Z","shell.execute_reply":"2024-04-11T23:44:14.106256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}