Optimize RAM usage for VideoSegmentationSam3Text - #70
Merged
cbentejac merged 2 commits intoAug 19, 2026
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR aims to reduce RAM usage and improve consistency in VideoSegmentationSam3Text by storing per-frame mask images as explicit single-channel arrays and updating definitive masks using in-place operations.
Changes:
- Initialize
mask_imagesas single-channel arrays with shape(H, W, 1)instead of mirroring the source image shape. - Update definitive mask accumulation to use
np.maximum(..., out=...)for in-place updates (lower peak memory).
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
355
to
+356
| if is_definitive: | ||
| mask_images[frame_id] = np.maximum(mask_images[frame_id], mask_global) | ||
| mask_images[frame_id] = np.maximum(mask_images[frame_id], mask_global, out=mask_images[frame_id]) |
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.
Suppressed comments (2)
meshroom/sam3/VideoSegmentationSam3Text.py:341
- The masks are only used as a binary union, but
np.sumfirst allocates a full-frameint64accumulator and the new cast then allocates anotherfloat32frame. Usingnp.anypreserves the later> 0behavior and allowsnp.maximumto write directly into thefloat32destination while substantially reducing peak RAM.
mask_global = np.expand_dims(np.sum(masks_stack, axis=0), axis=-1).astype(np.float32)
segmentationRDS/sam3Utils.py:429
- This cast turns a one-byte
uint8full-frame mask into a four-bytefloat32mask and requires another full-frame allocation afternp.where. All current callers only test> 0, index with it, or combine it with afloat32destination (whereuint8promotes safely), so retaining the existinguint8result avoids a shared memory regression.
bonded_binary_mask = np.where(action_mask > 0, global_mask_closed, global_mask_raw).astype(np.float32)
cbentejac
approved these changes
Aug 19, 2026
VideoSegmentationSam3Text
cbentejac
deleted the
dev/videoSegmentationSam3TextMemoryUsageOptimization
branch
August 19, 2026 16:42
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request updates the mask image handling logic in
VideoSegmentationSam3Text.pyto improve consistency and efficiency when initializing and updating mask images. The changes ensure that mask images are correctly shaped and that in-place operations are used for performance.Mask image initialization and updates:
_load_source_imagesto explicitly create a single-channel mask with shape(*img.shape[:2], 1), ensuring consistent mask dimensions._export_direction_masksto use theoutparameter innp.maximum, enabling in-place updates of mask images for better memory efficiency.