From 3ebc26511b12618b335bac6dff78959a0888bd9c Mon Sep 17 00:00:00 2001 From: mattiagaggi Date: Tue, 2 Dec 2025 02:04:15 +0000 Subject: [PATCH 1/3] Fix: Make decord import lazy to prevent cv2.imshow() segfault This fixes the issue where importing sam3 causes cv2.imshow() to segfault due to decord being imported at module level, which conflicts with OpenCV. Changes: - Removed module-level import of decord from sam3_image_dataset.py - Added lazy import inside _load_images() method when processing .mp4 files - Decord now only loads when actually needed, preventing the conflict Fixes the issue reported where: import cv2 from sam3.model_builder import build_sam3_image_model cv2.imshow('test', frame) # Would segfault before this fix --- sam3/train/data/sam3_image_dataset.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/sam3/train/data/sam3_image_dataset.py b/sam3/train/data/sam3_image_dataset.py index 97efb1d1..7d4c8a98 100644 --- a/sam3/train/data/sam3_image_dataset.py +++ b/sam3/train/data/sam3_image_dataset.py @@ -15,9 +15,13 @@ import torch import torch.utils.data import torchvision -from decord import cpu, VideoReader from iopath.common.file_io import g_pathmgr +# Lazy import of decord to avoid conflicts with OpenCV +# Decord will only be imported when actually needed in _load_images +# This prevents decord from loading when just importing sam3, which would +# cause cv2.imshow() to segfault due to library conflicts + from PIL import Image as PILImage from PIL.Image import DecompressionBombError @@ -202,6 +206,14 @@ def _load_images( try: if ".mp4" in path and path[-4:] == ".mp4": # Going to load a video frame + # Lazy import decord only when needed to avoid conflicts with OpenCV + try: + from decord import cpu, VideoReader + except ImportError: + raise ImportError( + "decord is required for video loading but is not installed. " + "Install it with: pip install decord or pip install -e '.[notebooks]'" + ) video_path, frame = path.split("@") video = VideoReader(video_path, ctx=cpu(0)) # Convert to PIL image From e332b67f44463571498883ec3bea27cd3d791d88 Mon Sep 17 00:00:00 2001 From: mattiagaggi Date: Tue, 2 Dec 2025 02:18:52 +0000 Subject: [PATCH 2/3] Add missing dependencies to notebooks optional dependencies - Add triton with platform-specific handling (triton-windows for Windows, triton for Linux/Mac) - Fixes issue where einops, decord, pycocotools, and triton were missing when running Jupyter notebooks - Addresses feedback from Windows users who had to manually install these packages --- pyproject.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index e4998de0..348caa9f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -64,6 +64,8 @@ notebooks = [ "einops", "scikit-image", "scikit-learn", + "triton; sys_platform != 'win32'", + "triton-windows; sys_platform == 'win32'", ] train = [ "hydra-core", From 6207585274387cfc7a54f0c54b499cb982fc4acf Mon Sep 17 00:00:00 2001 From: mattiagaggi Date: Wed, 7 Jan 2026 07:37:23 +0000 Subject: [PATCH 3/3] Document recent bug fixes in README MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add note about lazy loading of decord to prevent cv2.imshow() segfaults - Document updated notebook dependencies (einops, decord, pycocotools, triton) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 669242df..bc3b00df 100644 --- a/README.md +++ b/README.md @@ -88,13 +88,15 @@ pip install -e . 4. **Install additional dependencies for example notebooks or development:** ```bash -# For running example notebooks +# For running example notebooks (includes einops, decord, pycocotools, triton) pip install -e ".[notebooks]" # For development pip install -e ".[train,dev]" ``` +**Note:** Recent fixes include lazy loading of decord to prevent cv2.imshow() segfaults and updated notebook dependencies. + ## Getting Started ⚠️ Before using SAM 3, please request access to the checkpoints on the SAM 3