Skip to content
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

Filter OOB points when training #2060

Closed
wants to merge 4 commits into from
Closed

Conversation

gitttt-1234
Copy link
Contributor

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

Description

This PR addresses #1901, to remove OOB/ negative points while creating the dataset for training.

Types of changes

  • Bugfix
  • New feature
  • Refactor / Code style update (no logical changes)
  • Build / CI changes
  • Documentation Update
  • Other (explain)

Does this address any currently open issues?

#1901

Outside contributors checklist

  • Review the guidelines for contributing to this repository
  • Read and sign the CLA and add yourself to the authors list
  • Make sure you are making a pull request against the develop branch (not main). Also you should start your branch off develop
  • Add tests that prove your fix is effective or that your feature works
  • Add necessary documentation (if appropriate)

Thank you for contributing to SLEAP!

❤️

Summary by CodeRabbit

  • New Features

    • Added new configuration options for documentation settings.
    • Introduced new transformers for data processing in visualization and training pipelines.
    • Enhanced instance handling in dataset creation.
    • Added parameters for image resizing in inference and training processes.
    • Improved visualization capabilities with new callbacks for model training.
  • Bug Fixes

    • Refined error handling for data processing in pipelines.
  • Documentation

    • Updated configuration settings in documentation files.

Copy link

coderabbitai bot commented Dec 17, 2024

Walkthrough

This pull request introduces several modifications across multiple files in the SLEAP neural network data processing and training modules. The changes primarily focus on enhancing image processing, resizing, and visualization capabilities. Key updates include adding new parameters for image resizing in inference and training classes, improving instance handling in data providers, and refining visualization methods for different model trainers. The modifications aim to provide more flexibility in how input images are processed and how model performance is monitored during training.

Changes

File Changes
docs/conf.py - Added myst_number_code_blocks for Python code blocks
- Updated CSS file path quotation marks
- Added sphinx_tabs_disable_css_loading option
sleap/nn/data/pipelines.py - Added Resizer transformer to visualization pipelines
- Updated make_training_pipeline methods with new transformers
- Refined error handling in pipeline creation
sleap/nn/data/providers.py - Enhanced LabelsReader to filter and handle instance coordinates
- Improved data integrity when creating datasets
sleap/nn/inference.py - Added input_scale and resize_input_image parameters
- Modified preprocessing and peak finding methods
- Updated model export functionality
sleap/nn/training.py - Added resize_input_image parameter to peak finding
- Updated visualization methods
- Introduced new visualization callbacks

Sequence Diagram

sequenceDiagram
    participant DataProvider
    participant Pipeline
    participant Resizer
    participant Transformer
    participant Trainer
    
    DataProvider->>Pipeline: Provide data
    Pipeline->>Resizer: Apply image resizing
    Resizer-->>Pipeline: Resized image
    Pipeline->>Transformer: Process instances
    Transformer-->>Pipeline: Processed data
    Pipeline->>Trainer: Feed training data
    Trainer->>Trainer: Visualize and train
Loading

Poem

🐰 A Rabbit's Ode to Code Refinement 🐰

Pixels dance, resize with grace,
Transformers weave their magic trace,
Inference leaps with newfound might,
Training models, a coder's delight!
Code evolves, one change at a time. 🚀

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. We would love to hear your feedback on Discord.


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.

@gitttt-1234 gitttt-1234 deleted the divya/filter-oob branch December 17, 2024 16:46
Copy link

@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: 1

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 7dd41de and 0ca84bc.

📒 Files selected for processing (5)
  • docs/conf.py (1 hunks)
  • sleap/nn/data/pipelines.py (2 hunks)
  • sleap/nn/data/providers.py (2 hunks)
  • sleap/nn/inference.py (15 hunks)
  • sleap/nn/training.py (2 hunks)
✅ Files skipped from review due to trivial changes (1)
  • docs/conf.py
🔇 Additional comments (12)
sleap/nn/training.py (2)

1322-1322: Set resize_input_image=False in FindInstancePeaks initialization

Setting resize_input_image=False ensures that the input images are not resized during peak finding, which maintains the original image scale and is appropriate for this context.


1763-1763: Set resize_input_image=False in FindInstancePeaks initialization

Setting resize_input_image=False ensures that the input images are not resized during peak finding, aligning with the expected behavior in this trainer.

sleap/nn/inference.py (7)

734-738: Add input_scale parameter to CentroidCropGroundTruth

The introduction of the input_scale parameter allows for resizing images and centroids during centroid cropping, providing flexibility to handle different input scales.


769-771: Scale images and centroids based on input_scale

By resizing images and scaling centroids according to input_scale, consistency between image sizes and centroid coordinates is maintained.


940-940: Introduce resize_img parameter in InferenceLayer.preprocess

Adding the resize_img parameter allows control over image resizing during preprocessing, enhancing flexibility in handling resized inputs.

Also applies to: 961-961


1968-1970: Add resize_input_image parameter to FindInstancePeaks

Including the resize_input_image parameter provides control over whether crops should be resized during peak finding, enhancing flexibility in image processing.

Also applies to: 2016-2016


2114-2114: Use resize_input_image in FindInstancePeaks.call method

Passing resize_img=self.resize_input_image to the preprocess method ensures consistent resizing behavior based on the initialization parameter.


3901-3904: Add resize_input_image parameter to TopDownMultiClassFindPeaks

Adding the resize_input_image parameter allows for control over image resizing during peak finding in multi-class top-down models, improving flexibility.

Also applies to: 3917-3917


4035-4035: Pass resize_input_image to preprocess in TopDownMultiClassFindPeaks.call

Using resize_img=self.resize_input_image in the preprocess call ensures that the resizing behavior aligns with the specified parameter.

sleap/nn/data/providers.py (1)

4-4: Import Instance class from sleap.instance

The import of Instance is necessary for creating new instances after filtering invalid points.

Also applies to: 9-9

sleap/nn/data/pipelines.py (2)

778-778: Add Resizer to visualization pipeline in TopdownConfmapsPipeline

Including the Resizer transformer ensures that images are appropriately resized during visualization, matching the preprocessing steps used during training.


1254-1254: Add Resizer to visualization pipeline in TopDownMultiClassPipeline

Adding the Resizer transformer ensures consistent image resizing during visualization, aligning with the training preprocessing steps.

Comment on lines +202 to +218
height, width = raw_image_size

instances = []
for instance in lf.instances:
pts = instance.numpy()
# negative coords
pts[pts < 0] = np.NaN

# coordinates outside img frame
pts[:, 0][pts[:, 0] > height - 1] = np.NaN
pts[:, 1][pts[:, 1] > width - 1] = np.NaN

# remove all nans
pts = pts[~np.isnan(pts).any(axis=1), :]

instances.append(Instance.from_numpy(pts, lf.skeleton, lf.track))
lf.instances = instances
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Ensure instances have valid points before adding to the dataset

After filtering out invalid coordinates, instances may have no valid points remaining. Adding such instances could lead to issues downstream. Please check if pts contains at least one valid point before creating the Instance.

Apply this diff to verify that instances have valid points:

 for instance in lf.instances:
     pts = instance.numpy()
     # negative coords
     pts[pts < 0] = np.NaN
     # coordinates outside img frame
     pts[:, 0][pts[:, 0] > height - 1] = np.NaN
     pts[:, 1][pts[:, 1] > width - 1] = np.NaN
     # remove all nans
     pts = pts[~np.isnan(pts).any(axis=1), :]
+    if len(pts) > 0:
         instances.append(Instance.from_numpy(pts, lf.skeleton, lf.track))
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
height, width = raw_image_size
instances = []
for instance in lf.instances:
pts = instance.numpy()
# negative coords
pts[pts < 0] = np.NaN
# coordinates outside img frame
pts[:, 0][pts[:, 0] > height - 1] = np.NaN
pts[:, 1][pts[:, 1] > width - 1] = np.NaN
# remove all nans
pts = pts[~np.isnan(pts).any(axis=1), :]
instances.append(Instance.from_numpy(pts, lf.skeleton, lf.track))
lf.instances = instances
height, width = raw_image_size
instances = []
for instance in lf.instances:
pts = instance.numpy()
# negative coords
pts[pts < 0] = np.NaN
# coordinates outside img frame
pts[:, 0][pts[:, 0] > height - 1] = np.NaN
pts[:, 1][pts[:, 1] > width - 1] = np.NaN
# remove all nans
pts = pts[~np.isnan(pts).any(axis=1), :]
if len(pts) > 0:
instances.append(Instance.from_numpy(pts, lf.skeleton, lf.track))
lf.instances = instances

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.

1 participant