{"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":"# # !pip install tensorflow==2.9\n# # %tensorflow_version 2.9\n# !pip install -qq keras-cv\n# !pip install -qq wandb\n# !pip install -q keras-cv-attention-models\n# !pip install --quiet vit-keras\n\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom vit_keras import vit\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\nfrom matplotlib import pyplot as plt\nfrom sklearn.model_selection import train_test_split\nimport cv2\nfrom vit_keras import vit\nimport keras_cv_attention_models\n# from keras_cv.models.classification import ViTTiny16\ntf.__version__","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-02T17:55:10.032492Z","iopub.execute_input":"2023-11-02T17:55:10.032791Z","iopub.status.idle":"2023-11-02T17:55:18.857124Z","shell.execute_reply.started":"2023-11-02T17:55:10.032764Z","shell.execute_reply":"2023-11-02T17:55:18.856254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Config:\n    SEED = 42\n    IMAGE_SIZE = [256, 256]\n    BATCH_SIZE = 16\n    EPOCHS = 10\n    TARGET_COLS  = [\n        \"bowel_injury\", \"extravasation_injury\",\n        \"kidney_healthy\", \"kidney_low\", \"kidney_high\",\n        \"liver_healthy\", \"liver_low\", \"liver_high\",\n        \"spleen_healthy\", \"spleen_low\", \"spleen_high\",\n    ]\n    AUTOTUNE = tf.data.AUTOTUNE\n\nconfig = Config()","metadata":{"execution":{"iopub.status.busy":"2023-11-02T17:55:18.858412Z","iopub.execute_input":"2023-11-02T17:55:18.859068Z","iopub.status.idle":"2023-11-02T17:55:18.865414Z","shell.execute_reply.started":"2023-11-02T17:55:18.859033Z","shell.execute_reply":"2023-11-02T17:55:18.864506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PATH = f\"/kaggle/input/rsna-atd-512x512-png-v2-dataset\"\n# PATH = f\"/kaggle/input/rsna-2023-abdominal-trauma-detection\"","metadata":{"execution":{"iopub.status.busy":"2023-11-02T17:55:18.868058Z","iopub.execute_input":"2023-11-02T17:55:18.868778Z","iopub.status.idle":"2023-11-02T17:55:18.90017Z","shell.execute_reply.started":"2023-11-02T17:55:18.868752Z","shell.execute_reply":"2023-11-02T17:55:18.899272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train\ndataframe = pd.read_csv(f\"{PATH}/train.csv\")\ndataframe[\"image_path\"] = f\"{PATH}/train_images\"+ \"/\" + dataframe.patient_id.astype(str)+ \"/\" + dataframe.series_id.astype(str)+ \"/\" + dataframe.instance_number.astype(str) +\".png\"\ndataframe = dataframe.drop_duplicates()\n\ndataframe.head(2)","metadata":{"execution":{"iopub.status.busy":"2023-11-02T17:55:18.901421Z","iopub.execute_input":"2023-11-02T17:55:18.901744Z","iopub.status.idle":"2023-11-02T17:55:19.057593Z","shell.execute_reply.started":"2023-11-02T17:55:18.901713Z","shell.execute_reply":"2023-11-02T17:55:19.056742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Train, Val = train_test_split(dataframe, test_size=0.2,shuffle = True ,random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-11-02T17:55:19.059147Z","iopub.execute_input":"2023-11-02T17:55:19.05952Z","iopub.status.idle":"2023-11-02T17:55:19.067177Z","shell.execute_reply.started":"2023-11-02T17:55:19.059488Z","shell.execute_reply":"2023-11-02T17:55:19.066388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Train.shape, Val.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-02T17:55:19.068249Z","iopub.execute_input":"2023-11-02T17:55:19.068537Z","iopub.status.idle":"2023-11-02T17:55:19.078737Z","shell.execute_reply.started":"2023-11-02T17:55:19.068508Z","shell.execute_reply":"2023-11-02T17:55:19.077902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.__version__","metadata":{"execution":{"iopub.status.busy":"2023-11-02T17:55:19.07975Z","iopub.execute_input":"2023-11-02T17:55:19.08007Z","iopub.status.idle":"2023-11-02T17:55:19.091676Z","shell.execute_reply.started":"2023-11-02T17:55:19.080037Z","shell.execute_reply":"2023-11-02T17:55:19.090705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels = Train[config.TARGET_COLS].values\nval_labels = Val[config.TARGET_COLS].values\n\ndef Preprocess(image_path, label):\n \n    image =tf.cast( tf.image.resize(tf.io.decode_png(tf.io.read_file(image_path), channels=3, dtype=tf.uint8), config.IMAGE_SIZE, method=\"bilinear\")\n    , tf.float32) / 255.0\n    label = tf.cast(label, tf.float32)\n    labels = (label[0:1], label[1:2], label[2:5], label[5:8], label[8:11])\n    return (image, labels)\n\ntrain_ds = (\n        tf.data.Dataset.from_tensor_slices((Train.image_path.tolist(), train_labels))\n        .map(Preprocess, num_parallel_calls=config.AUTOTUNE)\n        .shuffle(config.BATCH_SIZE * 10)\n        .batch(config.BATCH_SIZE)\n        .prefetch(config.AUTOTUNE)\n    )\nval_ds = (\n        tf.data.Dataset.from_tensor_slices((Val.image_path.tolist(), val_labels))\n        .map(Preprocess, num_parallel_calls=config.AUTOTUNE)\n        .shuffle(config.BATCH_SIZE * 10)\n        .batch(config.BATCH_SIZE)\n        .prefetch(config.AUTOTUNE)\n    )","metadata":{"execution":{"iopub.status.busy":"2023-11-02T17:55:19.09262Z","iopub.execute_input":"2023-11-02T17:55:19.092876Z","iopub.status.idle":"2023-11-02T17:55:22.451454Z","shell.execute_reply.started":"2023-11-02T17:55:19.09285Z","shell.execute_reply":"2023-11-02T17:55:22.450695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.applications import ResNet50\nfrom tensorflow.keras.layers import GlobalAveragePooling2D, Dense, Dropout\nmodel_name = 'Resnet50'\n# INPUT layer\nInput = tf.keras.layers.Input(shape=(256, 256, 3))\n\n# Augment images\naug = tf.keras.layers.experimental.preprocessing.RandomFlip('horizontal')(Input)\n# aug = tf.keras.layers.experimental.preprocessing.RandomRotation(0.2)(aug)\naug = tf.keras.layers.experimental.preprocessing.RandomFlip('vertical')(aug)\naug = tf.keras.layers.experimental.preprocessing.RandomHeight(0.2)(aug)\naug = tf.keras.layers.experimental.preprocessing.RandomWidth(0.2)(aug)\n# aug = tf.keras.layers.experimental.preprocessing.Resizing(256,256)(aug)\n\n# Transfer learning layer\nTL = ResNet50(weights='imagenet', include_top=False)(aug)\n\n# Define the custom heads for five different outputs\noutput1 = GlobalAveragePooling2D()(TL)\noutput1 = Dense(32, activation='relu')(output1)\noutput1 = Dense(1, activation='sigmoid', name='bowel')(output1)\n\noutput2 = GlobalAveragePooling2D()(TL)\noutput2 = Dense(32, activation='relu')(output2)\noutput2 = Dense(1, activation='sigmoid', name='extra')(output2)\n\noutput3 = GlobalAveragePooling2D()(TL)\noutput3 = Dense(32, activation='relu')(output3)\noutput3 = Dense(3, activation='softmax', name='liver')(output3)\n\noutput4 = GlobalAveragePooling2D()(TL)\noutput4 = Dense(32, activation='relu')(output4)\noutput4 = Dense(3, activation='softmax', name='kidney')(output4)\n\noutput5 = GlobalAveragePooling2D()(TL)\noutput5 = Dense(32, activation='relu')(output5)\noutput5 = Dense(3, activation='softmax', name='spleen')(output5)\n\n# Create the model\nmodel = tf.keras.Model(inputs=Input, outputs=[output1, output2, output3, output4, output5])\n\nTL.trainable = True\n\n# Compile the model (you can adjust the loss functions and metrics as needed)\nmodel.compile(optimizer='adam',\n              loss={'bowel': 'binary_crossentropy', \n                    'extra': 'binary_crossentropy',\n                    'liver': 'categorical_crossentropy', \n                    'kidney': 'categorical_crossentropy',\n                    'spleen': 'categorical_crossentropy'\n                   },\n              metrics={'bowel': 'acc', \n                       'extra': 'acc',\n                       'liver': 'acc', \n                       'kidney': 'acc', \n                       'spleen': 'acc'})\n\n# You can print the model summary to inspect the architecture\nmodel.summary()\n","metadata":{"execution":{"iopub.status.busy":"2023-11-01T20:56:02.865245Z","iopub.execute_input":"2023-11-01T20:56:02.866063Z","iopub.status.idle":"2023-11-01T20:56:11.601474Z","shell.execute_reply.started":"2023-11-01T20:56:02.86602Z","shell.execute_reply":"2023-11-01T20:56:11.599659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"old=100\nclass myCallback(tf.keras.callbacks.Callback):\n        def on_epoch_end(self,epoch,logs={}):\n            global old\n            if(logs.get('val_loss') < old):\n                #change model name here\n                model.save(model_name+'.h5',overwrite=True)\n                old = logs.get('val_loss')\n                print(\"\\n new best found threshold now = \",old)\n\nsaver =myCallback()\nhistory = model.fit(\n    train_ds,\n    epochs=10,\n    validation_data=val_ds,\n    callbacks=[saver]\n)","metadata":{"execution":{"iopub.status.busy":"2023-10-22T22:04:01.654493Z","iopub.execute_input":"2023-10-22T22:04:01.655081Z","iopub.status.idle":"2023-10-22T22:33:58.19878Z","shell.execute_reply.started":"2023-10-22T22:04:01.655047Z","shell.execute_reply":"2023-10-22T22:33:58.197917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nfrom tensorflow.keras.layers import GlobalAveragePooling2D, Dense, Dropout\nInput = tf.keras.layers.Input(shape=(256, 256, 3))\nmodel_name = 'Xceptiion'\n# Augment images\naug = tf.keras.layers.experimental.preprocessing.RandomFlip('horizontal')(Input)\n# aug = tf.keras.layers.experimental.preprocessing.RandomRotation(0.2)(aug)\naug = tf.keras.layers.experimental.preprocessing.RandomFlip('vertical')(aug)\naug = tf.keras.layers.experimental.preprocessing.RandomHeight(0.2)(aug)\naug = tf.keras.layers.experimental.preprocessing.RandomWidth(0.2)(aug)\n# aug = tf.keras.layers.experimental.preprocessing.Resizing(256,256)(aug)\n\n# Transfer learning layer\nTL = tf.keras.applications.xception.Xception(weights='imagenet', include_top=False)(aug)\n\n# Define the custom heads for five different outputs\noutput1 = GlobalAveragePooling2D()(TL)\noutput1 = Dense(32, activation='relu')(output1)\noutput1 = Dense(1, activation='sigmoid', name='bowel')(output1)\n\noutput2 = GlobalAveragePooling2D()(TL)\noutput2 = Dense(32, activation='relu')(output2)\noutput2 = Dense(1, activation='sigmoid', name='extra')(output2)\n\noutput3 = GlobalAveragePooling2D()(TL)\noutput3 = Dense(32, activation='relu')(output3)\noutput3 = Dense(3, activation='softmax', name='liver')(output3)\n\noutput4 = GlobalAveragePooling2D()(TL)\noutput4 = Dense(32, activation='relu')(output4)\noutput4 = Dense(3, activation='softmax', name='kidney')(output4)\n\noutput5 = GlobalAveragePooling2D()(TL)\noutput5 = Dense(32, activation='relu')(output5)\noutput5 = Dense(3, activation='softmax', name='spleen')(output5)\n\n# Create the model\nmodel = tf.keras.Model(inputs=Input, outputs=[output1, output2, output3, output4, output5])\n\nTL.trainable = True\n\n# Compile the model (you can adjust the loss functions and metrics as needed)\nmodel.compile(optimizer='adam',\n              loss={'bowel': 'binary_crossentropy', \n                    'extra': 'binary_crossentropy',\n                    'liver': 'categorical_crossentropy', \n                    'kidney': 'categorical_crossentropy',\n                    'spleen': 'categorical_crossentropy'\n                   },\n              metrics={'bowel': 'acc', \n                       'extra': 'acc',\n                       'liver': 'acc', \n                       'kidney': 'acc', \n                       'spleen': 'acc'})\n\n# You can print the model summary to inspect the architecture\nmodel.summary()\nold=100\nclass myCallback(tf.keras.callbacks.Callback):\n        def on_epoch_end(self,epoch,logs={}):\n            global old\n            if(logs.get('val_loss') < old):\n                #change model name here\n                model.save(model_name+'.h5',overwrite=True)\n                old = logs.get('val_loss')\n                print(\"\\n new best found threshold now = \",old)\n\nsaver =myCallback()\nhistory = model.fit(\n    train_ds,\n    epochs=10,\n    validation_data=val_ds,\n    callbacks=[saver]\n)","metadata":{"execution":{"iopub.status.busy":"2023-10-23T18:51:23.253904Z","iopub.execute_input":"2023-10-23T18:51:23.254326Z","iopub.status.idle":"2023-10-23T19:59:16.319962Z","shell.execute_reply.started":"2023-10-23T18:51:23.254292Z","shell.execute_reply":"2023-10-23T19:59:16.318818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    train_ds,\n    epochs=5,\n    validation_data=val_ds,\n    callbacks=[saver]\n)","metadata":{"execution":{"iopub.status.busy":"2023-10-23T20:00:03.348718Z","iopub.execute_input":"2023-10-23T20:00:03.349105Z","iopub.status.idle":"2023-10-23T20:17:21.663699Z","shell.execute_reply.started":"2023-10-23T20:00:03.349074Z","shell.execute_reply":"2023-10-23T20:17:21.662692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.applications import ResNet50,ResNet152\nfrom tensorflow.keras.layers import GlobalAveragePooling2D, Dense, Dropout\nmodel_name = 'ResNet152'\n# INPUT layer\nInput = tf.keras.layers.Input(shape=(256, 256, 3))\n\n# Augment images\naug = tf.keras.layers.experimental.preprocessing.RandomFlip('horizontal')(Input)\n# aug = tf.keras.layers.experimental.preprocessing.RandomRotation(0.2)(aug)\naug = tf.keras.layers.experimental.preprocessing.RandomFlip('vertical')(aug)\naug = tf.keras.layers.experimental.preprocessing.RandomHeight(0.2)(aug)\naug = tf.keras.layers.experimental.preprocessing.RandomWidth(0.2)(aug)\n# aug = tf.keras.layers.experimental.preprocessing.Resizing(256,256)(aug)\n\n# Transfer learning layer\nTL = ResNet152(weights='imagenet', include_top=False)(aug)\n\n# Define the custom heads for five different outputs\noutput1 = GlobalAveragePooling2D()(TL)\noutput1 = Dense(32, activation='relu')(output1)\noutput1 = Dense(1, activation='sigmoid', name='bowel')(output1)\n\noutput2 = GlobalAveragePooling2D()(TL)\noutput2 = Dense(32, activation='relu')(output2)\noutput2 = Dense(1, activation='sigmoid', name='extra')(output2)\n\noutput3 = GlobalAveragePooling2D()(TL)\noutput3 = Dense(32, activation='relu')(output3)\noutput3 = Dense(3, activation='softmax', name='liver')(output3)\n\noutput4 = GlobalAveragePooling2D()(TL)\noutput4 = Dense(32, activation='relu')(output4)\noutput4 = Dense(3, activation='softmax', name='kidney')(output4)\n\noutput5 = GlobalAveragePooling2D()(TL)\noutput5 = Dense(32, activation='relu')(output5)\noutput5 = Dense(3, activation='softmax', name='spleen')(output5)\n\n# Create the model\nmodel = tf.keras.Model(inputs=Input, outputs=[output1, output2, output3, output4, output5])\n\nTL.trainable = True\n\n# Compile the model (you can adjust the loss functions and metrics as needed)\nmodel.compile(optimizer='adam',\n              loss={'bowel': 'binary_crossentropy', \n                    'extra': 'binary_crossentropy',\n                    'liver': 'categorical_crossentropy', \n                    'kidney': 'categorical_crossentropy',\n                    'spleen': 'categorical_crossentropy'\n                   },\n              metrics={'bowel': 'acc', \n                       'extra': 'acc',\n                       'liver': 'acc', \n                       'kidney': 'acc', \n                       'spleen': 'acc'})\n\n# You can print the model summary to inspect the architecture\nmodel.summary()\nold=float('inf')\nclass myCallback(tf.keras.callbacks.Callback):\n        def on_epoch_end(self,epoch,logs={}):\n            global old\n            if(logs.get('val_loss') < old):\n                #change model name here\n                model.save(model_name+'.h5',overwrite=True)\n                old = logs.get('val_loss')\n                print(\"\\n new best found threshold now = \",old)\ntrain_ds = (\n        tf.data.Dataset.from_tensor_slices((Train.image_path.tolist(), train_labels))\n        .map(Preprocess, num_parallel_calls=config.AUTOTUNE)\n        .shuffle(config.BATCH_SIZE * 10)\n        .batch(16)\n        .prefetch(config.AUTOTUNE)\n    )\nval_ds = (\n        tf.data.Dataset.from_tensor_slices((Val.image_path.tolist(), val_labels))\n        .map(Preprocess, num_parallel_calls=config.AUTOTUNE)\n        .shuffle(config.BATCH_SIZE * 10)\n        .batch(16)\n        .prefetch(config.AUTOTUNE)\n    )\nsaver =myCallback()\nhistory = model.fit(\n    train_ds,\n    epochs=10,\n    validation_data=val_ds,\n    callbacks=[saver]\n)","metadata":{"execution":{"iopub.status.busy":"2023-10-23T01:39:55.332581Z","iopub.execute_input":"2023-10-23T01:39:55.333255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.applications import ResNet50\nfrom tensorflow.keras.layers import GlobalAveragePooling2D, Dense, Dropout\nmodel_name = 'EfficientNetV1B0'\n# INPUT layer\nInput = tf.keras.layers.Input(shape=(256, 256, 3))\n\n# Augment images\naug = tf.keras.layers.experimental.preprocessing.RandomFlip('horizontal')(Input)\n# aug = tf.keras.layers.experimental.preprocessing.RandomRotation(0.2)(aug)\naug = tf.keras.layers.experimental.preprocessing.RandomFlip('vertical')(aug)\naug = tf.keras.layers.experimental.preprocessing.RandomHeight(0.2)(aug)\naug = tf.keras.layers.experimental.preprocessing.RandomWidth(0.2)(aug)\n# aug = tf.keras.layers.experimental.preprocessing.Resizing(256,256)(aug)\n\n# Transfer learning layer\n# TL = ResNet50(weights='imagenet', include_top=False)(aug)\n# TL =  keras_cv_attention_models.efficientnet.from\nTL = getattr(keras_cv_attention_models.efficientnet, 'EfficientNetV1B0')(input_shape=(256,256,3),\n                                    pretrained='imagenet',\n                                    num_classes=0)(aug) # get base model (efficientnet), use imgnet weights\n\n\n    \n    \n# Define the custom heads for five different outputs\noutput1 = GlobalAveragePooling2D()(TL)\noutput1 = Dense(32, activation='relu')(output1)\noutput1 = Dense(1, activation='sigmoid', name='bowel')(output1)\n\noutput2 = GlobalAveragePooling2D()(TL)\noutput2 = Dense(32, activation='relu')(output2)\noutput2 = Dense(1, activation='sigmoid', name='extra')(output2)\n\noutput3 = GlobalAveragePooling2D()(TL)\noutput3 = Dense(32, activation='relu')(output3)\noutput3 = Dense(3, activation='softmax', name='liver')(output3)\n\noutput4 = GlobalAveragePooling2D()(TL)\noutput4 = Dense(32, activation='relu')(output4)\noutput4 = Dense(3, activation='softmax', name='kidney')(output4)\n\noutput5 = GlobalAveragePooling2D()(TL)\noutput5 = Dense(32, activation='relu')(output5)\noutput5 = Dense(3, activation='softmax', name='spleen')(output5)\n\n# Create the model\nmodel = tf.keras.Model(inputs=Input, outputs=[output1, output2, output3, output4, output5])\n\nTL.trainable = True\n\n# Compile the model (you can adjust the loss functions and metrics as needed)\nmodel.compile(optimizer='adam',\n              loss={'bowel': 'binary_crossentropy', \n                    'extra': 'binary_crossentropy',\n                    'liver': 'categorical_crossentropy', \n                    'kidney': 'categorical_crossentropy',\n                    'spleen': 'categorical_crossentropy'\n                   },\n              metrics={'bowel': 'acc', \n                       'extra': 'acc',\n                       'liver': 'acc', \n                       'kidney': 'acc', \n                       'spleen': 'acc'})\n\n# You can print the model summary to inspect the architecture\nmodel.summary()\nold=float('inf')\nclass myCallback(tf.keras.callbacks.Callback):\n        def on_epoch_end(self,epoch,logs={}):\n            global old\n            if(logs.get('val_loss') < old):\n                #change model name here\n                model.save(model_name+'.h5',overwrite=True)\n                old = logs.get('val_loss')\n                print(\"\\n new best found threshold now = \",old)\ntrain_ds = (\n        tf.data.Dataset.from_tensor_slices((Train.image_path.tolist(), train_labels))\n        .map(Preprocess, num_parallel_calls=config.AUTOTUNE)\n        .shuffle(config.BATCH_SIZE * 10)\n        .batch(16)\n        .prefetch(config.AUTOTUNE)\n    )\nval_ds = (\n        tf.data.Dataset.from_tensor_slices((Val.image_path.tolist(), val_labels))\n        .map(Preprocess, num_parallel_calls=config.AUTOTUNE)\n        .shuffle(config.BATCH_SIZE * 10)\n        .batch(16)\n        .prefetch(config.AUTOTUNE)\n    )\nsaver =myCallback()\nhistory = model.fit(\n    train_ds,\n    epochs=10,\n    validation_data=val_ds,\n    callbacks=[saver]\n)","metadata":{"execution":{"iopub.status.busy":"2023-11-01T20:26:03.087479Z","iopub.execute_input":"2023-11-01T20:26:03.087834Z","iopub.status.idle":"2023-11-01T20:46:21.073615Z","shell.execute_reply.started":"2023-11-01T20:26:03.087807Z","shell.execute_reply":"2023-11-01T20:46:21.072813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Patches(tf.keras.layers.Layer):\n    def __init__(self, patch_size):\n        super().__init__()\n        self.patch_size = patch_size\n\n    def call(self, images):\n        batch_size = tf.shape(images)[0]\n        patches = tf.image.extract_patches(\n            images=images,\n            sizes=[1, self.patch_size, self.patch_size, 1],\n            strides=[1, self.patch_size, self.patch_size, 1],\n            rates=[1, 1, 1, 1],\n            padding=\"VALID\",\n        )\n        patch_dims = patches.shape[-1]\n        patches = tf.reshape(patches, [batch_size, -1, patch_dims])\n        return patches\nclass PatchEncoder(tf.keras.layers.Layer):\n    def __init__(self, num_patches, projection_dim):\n        super().__init__()\n        self.num_patches = num_patches\n        self.projection = tf.keras.layers.Dense(units=projection_dim)\n        self.position_embedding = tf.keras.layers.Embedding(\n            input_dim=num_patches, output_dim=projection_dim\n        )\n\n    def call(self, patch):\n        positions = tf.range(start=0, limit=self.num_patches, delta=1)\n        encoded = self.projection(patch) + self.position_embedding(positions)\n        return encoded","metadata":{"execution":{"iopub.status.busy":"2023-11-01T20:59:32.29681Z","iopub.execute_input":"2023-11-01T20:59:32.297735Z","iopub.status.idle":"2023-11-01T20:59:32.308055Z","shell.execute_reply.started":"2023-11-01T20:59:32.297684Z","shell.execute_reply":"2023-11-01T20:59:32.306983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.applications import ResNet50\nfrom tensorflow.keras.layers import GlobalAveragePooling2D, Dense, Dropout\nmodel_name = 'EfficientNetV1B0'\n# INPUT layer\nInput = tf.keras.layers.Input(shape=(256, 256, 3))\n\n# Augment images\naug = tf.keras.layers.experimental.preprocessing.RandomFlip('horizontal')(Input)\n# aug = tf.keras.layers.experimental.preprocessing.RandomRotation(0.2)(aug)\naug = tf.keras.layers.experimental.preprocessing.RandomFlip('vertical')(aug)\naug = tf.keras.layers.experimental.preprocessing.RandomHeight(0.2)(aug)\naug = tf.keras.layers.experimental.preprocessing.RandomWidth(0.2)(aug)\n# aug = tf.keras.layers.experimental.preprocessing.Resizing(256,256)(aug)\n\n# Transfer learning layer\n# TL = ResNet50(weights='imagenet', include_top=False)(aug)\n# TL =  keras_cv_attention_models.efficientnet.from\n# patch = Patches(6)(aug)\n# patch_enc = PatchEncoder((256 // 6) ** 2,64)(patch)\n# TL = keras_cv_attention_models.vit.ViT(pretrained=\"imagenet\",num_classes=0)(aug)\n# TL= getattr(keras_cv_attention_models.efficientvit, 'EfficientViT_B1')(input_shape=(256,256,3),\n#                                     pretrained='imagenet',\n#                                     num_classes=0)(aug) \n\n# Define the custom heads for five different outputs\n# TL = GlobalAveragePooling2D()(TL)\nTL = vit.vit_b16(\n        image_size=256,\n        pretrained=True,\n        include_top=False,\n        pretrained_top=False,\n        classes=0  # The original model has no classification head\n    )(aug)\n\noutput1 = tf.keras.layers.Flatten()(TL)\noutput1 = Dense(32, activation='relu')(output1)\noutput1 = Dense(1, activation='sigmoid', name='bowel')(output1)\n\noutput2 = tf.keras.layers.Flatten()(TL)\noutput2 = Dense(32, activation='relu')(output2)\noutput2 = Dense(1, activation='sigmoid', name='extra')(output2)\n\noutput3 = tf.keras.layers.Flatten()(TL)\noutput3 = Dense(32, activation='relu')(output3)\noutput3 = Dense(3, activation='softmax', name='liver')(output3)\n\noutput4 = tf.keras.layers.Flatten()(TL)\noutput4 = Dense(32, activation='relu')(output4)\noutput4 = Dense(3, activation='softmax', name='kidney')(output4)\n\noutput5 = tf.keras.layers.Flatten()(TL)\noutput5 = Dense(32, activation='relu')(output5)\noutput5 = Dense(3, activation='softmax', name='spleen')(output5)\n\n# Create the model\nmodel = tf.keras.Model(inputs=Input, outputs=[output1, output2, output3, output4, output5])\n\n# TL.trainable = True\n\n# Compile the model (you can adjust the loss functions and metrics as needed)\nmodel.compile(optimizer='adam',\n              loss={'bowel': 'binary_crossentropy', \n                    'extra': 'binary_crossentropy',\n                    'liver': 'categorical_crossentropy', \n                    'kidney': 'categorical_crossentropy',\n                    'spleen': 'categorical_crossentropy'\n                   },\n              metrics={'bowel': 'acc', \n                       'extra': 'acc',\n                       'liver': 'acc', \n                       'kidney': 'acc', \n                       'spleen': 'acc'})\n\n# You can print the model summary to inspect the architecture\nmodel.summary()\nold=float('inf')\nclass myCallback(tf.keras.callbacks.Callback):\n        def on_epoch_end(self,epoch,logs={}):\n            global old\n            if(logs.get('val_loss') < old):\n                #change model name here\n                model.save(model_name+'.h5',overwrite=True)\n                old = logs.get('val_loss')\n                print(\"\\n new best found threshold now = \",old)\ntrain_ds = (\n        tf.data.Dataset.from_tensor_slices((Train.image_path.tolist(), train_labels))\n        .map(Preprocess, num_parallel_calls=config.AUTOTUNE)\n        .shuffle(config.BATCH_SIZE * 10)\n        .batch(16)\n        .prefetch(config.AUTOTUNE)\n    )\nval_ds = (\n        tf.data.Dataset.from_tensor_slices((Val.image_path.tolist(), val_labels))\n        .map(Preprocess, num_parallel_calls=config.AUTOTUNE)\n        .shuffle(config.BATCH_SIZE * 10)\n        .batch(16)\n        .prefetch(config.AUTOTUNE)\n    )\nsaver =myCallback()\nhistory = model.fit(\n    train_ds,\n    epochs=10,\n#     validation_data=val_ds,\n    callbacks=[saver]\n)","metadata":{"execution":{"iopub.status.busy":"2023-11-02T17:57:00.389952Z","iopub.execute_input":"2023-11-02T17:57:00.390353Z","iopub.status.idle":"2023-11-02T17:57:39.996216Z","shell.execute_reply.started":"2023-11-02T17:57:00.39032Z","shell.execute_reply":"2023-11-02T17:57:39.994945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}