Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@gitttt-1234
Copy link
Collaborator

@gitttt-1234 gitttt-1234 commented Dec 13, 2024

Currently, we pass the actual crop_size to generate the initial crops. However, we need to crop extra to avoid blacking of edges and re-crop after augmentation is applied. This PR fixes the crop size.

Summary by CodeRabbit

  • Improvements
    • Enhanced dataset cropping mechanism to better support image rotation augmentation
    • Adjusted crop generation to provide more flexible image sampling

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 13, 2024

Walkthrough

The pull request modifies the _fill_cache method in the CenteredInstanceDataset class within the sleap_nn/data/custom_datasets.py file. The key change involves adjusting the crop generation process by introducing a dynamic crop size calculation. Instead of using a fixed self.crop_hw, the new implementation calculates the crop size by multiplying the original crop dimensions by the square root of 2, potentially providing more space for rotation augmentation during data preparation.

Changes

File Change Summary
sleap_nn/data/custom_datasets.py Modified _fill_cache method to dynamically calculate crop size using np.array(self.crop_hw) * np.sqrt(2)

Sequence Diagram

sequenceDiagram
    participant Dataset as CenteredInstanceDataset
    participant Crop as generate_crops()
    
    Dataset->>Dataset: Calculate crop_size
    Dataset->>Crop: Call with image, instance, centroid, crop_size
    Crop-->>Dataset: Return cropped image
Loading

Possibly related PRs

Suggested reviewers

  • talmo

Poem

🥕 Crop circles dance with might,
Squared root of two takes flight!
Augmentation's playful spin,
Datasets twirl, a rabbit's grin 🐰
Dimensions stretch, dimensions grow,
In code's embrace, new patterns flow!

Finishing Touches

  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@codecov
Copy link

codecov bot commented Dec 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.35%. Comparing base (f093ce2) to head (7ddbe70).
Report is 33 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #127      +/-   ##
==========================================
+ Coverage   96.64%   97.35%   +0.71%     
==========================================
  Files          23       39      +16     
  Lines        1818     4050    +2232     
==========================================
+ Hits         1757     3943    +2186     
- Misses         61      107      +46     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@gitttt-1234 gitttt-1234 requested a review from talmo January 3, 2025 21:27
@gitttt-1234 gitttt-1234 force-pushed the divya/add-group-wandb branch from 0e6b97c to 914af8d Compare January 15, 2025 01:37
@gitttt-1234 gitttt-1234 changed the base branch from divya/add-group-wandb to main January 15, 2025 06:39
@gitttt-1234 gitttt-1234 changed the base branch from main to divya/add-group-wandb January 15, 2025 06:40
@gitttt-1234 gitttt-1234 changed the base branch from divya/add-group-wandb to main January 15, 2025 06:41
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (3)
sleap_nn/data/custom_datasets.py (3)

383-384: Add bounds checking for crop dimensions.

While the mathematical approach is correct, the code should verify that the enlarged crop dimensions don't exceed the image boundaries.

 crop_size = np.array(self.crop_hw) * np.sqrt(2)  # crop extra for rotation augmentation
 crop_size = crop_size.astype(np.int32).tolist()
+# Ensure crop size doesn't exceed image dimensions
+image_height, image_width = image.shape[-2:]
+crop_size = [min(crop_size[0], image_height), min(crop_size[1], image_width)]

383-383: Enhance the comment to better explain the cropping strategy.

While the current comment mentions rotation augmentation, it would be helpful to provide more context about the two-step cropping process.

-crop_size = np.array(self.crop_hw) * np.sqrt(2)  # crop extra for rotation augmentation
+# Use sqrt(2) factor to create a larger initial crop that prevents black edges
+# during rotation augmentation. This larger crop will be re-cropped to the
+# original size after augmentation is applied.
+crop_size = np.array(self.crop_hw) * np.sqrt(2)

383-384: Consider caching the crop size calculation.

Since self.crop_hw is constant, consider calculating and storing crop_size during initialization to avoid repeated calculations.

 def __init__(
     self,
     labels: sio.Labels,
     data_config: DictConfig,
     crop_hw: Tuple[int],
     ...
 ) -> None:
     ...
     self.crop_hw = crop_hw
+    # Pre-calculate the enlarged crop size for rotation augmentation
+    self._augmentation_crop_size = (np.array(self.crop_hw) * np.sqrt(2)).astype(np.int32).tolist()

Then in _fill_cache:

-crop_size = np.array(self.crop_hw) * np.sqrt(2)  # crop extra for rotation augmentation
-crop_size = crop_size.astype(np.int32).tolist()
+crop_size = self._augmentation_crop_size
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 06fca76 and 7ddbe70.

📒 Files selected for processing (1)
  • sleap_nn/data/custom_datasets.py (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Tests (macos-14, Python 3.9)
  • GitHub Check: Tests (windows-latest, Python 3.9)
  • GitHub Check: Tests (ubuntu-latest, Python 3.9)
🔇 Additional comments (1)
sleap_nn/data/custom_datasets.py (1)

384-384: LGTM! Mathematically sound approach for rotation augmentation.

The use of sqrt(2) for the initial crop size is correct, as it provides sufficient padding to prevent black edges during rotation augmentation.

@gitttt-1234 gitttt-1234 merged commit f0efe52 into main Jan 15, 2025
7 checks passed
@gitttt-1234 gitttt-1234 deleted the divya/fix-crop-size branch January 15, 2025 07:17
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