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

Add trainer #29

Merged
merged 27 commits into from
Dec 22, 2023
Merged

Add trainer #29

merged 27 commits into from
Dec 22, 2023

Conversation

gitttt-1234
Copy link
Collaborator

@gitttt-1234 gitttt-1234 commented Dec 5, 2023

Issue #27

Summary by CodeRabbit

  • New Features

    • Enhanced the peak finding algorithm to better identify peak points in data.
    • Introduced a ModelTrainer class for streamlined model training using PyTorch Lightning.
    • Added comprehensive documentation for configuring the model training process.
  • Bug Fixes

    • Improved handling of values below the threshold in peak detection by using NaN and zero replacements.
  • Documentation

    • Provided a new config.md file detailing the setup for model training configurations.
  • Tests

    • Implemented test cases for the new ModelTrainer and TopDownCenteredInstanceModel functionalities.
    • Added a new test configuration fixture to aid in robust testing scenarios.

Copy link
Contributor

coderabbitai bot commented Dec 5, 2023

Walkthrough

The changes involve enhancing the peak-finding algorithm in sleap_nn by refining the peak detection logic and handling of sub-threshold values. A new ModelTrainer class has been introduced for training neural network models using PyTorch Lightning, complete with data loaders, logging, and weight initialization. Documentation has been added for the configuration of model training. Additionally, test cases and fixtures have been created to ensure the functionality of the new training classes and configurations.

Changes

File Path Change Summary
sleap_nn/inference/peak_finding.py Modified logic for peak detection and handling of sub-threshold values.
sleap_nn/model_trainer.py Introduced ModelTrainer class and TopDownCenteredInstanceModel for PyTorch Lightning training.
docs/config.md Added documentation for training configuration.
tests/test_model_trainer.py, tests/fixtures/datasets.py Introduced test cases for new classes and added fixture for testing.

🐇✨
In the code's warren, deep and vast,
A rabbit hopped and changed the past.
Peaks found anew, models trained with zest,
Tests ensure that all's the best.
🌟📈

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 X ?


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • You can directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
  • You can tag CodeRabbit on specific lines of code or entire files in the PR by tagging @coderabbitai in a comment. Examples:
  • You can tag @coderabbitai in a PR comment and ask questions about the PR and the codebase. Use quoted replies to pass the context for follow-up questions. Examples:
    • @coderabbitai render interesting statistics about this repository as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai generate unit tests for the src/utils.ts file.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link

codecov bot commented Dec 5, 2023

Codecov Report

Attention: 5 lines in your changes are missing coverage. Please review.

Comparison is base (c590ae5) 99.78% compared to head (33e27d3) 99.35%.
Report is 4 commits behind head on main.

Files Patch % Lines
sleap_nn/model_trainer.py 96.29% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #29      +/-   ##
==========================================
- Coverage   99.78%   99.35%   -0.44%     
==========================================
  Files          19       20       +1     
  Lines         949     1087     +138     
==========================================
+ Hits          947     1080     +133     
- Misses          2        7       +5     

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

@gitttt-1234 gitttt-1234 requested a review from talmo December 13, 2023 17:52
@gitttt-1234 gitttt-1234 marked this pull request as ready for review December 13, 2023 17:52
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.

Review Status

Actionable comments generated: 18

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between c590ae5 and 8091a91.
Files ignored due to filter (3)
  • environment.yml
  • environment_cpu.yml
  • environment_osx-arm64.yml
Files selected for processing (5)
  • docs/config.md (1 hunks)
  • sleap_nn/data/general.py (1 hunks)
  • sleap_nn/model_trainer.py (1 hunks)
  • tests/fixtures/datasets.py (2 hunks)
  • tests/test_model_trainer.py (1 hunks)
Files skipped from review due to trivial changes (1)
  • docs/config.md
Additional comments: 9
sleap_nn/data/general.py (1)
  • 21-22: The documentation update in the __iter__ method correctly reflects the changes in how frame_idx and video_idx are handled. This aligns with the PR's objective to modify the logic for accessing source video information.
sleap_nn/model_trainer.py (2)
  • 3-19:
    The imports and the ModelTrainer class setup indicate the introduction of a new training system using PyTorch Lightning. The class is well-documented and includes a constructor that sets up the device and seed for training reproducibility.

  • 160-272:
    The TopDownCenteredInstanceModel class is a subclass of LightningModule and is responsible for defining the training and validation steps, as well as configuring optimizers and learning rate schedulers. The class is well-documented and includes methods for the forward pass, training and validation steps, and optimizer configuration.

tests/fixtures/datasets.py (3)
  • 3-3: The import of OmegaConf is correctly added to support the new config fixture function.

  • 20-209: The config fixture function is well-defined and correctly utilizes the sleap_data_dir fixture to set up paths in the configuration. It's important to ensure that the keys and values within the configuration dictionary match the expected schema used elsewhere in the codebase.

  • 22-206: <!--Monologue:
    Thought 1: The rg command has now provided context lines around the matches, which shows how the configuration keys are used in the codebase. The keys 'data_config', 'model_config', and 'trainer_config' are indeed used in various parts of the codebase, and their structure seems to be consistent with the configuration defined in the config fixture in tests/fixtures/datasets.py.
    Reflect 1: This suggests that the structure and keys of the configuration dictionary in the config fixture are likely to match the expected schema throughout the codebase.

Thought 2: The find command has successfully located the file 'minimal_instance.pkg.slp' in the 'tests/assets' directory, confirming that the file path used in the configuration is valid and the file exists in the repository.
Reflect 2: This confirms that the file paths constructed within the configuration are valid and correspond to actual files in the repository.

Resolution: With the information gathered, we can conclude that the structure and keys of the configuration dictionary match the expected schema throughout the codebase, and the file paths used in the configuration are valid.
-->

The verification process has confirmed that the structure and keys of the configuration dictionary in the config fixture are consistent with the expected schema used throughout the codebase. Additionally, the file 'minimal_instance.pkg.slp' referenced in the configuration exists in the repository. No issues have been found.

Analysis chain

Verify that the structure and keys of the configuration dictionary match the expected schema throughout the codebase. This includes ensuring that the default values and types are consistent with how the configuration is consumed in other parts of the application.


Given the output, we need to adjust our approach to verify the configuration structure and also to check the existence of the file 'minimal_instance.pkg.slp'. Let's create a new script to address these points.


The output from the rg command did not provide the necessary context to verify the usage of the configuration keys, and the find command failed to check for the existence of the file 'minimal_instance.pkg.slp'. Let's correct these scripts to gather the required information.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Search for the usage of the keys within the configuration to ensure they match the expected schema.
rg 'data_config|model_config|trainer_config' --files-with-matches
# Verify that the paths constructed within the configuration are valid and correspond to actual files.
fd 'minimal_instance.pkg.slp' --exec test -f

Length of output: 334



Script:

#!/bin/bash
# Deep dive into the usage of the configuration keys to ensure they match the expected schema.
# We will look for the definitions and usages of these keys in the identified files.
rg 'data_config|model_config|trainer_config' -C 5 --files-with-matches

# Correctly check if the file 'minimal_instance.pkg.slp' exists in the repository.
# Since the previous fd command was incorrect, we will use find instead.
find . -name "minimal_instance.pkg.slp" -exec test -f {} \; -print

Length of output: 368



Script:

#!/bin/bash
# Adjust the ripgrep command to include context lines around the matches for detailed analysis.
rg 'data_config|model_config|trainer_config' -C 5

# Correct the find command to simply print the file if it exists, without using the 'test' command.
find . -name "minimal_instance.pkg.slp" -print

Length of output: 40106

tests/test_model_trainer.py (3)
  • 23-34: The assertions in the test test_create_data_loader seem to be correct, but it's important to ensure that the config fixture and sleap_data_dir are properly set up to provide meaningful test data.

  • 95-153: Ensure that the paths used in test_trainer are valid and that the test data is set up correctly to provide meaningful results.

  • 156-173: Ensure that the input data and model configuration in test_topdown_centered_instance_model are set up correctly to provide meaningful results.

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.

Review Status

Actionable comments generated: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 8091a91 and 7774ff4.
Files selected for processing (5)
  • docs/config.md (1 hunks)
  • sleap_nn/inference/peak_finding.py (1 hunks)
  • sleap_nn/model_trainer.py (1 hunks)
  • tests/fixtures/datasets.py (2 hunks)
  • tests/test_model_trainer.py (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • docs/config.md
  • sleap_nn/model_trainer.py
Additional comments: 6
sleap_nn/inference/peak_finding.py (1)
  • 96-122: Given the changes in the peak finding logic, it is crucial to verify that the unit tests for the find_global_peaks_rough function have been updated to reflect these changes and that they cover the new behavior, especially the handling of values below the threshold.
tests/fixtures/datasets.py (5)
  • 20-208: The new config fixture function is well-structured and provides a comprehensive configuration for testing purposes. It's important to ensure that the values used, especially paths and hyperparameters, align with the expected test scenarios and that they are documented if necessary for clarity.

  • 26-27: Verify that the files referenced in the labels_path configuration exist and are in the correct format to avoid test failures due to missing or malformed data.

Also applies to: 76-76

Verification successful

The file minimal_instance.pkg.slp does exist within the tests/assets directory of the repository. This confirms that the file referenced in the labels_path configuration of the config fixture is present and should be accessible during testing.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify the existence of the minimal_instance.pkg.slp file in the sleap_data_dir for both train and val configurations.
fd "minimal_instance.pkg.slp" "$(pytestconfig.rootdir)/tests/assets" --exec test -f

Length of output: 271



Script:

#!/bin/bash
# Search for the file 'minimal_instance.pkg.slp' within the entire repository.
fd "minimal_instance.pkg.slp"

Length of output: 67

* 48-48: Ensure that if data augmentation features are to be tested, there are test cases that specifically enable and test these configurations, as they are disabled by default in the fixture.

Also applies to: 97-97

  • 189-191: Confirm whether the Weights & Biases (wandb) configuration is intended for use in the test environment. Typically, external logging services are not used in unit tests to avoid polluting experiment tracking with test runs.

  • 188-188: The save_ckpt_path is set to f"{tmp_path}", which seems to be a placeholder. Ensure that this is replaced with an actual temporary directory path during test setup, or if tmp_path is a fixture, ensure that it is defined and available in the test environment.

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.

Review Status

Actionable comments generated: 7

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 7774ff4 and 63d6757.
Files selected for processing (2)
  • tests/fixtures/datasets.py (2 hunks)
  • tests/test_model_trainer.py (1 hunks)

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 63d6757 and 1d4f671.
Files selected for processing (2)
  • sleap_nn/inference/peak_finding.py (1 hunks)
  • tests/test_model_trainer.py (1 hunks)
Files skipped from review due to trivial changes (1)
  • tests/test_model_trainer.py
Files skipped from review as they are similar to previous changes (1)
  • sleap_nn/inference/peak_finding.py

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 1d4f671 and de01dbf.
Files selected for processing (2)
  • sleap_nn/model_trainer.py (1 hunks)
  • tests/test_model_trainer.py (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • sleap_nn/model_trainer.py
  • tests/test_model_trainer.py

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.

Review Status

Actionable comments generated: 9

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between de01dbf and 9182247.
Files selected for processing (2)
  • sleap_nn/model_trainer.py (1 hunks)
  • tests/test_model_trainer.py (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • tests/test_model_trainer.py

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.

Review Status

Actionable comments generated: 8

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 9182247 and c4cb0ab.
Files selected for processing (2)
  • sleap_nn/model_trainer.py (1 hunks)
  • tests/test_model_trainer.py (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • tests/test_model_trainer.py

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.

Review Status

Actionable comments generated: 4

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between c4cb0ab and f510bde.
Files selected for processing (4)
  • docs/config.md (1 hunks)
  • sleap_nn/model_trainer.py (1 hunks)
  • tests/fixtures/datasets.py (2 hunks)
  • tests/test_model_trainer.py (1 hunks)
Additional comments: 11
sleap_nn/model_trainer.py (6)
  • 33-33: The typo in the argument description trainer_cong should be corrected to trainer_config.
-                (iii) trainer_cong: trainer configs like accelerator, optimiser params.
+                (iii) trainer_config: trainer configs like accelerator, optimiser params.
  • 48-50: The assignment of self.provider to LabelsReader is still not dynamic. Consider implementing a factory pattern or a mapping to support extensibility.

  • 115-116: Consider adding error handling around the directory creation to handle potential exceptions, such as permission issues.

  • 155-159: The xavier_init_weights function should be extended to handle other types of layers and include checks to ensure that the layer has weight and bias attributes before attempting to initialize them.

  • 189-190: The weight initialization is still hardcoded to use the Xavier method. Consider making this configurable to allow for different initialization methods based on the model or training requirements.

  • 254-269: The optimizer and scheduler configuration should be made more flexible by allowing different types to be specified via the configuration file.

tests/fixtures/datasets.py (1)
  • 21-207: The config fixture is well-structured and provides a comprehensive configuration for testing. Ensure that the paths and values used here align with the actual test environment and intended test scenarios.
tests/test_model_trainer.py (4)
  • 4-4: Duplicate import of pytest on line 6 and 15.
- import pytest
  • 13-26: The test test_create_data_loader appears to be well-structured and checks the creation of data loaders. Ensure that the assertions are valid and that the number of items in the data loaders matches the expected dataset size.

  • 29-91: The test test_trainer seems to cover the training process, including the creation of checkpoint directories and files. Verify that the paths, configurations, and assertions are correct and that the test environment is properly isolated.

  • 94-111: The test test_topdown_centered_instance_model checks the model's forward pass and training step. Confirm that the input shapes, loss calculation, and assertions are accurate and that the test covers the expected behavior of the model.

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.

Review Status

Actionable comments generated: 5

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between f510bde and 73eb594.
Files selected for processing (3)
  • docs/config.md (1 hunks)
  • sleap_nn/model_trainer.py (1 hunks)
  • tests/test_model_trainer.py (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • tests/test_model_trainer.py
Additional comments: 1
docs/config.md (1)
  • 1-102: The documentation for the configuration file is clear and well-structured, providing detailed explanations for each configuration option. Ensure that the provided details match the actual implementation and usage within the codebase.
Verification successful

The initial search was too narrow, focusing only on Markdown files. To verify that the documented config keys match the keys used in the codebase, the search should include all file types, particularly Python files where these keys are likely to be used. Let's perform a broader search.


The search results confirm that the keys data_config, model_config, and trainer_config are indeed used within the codebase, specifically in the model_trainer.py and pipelines.py files. The references to these keys in the code align with the documentation provided in config.md, indicating that the documentation is consistent with the actual implementation.

Final response:

The documentation in config.md is consistent with the usage of configuration keys within the codebase, as verified by the search results from model_trainer.py and pipelines.py.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that the documented config keys match the keys used in the codebase
rg --type md "data_config|model_config|trainer_config" sleap_nn/

Length of output: 66



Script:

#!/bin/bash
# Broaden the search to include all file types for the documented config keys
rg "data_config|model_config|trainer_config" sleap_nn/

Length of output: 6765

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.

Review Status

Actionable comments generated: 7

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 73eb594 and 6fe6cf2.
Files selected for processing (2)
  • sleap_nn/model_trainer.py (1 hunks)
  • tests/test_model_trainer.py (1 hunks)

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 6fe6cf2 and b9e8697.
Files selected for processing (4)
  • docs/config.md (1 hunks)
  • sleap_nn/model_trainer.py (1 hunks)
  • tests/fixtures/datasets.py (2 hunks)
  • tests/test_model_trainer.py (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • sleap_nn/model_trainer.py
  • tests/test_model_trainer.py
Additional comments: 3
tests/fixtures/datasets.py (2)
  • 3-3: The import of OmegaConf is correctly added to support the new config fixture.

  • 21-210: The config fixture is well-constructed with a comprehensive set of parameters for testing. The use of f-strings for dynamic path construction in labels_path is noted and appreciated for its flexibility.

docs/config.md (1)
  • 1-107: The documentation for the configuration file is clear and comprehensive, detailing the parameters for data_config, model_config, and trainer_config. It provides users with the necessary information to effectively configure model training.

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between b9e8697 and d19f703.
Files selected for processing (2)
  • sleap_nn/model_trainer.py (1 hunks)
  • tests/fixtures/datasets.py (2 hunks)
Files skipped from review as they are similar to previous changes (2)
  • sleap_nn/model_trainer.py
  • tests/fixtures/datasets.py

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between d19f703 and 33e27d3.
Files selected for processing (1)
  • sleap_nn/model_trainer.py (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • sleap_nn/model_trainer.py

@gitttt-1234 gitttt-1234 merged commit 3db6fd7 into main Dec 22, 2023
3 of 5 checks passed
@gitttt-1234 gitttt-1234 mentioned this pull request Dec 22, 2023
@gitttt-1234 gitttt-1234 deleted the divya/trainer branch December 23, 2023 00:36
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.

3 participants