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

Fix input scaling in centered-instance model #2054

Merged
merged 9 commits into from
Dec 20, 2024

Conversation

gitttt-1234
Copy link
Contributor

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

Description

This PR fixes the issue with input scaling in centered-instance model addressed here.

  • We fix the issue with the visualizer pipeline by applying resizing to the make_viz_pipeline.
  • We also resize the images in CentroidCropGroundTruth while running inference on a centered-instance model, instead of resizing the crops in FindInstancePeaks class.

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?

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 resizing functionality to the visualization and inference pipelines, enhancing image preprocessing.
    • Introduced new parameters for better control over image scaling and resizing during inference and training.
    • New JSON configuration files for model training and setup have been added.
  • Bug Fixes

    • Improved error handling during model loading and inference processes.
  • Documentation

    • Updated documentation strings for clarity on new parameters and their usage.
  • Chores

    • Enhanced logging for better feedback during training setup.
    • Added new test functions to validate the functionality of the TopDownPredictor with scaling.
    • Added a new test fixture for model paths in the test suite.

Copy link

coderabbitai bot commented Dec 16, 2024

Walkthrough

The pull request introduces modifications across three key files in the SLEAP neural network pipeline: pipelines.py, inference.py, and training.py. The changes primarily focus on enhancing image preprocessing capabilities, specifically adding resizing transformations and introducing new parameters for input scaling and image resizing. These modifications provide more flexibility in handling image data during visualization, inference, and training processes, with a consistent approach to controlling image preprocessing across different components of the machine learning pipeline.

Changes

File Changes
sleap/nn/data/pipelines.py - Added Resizer transformation to make_viz_pipeline in TopDownMultiClassPipeline and BottomUpMultiClassPipeline
sleap/nn/inference.py - Added input_scale parameter to CentroidCropGroundTruth
- Added resize_img to InferenceLayer.preprocess()
- Added resize_input_image to FindInstancePeaks
- Added precrop_resize to CentroidCrop
- Added resize_input_image to multiple inference layer classes
sleap/nn/training.py - Updated FindInstancePeaks instantiation with input_scale=1.0
- Added resize_input_image=False
- Enhanced create_trainer_using_cli() with additional configuration options
tests/data/models/minimal_instance.UNet.centered_instance_with_scaling/initial_config.json - New configuration file created for model setup
tests/data/models/minimal_instance.UNet.centered_instance_with_scaling/training_config.json - New training configuration file created for model training
tests/fixtures/models.py - Added new fixture min_centered_instance_with_scaling_model_path
tests/nn/test_inference.py - Added new tests for TopDownPredictor with scaling

Suggested labels

MultiView Stack

Poem

🐰 Resizing pixels with rabbit might,
Scaling images, making data just right!
Inference, training, pipelines so neat,
Flexibility now makes our model complete!
Hop along, code, with preprocessing delight! 🖼️


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.

Copy link

codecov bot commented Dec 16, 2024

Codecov Report

Attention: Patch coverage is 82.60870% with 4 lines in your changes missing coverage. Please review.

Project coverage is 76.12%. Comparing base (7991f14) to head (e0f7547).
Report is 96 commits behind head on develop.

Files with missing lines Patch % Lines
sleap/nn/inference.py 80.95% 4 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #2054      +/-   ##
===========================================
+ Coverage    75.43%   76.12%   +0.69%     
===========================================
  Files          134      134              
  Lines        24749    24780      +31     
===========================================
+ Hits         18670    18865     +195     
+ Misses        6079     5915     -164     

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

@gitttt-1234 gitttt-1234 requested review from talmo, eberrigan and roomrys and removed request for talmo and eberrigan December 16, 2024 22:23
Copy link
Collaborator

@roomrys roomrys left a comment

Choose a reason for hiding this comment

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

When testing this in the GUI, we get offset predictions (and the validation images also show that the user-labeled keypoints are outside of the scaled image used in centered-instance - although this could just be a visualization error).

image

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: 4

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between ad03905 and d47c98a.

📒 Files selected for processing (1)
  • sleap/nn/inference.py (20 hunks)
🔇 Additional comments (7)
sleap/nn/inference.py (7)

734-738: Parameter input_scale added correctly to CentroidCropGroundTruth

The input_scale parameter is appropriately added to the __init__ method and initialized, allowing for flexibility in image resizing before passing to the model.

Also applies to: 741-741


2015-2015: Parameter resize_input_image added correctly to FindInstancePeaks

The addition of the resize_input_image parameter provides control over image resizing during preprocessing, enhancing flexibility.

Also applies to: 2028-2028


2126-2126: Pass resize_img parameter correctly in preprocess

The resize_input_image flag is correctly utilized to control resizing within the preprocess method, ensuring images are only resized when intended.


2411-2416: Set input_scale and precrop_resize consistently based on use_gt_centroid

The conditional assignment of input_scale and precrop_resize ensures that image scaling is handled correctly depending on whether ground truth centroids are used.


4052-4052: Use resize_input_image flag consistently in preprocessing

Passing resize_img=self.resize_input_image maintains control over image resizing, and ensures consistency across preprocessing steps.


4301-4301: Correctly set resize_input_image to False

Disabling image resizing at this stage is appropriate for the TopDownMultiClassFindPeaks use case.


4303-4304: Proper assignment of input_scale for ground truth centroids

Setting centroid_crop_layer.input_scale when using ground truth centroids ensures that the images are appropriately scaled during inference.

sleap/nn/inference.py Outdated Show resolved Hide resolved
sleap/nn/inference.py Show resolved Hide resolved
sleap/nn/inference.py Outdated Show resolved Hide resolved
sleap/nn/inference.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@roomrys roomrys left a comment

Choose a reason for hiding this comment

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

I am still seeing the same behavior after pulling the latest commit.
image

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: 0

🧹 Nitpick comments (2)
sleap/nn/inference.py (2)

730-741: Clarify the default param docstring

The docstrings mention the new input_scale parameter but do not explicitly note the default. Adding the default value (e.g., 1.0) in the docstring will help users understand the expected behavior.


3874-3878: Parameter documentation improvement

The docstring for resize_input_image is helpful. Consider referencing any interplay with precrop_resize here for completeness, especially when both might be used in the same pipeline.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between d47c98a and 28ccb70.

📒 Files selected for processing (2)
  • sleap/nn/inference.py (21 hunks)
  • sleap/nn/training.py (2 hunks)
🔇 Additional comments (6)
sleap/nn/training.py (1)

1318-1322: Parameter overrides risk overshadowing user configuration

Here, the code explicitly sets input_scale=1.0 and resize_input_image=False, which may prevent any user-specified values from taking effect. Ensure this is intentional and consistent with the rest of the pipeline configuration.

sleap/nn/inference.py (5)

768-770: Avoid in-place scaling of centroids

Modifying example_gt["centroids"] in place can lead to side effects if these centroids are used elsewhere. This repeats a previous recommendation about copying before scaling.


1834-1839: Ensure consistent handling of precrop_resize

This logic partially duplicates prior feedback about precrop_resize default values and checks (particularly checking for None vs. 1.0). Consider revisiting existing suggestions to unify the default and conditions around precrop_resize across the codebase.


1980-1984: Docstring clarity approved

The added docstrings describing resize_input_image are clear and sufficiently detailed.


2173-2175: Coordinate adjustment seems correct

The additional scaling of peak_points with crop_offsets and 1/self.input_scale appears logically consistent. Ensure that upstream computations rely on this adjusted coordinate system.


3920-3920: No issues detected at this line

No concerns with the newly introduced or modified code on this specific line.

Copy link
Collaborator

@roomrys roomrys left a comment

Choose a reason for hiding this comment

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

It works as expected during my manual tests - it would be nice to have some automated tests as well (which might involve adding a fixture for a trained model with the centered-instance scaled and ensuring that the predictions line up relatively well with the known body part locations).

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

🧹 Nitpick comments (3)
tests/data/models/minimal_instance.UNet.centered_instance_with_scaling/training_config.json (2)

162-164: Consider increasing validation batches for better model evaluation.

The current configuration uses only 1 validation batch (val_batches_per_epoch: 1), which might not provide a representative evaluation of the model's performance.

Consider increasing val_batches_per_epoch to at least 10% of training batches (10 batches in this case).


129-156: Consider enabling data augmentation for better model generalization.

All augmentation options are disabled (false). While this might be intentional for testing, enabling some basic augmentations could improve model robustness.

Consider enabling basic augmentations like rotation and translation:

            "rotate": false,
+           "rotate": true,
            "rotation_min_angle": -180.0,
            "rotation_max_angle": 180.0,
            "translate": false,
+           "translate": true,
tests/nn/test_inference.py (1)

720-720: Fix boolean comparison style

Replace equality comparisons with True with direct boolean expressions for better readability and maintainability.

Apply this diff:

-assert predictor.is_grayscale == True
+assert predictor.is_grayscale

Also applies to: 745-745

🧰 Tools
🪛 Ruff (0.8.2)

720-720: Avoid equality comparisons to True; use if predictor.is_grayscale: for truth checks

Replace with predictor.is_grayscale

(E712)

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between e4a5260 and 53d84f7.

⛔ Files ignored due to path filters (1)
  • tests/data/models/minimal_instance.UNet.centered_instance_with_scaling/training_log.csv is excluded by !**/*.csv
📒 Files selected for processing (5)
  • sleap/nn/inference.py (21 hunks)
  • tests/data/models/minimal_instance.UNet.centered_instance_with_scaling/initial_config.json (1 hunks)
  • tests/data/models/minimal_instance.UNet.centered_instance_with_scaling/training_config.json (1 hunks)
  • tests/fixtures/models.py (1 hunks)
  • tests/nn/test_inference.py (1 hunks)
🧰 Additional context used
🪛 Ruff (0.8.2)
tests/nn/test_inference.py

720-720: Avoid equality comparisons to True; use if predictor.is_grayscale: for truth checks

Replace with predictor.is_grayscale

(E712)


745-745: Avoid equality comparisons to True; use if predictor.is_grayscale: for truth checks

Replace with predictor.is_grayscale

(E712)

🔇 Additional comments (9)
tests/fixtures/models.py (1)

15-18: LGTM! The fixture follows the established pattern.

The new fixture min_centered_instance_with_scaling_model_path is well-implemented and maintains consistency with other fixtures in the file.

tests/data/models/minimal_instance.UNet.centered_instance_with_scaling/initial_config.json (1)

18-24: Verify the input scaling configuration.

The input_scaling parameter is set to 0.5 and resize_and_pad_to_target is enabled, but target_height and target_width are null. This combination might lead to undefined behavior.

Run the following script to check for similar configurations in other models:

✅ Verification successful

Having null target dimensions with resize_and_pad_to_target=true is the expected configuration

Based on the verification results, this is actually the standard configuration pattern in the codebase. We found two model configurations that use resize_and_pad_to_target=true, and both follow the exact same pattern where target_height and target_width are set to null. The input scaling values vary (0.5 and 1.0) but the null target dimensions are consistent, suggesting this is the intended design.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check other model configurations for input scaling patterns
# Look for files that have both input_scaling and resize_and_pad_to_target

fd 'initial_config.json' | xargs rg -l 'input_scaling.*resize_and_pad_to_target' | while read -r file; do
  echo "=== $file ==="
  jq '.data.preprocessing | {input_scaling, resize_and_pad_to_target, target_height, target_width}' "$file"
done

Length of output: 95


Script:

#!/bin/bash
# Let's try a different approach by searching for each field separately
echo "=== Files with input_scaling ==="
fd 'initial_config.json' | xargs jq -r 'select(.data.preprocessing.input_scaling != null) | .data.preprocessing.input_scaling'

echo -e "\n=== Files with resize_and_pad_to_target ==="
fd 'initial_config.json' | xargs jq -r 'select(.data.preprocessing.resize_and_pad_to_target == true) | .data.preprocessing.resize_and_pad_to_target'

echo -e "\n=== Files with target dimensions ==="
fd 'initial_config.json' | xargs jq -r 'select(.data.preprocessing.target_height != null or .data.preprocessing.target_width != null) | .data.preprocessing | {target_height, target_width}'

Length of output: 761


Script:

#!/bin/bash
# Let's get the full context of configurations that have resize_and_pad_to_target=true
echo "=== Full preprocessing configs with resize_and_pad_to_target=true ==="
fd 'initial_config.json' | while read -r file; do
    echo "=== $file ==="
    jq -r 'select(.data.preprocessing.resize_and_pad_to_target == true) | .data.preprocessing' "$file"
done

Length of output: 3162

tests/data/models/minimal_instance.UNet.centered_instance_with_scaling/training_config.json (1)

74-83: LGTM! Input scaling configuration is properly set.

The preprocessing configuration correctly specifies:

  • Input scaling factor: 0.5
  • Target dimensions: 384x384
  • Padding stride: 1
tests/nn/test_inference.py (2)

708-730: LGTM! Test coverage for TopDownPredictor with scaling

The test function properly validates the behavior of TopDownPredictor with scaling by:

  • Initializing the predictor with a centered instance model that has scaling
  • Verifying the grayscale property
  • Checking the number of instances and their points match expectations
  • Validating point coordinates are within tolerance
🧰 Tools
🪛 Ruff (0.8.2)

720-720: Avoid equality comparisons to True; use if predictor.is_grayscale: for truth checks

Replace with predictor.is_grayscale

(E712)


732-755: LGTM! Test coverage for TopDownPredictor with both models and scaling

The test function thoroughly validates the behavior of TopDownPredictor when using both centroid and centered instance models with scaling by:

  • Initializing the predictor with both models
  • Verifying the grayscale property
  • Checking the number of instances and their points match expectations
  • Validating point coordinates are within tolerance
🧰 Tools
🪛 Ruff (0.8.2)

745-745: Avoid equality comparisons to True; use if predictor.is_grayscale: for truth checks

Replace with predictor.is_grayscale

(E712)

sleap/nn/inference.py (4)

730-731: LGTM! Input scaling support in CentroidCropGroundTruth

The implementation correctly adds input scaling support to CentroidCropGroundTruth by:

  • Adding clear documentation for the input_scale parameter
  • Adding input_scale parameter with default value of 1.0
  • Implementing scaling functionality that resizes images and adjusts centroids

Also applies to: 734-741, 768-770


1652-1654: LGTM! Pre-crop resizing support in CentroidCrop

The implementation correctly adds pre-crop resizing support to CentroidCrop by:

  • Adding clear documentation for the precrop_resize parameter
  • Adding precrop_resize parameter with default value of 1.0
  • Implementing resizing functionality that resizes full images and adjusts centroid points

Also applies to: 1695-1695, 1716-1716, 1835-1841


1982-1986: LGTM! Resize control in FindInstancePeaks

The implementation correctly adds resize control to FindInstancePeaks by:

  • Adding clear documentation for the resize_input_image parameter
  • Adding resize_input_image parameter with default value of True
  • Modifying preprocess call to respect resize_input_image flag
  • Adjusting peak points correctly based on input scale

Also applies to: 2017-2017, 2030-2030, 2128-2128, 2175-2177


3877-3881: LGTM! Resize control in TopDownMultiClassFindPeaks

The implementation correctly adds resize control to TopDownMultiClassFindPeaks by:

  • Adding clear documentation for the resize_input_image parameter
  • Adding resize_input_image parameter with default value of True
  • Modifying preprocess call to respect resize_input_image flag
  • Setting resize_input_image to False and configuring input_scale correctly when used with CentroidCropGroundTruth

Also applies to: 3923-3923, 3939-3939, 4057-4057, 4306-4306, 4308-4309

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

🧹 Nitpick comments (4)
tests/data/models/minimal_instance.UNet.centered_instance_with_scaling/training_config.json (4)

93-102: Consider model capacity and precision trade-offs.

The current configuration has some notable characteristics that might affect model performance:

  1. Small initial filters (16) with 1.5x growth rate
  2. Aggressive output stride of 2
  3. Offset refinement enabled

While these settings might be suitable for a minimal test configuration, they could impact model capacity and precision in production scenarios.

Consider adjusting these parameters if you need better precision or model capacity in production.

Also applies to: 110-120


130-155: Consider enabling data augmentations for better model robustness.

All data augmentations (rotation, translation, scale, noise, etc.) are currently disabled. While this might be intentional for testing, enabling appropriate augmentations could help:

  1. Improve model robustness
  2. Prevent overfitting
  3. Handle variations in input data better

Consider enabling relevant augmentations based on your use case characteristics.


196-213: Consider enabling debugging features during development.

Several debugging features are disabled:

  • save_visualizations: false
  • tensorboard.write_logs: false
  • tensorboard.architecture_graph: false

Enabling these during development could help diagnose potential issues with the input scaling fix.


222-223: Add descriptive metadata.

The name and description fields are empty. Consider adding:

  • A descriptive name indicating this is a minimal instance configuration with scaling
  • A description explaining the purpose and expected usage of this configuration
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 53d84f7 and 065686a.

📒 Files selected for processing (1)
  • tests/data/models/minimal_instance.UNet.centered_instance_with_scaling/training_config.json (1 hunks)
🔇 Additional comments (1)
tests/data/models/minimal_instance.UNet.centered_instance_with_scaling/training_config.json (1)

78-82: Verify input scaling and resize configuration.

The configuration shows both input scaling (0.5) and resize_and_pad_to_target (true) being used together. This combination needs careful consideration:

  1. Input images will first be scaled by 0.5
  2. Then resized to 384x384

Please verify that this is the intended behavior for fixing the input scaling issue mentioned in #1993.

"name": "",
"description": "",
"sleap_version": "1.4.1",
"filename": "models\\minimal_instance.UNet.centered_instance_with_scaling\\training_config.json"
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Replace Windows path separator with forward slashes.

The filename uses Windows-style backslashes which could cause issues on other platforms.

-    "filename": "models\\minimal_instance.UNet.centered_instance_with_scaling\\training_config.json"
+    "filename": "models/minimal_instance.UNet.centered_instance_with_scaling/training_config.json"
📝 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
"filename": "models\\minimal_instance.UNet.centered_instance_with_scaling\\training_config.json"
"filename": "models/minimal_instance.UNet.centered_instance_with_scaling/training_config.json"

Copy link
Collaborator

@roomrys roomrys left a comment

Choose a reason for hiding this comment

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

There are some files in here that I doubt we will ever use, but other than that I think it looks good!

Copy link
Collaborator

Choose a reason for hiding this comment

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

We don't necessarily need this file

@gitttt-1234 gitttt-1234 merged commit 7785f66 into develop Dec 20, 2024
5 checks passed
@gitttt-1234 gitttt-1234 deleted the divya/fix-scaling-topdown branch December 20, 2024 18:16
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