Skip to content

Fix OpenCV video loader normalization scale - #603

Open
SakshamKapoor2911 wants to merge 1 commit into
facebookresearch:mainfrom
SakshamKapoor2911:fix-cv2-video-loader-scale
Open

Fix OpenCV video loader normalization scale#603
SakshamKapoor2911 wants to merge 1 commit into
facebookresearch:mainfrom
SakshamKapoor2911:fix-cv2-video-loader-scale

Conversation

@SakshamKapoor2911

Copy link
Copy Markdown

The OpenCV video-file loader normalized decoded uint8 frames without first scaling them to [0, 1]. Other image/video input paths divide by 255 before applying image_mean/image_std, so cv2-loaded videos could enter the model at a different scale.

This PR adds the missing scaling step and a generated temporary-video regression test that verifies the cv2 loader output against normalized decoded frames.

Validation:

  • /home/skapoor/oss_work/.venvs/sam3/bin/python -m pytest test/test_io_utils.py -q
  • /home/skapoor/oss_work/.venvs/sam3/bin/python -m py_compile sam3/model/io_utils.py test/test_io_utils.py

Copilot AI review requested due to automatic review settings July 3, 2026 17:42
@meta-cla

meta-cla Bot commented Jul 3, 2026

Copy link
Copy Markdown

Hi @SakshamKapoor2911!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Fixes inconsistent normalization for the OpenCV (cv2) video-file loader by scaling decoded uint8 frames into [0, 1] before applying img_mean/img_std, aligning it with the other image/video input paths.

Changes:

  • Scale cv2-decoded video frames by 1/255 prior to normalization.
  • Add a regression test that generates a tiny temporary MJPG video and checks cv2 loader output against manually normalized decoded frames.

Reviewed changes

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

File Description
sam3/model/io_utils.py Applies the missing /255.0 scaling step in the cv2 video loader before mean/std normalization.
test/test_io_utils.py Adds a regression test that writes/reads a small cv2 video and compares loader output to an independently normalized decode.

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

Comment thread test/test_io_utils.py
Comment thread sam3/model/io_utils.py Outdated
@SakshamKapoor2911
SakshamKapoor2911 force-pushed the fix-cv2-video-loader-scale branch from 74490bd to a2771fb Compare July 3, 2026 17:50
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jul 3, 2026
@SakshamKapoor2911

Copy link
Copy Markdown
Author

Updated this PR to address the Copilot review comments:

  • The regression test now applies the same cv2.resize(..., interpolation=cv2.INTER_CUBIC) step as the cv2 loader when building expected frames, using non-square generated source frames.
  • The cv2 loader now converts stacked frames to float16 before moving to CUDA and applies / 255.0 in torch before mean/std normalization, matching the dtype behavior documented for this loader and used by the other loading paths.

Validation rerun:

  • /home/skapoor/oss_work/.venvs/sam3/bin/python -m pytest test/test_io_utils.py -q
  • /home/skapoor/oss_work/.venvs/sam3/bin/python -m py_compile sam3/model/io_utils.py test/test_io_utils.py

@SakshamKapoor2911
SakshamKapoor2911 force-pushed the fix-cv2-video-loader-scale branch from a2771fb to 8cd357a Compare July 17, 2026 14:48
@SakshamKapoor2911
SakshamKapoor2911 force-pushed the fix-cv2-video-loader-scale branch from 8cd357a to 323a42f Compare July 31, 2026 11:16
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 Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants