{"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":"gpu","dataSources":[{"sourceType":"competition","sourceId":14774,"databundleVersionId":875431},{"sourceType":"datasetVersion","sourceId":10342395,"datasetId":6391555,"databundleVersionId":10650910},{"sourceType":"datasetVersion","sourceId":10353034,"datasetId":6391545,"databundleVersionId":10662923},{"sourceType":"datasetVersion","sourceId":10227083,"datasetId":6322941,"databundleVersionId":10521043},{"sourceType":"datasetVersion","sourceId":5130126,"datasetId":2979940,"databundleVersionId":5201622},{"sourceType":"modelInstanceVersion","sourceId":129944,"databundleVersionId":9795609,"modelInstanceId":109498}],"dockerImageVersionId":30786,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"pip install -U tensorflow==2.15.1 > /dev/null 2>&1","metadata":{"execution":{"iopub.status.busy":"2025-01-04T19:02:17.406854Z","iopub.execute_input":"2025-01-04T19:02:17.407601Z","iopub.status.idle":"2025-01-04T19:03:16.202561Z","shell.execute_reply.started":"2025-01-04T19:02:17.407559Z","shell.execute_reply":"2025-01-04T19:03:16.201452Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport tensorflow as tf\nimport re\nimport matplotlib.image as mpimg\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.callbacks import ModelCheckpoint\nfrom tensorflow.keras.layers import RandomRotation, RandomZoom, RandomTranslation\nfrom tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, Flatten, Dense, Dropout, BatchNormalization, ReLU\nfrom tensorflow.keras.models import Model\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.preprocessing.image import img_to_array, load_img\nfrom collections import Counter","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2025-01-04T19:03:16.204706Z","iopub.execute_input":"2025-01-04T19:03:16.204993Z","iopub.status.idle":"2025-01-04T19:03:22.222382Z","shell.execute_reply.started":"2025-01-04T19:03:16.204965Z","shell.execute_reply":"2025-01-04T19:03:22.221711Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"TARGET_SIZE = (240, 240)","metadata":{"execution":{"iopub.status.busy":"2025-01-04T19:03:22.223860Z","iopub.execute_input":"2025-01-04T19:03:22.224532Z","iopub.status.idle":"2025-01-04T19:03:22.228498Z","shell.execute_reply.started":"2025-01-04T19:03:22.224487Z","shell.execute_reply":"2025-01-04T19:03:22.227657Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport shutil\nfrom sklearn.model_selection import train_test_split\nfrom collections import defaultdict\nfrom glob import glob\n\n# Base path where the images are located\nbase_dir = '/kaggle/input/aptos19-clahehistoeq/Aptos19_ClaHist_onlyDR_augMain/Split'\n\n# Output directories for train, validation, and test\noutput_dir = '/kaggle/working/Aptos19_Clahe_onlyDR-split'\ntrain_dir = os.path.join(output_dir, 'train')\nvalid_dir = os.path.join(output_dir, 'valid')\ntest_dir = os.path.join(output_dir, 'test')\n\n# Create directories if they don't exist\nos.makedirs(train_dir, exist_ok=True)\nos.makedirs(valid_dir, exist_ok=True)\nos.makedirs(test_dir, exist_ok=True)\n\n# Step 1: Collect image paths and their labels\nimage_paths = []\nlabels = []\n\n# Assuming each label is a folder under the base directory\nfor label in os.listdir(base_dir):\n    label_dir = os.path.join(base_dir, label)\n    if os.path.isdir(label_dir):\n        for img_path in glob(os.path.join(label_dir, '*.png')):\n            image_paths.append(img_path)\n            labels.append(label)\n\n# Step 2: Perform stratified split (80% train, 10% valid, 10% test)\ntrain_paths, test_paths, train_labels, test_labels = train_test_split(\n    image_paths, labels, test_size=0.2, stratify=labels, random_state=42\n)\n\nvalid_paths, test_paths, valid_labels, test_labels = train_test_split(\n    test_paths, test_labels, test_size=0.5, stratify=test_labels, random_state=42\n)\n\n# Step 3: Move/copy images to train, validation, and test directories\ndef copy_images(image_paths, labels, split_dir):\n    # Create label subdirectories and copy images\n    for img_path, label in zip(image_paths, labels):\n        label_dir = os.path.join(split_dir, label)\n        os.makedirs(label_dir, exist_ok=True)\n        shutil.copy(img_path, os.path.join(label_dir, os.path.basename(img_path)))\n\n# Copy images to the respective directories\ncopy_images(train_paths, train_labels, train_dir)\ncopy_images(valid_paths, valid_labels, valid_dir)\ncopy_images(test_paths, test_labels, test_dir)\n\nprint(f\"Train, valid, and test splits created in {output_dir}\")\n","metadata":{"execution":{"iopub.status.busy":"2025-01-04T19:06:41.938020Z","iopub.execute_input":"2025-01-04T19:06:41.938763Z","iopub.status.idle":"2025-01-04T19:07:22.176329Z","shell.execute_reply.started":"2025-01-04T19:06:41.938726Z","shell.execute_reply":"2025-01-04T19:07:22.175380Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Install cleanvision from git (the > /dev/null 2>&1 suppresses the output):\n!pip install git+https://github.com/cleanlab/cleanvision.git > /dev/null 2>&1","metadata":{"execution":{"iopub.status.busy":"2025-01-04T19:07:32.256322Z","iopub.execute_input":"2025-01-04T19:07:32.257133Z","iopub.status.idle":"2025-01-04T19:07:50.172684Z","shell.execute_reply.started":"2025-01-04T19:07:32.257097Z","shell.execute_reply":"2025-01-04T19:07:50.171499Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Various imports:\nimport os\nimport re\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nfrom cleanvision.imagelab import Imagelab\nfrom collections import Counter","metadata":{"execution":{"iopub.status.busy":"2025-01-04T19:07:50.174706Z","iopub.execute_input":"2025-01-04T19:07:50.175002Z","iopub.status.idle":"2025-01-04T19:07:50.180077Z","shell.execute_reply.started":"2025-01-04T19:07:50.174974Z","shell.execute_reply":"2025-01-04T19:07:50.179149Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Running clean vision's find issues logic\nimagelab = Imagelab(data_path=\"/kaggle/input/aptos2019-blindness-detection/train_images\")\nimagelab.find_issues()","metadata":{"execution":{"iopub.status.busy":"2025-01-04T19:16:08.958245Z","iopub.execute_input":"2025-01-04T19:16:08.958682Z","iopub.status.idle":"2025-01-04T19:31:07.299478Z","shell.execute_reply.started":"2025-01-04T19:16:08.958647Z","shell.execute_reply":"2025-01-04T19:31:07.298531Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Running a report on the found issues:\nimagelab.report()","metadata":{"execution":{"iopub.status.busy":"2025-01-04T19:37:24.091901Z","iopub.execute_input":"2025-01-04T19:37:24.092274Z","iopub.status.idle":"2025-01-04T19:37:38.812459Z","shell.execute_reply.started":"2025-01-04T19:37:24.092240Z","shell.execute_reply":"2025-01-04T19:37:38.811455Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"imagelab.info[\"exact_duplicates\"][\"sets\"]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-04T19:37:54.725916Z","iopub.execute_input":"2025-01-04T19:37:54.726263Z","iopub.status.idle":"2025-01-04T19:37:54.737719Z","shell.execute_reply.started":"2025-01-04T19:37:54.726230Z","shell.execute_reply":"2025-01-04T19:37:54.736866Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\n\n# Filter for rows where any of the three issues are True\nissue_paths = imagelab.issues[\n    (imagelab.issues[\"is_low_information_issue\"] == True) |\n    (imagelab.issues[\"is_light_issue\"] == True) |\n    (imagelab.issues[\"is_dark_issue\"] == True)\n].index.tolist()\n\n# Initialize a counter for the deleted files\ndeleted_count = 0\n\n# Delete each file in the issue paths\nfor img_path in issue_paths:\n    try:\n        if os.path.exists(img_path):\n            os.remove(img_path)\n            deleted_count += 1  # Increment the counter when a file is deleted\n        else:\n            print(f\"File not found: {img_path}\")\n    except Exception as e:\n        print(f\"Error deleting {img_path}: {e}\")\n\n# Print the final count of deleted files\nprint(f\"Total files deleted: {deleted_count}\")\n","metadata":{"execution":{"iopub.status.busy":"2024-12-30T14:29:07.026969Z","iopub.execute_input":"2024-12-30T14:29:07.027272Z","iopub.status.idle":"2024-12-30T14:29:07.043676Z","shell.execute_reply.started":"2024-12-30T14:29:07.027224Z","shell.execute_reply":"2024-12-30T14:29:07.042712Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport hashlib\nfrom collections import defaultdict\n\ndef hash_image(image_path, block_size=65536):\n    \"\"\"Create a hash of the image to compare for duplicates.\"\"\"\n    hasher = hashlib.md5()  # Using MD5 for simplicity, you can use SHA-256 if needed.\n    with open(image_path, 'rb') as image_file:\n        buf = image_file.read(block_size)\n        while len(buf) > 0:\n            hasher.update(buf)\n            buf = image_file.read(block_size)\n    return hasher.hexdigest()\n\ndef scan_for_duplicates_across_sets(train_dir, valid_dir, test_dir):\n    \"\"\"Scan train, valid, and test directories for duplicates.\"\"\"\n    hashes = defaultdict(list)  # Store the file paths of images with the same hash.\n    \n    # Scan the three directories and group by hash\n    for foldername, set_type in [(train_dir, 'train'), (valid_dir, 'valid'), (test_dir, 'test')]:\n        for subfolder, _, filenames in os.walk(foldername):\n            for filename in filenames:\n                if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.tiff')):\n                    image_path = os.path.join(subfolder, filename)\n                    image_hash = hash_image(image_path)\n                    hashes[image_hash].append((image_path, set_type))  # Save path and set type\n    \n    # Identify duplicates and apply removal rules\n    duplicates = {hash_value: files for hash_value, files in hashes.items() if len(files) > 1}\n    return duplicates\n\ndef remove_duplicates(duplicates):\n    \"\"\"Remove duplicates while keeping one copy based on the priority: train > valid > test.\"\"\"\n    for hash_value, files in duplicates.items():\n        # Prioritize keeping train > valid > test\n        train_files = [f for f, set_type in files if set_type == 'train']\n        valid_files = [f for f, set_type in files if set_type == 'valid']\n        test_files = [f for f, set_type in files if set_type == 'test']\n        \n        # Determine which file to keep based on the priority\n        if train_files:\n            file_to_keep = train_files[0]\n        elif valid_files:\n            file_to_keep = valid_files[0]\n        else:\n            file_to_keep = test_files[0]\n        \n        print(f\"Keeping: {file_to_keep}\")\n\n        # Remove all other duplicates\n        for f, set_type in files:\n            if f != file_to_keep:\n#                 print(f\"Removing: {f}\")\n                os.remove(f)\n\ndef main():\n    base_dir = '/kaggle/working/Aptos19_Clahe_onlyDR-split'\n    \n    # Directories for train, valid, and test\n    train_dir = os.path.join(base_dir, 'train')\n    valid_dir = os.path.join(base_dir, 'valid')\n    test_dir = os.path.join(base_dir, 'test')\n    \n    # Scan for duplicates across train, valid, and test\n    print(\"Scanning train, valid, and test folders for duplicates...\")\n    duplicates = scan_for_duplicates_across_sets(train_dir, valid_dir, test_dir)\n    \n    print(f\"Found {len(duplicates)} sets of duplicates.\")\n    \n    # Remove duplicates based on rules\n    if duplicates:\n        remove_duplicates(duplicates)\n    else:\n        print(\"No duplicates found.\")\n\nif __name__ == \"__main__\":\n    main()\n","metadata":{"execution":{"iopub.status.busy":"2024-12-30T14:29:07.045456Z","iopub.execute_input":"2024-12-30T14:29:07.046054Z","iopub.status.idle":"2024-12-30T14:29:07.521739Z","shell.execute_reply.started":"2024-12-30T14:29:07.046013Z","shell.execute_reply":"2024-12-30T14:29:07.520666Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nimport os\n\noutput_dir = \"/kaggle/working/Aptos19_Clahe_onlyDR-split\"\nIMAGE_SIZE = (240, 240)\nBATCH_SIZE = 32\n\ndef load_dataset(base_dir, image_size, batch_size, shuffle=True):\n    return tf.keras.preprocessing.image_dataset_from_directory(\n        base_dir,\n        label_mode='categorical',\n        image_size=image_size,\n        batch_size=batch_size,\n        shuffle=shuffle\n    )\n\ntrain_dataset = load_dataset(os.path.join(output_dir, 'train'), IMAGE_SIZE, BATCH_SIZE, shuffle=False)\nvalid_dataset = load_dataset(os.path.join(output_dir, 'valid'), IMAGE_SIZE, BATCH_SIZE, shuffle=False)\ntest_dataset = load_dataset(os.path.join(output_dir, 'test'), IMAGE_SIZE, BATCH_SIZE, shuffle=False)\n\nprint(f\"Train dataset batches: {len(train_dataset)}\")\nprint(f\"Validation dataset batches: {len(valid_dataset)}\")\nprint(f\"Test dataset batches: {len(test_dataset)}\")\n","metadata":{"execution":{"iopub.status.busy":"2024-12-30T14:29:07.523112Z","iopub.execute_input":"2024-12-30T14:29:07.523709Z","iopub.status.idle":"2024-12-30T14:29:08.388978Z","shell.execute_reply.started":"2024-12-30T14:29:07.523669Z","shell.execute_reply":"2024-12-30T14:29:08.388144Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import layers\n\nclass Patches(layers.Layer):\n    def __init__(self, patch_size, **kwargs):\n        # Accept extra arguments like 'name', 'trainable', etc.\n        super(Patches, self).__init__(**kwargs)\n        self.patch_size = patch_size\n\n    def call(self, images):\n        input_shape = tf.shape(images)  \n        batch_size = input_shape[0]\n        height = input_shape[1]\n        width = input_shape[2]\n        channels = input_shape[3]\n\n        num_patches_h = height // self.patch_size\n        num_patches_w = width // self.patch_size\n\n        # Extract patches\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            name=\"patch_extraction\"\n        )\n\n        patches = tf.reshape(\n            patches,\n            (batch_size, num_patches_h * num_patches_w, self.patch_size * self.patch_size * channels),\n            name=\"patch_reshaping\"\n        )\n        return patches\n\n    def get_config(self):\n        config = super().get_config()\n        config.update({\"patch_size\": self.patch_size})\n        return config\n","metadata":{"execution":{"iopub.status.busy":"2024-12-30T14:29:08.390050Z","iopub.execute_input":"2024-12-30T14:29:08.390370Z","iopub.status.idle":"2024-12-30T14:29:08.397441Z","shell.execute_reply.started":"2024-12-30T14:29:08.390333Z","shell.execute_reply":"2024-12-30T14:29:08.396551Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Iterate over the test dataset\nfor images, labels in test_dataset.take(1):\n    plt.figure(figsize=(4, 4))\n    image = images[np.random.choice(range(images.shape[0]))].numpy().astype(\"uint8\")\n    plt.imshow(image)\n    plt.axis(\"off\")\n    plt.show()\n\n    # Convert to tensor and resize\n    resized_image = tf.image.resize([image], size=(240, 240))\n    patches = Patches(20)(resized_image)\n    \n    # Patch info\n    print(f\"Image size: 240 X 240\")\n    print(f\"Patch size: 20 X 20\")\n    print(f\"Patches per image: {patches.shape[1]}\")\n    print(f\"Elements per patch: {patches.shape[-1]}\")\n\n    # Visualizing patches\n    n = int(np.sqrt(patches.shape[1]))\n    plt.figure(figsize=(4, 4))\n    for i, patch in enumerate(patches[0]):\n        ax = plt.subplot(n, n, i + 1)\n        patch_img = tf.reshape(patch, (20, 20, 3))\n        plt.imshow(patch_img.numpy().astype(\"uint8\"))\n        plt.axis(\"off\")\n\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-12-30T14:29:08.398611Z","iopub.execute_input":"2024-12-30T14:29:08.398918Z","iopub.status.idle":"2024-12-30T14:29:12.670790Z","shell.execute_reply.started":"2024-12-30T14:29:08.398893Z","shell.execute_reply":"2024-12-30T14:29:12.669841Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import layers\n\nclass PatchEncoder(layers.Layer):\n    def __init__(self, num_patches, projection_dim, trainable=False, **kwargs):\n        # Pass additional keyword arguments to the parent class\n        super(PatchEncoder, self).__init__(**kwargs)\n        self.num_patches = num_patches\n        self.projection = layers.Dense(units=projection_dim, trainable=trainable, name=\"projection_layer\")\n        self.position_embedding = layers.Embedding(\n            input_dim=num_patches, output_dim=projection_dim, trainable=trainable, name=\"position_embedding\"\n        )\n        self._trainable = trainable\n\n    @property\n    def trainable(self):\n        return self._trainable\n\n    @trainable.setter\n    def trainable(self, value):\n        self._trainable = value\n        self.projection.trainable = value\n        self.position_embedding.trainable = value\n        super(PatchEncoder, self).trainable = value\n\n    def call(self, patch):\n        positions = tf.expand_dims(tf.range(start=0, limit=self.num_patches, delta=1), axis=0)\n        projected_patches = self.projection(patch)\n        encoded = projected_patches + self.position_embedding(positions)\n        return encoded\n\n    def get_config(self):\n        config = super().get_config()\n        config.update({\n            \"num_patches\": self.num_patches,\n            \"projection_dim\": self.projection.units,\n            \"trainable\": self._trainable\n        })\n        return config\n","metadata":{"execution":{"iopub.status.busy":"2024-12-30T14:29:12.672042Z","iopub.execute_input":"2024-12-30T14:29:12.672362Z","iopub.status.idle":"2024-12-30T14:29:12.680627Z","shell.execute_reply.started":"2024-12-30T14:29:12.672333Z","shell.execute_reply":"2024-12-30T14:29:12.679734Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# custom\ndef transformer_block(inputs, size, num_heads):\n    x = layers.LayerNormalization(epsilon=1e-6)(inputs)\n    x = layers.MultiHeadAttention(num_heads=num_heads, key_dim=size)(x, x)\n    x = layers.Dropout(0.1)(x)\n    x = layers.Add()([x, inputs])  # Skip connection\n\n    x = layers.LayerNormalization(epsilon=1e-6)(x)\n    x = layers.Dense(size * 4, activation='relu')(x)\n    x = layers.Dropout(0.1)(x)\n    x = layers.Dense(size)(x)  # Feedforward network\n    return layers.Add()([x, inputs])  # Skip connection\n\n\ndef create_vit_model(input_shape=(224, 224, 3), \n                     patch_size=20,\n                     projection_dim=64,\n                     num_heads=4,\n                     transformer_layers=8,\n                     num_classes=4):\n\n    inputs = layers.Input(shape=input_shape)\n\n    # Create patches\n    patches_layer = Patches(patch_size)(inputs)\n\n    # Encode patches\n    num_patches = (input_shape[0] // patch_size) * (input_shape[1] // patch_size)\n    encoded_patches_layer = PatchEncoder(num_patches=num_patches, projection_dim=projection_dim)(patches_layer)\n\n    # Create multiple layers of the Transformer block.\n    for _ in range(transformer_layers):\n        encoded_patches_layer = transformer_block(encoded_patches_layer, projection_dim, num_heads)\n\n    # Classification head\n    representation = layers.GlobalAveragePooling1D()(encoded_patches_layer)\n    outputs = layers.Dense(num_classes, activation='softmax')(representation)\n\n    return Model(inputs=inputs, outputs=outputs)\n\n\n# Create the Vision Transformer model\nmodel_vit = create_vit_model(input_shape=(240, 240, 3), \n                              patch_size=20,\n                              projection_dim=64,\n                              num_heads=4,\n                              transformer_layers=8,\n                              num_classes=4)\n\n# Compile the model\nmodel_vit.compile(optimizer='adam', \n                  loss='categorical_crossentropy', \n                  metrics=['accuracy'])\n\n# Display model summary\nmodel_vit.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T14:29:12.681852Z","iopub.execute_input":"2024-12-30T14:29:12.682139Z","iopub.status.idle":"2024-12-30T14:29:14.370892Z","shell.execute_reply.started":"2024-12-30T14:29:12.682113Z","shell.execute_reply":"2024-12-30T14:29:14.369930Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import layers, Model\n\n# Assuming you've already defined your create_vit_model function\n\n# Create the Vision Transformer model\nmodel_vit = create_vit_model(input_shape=(240, 240, 3), \n                              patch_size=20,\n                              projection_dim=64,\n                              num_heads=4,\n                              transformer_layers=8,\n                              num_classes=4)\n\n# Compile the model with a specified learning rate\nlearning_rate = 0.0001\nmodel_vit.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=learning_rate), \n                  loss='categorical_crossentropy', \n                  metrics=['accuracy'])\n\n# Assuming train_dataset and valid_dataset are defined as tf.data.Dataset objects\n# Example:\n# train_dataset = tf.data.Dataset.from_tensor_slices((train_images, train_labels)).batch(batch_size)\n# valid_dataset = tf.data.Dataset.from_tensor_slices((valid_images, valid_labels)).batch(batch_size)\n\n# Train the model for 30 epochs\nhistory = model_vit.fit(train_dataset,\n                         validation_data=valid_dataset,\n                         epochs=100)\n\n# Optionally, you can save the model after training\nmodel_vit.save('vit_model.h5')\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T14:30:48.885321Z","iopub.execute_input":"2024-12-30T14:30:48.885680Z","iopub.status.idle":"2024-12-30T14:36:45.913503Z","shell.execute_reply.started":"2024-12-30T14:30:48.885652Z","shell.execute_reply":"2024-12-30T14:36:45.912732Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Assuming you have already trained your model and have a test dataset ready\n\n# Load the test dataset\n# Example:\n# test_dataset = tf.data.Dataset.from_tensor_slices((test_images, test_labels)).batch(batch_size)\n\n# Evaluate the model on the test dataset\ntest_loss, test_accuracy = model_vit.evaluate(test_dataset)\n\n# Print the results\nprint(f\"Test Loss: {test_loss:.4f}\")\nprint(f\"Test Accuracy: {test_accuracy:.4f}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T14:51:02.590029Z","iopub.execute_input":"2024-12-30T14:51:02.590417Z","iopub.status.idle":"2024-12-30T14:51:02.919340Z","shell.execute_reply.started":"2024-12-30T14:51:02.590386Z","shell.execute_reply":"2024-12-30T14:51:02.918527Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay\n\n# Assuming y_test is one-hot encoded and Y_pred is obtained from model predictions\n# Convert y_test from one-hot encoding to single labels\ny_true = np.argmax(test_dataset, axis=1)  # Convert one-hot encoded labels to class indices\n\n# Get predictions from the model\nY_pred = np.argmax(model.predict(X_test), axis=1)  # Get predicted class indices\n\n# Calculate the confusion matrix\ncm = confusion_matrix(y_true, Y_pred)\n\n# Plotting the confusion matrix using Seaborn\nplt.figure(figsize=(10, 8))\nsns.heatmap(cm, annot=True, fmt='d', cmap='Blues', \n            xticklabels=np.unique(y_true), \n            yticklabels=np.unique(y_true))\nplt.xlabel('Predicted Labels')\nplt.ylabel('True Labels')\nplt.title('Confusion Matrix')\nplt.show()\n\n# Alternatively, using sklearn's ConfusionMatrixDisplay\ndisp = ConfusionMatrixDisplay(confusion_matrix=cm)\ndisp.plot(cmap=plt.cm.Blues)\nplt.title('Confusion Matrix')\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-30T14:29:16.723460Z","iopub.status.idle":"2024-12-30T14:29:16.723804Z","shell.execute_reply.started":"2024-12-30T14:29:16.723646Z","shell.execute_reply":"2024-12-30T14:29:16.723664Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# import tensorflow as tf\n# model = tf.keras.models.load_model('/kaggle/input/pavement-model-vit/tensorflow2/default/1/pavement-big-export-224x224')\n\n# model.summary()\n# # Compile the new model\n# model.compile(optimizer='adam', \n#                   loss='categorical_crossentropy', \n#                   metrics=['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2024-12-30T14:29:16.724819Z","iopub.status.idle":"2024-12-30T14:29:16.725133Z","shell.execute_reply.started":"2024-12-30T14:29:16.724981Z","shell.execute_reply":"2024-12-30T14:29:16.724998Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import classification_report, confusion_matrix, ConfusionMatrixDisplay, f1_score\n\n# Step 1: Get predictions and true labels\ny_true = np.concatenate([y for x, y in train_dataset], axis=0)  # Extract the true labels from the dataset\ny_true = np.argmax(y_true, axis=1)\n\ny_pred = model.predict(train_dataset, verbose=1)  # No need for `steps` with a TensorFlow dataset\ny_pred_classes = np.argmax(y_pred, axis=1)\n\n# Step 2: Calculate F1 Score\nf1 = f1_score(y_true, y_pred_classes, average='weighted')\nprint(f\"Weighted F1 Score: {f1:.4f}\")\n\n# Step 3: Generate Confusion Matrix\nconf_matrix = confusion_matrix(y_true, y_pred_classes)\n\n# Use the class names (assuming you have access to them via class indices)\nclass_names = train_dataset.class_names  # Replace this with how you get class names from your dataset\nconf_matrix_display = ConfusionMatrixDisplay(confusion_matrix=conf_matrix, display_labels=class_names)\n\n# Step 4: Plot the Confusion Matrix\nplt.figure(figsize=(12, 12))\nconf_matrix_display.plot(cmap='Blues', xticks_rotation='vertical')\nplt.title('Confusion Matrix')\nplt.show()\n\n# Step 5: Print Classification Report\nclass_report = classification_report(y_true, y_pred_classes, target_names=class_names)\nprint(\"Classification Report:\\n\", class_report)\n","metadata":{"execution":{"iopub.status.busy":"2024-12-30T14:29:16.727001Z","iopub.status.idle":"2024-12-30T14:29:16.727399Z","shell.execute_reply.started":"2024-12-30T14:29:16.727196Z","shell.execute_reply":"2024-12-30T14:29:16.727214Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import classification_report, confusion_matrix, ConfusionMatrixDisplay, f1_score\n\n# Step 1: Get predictions and true labels\ny_true = np.concatenate([y for x, y in valid_dataset], axis=0)  # Extract the true labels from the dataset\ny_true = np.argmax(y_true, axis=1)\n\ny_pred = model.predict(valid_dataset, verbose=1)  # No need for `steps` with a TensorFlow dataset\ny_pred_classes = np.argmax(y_pred, axis=1)\n\n# Step 2: Calculate F1 Score\nf1 = f1_score(y_true, y_pred_classes, average='weighted')\nprint(f\"Weighted F1 Score: {f1:.4f}\")\n\n# Step 3: Generate Confusion Matrix\nconf_matrix = confusion_matrix(y_true, y_pred_classes)\n\n# Use the class names (assuming you have access to them via class indices)\nclass_names = train_dataset.class_names  # Replace this with how you get class names from your dataset\nconf_matrix_display = ConfusionMatrixDisplay(confusion_matrix=conf_matrix, display_labels=class_names)\n\n# Step 4: Plot the Confusion Matrix\nplt.figure(figsize=(12, 12))\nconf_matrix_display.plot(cmap='Blues', xticks_rotation='vertical')\nplt.title('Confusion Matrix')\nplt.show()\n\n# Step 5: Print Classification Report\nclass_report = classification_report(y_true, y_pred_classes, target_names=class_names)\nprint(\"Classification Report:\\n\", class_report)\n","metadata":{"execution":{"iopub.status.busy":"2024-12-30T14:29:16.729015Z","iopub.status.idle":"2024-12-30T14:29:16.729357Z","shell.execute_reply.started":"2024-12-30T14:29:16.729165Z","shell.execute_reply":"2024-12-30T14:29:16.729180Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import classification_report, confusion_matrix, ConfusionMatrixDisplay, f1_score\n\n# Step 1: Get predictions and true labels\ny_true = np.concatenate([y for x, y in test_dataset], axis=0)  # Extract the true labels from the dataset\ny_true = np.argmax(y_true, axis=1)\n\ny_pred = model.predict(test_dataset, verbose=1)  # No need for `steps` with a TensorFlow dataset\ny_pred_classes = np.argmax(y_pred, axis=1)\n\n# Step 2: Calculate F1 Score\nf1 = f1_score(y_true, y_pred_classes, average='weighted')\nprint(f\"Weighted F1 Score: {f1:.4f}\")\n\n# Step 3: Generate Confusion Matrix\nconf_matrix = confusion_matrix(y_true, y_pred_classes)\n\n# Use the class names (assuming you have access to them via class indices)\nclass_names = train_dataset.class_names  # Replace this with how you get class names from your dataset\nconf_matrix_display = ConfusionMatrixDisplay(confusion_matrix=conf_matrix, display_labels=class_names)\n\n# Step 4: Plot the Confusion Matrix\nplt.figure(figsize=(12, 12))\nconf_matrix_display.plot(cmap='Blues', xticks_rotation='vertical')\nplt.title('Confusion Matrix')\nplt.show()\n\n# Step 5: Print Classification Report\nclass_report = classification_report(y_true, y_pred_classes, target_names=class_names)\nprint(\"Classification Report:\\n\", class_report)\n","metadata":{"execution":{"iopub.status.busy":"2024-12-30T14:29:16.730412Z","iopub.status.idle":"2024-12-30T14:29:16.730891Z","shell.execute_reply.started":"2024-12-30T14:29:16.730634Z","shell.execute_reply":"2024-12-30T14:29:16.730659Z"},"trusted":true},"outputs":[],"execution_count":null}]}