{"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"}],"dockerImageVersionId":30616,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-11T18:57:07.203612Z","iopub.execute_input":"2023-12-11T18:57:07.204136Z","iopub.status.idle":"2023-12-11T18:57:07.234788Z","shell.execute_reply.started":"2023-12-11T18:57:07.204109Z","shell.execute_reply":"2023-12-11T18:57:07.233766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\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":{"execution":{"iopub.status.busy":"2023-12-11T20:26:11.895729Z","iopub.execute_input":"2023-12-11T20:26:11.896001Z","iopub.status.idle":"2023-12-11T20:26:30.337784Z","shell.execute_reply.started":"2023-12-11T20:26:11.895977Z","shell.execute_reply":"2023-12-11T20:26:30.336843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Config:\n    is_submission = False\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 = 64\n    learning_rate = 1e-2\n    epochs = 10\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-11T21:01:01.419547Z","iopub.execute_input":"2023-12-11T21:01:01.420487Z","iopub.status.idle":"2023-12-11T21:01:01.42566Z","shell.execute_reply.started":"2023-12-11T21:01:01.420452Z","shell.execute_reply":"2023-12-11T21:01:01.424754Z"},"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-11T20:26:30.348417Z","iopub.execute_input":"2023-12-11T20:26:30.34891Z","iopub.status.idle":"2023-12-11T20:26:30.392536Z","shell.execute_reply.started":"2023-12-11T20:26:30.348869Z","shell.execute_reply":"2023-12-11T20:26:30.391655Z"},"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    train_dataframe.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-11T20:26:30.395245Z","iopub.execute_input":"2023-12-11T20:26:30.395863Z","iopub.status.idle":"2023-12-11T20:26:30.402558Z","shell.execute_reply.started":"2023-12-11T20:26:30.395828Z","shell.execute_reply":"2023-12-11T20:26:30.401808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not config.is_submission:\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-11T20:26:30.403842Z","iopub.execute_input":"2023-12-11T20:26:30.40447Z","iopub.status.idle":"2023-12-11T20:26:30.41184Z","shell.execute_reply.started":"2023-12-11T20:26:30.404434Z","shell.execute_reply":"2023-12-11T20:26:30.410903Z"},"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-11T20:26:30.412899Z","iopub.execute_input":"2023-12-11T20:26:30.413189Z","iopub.status.idle":"2023-12-11T20:26:31.112077Z","shell.execute_reply.started":"2023-12-11T20:26:30.413165Z","shell.execute_reply":"2023-12-11T20:26:31.111035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not config.is_submission:\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-11T20:26:31.113204Z","iopub.execute_input":"2023-12-11T20:26:31.11349Z","iopub.status.idle":"2023-12-11T20:26:31.368979Z","shell.execute_reply.started":"2023-12-11T20:26:31.113465Z","shell.execute_reply":"2023-12-11T20:26:31.368078Z"},"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-11T20:26:31.370548Z","iopub.execute_input":"2023-12-11T20:26:31.371139Z","iopub.status.idle":"2023-12-11T20:26:31.576617Z","shell.execute_reply.started":"2023-12-11T20:26:31.371083Z","shell.execute_reply":"2023-12-11T20:26:31.575818Z"},"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-11T20:26:31.577657Z","iopub.execute_input":"2023-12-11T20:26:31.57791Z","iopub.status.idle":"2023-12-11T20:26:31.59193Z","shell.execute_reply.started":"2023-12-11T20:26:31.577887Z","shell.execute_reply":"2023-12-11T20:26:31.591147Z"},"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-11T20:26:31.59576Z","iopub.execute_input":"2023-12-11T20:26:31.596076Z","iopub.status.idle":"2023-12-11T20:26:31.601122Z","shell.execute_reply.started":"2023-12-11T20:26:31.596053Z","shell.execute_reply":"2023-12-11T20:26:31.60016Z"},"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-11T20:26:31.602262Z","iopub.execute_input":"2023-12-11T20:26:31.602579Z","iopub.status.idle":"2023-12-11T20:26:31.614545Z","shell.execute_reply.started":"2023-12-11T20:26:31.602548Z","shell.execute_reply":"2023-12-11T20:26:31.613637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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    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\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=(224, 224),\n        batch_size=config.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=(224, 224),\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-11T20:30:00.477388Z","iopub.execute_input":"2023-12-11T20:30:00.477794Z","iopub.status.idle":"2023-12-11T20:30:00.723759Z","shell.execute_reply.started":"2023-12-11T20:30:00.477764Z","shell.execute_reply":"2023-12-11T20:30:00.722802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-11T20:30:01.991641Z","iopub.execute_input":"2023-12-11T20:30:01.992634Z","iopub.status.idle":"2023-12-11T20:30:01.998759Z","shell.execute_reply.started":"2023-12-11T20:30:01.992591Z","shell.execute_reply":"2023-12-11T20:30:01.99769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-11T20:30:02.501073Z","iopub.execute_input":"2023-12-11T20:30:02.501449Z","iopub.status.idle":"2023-12-11T20:30:55.518803Z","shell.execute_reply.started":"2023-12-11T20:30:02.501422Z","shell.execute_reply":"2023-12-11T20:30:55.517929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-11T20:41:24.849815Z","iopub.execute_input":"2023-12-11T20:41:24.850696Z","iopub.status.idle":"2023-12-11T20:42:02.985581Z","shell.execute_reply.started":"2023-12-11T20:41:24.850663Z","shell.execute_reply":"2023-12-11T20:42:02.984661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-11T20:42:59.761488Z","iopub.execute_input":"2023-12-11T20:42:59.762336Z","iopub.status.idle":"2023-12-11T20:42:59.768322Z","shell.execute_reply.started":"2023-12-11T20:42:59.762303Z","shell.execute_reply":"2023-12-11T20:42:59.767398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not config.is_submission:\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    model = tf.keras.Sequential([\n        tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(224, 224, 3)),\n        tf.keras.layers.MaxPooling2D((2, 2)),\n        tf.keras.layers.Flatten(),\n        tf.keras.layers.Dense(64, activation='relu'),\n        tf.keras.layers.Dense(5, activation='softmax')\n    ])\n    model.summary()\n\n","metadata":{"execution":{"iopub.status.busy":"2023-12-11T20:43:07.450859Z","iopub.execute_input":"2023-12-11T20:43:07.45125Z","iopub.status.idle":"2023-12-11T20:43:07.517315Z","shell.execute_reply.started":"2023-12-11T20:43:07.451219Z","shell.execute_reply":"2023-12-11T20:43:07.516443Z"},"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-11T20:43:08.590368Z","iopub.execute_input":"2023-12-11T20:43:08.590717Z","iopub.status.idle":"2023-12-11T20:43:08.602878Z","shell.execute_reply.started":"2023-12-11T20:43:08.59069Z","shell.execute_reply":"2023-12-11T20:43:08.602024Z"},"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\n\n    history = model.fit(\n        train_generator,\n        epochs=config.epochs,\n        validation_data=validation_generator,\n    )\n    \n    model.save_weights(\"ucb_ocean_checkpoint.weights.h5\")\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-12-11T20:43:09.910535Z","iopub.execute_input":"2023-12-11T20:43:09.910879Z","iopub.status.idle":"2023-12-11T20:57:26.99054Z","shell.execute_reply.started":"2023-12-11T20:43:09.910853Z","shell.execute_reply":"2023-12-11T20:57:26.989509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" if not config.is_submission:\n    model.load_weights(\"ucb_ocean_checkpoint.weights.h5\")\n    print(\"Hi\")","metadata":{"execution":{"iopub.status.busy":"2023-12-11T20:58:12.598857Z","iopub.execute_input":"2023-12-11T20:58:12.59925Z","iopub.status.idle":"2023-12-11T20:58:13.128317Z","shell.execute_reply.started":"2023-12-11T20:58:12.599206Z","shell.execute_reply":"2023-12-11T20:58:13.127371Z"},"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-11T20:58:14.431406Z","iopub.execute_input":"2023-12-11T20:58:14.431843Z","iopub.status.idle":"2023-12-11T20:58:40.256285Z","shell.execute_reply.started":"2023-12-11T20:58:14.431808Z","shell.execute_reply":"2023-12-11T20:58:40.255334Z"},"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-11T20:59:08.968093Z","iopub.execute_input":"2023-12-11T20:59:08.968939Z","iopub.status.idle":"2023-12-11T20:59:35.768732Z","shell.execute_reply.started":"2023-12-11T20:59:08.968907Z","shell.execute_reply":"2023-12-11T20:59:35.767768Z"},"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-11T20:59:35.770484Z","iopub.execute_input":"2023-12-11T20:59:35.770773Z","iopub.status.idle":"2023-12-11T20:59:35.786509Z","shell.execute_reply.started":"2023-12-11T20:59:35.770747Z","shell.execute_reply":"2023-12-11T20:59:35.785576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"config.is_submission = True\nif 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    # Load the model weights\n    model.load_weights(\"ucb_ocean_checkpoint.weights.h5\")\n    \n    # Load the id to name dictionary\n    with open(\"id_to_name.pkl\", \"rb\") as f:\n        id_to_name = pickle.load(f)","metadata":{"execution":{"iopub.status.busy":"2023-12-11T21:01:14.052844Z","iopub.execute_input":"2023-12-11T21:01:14.053601Z","iopub.status.idle":"2023-12-11T21:01:14.59531Z","shell.execute_reply.started":"2023-12-11T21:01:14.053571Z","shell.execute_reply":"2023-12-11T21:01:14.594471Z"},"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 = 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-11T21:04:38.921241Z","iopub.execute_input":"2023-12-11T21:04:38.921605Z","iopub.status.idle":"2023-12-11T21:04:39.120015Z","shell.execute_reply.started":"2023-12-11T21:04:38.921577Z","shell.execute_reply":"2023-12-11T21:04:39.119166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if config.is_submission:\n    # Create the submission\n    submission_df = df[[\"image_id\", \"label\"]]\n    submission_df.to_csv(\"submission.csv\", index=False)\n    print(submission_df)","metadata":{"execution":{"iopub.status.busy":"2023-12-11T21:05:37.399936Z","iopub.execute_input":"2023-12-11T21:05:37.400757Z","iopub.status.idle":"2023-12-11T21:05:37.409631Z","shell.execute_reply.started":"2023-12-11T21:05:37.400723Z","shell.execute_reply":"2023-12-11T21:05:37.408755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}