Skip to content

feat: implement scoring module for segments#7

Open
dhalmazna wants to merge 6 commits intofeat/model-predictorfrom
feat/segment-and-hyperpixel-scoring
Open

feat: implement scoring module for segments#7
dhalmazna wants to merge 6 commits intofeat/model-predictorfrom
feat/segment-and-hyperpixel-scoring

Conversation

@dhalmazna
Copy link
Collaborator

Context:
This PR introduces the scoring/ module, which provides the core mechanisms to evaluate the importance of individual image segments during the explainer's execution. It also establishes the foundation for selecting the initial starting points (seeds) for the main search algorithms (creation of the surrogate dataset).

What was added:

  • scoring/segments.py: Contains the logic for scoring base segments (create_surrogate_dataset, calculate_segment_scores). It evaluates each individual segment by masking its local neighborhood and measuring the model's confidence drop. These importance scores are crucial for selecting the initial "seeds" from which the main algorithms (like MCTS or MCGS) will grow.
  • scoring/hyperpixel.py: Handles the evaluation of discovered hyperpixels (calculating deltas and selecting the top-k most important regions).
  • algorithm/bitmask_graph.py: Introduces highly efficient, low-level bitwise graph utilities (get_frontier, iter_bits). These primitives enable fast, memory-efficient BFS traversal during the surrogate dataset creation without relying on heavy external graph libraries. More bitwise utilities will be added in the next PR, these are just the ones necessary for the create_surrogate_dataset function to work.

Related Task:
XAI-29

@dhalmazna dhalmazna self-assigned this Mar 13, 2026
Copilot AI review requested due to automatic review settings March 13, 2026 15:05
@gemini-code-assist
Copy link

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request establishes the foundational scoring mechanisms for the explainer, enabling the evaluation of individual image segments and hyperpixels. By introducing a dedicated scoring module and efficient bitmask-based graph utilities, it provides the core logic for determining segment importance and selecting initial starting points for subsequent search algorithms, thereby enhancing the interpretability capabilities of the system.

Highlights

  • New Scoring Module: Introduced a new scoring/ module to house functionalities for evaluating the importance of image segments and hyperpixels, replacing the previous evaluation/ directory.
  • Segment Scoring and Surrogate Dataset Creation: Implemented scoring/segments.py which includes logic for creating a surrogate dataset by masking local neighborhoods of segments and calculating their importance scores based on model confidence drops. These scores are crucial for selecting initial 'seeds' for main search algorithms.
  • Hyperpixel Evaluation: Added scoring/hyperpixel.py to handle the evaluation of discovered hyperpixels, including calculating deltas and selecting the top-k most important regions.
  • Efficient Bitmask Graph Utilities: Integrated algorithm/bitmask_graph.py with highly efficient, low-level bitwise graph utilities (get_frontier, iter_bits) to enable fast and memory-efficient BFS traversal during surrogate dataset creation, avoiding heavy external graph libraries.
Changelog
  • README.md
    • Refactored evaluation/ directory to scoring/ in the project structure documentation.
    • Updated file paths for surrogate.py to segments.py and hyperpixel.py within the new scoring/ module.
  • ciao/algorithm/init.py
    • Added get_frontier and iter_bits from bitmask_graph to the module's public API.
  • ciao/algorithm/bitmask_graph.py
    • Introduced iter_bits function for efficient iteration over set bits in an integer mask.
    • Implemented get_frontier function to compute the expansion frontier for graph traversal using bitmasks.
  • ciao/scoring/init.py
    • Added imports for calculate_hyperpixel_deltas, select_top_hyperpixels from hyperpixel.py.
    • Added imports for calculate_segment_scores, create_surrogate_dataset from segments.py.
    • Defined __all__ to expose the new scoring functions publicly.
  • ciao/scoring/hyperpixel.py
    • Implemented calculate_hyperpixel_deltas to compute masking deltas for hyperpixel candidates using batched inference.
    • Added select_top_hyperpixels function to sort and select the most important hyperpixels based on their scores.
  • ciao/scoring/segments.py
    • Implemented create_surrogate_dataset to generate a dataset of masked image samples and their corresponding model confidence drops using a BFS-like approach with bitmasks.
    • Added calculate_segment_scores to derive neighborhood-smoothed importance scores for segments from the generated surrogate dataset.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new scoring module, which is a core component for evaluating image segment importance. The implementation is well-structured and includes efficient bitmask-based graph utilities. My review focuses on a couple of performance optimizations. Specifically, I've suggested removing some expensive logging operations that trigger redundant model forward passes and vectorizing a loop that applies masks to an image batch to better leverage PyTorch's capabilities. Overall, the changes are a solid addition to the project.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Introduces a new scoring/ module to quantify segment and hyperpixel importance for CIAO’s explainer workflow, plus bitmask-based graph traversal utilities to support efficient neighborhood masking.

Changes:

  • Added surrogate dataset generation and per-segment score aggregation (ciao/scoring/segments.py).
  • Added batched masking delta computation and top-k hyperpixel selection helpers (ciao/scoring/hyperpixel.py).
  • Added bitmask graph primitives for fast neighborhood/frontier expansion (ciao/algorithm/bitmask_graph.py) and exported new modules via package __init__.py files; updated README tree.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
ciao/scoring/segments.py Builds local neighborhood masking groups and produces a surrogate dataset + segment score aggregation.
ciao/scoring/hyperpixel.py Computes masking deltas for candidate hyperpixels and provides a top-k selection helper.
ciao/scoring/init.py Exposes scoring utilities as package-level exports.
ciao/algorithm/bitmask_graph.py Adds iter_bits and get_frontier utilities for bitmask-based traversal.
ciao/algorithm/init.py Exposes algorithm utilities as package-level exports.
README.md Updates module layout documentation to reflect the new scoring/ package.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +98 to +107
def select_top_hyperpixels(
hyperpixels: list[dict[str, object]], max_hyperpixels: int = 10
) -> list[dict[str, object]]:
"""Select top hyperpixels by their primary algorithm-specific score."""
# Use hyperpixel_score
return sorted(
hyperpixels,
key=lambda hp: abs(hp.get("hyperpixel_score", 0)), # type: ignore[arg-type]
reverse=True,
)[:max_hyperpixels]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually not a bad idea, but I feel like this would be a too complex solution for now. We can decide on it later after the reviewers will see how the hyperpixels dict looks like in different algorithms.

@coderabbitai
Copy link

coderabbitai bot commented Mar 13, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 00e597b0-294b-48b9-96bb-39b467a7471d

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/segment-and-hyperpixel-scoring
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants