-
Couldn't load subscription status.
- Fork 6
Fix crop size #127
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
Fix crop size #127
Conversation
WalkthroughThe pull request modifies the Changes
Sequence DiagramsequenceDiagram
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
Possibly related PRs
Suggested reviewers
Poem
Finishing Touches
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
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. |
0e6b97c to
914af8d
Compare
22a37c9 to
7ddbe70
Compare
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.
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_hwis constant, consider calculating and storingcrop_sizeduring 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
📒 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.
Currently, we pass the actual
crop_sizeto 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