{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":45867,"databundleVersionId":6924515,"sourceType":"competition"},{"sourceId":7156529,"sourceType":"datasetVersion","datasetId":4123855}],"dockerImageVersionId":30616,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\n#os.environ[\"KERAS_BACKEND\"] = \"jax\" # or \"tensorflow\", \"torch\"\n\nimport cv2\nimport pickle\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\nimport tensorflow as tf\nimport keras_cv\nimport keras_core as keras\nfrom keras_core import ops","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-08T18:03:22.484886Z","iopub.execute_input":"2023-12-08T18:03:22.485702Z","iopub.status.idle":"2023-12-08T18:03:39.675343Z","shell.execute_reply.started":"2023-12-08T18:03:22.48567Z","shell.execute_reply":"2023-12-08T18:03:39.674414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Config:\n    is_submission = True\n    \n    # Reproducibility\n    SEED = 42\n    \n    # Training\n    train_csv_path = \"/kaggle/input/UBC-OCEAN/train.csv\"\n    train_thumbnail_paths = \"/kaggle/input/UBC-OCEAN/train_thumbnails\"\n    batch_size = 16\n    learning_rate = 1e-2\n    epochs = 100\n    \n    # Inference\n    test_csv_path = \"/kaggle/input/UBC-OCEAN/test.csv\"\n    test_thumbnail_paths = \"/kaggle/input/UBC-OCEAN/test_thumbnails\"\n\nconfig = Config()","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:03:39.677184Z","iopub.execute_input":"2023-12-08T18:03:39.678146Z","iopub.status.idle":"2023-12-08T18:03:39.683465Z","shell.execute_reply.started":"2023-12-08T18:03:39.678111Z","shell.execute_reply":"2023-12-08T18:03:39.682477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not config.is_submission:\n    # Read the CSV file\n    train_dataframe = pd.read_csv(config.train_csv_path)\n    print(train_dataframe.head())","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:03:39.68481Z","iopub.execute_input":"2023-12-08T18:03:39.685098Z","iopub.status.idle":"2023-12-08T18:03:39.717652Z","shell.execute_reply.started":"2023-12-08T18:03:39.685075Z","shell.execute_reply":"2023-12-08T18:03:39.71682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not config.is_submission:\n    import os\n    import pandas as pd\n\n    def create_image_path_dataframe(csv_data, images_dir):\n        image_paths = []\n        for index, row in csv_data.iterrows():\n            image_id = row['image_id']\n            image_label = row['label']  # Change this according to your columns\n            image_filename = f\"{image_id}_thumbnail.png\"  # Assuming image filenames are based on image_id with png extension\n            image_path = os.path.join(images_dir, image_filename)\n\n            if os.path.exists(image_path):\n                image_paths.append({\n                    'image_id': image_id,\n                    'image_path': image_path,\n                    'label': image_label,\n                    'image_width': row['image_width'],\n                    'image_height': row['image_height'],\n                    'is_tma': row['is_tma']\n                    # Add more columns as needed\n                })\n\n        # Create a DataFrame from the matched information\n        image_path_df = pd.DataFrame(image_paths)\n        return image_path_df\n","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:03:39.71953Z","iopub.execute_input":"2023-12-08T18:03:39.719834Z","iopub.status.idle":"2023-12-08T18:03:39.729037Z","shell.execute_reply.started":"2023-12-08T18:03:39.719808Z","shell.execute_reply":"2023-12-08T18:03:39.728146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not config.is_submission:\n    main_dataset = create_image_path_dataframe(train_dataframe, config.train_thumbnail_paths)\n    print(main_dataset.head())\n","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:03:41.242051Z","iopub.execute_input":"2023-12-08T18:03:41.242674Z","iopub.status.idle":"2023-12-08T18:03:41.24687Z","shell.execute_reply.started":"2023-12-08T18:03:41.242641Z","shell.execute_reply":"2023-12-08T18:03:41.245945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Distribution of Image Labels in the Dataset**","metadata":{}},{"cell_type":"code","source":"if not config.is_submission:\n    import matplotlib.pyplot as plt\n    import numpy as np\n\n    # Calculate label counts from the main_dataset\n    label_counts_main_dataset = main_dataset['label'].value_counts()\n\n    # Define custom colors for the plot\n    custom_colors = ['#FF9999', '#66B2FF', '#99FF99', '#FFCC99', '#c2c2f0']\n\n    # Plotting the label distribution for the main dataset\n    plt.figure(figsize=(10, 6))\n    bars = label_counts_main_dataset.plot(kind='bar', color=custom_colors)\n\n    plt.title('Label Distribution in Dataset', fontname='serif', fontsize=18, weight='bold', pad=20)\n    plt.xlabel('Labels', fontname='serif', fontsize=14, weight='bold', labelpad=10)\n    plt.ylabel('Count', fontname='serif', fontsize=14, weight='bold', labelpad=10)\n    plt.xticks(rotation=45, fontname='serif', fontsize=12, weight='bold')\n    plt.yticks(fontname='serif', fontsize=12, weight='bold')\n    # Annotate the bars with their respective counts\n    for i, count in enumerate(label_counts_main_dataset):\n        plt.text(i, count + 10, str(count), ha='center', va='bottom', fontname='serif', fontsize=10, weight='bold')\n\n    # Customize grid lines\n    plt.grid(axis='y', linestyle='--', alpha=0.7)\n\n    # Remove spines\n    plt.gca().spines['top'].set_visible(False)\n    plt.gca().spines['right'].set_visible(False)\n\n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:03:42.585829Z","iopub.execute_input":"2023-12-08T18:03:42.586633Z","iopub.status.idle":"2023-12-08T18:03:42.59625Z","shell.execute_reply.started":"2023-12-08T18:03:42.586595Z","shell.execute_reply":"2023-12-08T18:03:42.595237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#keras.utils.set_random_seed(seed=config.SEED)","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:03:44.40158Z","iopub.execute_input":"2023-12-08T18:03:44.402457Z","iopub.status.idle":"2023-12-08T18:03:44.406237Z","shell.execute_reply.started":"2023-12-08T18:03:44.402421Z","shell.execute_reply":"2023-12-08T18:03:44.405302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not config.is_submission:\n    df = pd.read_csv(config.train_csv_path)\n\n    # Create the thumbnail df where is_tma == False\n    df = df[df[\"is_tma\"] == False]\n    \n    # Get basic statistics about the dataset\n    num_rows = df.shape[0]\n    num_unique_images = df['image_id'].nunique()\n    num_unique_labels = df['label'].nunique()\n    unique_labels = df['label'].unique()\n\n    print(f\"{num_rows=}\")\n    print(f\"{num_unique_images=}\")\n    print(f\"{num_unique_labels=}\")\n    print(f\"{unique_labels=}\")\n    \n    # Plot the distribution of the target classes\n    plt.figure(figsize=(10, 6))\n    sns.countplot(data=df, x='label', order=df['label'].value_counts().index)\n    plt.title('Distribution of Target Classes')\n    plt.xlabel('Label')\n    plt.ylabel('Count')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:03:45.201975Z","iopub.execute_input":"2023-12-08T18:03:45.202341Z","iopub.status.idle":"2023-12-08T18:03:45.209351Z","shell.execute_reply.started":"2023-12-08T18:03:45.202304Z","shell.execute_reply":"2023-12-08T18:03:45.208442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not config.is_submission:\n    # Perform one-hot encoding of the 'label' column and explicitly convert to integer type\n    df_one_hot = pd.get_dummies(df[\"label\"], prefix=\"label\").astype(int)\n\n    # Concatenate the original DataFrame with the one-hot encoded labels\n    train_df = pd.concat([df[\"image_id\"], df_one_hot], axis=1)\n\n    # Get the thumbnail image paths\n    train_df[\"image_thumbnail_path\"] = train_df[\"image_id\"].apply(lambda x: f\"{config.train_thumbnail_paths}/{x}_thumbnail.png\")\n    \n    image_thumbnail_paths = train_df[\"image_thumbnail_path\"].values\n    labels = train_df[[col for col in train_df.columns if col.startswith(\"label_\")]].values\n\n    label_names = [col for col in train_df.columns if col.startswith(\"label_\")]\n    name_to_id = {key.replace(\"label_\", \"\"):value for value,key in enumerate(label_names)}\n    id_to_name = {key:value for value, key in name_to_id.items()}\n    \n    # Save to dictionary to disk\n    with open(\"id_to_name.pkl\", \"wb\") as f:\n        pickle.dump(id_to_name, f)","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:03:47.345677Z","iopub.execute_input":"2023-12-08T18:03:47.346523Z","iopub.status.idle":"2023-12-08T18:03:47.353926Z","shell.execute_reply.started":"2023-12-08T18:03:47.346487Z","shell.execute_reply":"2023-12-08T18:03:47.352955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not config.is_submission:\n    weight_0 = 0.35\n    weight_1 = 0.30\n    weight_2 = 0.16\n    weight_3 = 0.45\n    weight_4 = 0.45\n\n    # Create class weights dictionary\n    class_weights = {0: weight_0, 1: weight_1, 2: weight_2, 3: weight_3, 4: weight_4}\n    print(class_weights)","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:03:49.042205Z","iopub.execute_input":"2023-12-08T18:03:49.042552Z","iopub.status.idle":"2023-12-08T18:03:49.047733Z","shell.execute_reply.started":"2023-12-08T18:03:49.042524Z","shell.execute_reply":"2023-12-08T18:03:49.046839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_image(path):\n    file = tf.io.read_file(path)\n    image = tf.io.decode_png(file, 3)\n    image = tf.image.resize(image, (224, 224))\n    image = tf.image.per_image_standardization(image)\n    return image","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:03:50.321856Z","iopub.execute_input":"2023-12-08T18:03:50.32226Z","iopub.status.idle":"2023-12-08T18:03:50.327562Z","shell.execute_reply.started":"2023-12-08T18:03:50.322229Z","shell.execute_reply":"2023-12-08T18:03:50.326641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Creating Image Data Generators for Training and Validation**","metadata":{}},{"cell_type":"code","source":"if not config.is_submission:\n    from sklearn.model_selection import train_test_split\n    from tensorflow.keras.preprocessing.image import ImageDataGenerator\n    import albumentations as A\n    from albumentations.pytorch import ToTensorV2\n    img_width, img_height = 224, 224\n    # Image dimensions and other parameters\n\n    # Data generators for RGB images\n    train_datagen = ImageDataGenerator(rescale=1./255)\n    validation_datagen = ImageDataGenerator(rescale=1./255)\n    # Function to apply Albumentations augmentation to a batch of images\n    def albumentations_augmentation(images, labels):\n        transformed = []\n        for i in range(len(images)):\n            augmented = transform(image=images[i])\n            transformed.append(augmented['image'])\n        return transformed, labels\n\n    # Albumentations transformations\n    transform = A.Compose([\n        A.HorizontalFlip(p=0.5),\n        A.VerticalFlip(p=0.5),\n        A.Rotate(limit=30, p=0.5),\n        A.RandomBrightnessContrast(p=0.5),\n        A.RandomResizedCrop(height=img_height, width=img_width, scale=(0.8, 1.0), p=0.5),\n        A.GaussNoise(p=0.5),\n        A.Normalize(),  # Normalize pixel values to the range [0, 1]\n        ToTensorV2()     # Convert the image to a PyTorch tensor\n    ])\n\n    # Splitting the dataset into 80% for training and 20% for testing\n    train_data, test_data = train_test_split(main_dataset, test_size=0.2, random_state=42)\n\n    # Data generators for Albumentations augmented images\n    train_generator = train_datagen.flow_from_dataframe(\n        dataframe=train_data,\n        x_col='image_path',\n        y_col='label',\n        target_size=(img_width, img_height),\n        batch_size=config.batch_size,\n        class_mode='categorical',\n        shuffle=True,  # Set to True for training\n        preprocessing_function=albumentations_augmentation  # Custom augmentation function\n    )\n\n    validation_generator = validation_datagen.flow_from_dataframe(\n        dataframe=test_data,\n        x_col='image_path',\n        y_col='label',\n        target_size=(img_width, img_height),\n        batch_size=config.batch_size,\n        class_mode='categorical',\n        shuffle=False  # Set to False for validation/testing\n    )\n","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:03:51.731567Z","iopub.execute_input":"2023-12-08T18:03:51.732261Z","iopub.status.idle":"2023-12-08T18:03:51.742535Z","shell.execute_reply.started":"2023-12-08T18:03:51.732228Z","shell.execute_reply":"2023-12-08T18:03:51.741639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Checking Index Numbers From Train and Validation Dataset**","metadata":{}},{"cell_type":"code","source":"if not config.is_submission:\n    train_class_indices = train_generator.class_indices\n    print(\"Train Dataset Indexing:\", train_class_indices)\n\n    test_class_indices = validation_generator.class_indices\n    print(\"Validation Dataset Indexing:\", test_class_indices)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:03:52.817281Z","iopub.execute_input":"2023-12-08T18:03:52.817751Z","iopub.status.idle":"2023-12-08T18:03:52.823939Z","shell.execute_reply.started":"2023-12-08T18:03:52.81771Z","shell.execute_reply":"2023-12-08T18:03:52.822968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Display 5 images from the Train Dataset**","metadata":{}},{"cell_type":"code","source":"if not config.is_submission:\n    import matplotlib.pyplot as plt\n\n    # Display 5 images with their respective details from the training generator\n    fig, axes = plt.subplots(nrows=1, ncols=5, figsize=(15, 3))\n\n    for idx in range(5):\n        batch = train_generator.next()  # Get a batch of images from the generator\n        image = batch[0][idx]  # Fetch an image from the batch\n        label = batch[1][idx]  # Fetch the corresponding label\n        image_id = train_data.iloc[idx]['image_id']  # Fetch image ID from the DataFrame\n        actual_label = train_data.iloc[idx]['label']  # Fetch actual label from the DataFrame\n    \n        ax = axes[idx]\n        ax.imshow(image)\n        ax.set_title(f\"ID: {image_id}\\nNumerical Label: {label.argmax()}\\nCategorical Label: {actual_label}\\nShape: {image.shape}\", fontdict={'family':'serif'})\n        ax.axis('off')\n\n    plt.tight_layout()\n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:03:54.6657Z","iopub.execute_input":"2023-12-08T18:03:54.666436Z","iopub.status.idle":"2023-12-08T18:03:54.673661Z","shell.execute_reply.started":"2023-12-08T18:03:54.666401Z","shell.execute_reply":"2023-12-08T18:03:54.672664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Display 5 images from the Validation Dataset**","metadata":{}},{"cell_type":"code","source":"if not config.is_submission:\n    import matplotlib.pyplot as plt\n\n    # Display 5 images with their respective details from the validation generator\n    fig, axes = plt.subplots(nrows=1, ncols=5, figsize=(15, 3))\n\n    for idx in range(5):\n        batch = validation_generator.next()  # Get a batch of images from the generator\n        image = batch[0][idx]  # Fetch an image from the batch\n        label = batch[1][idx]  # Fetch the corresponding label\n        image_id = test_data.iloc[idx]['image_id']  # Fetch image ID from the DataFrame\n        actual_label = test_data.iloc[idx]['label']  # Fetch actual label from the DataFrame\n    \n        ax = axes[idx]\n        ax.imshow(image)\n        ax.set_title(f\"ID: {image_id}\\nNumerical Label: {label.argmax()}\\nCategorical Label: {actual_label}\\nShape: {image.shape}\", fontdict={'family':'serif'})\n        ax.axis('off')\n\n    plt.tight_layout()\n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:03:56.26579Z","iopub.execute_input":"2023-12-08T18:03:56.266505Z","iopub.status.idle":"2023-12-08T18:03:56.273397Z","shell.execute_reply.started":"2023-12-08T18:03:56.266473Z","shell.execute_reply":"2023-12-08T18:03:56.272404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Check for GPU availability**","metadata":{}},{"cell_type":"code","source":"if not config.is_submission:\n    import tensorflow as tf\n\n    # Set memory growth for GPUs\n    gpus = tf.config.experimental.list_physical_devices('GPU')\n    if gpus:\n        for gpu in gpus:\n            # Set memory growth before initializing the GPU\n            tf.config.experimental.set_memory_growth(gpu, True)\n        print(\"GPU device configured\")\n    else:\n        print(\"No GPU device found\")\n\n","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:03:58.057552Z","iopub.execute_input":"2023-12-08T18:03:58.05821Z","iopub.status.idle":"2023-12-08T18:03:58.063136Z","shell.execute_reply.started":"2023-12-08T18:03:58.058179Z","shell.execute_reply":"2023-12-08T18:03:58.062188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n    import tensorflow as tf\n    from tensorflow.keras.layers import Input, GlobalAveragePooling2D, BatchNormalization, Dense, Dropout\n    from tensorflow.keras.applications import InceptionV3\n    from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n    # Load InceptionV3 backbone\n    weight=\"/kaggle/input/kerascv-train-and-infer-on-thumbnails/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5\"\n    base_model = InceptionV3(input_shape=(224, 224, 3), include_top=False, weights=weight)\n\n    base_model.trainable = False\n\n    # Create a Functional model using the backbone\n    image_inputs = Input(shape=(224, 224, 3))\n    image_embeddings = base_model(image_inputs, training=False)\n    image_embeddings = GlobalAveragePooling2D()(image_embeddings)\n\n    x = BatchNormalization(epsilon=1e-05, momentum=0.1)(image_embeddings)\n    x = Dense(units=1024, activation=\"relu\")(x)\n    x = Dropout(0.1)(x)\n    x = Dense(units=512, activation=\"relu\")(x)\n    x = Dropout(0.1)(x)\n    x = Dense(units=256, activation=\"relu\")(x)\n    outputs = Dense(units=5, activation=\"softmax\")(x)\n\n    # Build the model with the Functional API\n    model = tf.keras.models.Model(inputs=image_inputs, outputs=outputs)\n\n    # Display the model summary\n    model.summary()\n","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:06:47.61794Z","iopub.execute_input":"2023-12-08T18:06:47.618551Z","iopub.status.idle":"2023-12-08T18:06:57.419414Z","shell.execute_reply.started":"2023-12-08T18:06:47.618513Z","shell.execute_reply":"2023-12-08T18:06:57.417692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" if not config.is_submission:\n    model.compile(\n        optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),\n        loss=tf.keras.losses.CategoricalCrossentropy(),\n        metrics=[\"accuracy\"]\n    )","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:06:57.424111Z","iopub.execute_input":"2023-12-08T18:06:57.424396Z","iopub.status.idle":"2023-12-08T18:06:57.430092Z","shell.execute_reply.started":"2023-12-08T18:06:57.424371Z","shell.execute_reply":"2023-12-08T18:06:57.429091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# if not config.is_submission:\n#     # Step 3: Load the model weights\n#     model.load_weights(\"/kaggle/input/kerascv-train-and-infer-on-thumbnails/ucb_ocean_mv3s.h5\")\n#     model.compile(\n#         optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),\n#         loss=tf.keras.losses.CategoricalCrossentropy(),\n#         metrics=[\"accuracy\"]\n#     )\n#     # Step 4: Continue training the model\n#     history = model.fit(\n#     train_generator,\n#     epochs=config.epochs,\n#     validation_data=validation_generator,\n#     class_weight=class_weights\n#     )\n\n#     # Optionally, save the model weights again\n#     model.save_weights(\"/kaggle/working/ucb_ocean_mv3s.h5\")\n","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:06:57.431163Z","iopub.execute_input":"2023-12-08T18:06:57.431435Z","iopub.status.idle":"2023-12-08T18:06:57.443182Z","shell.execute_reply.started":"2023-12-08T18:06:57.431412Z","shell.execute_reply":"2023-12-08T18:06:57.442327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not config.is_submission:\n    import tensorflow as tf \n    from tensorflow.keras.callbacks import ModelCheckpoint\n\nif not config.is_submission:\n    # Step 3: Load the model weights\n    model.load_weights(\"/kaggle/working/final_model_inception_v3.h5\")\n    model.compile(\n        optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),\n        loss=tf.keras.losses.CategoricalCrossentropy(),\n        metrics=[\"accuracy\"]\n    )\n\n    # Step 4: Define ModelCheckpoint callback\n    checkpoint_path = \"/kaggle/working/best_model_inception_v3.h5\"\n    checkpoint = ModelCheckpoint(\n        checkpoint_path,\n        monitor='val_accuracy',  # You can choose the metric you want to monitor\n        save_best_only=True,\n        mode='max',  # 'max' for accuracy, 'min' for loss, etc.\n        verbose=1\n    )\n\n    # Step 5: Continue training the model with ModelCheckpoint callback\n    history = model.fit(\n        train_generator,\n        epochs=10,\n        validation_data=validation_generator,\n        class_weight=class_weights,\n        callbacks=[checkpoint]  # Pass the ModelCheckpoint callback here\n    )\n\n    # Optionally, save the final model weights\n    model.save_weights(\"/kaggle/working/final_model_inception_v3.h5\")\n\n","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:06:57.445141Z","iopub.execute_input":"2023-12-08T18:06:57.445426Z","iopub.status.idle":"2023-12-08T18:06:57.457236Z","shell.execute_reply.started":"2023-12-08T18:06:57.445403Z","shell.execute_reply":"2023-12-08T18:06:57.456514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not config.is_submission:\n    from pathlib import Path\n    from IPython.display import FileLink\n    if Path(\"/kaggle/working//kaggle/working/best_model_inception_v3.h5\").exists:\n        display(FileLink(\"best_model_inception_v3.h5\"))","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:06:57.458252Z","iopub.execute_input":"2023-12-08T18:06:57.45853Z","iopub.status.idle":"2023-12-08T18:06:57.469148Z","shell.execute_reply.started":"2023-12-08T18:06:57.458508Z","shell.execute_reply":"2023-12-08T18:06:57.468363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" if not config.is_submission:\n    model.load_weights(\"/kaggle/working/best_model_inception_v3.h5\")\n    print(\"Hi\")","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:06:57.470518Z","iopub.execute_input":"2023-12-08T18:06:57.470753Z","iopub.status.idle":"2023-12-08T18:06:57.483532Z","shell.execute_reply.started":"2023-12-08T18:06:57.470732Z","shell.execute_reply":"2023-12-08T18:06:57.482704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" if not config.is_submission:\n    # Evaluate the model on the test set\n    test_loss, test_acc = model.evaluate(validation_generator)\n    print(f\"Test Accuracy: {test_acc}\")","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:06:57.484464Z","iopub.execute_input":"2023-12-08T18:06:57.484698Z","iopub.status.idle":"2023-12-08T18:06:57.493974Z","shell.execute_reply.started":"2023-12-08T18:06:57.484678Z","shell.execute_reply":"2023-12-08T18:06:57.493087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not config.is_submission:\n    # Predict labels for the test set\n    predictions = model.predict(validation_generator)\n    predicted_classes = np.argmax(predictions, axis=1)  # Get the index of the highest probability class\n    true_classes = validation_generator.classes\n\n    # Display some of the predicted and true classes\n    print(\"Predicted Classes:\", predicted_classes[-10:])  # Display first 10 predicted classes\n    print(\"True Classes:\", true_classes[-10:])  # Display first 10 true classes\n","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:06:57.494991Z","iopub.execute_input":"2023-12-08T18:06:57.495222Z","iopub.status.idle":"2023-12-08T18:06:57.505361Z","shell.execute_reply.started":"2023-12-08T18:06:57.495202Z","shell.execute_reply":"2023-12-08T18:06:57.504621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not config.is_submission:\n    from sklearn.metrics import classification_report, confusion_matrix, accuracy_score, precision_score, recall_score, f1_score, log_loss, jaccard_score\n    print(\"\\nClassification Report:\")\n    print(classification_report(true_classes, predicted_classes,digits=4))","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:06:57.506398Z","iopub.execute_input":"2023-12-08T18:06:57.506652Z","iopub.status.idle":"2023-12-08T18:06:57.515629Z","shell.execute_reply.started":"2023-12-08T18:06:57.50663Z","shell.execute_reply":"2023-12-08T18:06:57.514837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Generate confusion matrix\n# conf_matrix = confusion_matrix(true_classes, predicted_classes)\n\n# # Plot confusion matrix\n# plt.figure(figsize=(6, 4.5))\n# # Define the custom palette\n# custom_palette = sns.color_palette(palette='GnBu')# Modify the number based on number of classes in the dataset\n# # Define custom font dictionary for title and labels\n# font = {'family': 'Serif', 'weight': 'bold', 'size': 12}\n\n# # Create heatmap with annotations and colormap\n# heatmap = sns.heatmap(conf_matrix, annot=True, fmt='d', cmap=custom_palette,\n#                       xticklabels=['CC', 'EC','HGSC','LGSC','MC'], yticklabels=['CC', 'EC','HGSC','LGSC','MC'],annot_kws={\"family\": \"Serif\",'weight': 'bold', 'size': 12})\n# #CC': 0, 'EC': 1, 'HGSC': 2, 'LGSC': 3, 'MC': 4\n# # Set x and y labels with the custom font dictionary\n# heatmap.set_xlabel('Predicted Labels', fontdict=font)\n# heatmap.set_ylabel('True Labels', fontdict=font)\n# heatmap.set_title('Fracture Classification', fontdict=font, pad=12)\n\n# # Set font properties for tick labels on both axes\n# heatmap.set_xticklabels(heatmap.get_xticklabels(), fontname='Serif', fontsize=12)\n# heatmap.set_yticklabels(heatmap.get_yticklabels(), fontname='Serif', fontsize=12)\n\n# # Create a color bar to indicate the scale\n# cbar = heatmap.collections[0].colorbar\n# cbar.set_label('Count', fontdict=font)\n# cbar.ax.tick_params(labelsize=10)\n# # Adjust padding between x-axis label and x-axis ticks\n# plt.gca().xaxis.labelpad = 10  # Change the value as needed to adjust the space\n# # Adjust layout to prevent cropping\n# plt.tight_layout()\n# #plt.savefig('D:\\\\Data\\\\densenet121_cm.pdf')  # Save as pdf format\n# plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:06:57.518049Z","iopub.execute_input":"2023-12-08T18:06:57.518309Z","iopub.status.idle":"2023-12-08T18:06:57.52705Z","shell.execute_reply.started":"2023-12-08T18:06:57.518286Z","shell.execute_reply":"2023-12-08T18:06:57.526301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if config.is_submission:\n    df = pd.read_csv(config.test_csv_path)\n    df[\"image_path\"] = df[\"image_id\"].apply(lambda x: f\"{config.test_thumbnail_paths}/{x}_thumbnail.png\")\n    \n    # Load the model weights\n    model.load_weights(\"/kaggle/input/kerascv-train-and-infer-on-thumbnails/best_model_inception_v3.h5\")\n    \n    # Load the id to name dictionary\n    with open(\"/kaggle/input/kerascv-train-and-infer-on-thumbnails/id_to_name (1).pkl\", \"rb\") as f:\n        id_to_name = pickle.load(f)","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:07:12.196668Z","iopub.execute_input":"2023-12-08T18:07:12.197069Z","iopub.status.idle":"2023-12-08T18:07:13.538385Z","shell.execute_reply.started":"2023-12-08T18:07:12.197036Z","shell.execute_reply":"2023-12-08T18:07:13.537385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" import numpy as np","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:09:47.255595Z","iopub.execute_input":"2023-12-08T18:09:47.255966Z","iopub.status.idle":"2023-12-08T18:09:47.263604Z","shell.execute_reply.started":"2023-12-08T18:09:47.255937Z","shell.execute_reply":"2023-12-08T18:09:47.262757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if config.is_submission:\n    predicted_labels = []\n   \n\n    for index, row in df.iterrows():\n        # Get the image path\n        image_path = row[\"image_path\"]\n\n        # Get the image\n        image = read_image(image_path)[None, ...]\n        \n        # Predict the label\n        logits = model.predict(image)\n        pred = np.argmax(logits, axis=-1).tolist()[0]\n\n        # Map the pred to the name\n        label = id_to_name[pred]\n\n        predicted_labels.append(label)\n\n    # Add the predicted labels to the csv\n    df[\"label\"] = predicted_labels","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:10:04.812565Z","iopub.execute_input":"2023-12-08T18:10:04.813453Z","iopub.status.idle":"2023-12-08T18:10:05.009581Z","shell.execute_reply.started":"2023-12-08T18:10:04.813417Z","shell.execute_reply":"2023-12-08T18:10:05.008701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if config.is_submission:\n    # Create the submission\n    #print(df)\n    submission_df = df[[\"image_id\", \"label\"]]\n    submission_df.to_csv(\"/kaggle/working/submission.csv\", index=False)\n    print(submission_df)","metadata":{"execution":{"iopub.status.busy":"2023-12-08T18:10:07.174718Z","iopub.execute_input":"2023-12-08T18:10:07.175452Z","iopub.status.idle":"2023-12-08T18:10:07.198264Z","shell.execute_reply.started":"2023-12-08T18:10:07.175419Z","shell.execute_reply":"2023-12-08T18:10:07.197353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}