Skip to content

Improve code quality: proper logging, error handling, and optional de…#5507

Open
Alanperry1 wants to merge 3 commits intofacebookresearch:mainfrom
Alanperry1:improve_tooling
Open

Improve code quality: proper logging, error handling, and optional de…#5507
Alanperry1 wants to merge 3 commits intofacebookresearch:mainfrom
Alanperry1:improve_tooling

Conversation

@Alanperry1
Copy link

Summary

This PR improves code quality across the codebase by replacing print() statements with proper logging, converting assert statements to explicit error handling with clear error messages, and moving matplotlib from a required dependency to an optional visualization dependency.

Changes

Logging Improvements

  • tools/train_net.py: Moved command line args logging to after default_setup() is called, ensuring proper logging configuration
  • tools/visualize_data.py: Replaced print() calls with logger.info() for file output messages
  • datasets/prepare_panoptic_fpn.py:
    • Added logging import at the top of the file (proper Python convention)
    • Replaced print() statements with logger.info() for progress messages

Error Handling

  • demo/demo.py: Converted all assert statements to explicit ValueError/FileNotFoundError exceptions with descriptive error messages:
    • Input path validation
    • Output path validation
    • Webcam/input conflict validation
    • Video file existence checks
    • Removed redundant assert os.path.isdir(args.output) check that was logically impossible

Dependency Management

  • setup.py: Moved matplotlib from install_requires to extras_require["viz"], completing the TODO
  • detectron2/utils/visualizer.py: Added try-except block around matplotlib imports with helpful error message directing users to install detectron2[viz]

Benefits

  • Better logging control and consistency across the codebase
  • More informative error messages for debugging
  • Reduced required dependencies - matplotlib only needed for visualization features
  • Users can now install with pip install detectron2 for core functionality or pip install detectron2[viz] for visualization features

Breaking Changes

Users who don't install the viz extra will need to run pip install detectron2[viz] to use visualization features.

…pendencies

- Replace print() statements with logger.info() for better logging control
- Convert assert statements to explicit ValueError/FileNotFoundError with descriptive messages
- Move matplotlib from required dependencies to optional "viz" extra
- Add try-except block for matplotlib import with helpful installation message
- Move logging calls to after default_setup() configuration in train_net.py
- Move import statements to top of files following Python conventions

Fixes all issues identified in code review.
Copilot AI review requested due to automatic review settings January 12, 2026 23:36
@meta-cla meta-cla bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jan 12, 2026
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR improves code quality by replacing print() statements with proper logging, converting assert statements to explicit error handling with descriptive error messages, and moving matplotlib from a required dependency to an optional visualization dependency.

Changes:

  • Replaced print() calls with logger.info() in visualization and training tools
  • Converted assert statements to explicit exceptions (ValueError, FileNotFoundError) with clear error messages in demo.py
  • Moved matplotlib to extras_require["viz"] in setup.py and added import handling in visualizer.py

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
tools/visualize_data.py Replaced print statements with logger.info for file output messages
tools/train_net.py Moved command line args logging to after default_setup() for proper configuration
setup.py Moved matplotlib from install_requires to extras_require["viz"]
detectron2/utils/visualizer.py Added try-except for matplotlib imports with helpful error message
demo/demo.py Converted assert statements to explicit exceptions with descriptive messages
datasets/prepare_panoptic_fpn.py Added logging import and replaced print statements with logger.info

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +181 to +182
if not os.path.isfile(args.video_input):
raise FileNotFoundError(f"Video input file not found: {args.video_input}")
Copy link

Copilot AI Jan 12, 2026

Choose a reason for hiding this comment

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

The video file existence check is performed after the output file setup (lines 164-180). This means if the video input file doesn't exist, the code will still create the output file writer first, potentially creating an empty output file before failing. The video file existence check should be moved earlier, before any output file setup, to fail fast and avoid creating unnecessary files.

Copilot uses AI. Check for mistakes.
Copy link
Author

Choose a reason for hiding this comment

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

@copilot open a new pull request to apply changes based on this feedback

Alanperry1 and others added 2 commits January 12, 2026 22:14
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants