forked from FLAIROx/jafar
-
Notifications
You must be signed in to change notification settings - Fork 11
feat: add darkness filtering capability to dataloader #150
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
maharajamihir
wants to merge
3
commits into
main
Choose a base branch
from
feat/darkness-filter
base: main
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
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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
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 | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |||||||||||||||||||||||
| from typing import Any | ||||||||||||||||||||||||
| import pickle | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| RGB_TO_GRAYSCALE_WEIGHTS = np.array([0.2989, 0.5870, 0.1140]) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| class EpisodeLengthFilter(grain.transforms.Filter): | ||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||
|
|
@@ -96,13 +97,46 @@ def random_map(self, element: dict, rng: np.random.Generator) -> Any: | |||||||||||||||||||||||
| return seq | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| class DarknessFilter(grain.transforms.Filter): | ||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||
| A Grain Filter that filters out sequences with images that are too dark. | ||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def __init__(self, darkness_threshold: float): | ||||||||||||||||||||||||
| """Initializes the filter with darkness threshold.""" | ||||||||||||||||||||||||
| self.darkness_threshold = darkness_threshold | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def filter(self, element: Any) -> bool: | ||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||
| Filters sequences based on darkness. | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Args: | ||||||||||||||||||||||||
| element: A NumPy array representing a processed video sequence. | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Returns: | ||||||||||||||||||||||||
| True if the sequence is not too dark, False otherwise. | ||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||
| # Convert the RGB image to grayscale using numpy | ||||||||||||||||||||||||
| element_greyscale = np.dot(element[...,:3], RGB_TO_GRAYSCALE_WEIGHTS) | ||||||||||||||||||||||||
| average_brightness = np.mean(element_greyscale) | ||||||||||||||||||||||||
| if average_brightness < self.darkness_threshold: | ||||||||||||||||||||||||
| print( | ||||||||||||||||||||||||
| f"Filtering out sequence with average brightness {average_brightness}, " | ||||||||||||||||||||||||
| f"which is below the darkness threshold {self.darkness_threshold}." | ||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||
| return False | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| return True | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def get_dataloader( | ||||||||||||||||||||||||
| array_record_paths: list[str], | ||||||||||||||||||||||||
| seq_len: int, | ||||||||||||||||||||||||
| global_batch_size: int, | ||||||||||||||||||||||||
| image_h: int, | ||||||||||||||||||||||||
| image_w: int, | ||||||||||||||||||||||||
| image_c: int, | ||||||||||||||||||||||||
| darkness_threshold: float = 0., | ||||||||||||||||||||||||
| num_workers: int = 1, | ||||||||||||||||||||||||
| prefetch_buffer_size: int = 1, | ||||||||||||||||||||||||
| seed: int = 42, | ||||||||||||||||||||||||
|
|
@@ -139,6 +173,9 @@ def get_dataloader( | |||||||||||||||||||||||
| ProcessEpisodeAndSlice( | ||||||||||||||||||||||||
| seq_len=seq_len, image_h=image_h, image_w=image_w, image_c=image_c | ||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||
| DarknessFilter( | ||||||||||||||||||||||||
| darkness_threshold=darkness_threshold | ||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||
| grain.transforms.Batch(batch_size=per_process_batch_size, drop_remainder=True), | ||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
| ] | |
| ] | |
| if darkness_threshold > 0.0: | |
| operations.append( | |
| DarknessFilter( | |
| darkness_threshold=darkness_threshold | |
| ) | |
| ) | |
| operations.append( | |
| grain.transforms.Batch(batch_size=per_process_batch_size, drop_remainder=True) | |
| ) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using print() statements for logging in production code is not recommended. Consider using a proper logging framework like Python's logging module to allow for better control over log levels and output destinations.