-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add foundational search utilities and extend bitmask primitives #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dhalmazna
wants to merge
2
commits into
feat/segment-and-hyperpixel-scoring
Choose a base branch
from
feat/algorithm-utils
base: feat/segment-and-hyperpixel-scoring
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,29 @@ | ||
| """CIAO algorithm implementations.""" | ||
|
|
||
| from ciao.algorithm.bitmask_graph import get_frontier, iter_bits | ||
| from ciao.algorithm.bitmask_graph import ( | ||
| add_node, | ||
| get_frontier, | ||
| has_node, | ||
| iter_bits, | ||
| mask_to_ids, | ||
| pick_random_set_bit, | ||
| remove_node, | ||
| sample_connected_superset, | ||
| ) | ||
| from ciao.algorithm.search_helpers import evaluate_masks, is_terminal | ||
|
|
||
|
|
||
| __all__ = [ | ||
| # Bitmask graph utilities | ||
| "add_node", | ||
| # Shared MCTS/MCGS utilities | ||
| "evaluate_masks", | ||
| "get_frontier", | ||
| "has_node", | ||
| "is_terminal", | ||
| "iter_bits", | ||
| "mask_to_ids", | ||
| "pick_random_set_bit", | ||
| "remove_node", | ||
| "sample_connected_superset", | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| """Shared utilities for MCTS and MCGS search algorithms. | ||
|
|
||
| This module contains common functions used by both Monte Carlo Tree Search (MCTS) | ||
| and Monte Carlo Graph Search (MCGS) implementations. | ||
| """ | ||
|
|
||
| import numpy as np | ||
| import torch | ||
|
|
||
| from ciao.algorithm.bitmask_graph import get_frontier, mask_to_ids | ||
| from ciao.model.predictor import ModelPredictor | ||
| from ciao.scoring.hyperpixel import calculate_hyperpixel_deltas | ||
|
|
||
|
|
||
| def is_terminal( | ||
| mask: int, adj_masks: tuple[int, ...], used_mask: int, max_depth: int | ||
| ) -> bool: | ||
| """Check if state is terminal (max depth or no frontier).""" | ||
| return ( | ||
| mask.bit_count() >= max_depth or get_frontier(mask, adj_masks, used_mask) == 0 | ||
| ) | ||
|
|
||
|
|
||
| def evaluate_masks( | ||
| predictor: ModelPredictor, | ||
| input_batch: torch.Tensor, | ||
| segments: np.ndarray, | ||
| target_class_idx: int, | ||
| masks: list[int], | ||
| replacement_image: torch.Tensor, | ||
| ) -> list[float]: | ||
| """Evaluate multiple segment masks by computing class score deltas (batched).""" | ||
| # Guard against invalid masks (zero or negative) | ||
| if any(mask <= 0 for mask in masks): | ||
| raise ValueError( | ||
| "Cannot evaluate invalid mask: A mask must be a positive integer. " | ||
| "Zero masks contain no segments, and negative masks cause " | ||
| "incorrect bit iteration due to two's complement representation." | ||
| ) | ||
|
|
||
| all_segment_ids = [mask_to_ids(mask) for mask in masks] | ||
|
|
||
| rewards = calculate_hyperpixel_deltas( | ||
| predictor=predictor, | ||
| input_batch=input_batch, | ||
| segments=segments, | ||
| replacement_image=replacement_image, | ||
| target_class_idx=target_class_idx, | ||
| hyperpixel_segment_ids_list=all_segment_ids, | ||
| ) | ||
|
|
||
| return rewards |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.