Skip to content

Optimize RAM usage for VideoSegmentationSam3Text - #70

Merged
cbentejac merged 2 commits into
mainfrom
dev/videoSegmentationSam3TextMemoryUsageOptimization
Aug 19, 2026
Merged

Optimize RAM usage for VideoSegmentationSam3Text#70
cbentejac merged 2 commits into
mainfrom
dev/videoSegmentationSam3TextMemoryUsageOptimization

Conversation

@demoulinv

Copy link
Copy Markdown
Collaborator

This pull request updates the mask image handling logic in VideoSegmentationSam3Text.py to 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:

  • Changed the initialization of mask images in _load_source_images to explicitly create a single-channel mask with shape (*img.shape[:2], 1), ensuring consistent mask dimensions.
  • Updated the mask update logic in _export_direction_masks to use the out parameter in np.maximum, enabling in-place updates of mask images for better memory efficiency.

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 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_images as 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])

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

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.sum first allocates a full-frame int64 accumulator and the new cast then allocates another float32 frame. Using np.any preserves the later > 0 behavior and allows np.maximum to write directly into the float32 destination 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 uint8 full-frame mask into a four-byte float32 mask and requires another full-frame allocation after np.where. All current callers only test > 0, index with it, or combine it with a float32 destination (where uint8 promotes safely), so retaining the existing uint8 result avoids a shared memory regression.
    bonded_binary_mask = np.where(action_mask > 0, global_mask_closed, global_mask_raw).astype(np.float32)

@demoulinv
demoulinv requested a review from cbentejac August 19, 2026 07:43
@cbentejac cbentejac changed the title RAM usage optimization for VideoSegmentationSam3Text Optimize RAM usage for VideoSegmentationSam3Text Aug 19, 2026
@cbentejac
cbentejac merged commit 695d88f into main Aug 19, 2026
1 check passed
@cbentejac
cbentejac deleted the dev/videoSegmentationSam3TextMemoryUsageOptimization branch August 19, 2026 16:42
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