{"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":"nvidiaTeslaT4","dataSources":[{"sourceType":"competition","sourceId":45867,"databundleVersionId":6924515},{"sourceType":"datasetVersion","sourceId":7155427,"datasetId":4123855,"databundleVersionId":7244451}],"dockerImageVersionId":30587,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nos.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\n# Set the style for the plot\nsns.set(style=\"whitegrid\")\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-08T14:52:36.644306Z","iopub.execute_input":"2023-12-08T14:52:36.644652Z","iopub.status.idle":"2023-12-08T14:52:56.456406Z","shell.execute_reply.started":"2023-12-08T14:52:36.644623Z","shell.execute_reply":"2023-12-08T14:52:56.455436Z"},"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-08T14:53:34.554082Z","iopub.execute_input":"2023-12-08T14:53:34.555343Z","iopub.status.idle":"2023-12-08T14:53:34.561785Z","shell.execute_reply.started":"2023-12-08T14:53:34.555297Z","shell.execute_reply":"2023-12-08T14:53:34.560581Z"},"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-08T14:53:34.968844Z","iopub.execute_input":"2023-12-08T14:53:34.969191Z","iopub.status.idle":"2023-12-08T14:53:34.973801Z","shell.execute_reply.started":"2023-12-08T14:53:34.96915Z","shell.execute_reply":"2023-12-08T14:53:34.972857Z"},"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-08T14:53:35.351965Z","iopub.execute_input":"2023-12-08T14:53:35.352333Z","iopub.status.idle":"2023-12-08T14:53:35.359704Z","shell.execute_reply.started":"2023-12-08T14:53:35.352304Z","shell.execute_reply":"2023-12-08T14:53:35.358828Z"},"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    main_dataset.head()\n","metadata":{"execution":{"iopub.status.busy":"2023-12-08T14:53:35.63983Z","iopub.execute_input":"2023-12-08T14:53:35.640208Z","iopub.status.idle":"2023-12-08T14:53:35.644858Z","shell.execute_reply.started":"2023-12-08T14:53:35.640159Z","shell.execute_reply":"2023-12-08T14:53:35.643915Z"},"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-08T14:53:36.279514Z","iopub.execute_input":"2023-12-08T14:53:36.279865Z","iopub.status.idle":"2023-12-08T14:53:36.289978Z","shell.execute_reply.started":"2023-12-08T14:53:36.279838Z","shell.execute_reply":"2023-12-08T14:53:36.288715Z"},"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-08T14:53:36.568695Z","iopub.execute_input":"2023-12-08T14:53:36.569585Z","iopub.status.idle":"2023-12-08T14:53:36.573845Z","shell.execute_reply.started":"2023-12-08T14:53:36.569552Z","shell.execute_reply":"2023-12-08T14:53:36.572874Z"},"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-08T14:53:36.801038Z","iopub.execute_input":"2023-12-08T14:53:36.801762Z","iopub.status.idle":"2023-12-08T14:53:36.808987Z","shell.execute_reply.started":"2023-12-08T14:53:36.801726Z","shell.execute_reply":"2023-12-08T14:53:36.808121Z"},"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-08T14:53:37.053421Z","iopub.execute_input":"2023-12-08T14:53:37.053768Z","iopub.status.idle":"2023-12-08T14:53:37.061705Z","shell.execute_reply.started":"2023-12-08T14:53:37.05374Z","shell.execute_reply":"2023-12-08T14:53:37.060662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not config.is_submission:\n    class_weights = np.sum(labels) - np.sum(labels, axis=0)\n    class_weights = class_weights / np.sum(class_weights) # Normalize the weights\n\n    class_weights = {idx:weight for idx, weight in enumerate(class_weights)}\n\n    for idx, weight in class_weights.items():\n        print(f\"{id_to_name[idx]}: {weight:0.2f}\")","metadata":{"execution":{"iopub.status.busy":"2023-12-08T14:53:37.303283Z","iopub.execute_input":"2023-12-08T14:53:37.303631Z","iopub.status.idle":"2023-12-08T14:53:37.309269Z","shell.execute_reply.started":"2023-12-08T14:53:37.303604Z","shell.execute_reply":"2023-12-08T14:53:37.308242Z"},"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-08T14:53:37.565155Z","iopub.execute_input":"2023-12-08T14:53:37.566234Z","iopub.status.idle":"2023-12-08T14:53:37.576203Z","shell.execute_reply.started":"2023-12-08T14:53:37.566189Z","shell.execute_reply":"2023-12-08T14:53:37.574847Z"},"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\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\n    # Image dimensions and other parameters\n    img_width, img_height = 224, 224\n    batch_size = 32\n\n    # Data generators for RGB images\n    train_datagen = ImageDataGenerator(rescale=1./255)\n    validation_datagen = ImageDataGenerator(rescale=1./255)\n\n    # Train and validation generators\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=batch_size,\n        class_mode='categorical',\n        shuffle=True  # Set to True for training\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=batch_size,\n        class_mode='categorical',\n        shuffle=False  # Set to False for validation/testing\n    )\n\n","metadata":{"execution":{"iopub.status.busy":"2023-12-08T14:53:38.008784Z","iopub.execute_input":"2023-12-08T14:53:38.009131Z","iopub.status.idle":"2023-12-08T14:53:38.016746Z","shell.execute_reply.started":"2023-12-08T14:53:38.009103Z","shell.execute_reply":"2023-12-08T14:53:38.015673Z"},"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-08T14:53:38.4242Z","iopub.execute_input":"2023-12-08T14:53:38.425035Z","iopub.status.idle":"2023-12-08T14:53:38.42928Z","shell.execute_reply.started":"2023-12-08T14:53:38.425004Z","shell.execute_reply":"2023-12-08T14:53:38.428393Z"},"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-08T14:53:38.871038Z","iopub.execute_input":"2023-12-08T14:53:38.871997Z","iopub.status.idle":"2023-12-08T14:53:38.878986Z","shell.execute_reply.started":"2023-12-08T14:53:38.871957Z","shell.execute_reply":"2023-12-08T14:53:38.878031Z"},"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-08T14:53:39.319638Z","iopub.execute_input":"2023-12-08T14:53:39.320376Z","iopub.status.idle":"2023-12-08T14:53:39.327941Z","shell.execute_reply.started":"2023-12-08T14:53:39.320343Z","shell.execute_reply":"2023-12-08T14:53:39.326744Z"},"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-08T14:53:39.768803Z","iopub.execute_input":"2023-12-08T14:53:39.769222Z","iopub.status.idle":"2023-12-08T14:53:39.774695Z","shell.execute_reply.started":"2023-12-08T14:53:39.769185Z","shell.execute_reply":"2023-12-08T14:53:39.773702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.layers import Input, GlobalAveragePooling2D, BatchNormalization, Dense, Dropout\nfrom tensorflow.keras.applications import MobileNetV3Large,MobileNetV3Small\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n\n# Load MobileNetV3Large backbone\nweights_path = '/kaggle/input/kerascv-train-and-infer-on-thumbnails/weights_mobilenet_v3_small_224_1.0_float_no_top_v2.h5'\nbase_model = MobileNetV3Small(input_shape=(224, 224, 3), include_top=False, weights=weights_path)\n\nbase_model.trainable = False\n\n# Create a Functional model using the backbone\nimage_inputs = Input(shape=(224, 224, 3))\nimage_embeddings = base_model(image_inputs, training=False)\nimage_embeddings = GlobalAveragePooling2D()(image_embeddings)\n\nx = BatchNormalization(epsilon=1e-05, momentum=0.1)(image_embeddings)\nx = Dense(units=1024, activation=\"relu\")(x)\nx = Dropout(0.1)(x)\nx = Dense(units=512, activation=\"relu\")(x)\nx = Dropout(0.1)(x)\nx = Dense(units=256, activation=\"relu\")(x)\noutputs = Dense(units=5, activation=\"softmax\")(x)\n\n# Build the model with the Functional API\nmodel = tf.keras.models.Model(inputs=image_inputs, outputs=outputs)\n\n# Display the model summary\nmodel.summary()\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-12-08T14:53:40.216138Z","iopub.execute_input":"2023-12-08T14:53:40.216975Z","iopub.status.idle":"2023-12-08T14:53:50.438045Z","shell.execute_reply.started":"2023-12-08T14:53:40.216942Z","shell.execute_reply":"2023-12-08T14:53:50.435563Z"},"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.01),\n        loss=tf.keras.losses.CategoricalCrossentropy(),\n        metrics=[\"accuracy\"]\n    )","metadata":{"execution":{"iopub.status.busy":"2023-12-08T14:53:50.440281Z","iopub.execute_input":"2023-12-08T14:53:50.440564Z","iopub.status.idle":"2023-12-08T14:53:50.445274Z","shell.execute_reply.started":"2023-12-08T14:53:50.440539Z","shell.execute_reply":"2023-12-08T14:53:50.444431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nif 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-08T14:53:50.446508Z","iopub.execute_input":"2023-12-08T14:53:50.4468Z","iopub.status.idle":"2023-12-08T14:53:50.457374Z","shell.execute_reply.started":"2023-12-08T14:53:50.446775Z","shell.execute_reply":"2023-12-08T14:53:50.456508Z"},"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/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\n    # Step 4: Define ModelCheckpoint callback\n    checkpoint_path = \"/kaggle/working/best_model.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=50,\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.h5\")\n\n","metadata":{"execution":{"iopub.status.busy":"2023-12-08T14:53:50.459395Z","iopub.execute_input":"2023-12-08T14:53:50.4597Z","iopub.status.idle":"2023-12-08T14:53:50.47291Z","shell.execute_reply.started":"2023-12-08T14:53:50.459659Z","shell.execute_reply":"2023-12-08T14:53:50.472011Z"},"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.h5\").exists:\n        display(FileLink(\"best_model.h5\"))","metadata":{"execution":{"iopub.status.busy":"2023-12-08T14:54:11.481336Z","iopub.execute_input":"2023-12-08T14:54:11.482196Z","iopub.status.idle":"2023-12-08T14:54:11.487097Z","shell.execute_reply.started":"2023-12-08T14:54:11.482142Z","shell.execute_reply":"2023-12-08T14:54:11.485969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" if not config.is_submission:\n    model.load_weights(\"/kaggle/working/best_model.h5\")\n    print(\"Hi\")","metadata":{"execution":{"iopub.status.busy":"2023-12-08T14:54:56.915121Z","iopub.execute_input":"2023-12-08T14:54:56.915538Z","iopub.status.idle":"2023-12-08T14:54:56.919797Z","shell.execute_reply.started":"2023-12-08T14:54:56.91551Z","shell.execute_reply":"2023-12-08T14:54:56.918992Z"},"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-08T14:55:00.375525Z","iopub.execute_input":"2023-12-08T14:55:00.375878Z","iopub.status.idle":"2023-12-08T14:55:00.380259Z","shell.execute_reply.started":"2023-12-08T14:55:00.37585Z","shell.execute_reply":"2023-12-08T14:55:00.37932Z"},"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.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-08T14:55:01.238843Z","iopub.execute_input":"2023-12-08T14:55:01.239571Z","iopub.status.idle":"2023-12-08T14:55:01.524821Z","shell.execute_reply.started":"2023-12-08T14:55:01.239538Z","shell.execute_reply":"2023-12-08T14:55:01.524021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if config.is_submission:\n    predicted_labels = []\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 = ops.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-08T14:55:09.559094Z","iopub.execute_input":"2023-12-08T14:55:09.559961Z","iopub.status.idle":"2023-12-08T14:55:19.084884Z","shell.execute_reply.started":"2023-12-08T14:55:09.559928Z","shell.execute_reply":"2023-12-08T14:55:19.083924Z"},"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-08T14:55:20.472138Z","iopub.execute_input":"2023-12-08T14:55:20.472521Z","iopub.status.idle":"2023-12-08T14:55:20.491071Z","shell.execute_reply.started":"2023-12-08T14:55:20.472495Z","shell.execute_reply":"2023-12-08T14:55:20.490045Z"},"trusted":true},"execution_count":null,"outputs":[]}]}