{ "cells": [ { "cell_type": "markdown", "id": "362e9f36-2300-4851-8f49-b952e62a2c78", "metadata": {}, "source": [ "\"IOAI\n", "\n", "[IOAI 2025 (Beijing, China), Individual Contest](https://ioai-official.org/china-2025)\n", "\n", "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/IOAI-official/IOAI-2025/blob/main/Individual-Contest/Pixel/Pixel.ipynb)" ] }, { "cell_type": "markdown", "id": "4509a190", "metadata": {}, "source": [ "# Pixel Efficiency\n", "\n", "## 1. Problem Description\n", "\n", "You are a student in wildlife biology, working on a groundbreaking research project at the Starr Park Research Center. Your team has deployed thousands of camera traps across remote wilderness areas to monitor endangered species populations. However, the satellite internet connections in these remote locations have extremely limited bandwidth. Your job is to write code that identifies the most critical pixels in each wildlife photograph so that only the essential visual information needs to be transmitted back to headquarters.\n", "\n", "\n", "\n", "## 2. Dataset\n", "\n", "The dataset consists of a training set and a test set. Datasets are loaded using `load_from_disk`, and are in the format of `datasets`. Test set is not visible to the contestants.\n", "\n", "In the dataset there are the following fields:\n", "\n", "- `image`: the image are RGB full color images in PIL format, the size of each image is (224, 224)\n", "- `name`: the animal species label\n", "- `idx`: unique identifiers used to track the records.\n", "\n", "1. **Training Set (`train_dataset` folder)**:\n", " - The training set is used for training your models/ doing experimentations on and can be accessed and downloaded directly during the competition.\n", " - There are 700 images in the training set.\n", "\n", "2. **Test Set (`test_dataset` folder)**: \n", " - These follow the same format as the training set but do not contain the `name` field.\n", " - There are 698 images in test set, which had been separated into 2 testing sets within the ratio of 3:7, i.e. 30% of the data would be used to calculate the Leaderboard A score, another 70% data would be used to calculate the Leaderboard B score.\n", " - The testing set is used to calculate the Leaderboard A score and the Leaderboard B score and is not directly accessible during the competition. Contestants can access the result on Leaderboard A , but cannot access the result on Leaderboard B. The final score would be counted using Leaderboard B only. The subsets for Leaderboard A and Leaderboard B are completely distinct.\n", " \n", "\n", "## 3. Task\n", "You are given a dataset of animal photographs and a CLIP model that can do a zero-shot classification of animal species. To conserve bandwidth, you need to retain at most **6.25%** of the pixels of each image, while keeping classification accuracy as high as possible.\n", "\n", "More specifically, your task is to return **one rectangle mask** for each image, which contain a single rectangular area indicating the area to keep. Each mask is defined by two coordinate tuples: one for the top-left corner and one for the bottom-right corner of the rectangle. Below is a visualization of what the image would look like after applying a rectangular mask using the process from the baseline:\n", "\n", "\n", "\n", "**Coordinate Convention:**\n", "- Top-left corner coordinates are **inclusive** (the pixel at this position is included in the mask) \n", "- Bottom-right corner coordinates are **exclusive** (the pixel at this position is NOT included in the mask)\n", "\n", "For example, if you specify coordinates `((10, 20), (15, 25))`, the mask will cover pixels from row 10 to 14 (inclusive) and column 20 to 24 (inclusive), for a total area of 25 pixels.\n", "\n", "As an illustration, if an image size is 3x3 and we wanted to keep only the top-right pixel using coordinates `((0, 2), (1, 3))`, the resulting binary mask would be:\n", "\n", "```\n", "[[0, 0, 1],\n", " [0, 0, 0],\n", " [0, 0, 0]]\n", "```\n", "\n", "Below is a summary of the requirements for your masks:\n", "\n", "- Return one rectangle mask defined by coordinate tuples: `((top, left), (bottom, right))`\n", "- Top-left corner coordinates are inclusive, bottom-right corner coordinates are exclusive\n", "- The rectangle mask should cover at most *6.25%* of the original pixels (minimum 93.75% reduction of the original pixels)\n", "- All images are of size (224, 224), so coordinate values should be within the range [0, 224]\n", "\n", "\n", "Images would be masked using the mask you created, outside the masked rectangle, all pixels outside the masked rectangle will be replaced with RGB(0, 0, 0) (black) values. The masked image will be then passed through the CLIP model during evaluation, and your task is to keep the classification accuracy of the CLIP model on these masked images as high as possible. **An additional `other` class would be added into the classes for classification** to ensure that your masked image retains actual useful information for the researchers back at Starr Park Headquarters. So for example, if your image doesn't contain any animal information, the model will predict the `others` class instead of predicting a random animal and having a chance of getting it correct.\n", "\n", "You need to work only with the provided CLIP model and dataset. As a reminder, CLIP generates representations for both text and image, and it can compute a similarity score between them. So if you have ten animal classes, CLIP can look at the provided image and decide which text (class) is closest to the image. \n", "\n", "To ensure that your solution would handle the traffic of images for the research center, your code should run in **UNDER 8 MINUTES for the 698 images in the test dataset**. It is recommended that you test your solution on the training set first, which contain 700 images, to understand how much time your solution takes (testing set would take slightly longer due to dataset loading).\n", "\n", "## 4. Submission\n", "\n", "Contestants need to submit a notebook file named `submission.ipynb`. The file should output a `.jsonl` file titled `submission.jsonl`, which contains all the generated masks for the dataset split. Each mask in the `submission.jsonl` file should be stored as a tuple of two coordinate tuples: `((top, left), (bottom, right))`, where the top-left corner is inclusive and the bottom-right corner is exclusive.\n", "\n", "Contestants don't need to separate test sets into Leaderboard A and Leaderboard B, the evaluation machine will read `submission.jsonl` and automatically calculate the scores for Leaderboard A and Leaderboard B based on the prediction results and true labels. \n", "\n", "The submission files must strictly follow the above format and naming; otherwise, the system will not be able to read them correctly. \n", "\n", "## 5. Score\n", "\n", "The evaluation metric will be **classification accuracy**, defined as the proportion of correctly predicted samples over the total number of evaluated samples.\n", "\n", "Your score is the zero-shot classification accuracy of CLIP on the masked test images. **If a submitted mask for an image is invalid (wrong shape, more than 6.25% pixels retained, etc.), that image is counted as incorrect. A sample script is provided to compute the training split score.**\n", "\n", "\n", "## 6. Baseline and Training Set\n", "\n", "- Below you can find the baseline solution.\n", "- The dataset is in `training_set` folder.\n", "- The highest score by the Scientific Committee for this task is 0.83 in Leader Board B, this score is used for score unification.\n", "- The baseline score by the Scientific Committee for this task is 0.19 in Leader Board B, this score is used for score unification." ] }, { "cell_type": "code", "execution_count": null, "id": "9d1ad03b-ba1e-4c24-b866-fe6a138b58c9", "metadata": {}, "outputs": [], "source": [ "import random\n", "import numpy as np\n", "import torch\n", "\n", "seed = 42\n", "\n", "random.seed(seed) # Python built-in random\n", "np.random.seed(seed) # NumPy\n", "torch.manual_seed(seed) # PyTorch (CPU)\n", "torch.cuda.manual_seed(seed) # PyTorch (single GPU)\n", "torch.cuda.manual_seed_all(seed) # PyTorch (all GPUs)\n", "\n", "# Ensures deterministic behavior\n", "torch.backends.cudnn.deterministic = True\n", "torch.backends.cudnn.benchmark = False" ] }, { "cell_type": "markdown", "id": "af37a8ed", "metadata": {}, "source": [ "### Dependencies and Config Variables" ] }, { "cell_type": "code", "execution_count": null, "id": "23b68a41", "metadata": {}, "outputs": [], "source": [ "import os\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "from collections import Counter\n", "from PIL import Image\n", "from tqdm import tqdm\n", "import glob\n", "import json\n", "import math\n", "import torch\n", "import matplotlib.pyplot as plt\n", "from datasets import load_dataset, load_from_disk\n", "from transformers import CLIPProcessor, CLIPModel\n", "from PIL import Image\n", "from tqdm.auto import tqdm \n", "\n", "TRAIN_PATH = \"./training_set/\"\n", "# The training set is deployed automatically in the testing machine. \n", "# You notebook can access the TRAIN_PATH even if you do not mount it along with notebook.\n", "\n", "MODEL_PATH = \"./clip-vit-large-patch14\"\n", "# The clip model is deployed automatically in the testing machine. \n", "# You notebook can access the MODEL_PATH even if you do not mount it along with notebook.\n", "\n", "DATASET_PATH = TRAIN_PATH + \"train_dataset\"\n", "SPLIT = \"train\"\n", "DEVICE = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n", "BACKGROUND_CLASS = \"other\" # Class used to catch masked images that have no useful information, preventing completely off masks from \"guessing\" the answer from the 10 classes\n", "\n", "# Image and Masking Configuration\n", "HEIGHT = 224\n", "WIDTH = 224\n", "RETAIN_RATIO = 0.0625 # Retain 6.25% of pixels\n", "MEAN_COLOR = (0, 0, 0) # RGB mean values for masked out areas\n" ] }, { "cell_type": "markdown", "id": "b888c040", "metadata": {}, "source": [ "### Dataset loading\n", "\n", "Let's first load the dataset in and see what's in it:\n" ] }, { "cell_type": "code", "execution_count": null, "id": "857ab7ff", "metadata": {}, "outputs": [], "source": [ "# Load the dataset\n", "print(\"Loading dataset...\")\n", "dataset_whole = load_from_disk(DATASET_PATH)\n", "dataset = dataset_whole[SPLIT]\n", "\n", "# Print first item to check available fields\n", "print(\"\\nFirst item keys:\")\n", "print(dataset_whole[SPLIT][0].keys())\n", "\n", "# Show basic dataset statistics without converting fields yet\n", "print(f\"\\nDataset loaded successfully!\")\n", "print(f\"Total samples: {len(dataset)}\")\n", "\n", "print(f\"\\nSample item structure:\")\n", "sample_item = dataset[0]\n", "print(f\" Keys: {list(sample_item.keys())}\")\n", "print(f\" Image type: {type(sample_item['image'])}\")\n", "print(f\" Image size: {sample_item['image'].size}\")\n", "print(f\" Index: {sample_item['idx']}\")\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "37c2dbfa", "metadata": {}, "outputs": [], "source": [ "# Visualize first 10 samples\n", "fig, axes = plt.subplots(2, 5, figsize=(15, 8))\n", "axes = axes.flatten()\n", "\n", "print(\"Visualizing first 10 samples...\")\n", "\n", "for i in range(10):\n", " sample = dataset[i]\n", " image = sample['image']\n", " label = sample['name']\n", " \n", " axes[i].imshow(image)\n", " axes[i].set_title(f\"{label}\\n\", fontsize=12)\n", " axes[i].axis('off')\n", "\n", "plt.tight_layout()\n", "plt.suptitle('First 10 Samples from Dataset', fontsize=16, y=1.02)\n", "plt.show()\n" ] }, { "cell_type": "markdown", "id": "24cee96a", "metadata": {}, "source": [ "### Model\n", "\n", "Now let's load the model and see some predictions:" ] }, { "cell_type": "code", "execution_count": null, "id": "fd92b376", "metadata": {}, "outputs": [], "source": [ "print(f\"Loading CLIP model and processor: {MODEL_PATH}...\")\n", "model = CLIPModel.from_pretrained(MODEL_PATH).to(DEVICE)\n", "processor = CLIPProcessor.from_pretrained(MODEL_PATH)\n", "print(\"Model and processor loaded successfully.\")" ] }, { "cell_type": "code", "execution_count": null, "id": "a2e25924", "metadata": {}, "outputs": [], "source": [ "image = dataset[0]['image']\n", "# Visualize the image with its true label\n", "plt.figure(figsize=(8, 6))\n", "plt.imshow(image)\n", "plt.title(f\"Sample Image\\nTrue Label: {dataset[0]['name']}\", fontsize=14)\n", "plt.axis('off')\n", "plt.show()\n", "\n", "\n", "labels = sorted(list(set(dataset['name']))) + [BACKGROUND_CLASS]\n", "text_inputs = processor(text=labels, return_tensors=\"pt\", padding=True).to(DEVICE)\n", "image_processed = processor(images=image, return_tensors=\"pt\").to(DEVICE)\n", "pixel_values = image_processed['pixel_values']\n", "outputs_full = model(pixel_values=pixel_values, **text_inputs)\n", "logits_full = outputs_full.logits_per_image # Shape: (1, num_styles)\n", "predicted_index_full = logits_full.argmax(dim=-1).item()\n", "\n", "print(f\"Predicted label: {labels[predicted_index_full]}\")\n" ] }, { "cell_type": "markdown", "id": "54467f0c", "metadata": {}, "source": [ "### Baseline: A trivial masking method\n", "\n", "We will now be implementing a trivial masking solution, one that randomly masks out 90% of the pixels." ] }, { "cell_type": "code", "execution_count": null, "id": "0ee24b90", "metadata": {}, "outputs": [], "source": [ "def generate_center_crop_coordinates(image):\n", " \"\"\"\n", " Generate coordinates for a center crop mask.\n", " \n", " Returns:\n", " tuple: ((top, left), (bottom, right)) coordinates for the crop\n", " \"\"\"\n", " H, W = image.size\n", " total_px = H * W\n", " k = int(total_px * RETAIN_RATIO)\n", " \n", " # Calculate side length of the square crop\n", " side_length = int(np.sqrt(k))\n", " \n", " # Calculate center coordinates\n", " center_h, center_w = H // 2, W // 2\n", " \n", " # Calculate crop boundaries\n", " half_side = side_length // 2\n", " top = max(0, center_h - half_side)\n", " left = max(0, center_w - half_side)\n", " bottom = min(H, top + side_length)\n", " right = min(W, left + side_length)\n", " \n", " return ((top, left), (bottom, right))\n", "\n", "def generate_mask_from_coordinates(image, coordinates):\n", " \"\"\"\n", " Generate a binary mask from crop coordinates.\n", " \n", " Parameters:\n", " image: PIL Image\n", " coordinates: tuple of ((top, left), (bottom, right))\n", " \n", " Returns:\n", " numpy array: Binary mask with 1s in the crop area\n", " \"\"\"\n", " H, W = image.size\n", " mask = np.zeros((H, W), dtype=np.int8)\n", " \n", " (top, left), (bottom, right) = coordinates\n", " mask[top:bottom, left:right] = 1\n", " \n", " return mask" ] }, { "cell_type": "code", "execution_count": null, "id": "e87f8f85", "metadata": {}, "outputs": [], "source": [ "def apply_mask_with_mean(image, mask, mean_rgb=MEAN_COLOR):\n", " \"\"\"\n", " Apply arbitrary binary mask to image, replacing masked areas with mean values\n", "\n", " Parameters:\n", " - image: PIL Image (224x224)\n", " - mask: Binary numpy array or PIL Image (224x224) where 0 is the area to drop and 1 is the area to keep\n", " - mean_rgb: RGB mean values to use (default: from config)\n", "\n", " Returns: Modified PIL Image\n", " \"\"\"\n", " # Convert images to numpy arrays\n", " img_array = np.array(image).copy()\n", "\n", " # Ensure mask is numpy array\n", " if isinstance(mask, Image.Image):\n", " mask_array = np.array(mask.convert('L')) > 127 # Convert to binary\n", " else:\n", " mask_array = mask > 0\n", "\n", " # Reshape mask for broadcasting with RGB\n", " mask_3d = np.stack([mask_array] * 3, axis=2)\n", "\n", " # Convert mean values to 0-255 range\n", " mean_values = np.array([int(m * 255) for m in mean_rgb])\n", " # Apply mask - replace areas where mask is 0 (drop) with mean values, keep areas where mask is 1\n", " img_array = np.where(mask_3d, img_array, mean_values.reshape(1, 1, 3))\n", "\n", " return Image.fromarray(img_array.astype(np.uint8))" ] }, { "cell_type": "code", "execution_count": null, "id": "a84ed28b", "metadata": {}, "outputs": [], "source": [ "image = dataset[0]['image']\n", "# Visualize the image with its true label\n", "plt.figure(figsize=(8, 6))\n", "plt.imshow(image)\n", "plt.title(f\"Sample Image\\nTrue Label: {dataset[0]['name']}\", fontsize=14)\n", "plt.axis('off')\n", "plt.show()\n", "\n", "\n", "labels = sorted(list(set(dataset['name']))) + [BACKGROUND_CLASS]\n", "text_inputs = processor(text=labels, return_tensors=\"pt\", padding=True).to(DEVICE)\n", "\n", "mask = generate_mask_from_coordinates(image, generate_center_crop_coordinates(image))\n", "image_masked = apply_mask_with_mean(image, mask)\n", "\n", "plt.figure(figsize=(8, 6))\n", "plt.imshow(image_masked)\n", "plt.title(f\"Masked Image\\nTrue Label: {dataset[0]['name']}\", fontsize=14)\n", "plt.axis('off')\n", "plt.show()\n", "\n", "image_processed = processor(images=image_masked, return_tensors=\"pt\").to(DEVICE)\n", "pixel_values = image_processed['pixel_values']\n", "outputs_full = model(pixel_values=pixel_values, **text_inputs)\n", "logits_full = outputs_full.logits_per_image # Shape: (1, num_styles)\n", "predicted_index_full = logits_full.argmax(dim=-1).item()\n", "\n", "print(f\"Predicted label: {labels[predicted_index_full]}\")" ] }, { "cell_type": "markdown", "id": "d6987536", "metadata": {}, "source": [ "### Exporting the masks\n" ] }, { "cell_type": "code", "execution_count": null, "id": "8aa65a80", "metadata": {}, "outputs": [], "source": [ "#DATA_PATH is the secret environment variable to point the address of the validation set and test set on the testing machine. \n", "#Contestants cannot access this address locally.\n", "import os\n", "if os.environ.get('DATA_PATH'):\n", " TEST_PATH = os.environ.get(\"DATA_PATH\") + \"/\" \n", "else:\n", " TEST_PATH = \"\" # Fallback for local testing\n", "\n", "dataset = load_from_disk(TEST_PATH + \"test_dataset\")\n", "split = \"test\"\n", "dataset = dataset[split]" ] }, { "cell_type": "code", "execution_count": null, "id": "5cae01f0", "metadata": {}, "outputs": [], "source": [ "## Exporting results and validating on full dataset\n", "RETAIN_RATIO = 0.0625\n", "\n", "masks = {}\n", "for item in tqdm(dataset):\n", " image = item['image']\n", "\n", " ## you should replace mask generation with your function\n", " coordinates = generate_center_crop_coordinates(image)\n", " \n", " # don't need to change below, it's just saving to file\n", " idx = item['idx']\n", " # For validation, we still need to generate the full mask\n", " mask = generate_mask_from_coordinates(image, coordinates)\n", " assert mask.shape == (224, 224), \"Mask should be 224x224\"\n", " assert mask.sum() <= RETAIN_RATIO * 224 * 224, \"You should leave only 6.25% of pixels\"\n", " \n", " # Save only the coordinates (topleft, bottomright) instead of the full mask\n", " masks[idx] = coordinates\n", "\n", "# Save as JSONL (one JSON object per line) - much safer than pickle\n", "with open('submission.jsonl', 'w') as f:\n", " for idx, coordinates in masks.items():\n", " json.dump({\"idx\": idx, \"coordinates\": coordinates}, f)\n", " f.write('\\n')\n", "\n", "print(\"Masks saved to masks.jsonl\")" ] }, { "cell_type": "markdown", "id": "ed60e044", "metadata": {}, "source": [ "### Validation" ] }, { "cell_type": "code", "execution_count": null, "id": "3d1ef680", "metadata": {}, "outputs": [], "source": [ "# # Validation code for generated masks\n", "\n", "# def check_validity(coordinates):\n", "# \"\"\"\n", "# Check if coordinates are valid according to the requirements.\n", "# Returns True if valid, False otherwise.\n", "# \"\"\"\n", "# try:\n", "# # Check if coordinates is a tuple of two tuples\n", "# if not isinstance(coordinates, tuple) or len(coordinates) != 2:\n", "# print(f\"Coordinates is not a tuple of two tuples\")\n", "# return False\n", " \n", "# (top, left), (bottom, right) = coordinates\n", " \n", "# # Check if all coordinates are integers\n", "# if not all(isinstance(coord, (int, np.integer)) for coord in [top, left, bottom, right]):\n", "# print(f\"Coordinates are not integers\")\n", "# return False\n", " \n", "# # Check if coordinates are within image bounds\n", "# # For slicing mask[top:bottom, left:right], valid ranges are:\n", "# # top, left: [0, 223] (inclusive)\n", "# # bottom, right: [1, 224] (inclusive) since we need top < bottom and left < right\n", "# if not (0 <= top < 224 and 0 <= left < 224 and 1 <= bottom <= 224 and 1 <= right <= 224):\n", "# print(f\"Coordinates are not within image bounds\")\n", "# return False\n", " \n", "# # Check if top-left is actually top-left of bottom-right (proper ordering)\n", "# if not (top < bottom and left < right):\n", "# print(f\"Top-left is not actually top-left of bottom-right\")\n", "# return False\n", " \n", "# # Check that the crop area doesn't exceed RETAIN_RATIO\n", "# crop_area = (bottom - top) * (right - left)\n", "# max_area = RETAIN_RATIO * 224 * 224\n", "# if crop_area > max_area:\n", "# print(f\"Crop area {crop_area} exceeds max area {max_area}\")\n", "# return False\n", " \n", "# return True\n", "# except Exception:\n", "# return False\n", "\n", "\n", "\n", "\n", "# def validate_masks(masks):\n", "# \"\"\"Simple validation of generated masks on the dataset\"\"\"\n", "# correct = 0\n", "# total = 0\n", "\n", "# labels = sorted(list(set(dataset['name']))) + ['other']\n", "# text_inputs = processor(text=labels, return_tensors=\"pt\", padding=True).to(DEVICE)\n", "\n", "# with torch.no_grad():\n", "# for item in tqdm(dataset, desc=\"Validating masks\"):\n", "# idx = item['idx']\n", "# if idx not in masks:\n", "# continue\n", "\n", "# if not check_validity(masks[idx]):\n", "# continue\n", " \n", "# mask_coordinates = masks[idx]\n", "# image = item['image']\n", "# true_label = item['name']\n", " \n", "# # Apply mask to image\n", "# if image.mode != \"RGB\":\n", "# image = image.convert(\"RGB\")\n", " \n", "# mask = generate_mask_from_coordinates(image, mask_coordinates)\n", "\n", "# # Apply mask with mean color replacement\n", "# img_array = np.array(image).copy()\n", "# mask_array = mask > 0\n", "# mask_3d = np.stack([mask_array] * 3, axis=2)\n", "# mean_values = np.array([0, 0, 0]) # Black mean color\n", "# img_array = np.where(mask_3d, img_array, mean_values.reshape(1, 1, 3))\n", "# masked_image = Image.fromarray(img_array.astype(np.uint8))\n", " \n", "# # Get prediction on masked image\n", "# image_processed = processor(images=masked_image, return_tensors=\"pt\").to(DEVICE)\n", "# pixel_values = image_processed['pixel_values']\n", "# outputs = model(pixel_values=pixel_values, **text_inputs)\n", "# logits = outputs.logits_per_image\n", "# predicted_idx = logits.argmax(dim=-1).item()\n", "# predicted_label = labels[predicted_idx]\n", " \n", "# # Check if prediction is correct\n", "# if predicted_label == true_label:\n", "# correct += 1\n", "# total += 1\n", " \n", "# accuracy = correct / total if total > 0 else 0\n", "# print(f\"Validation Results:\")\n", "# print(f\"Total samples: {total}\")\n", "# print(f\"Correct predictions: {correct}\")\n", "# print(f\"Accuracy: {accuracy:.4f} ({accuracy*100:.2f}%)\")\n", " \n", "# return accuracy\n", "\n", "# # Run validation\n", "# accuracy = validate_masks(masks)\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.9" } }, "nbformat": 4, "nbformat_minor": 5 }