Skip to content

eliminate alpha image double-decode#2847

Merged
bghira merged 1 commit into
mainfrom
perf/double-decode-for-alpha-images
Jul 17, 2026
Merged

eliminate alpha image double-decode#2847
bghira merged 1 commit into
mainfrom
perf/double-decode-for-alpha-images

Conversation

@bghira

@bghira bghira commented Jul 17, 2026

Copy link
Copy Markdown
Owner

This pull request refactors image loading and resizing logic to improve efficiency and reliability by reusing decoded image arrays and standardizing on direct calls to trainingsample utilities. It also updates the test suite to verify these optimizations and ensure correct integration.

Image loading and decoding improvements:

  • Refactored load_image and related helpers in load.py to decode image bytes only once (with unchanged channels), then reuse the resulting array for RGB conversion, reducing redundant decoding and improving performance. (simpletuner/helpers/image_manipulation/load.py) [1] [2]
  • Added a new helper function _trainingsample_array_to_rgb_image to encapsulate optimized format conversions from array to RGB image, including handling of grayscale and RGBA inputs. (simpletuner/helpers/image_manipulation/load.py)

Resizing logic standardization:

  • Updated TrainingSample.resize to call tsr.batch_resize_images directly for resizing, bypassing the batch_processor indirection for improved clarity and reliability. (simpletuner/helpers/image_manipulation/training_sample.py) [1] [2]
  • Ensured that trainingsample is imported as tsr where needed. (simpletuner/helpers/image_manipulation/training_sample.py)

Testing enhancements:

  • Added a new test to verify that load_image reuses the unchanged-channel decode and does not redundantly decode RGB, ensuring the optimization is effective. (tests/test_image_load.py)
  • Added a test to confirm that intermediary resizing in TrainingSample uses the direct trainingsample call and not the batch processor, verifying correct integration of the new resizing logic. (tests/test_training_sample.py)

These changes streamline image handling, reduce unnecessary computation, and add tests to prevent regressions.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This pull request refactors the image loading and resizing pipeline to avoid redundant decoding/work, aiming to improve performance and determinism by reusing decoded arrays and calling trainingsample utilities directly.

Changes:

  • Reuses a single IMREAD_UNCHANGED decode in load_image and centralizes array→RGB conversion via _trainingsample_array_to_rgb_image.
  • Standardizes TrainingSample.resize to call trainingsample.batch_resize_images directly (instead of delegating through BatchedTrainingSamples for image resizing).
  • Adds focused unit tests to guard the “no double-decode” behavior and the direct trainingsample resize integration.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
simpletuner/helpers/image_manipulation/load.py Reuses the unchanged-channel decode and consolidates array→RGB conversions to prevent redundant decoding.
simpletuner/helpers/image_manipulation/training_sample.py Switches image resizing to a direct tsr.batch_resize_images call to reduce indirection and standardize behavior.
tests/test_image_load.py Adds a unit test ensuring load_image does not perform a second decode after a successful unchanged decode.
tests/test_training_sample.py Adds a unit test asserting intermediary resizing uses the direct trainingsample call (not the batch processor).

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

@bghira
bghira merged commit c6f0709 into main Jul 17, 2026
3 checks passed
@bghira
bghira deleted the perf/double-decode-for-alpha-images branch July 17, 2026 15:44
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