-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27288 from hmaarrfk/sam2
Add sam-2
- Loading branch information
Showing
4 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
From c922127f72559cf503acc72cd18ebf808623d06b Mon Sep 17 00:00:00 2001 | ||
From: Ko Sugawara <ko.sugawara@riken.jp> | ||
Date: Wed, 14 Aug 2024 11:51:45 +0900 | ||
Subject: [PATCH] Use skimage.measure.label if failed in importing sam2._C | ||
module | ||
|
||
--- | ||
sam2/utils/misc.py | 11 +++++++++-- | ||
1 file changed, 9 insertions(+), 2 deletions(-) | ||
|
||
diff --git a/sam2/utils/misc.py b/sam2/utils/misc.py | ||
index e2d39a08..bfc27c73 100644 | ||
--- a/sam2/utils/misc.py | ||
+++ b/sam2/utils/misc.py | ||
@@ -58,9 +58,16 @@ def get_connected_components(mask): | ||
- counts: A tensor of shape (N, 1, H, W) containing the area of the connected | ||
components for foreground pixels and 0 for background pixels. | ||
""" | ||
- from sam2 import _C | ||
+ try: | ||
+ from sam2 import _C | ||
+ | ||
+ return _C.get_connected_componnets(mask.to(torch.uint8).contiguous()) | ||
+ except Exception: | ||
+ import skimage.measure | ||
|
||
- return _C.get_connected_componnets(mask.to(torch.uint8).contiguous()) | ||
+ return skimage.measure.label( | ||
+ mask.to(torch.uint8).contiguous().cpu().numpy(), return_num=True | ||
+ ) | ||
|
||
|
||
def mask_to_box(masks: torch.Tensor): |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
pytorch: | ||
- 2.3.1 | ||
- 2.4.0 | ||
|
||
libtorch: | ||
- 2.3.1 | ||
- 2.4.0 | ||
|
||
zip_keys: | ||
- | ||
- pytorch | ||
- libtorch |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
{% set git_hash_date = "20240814" %} | ||
{% set git_hash = "7e1596c0b6462eb1d1ba7e1492430fed95023598" %} | ||
# They claim version 1.0 but they don't really release anything on pypi | ||
{% set version = "1.0.0." + git_hash_date + "." + git_hash %} | ||
|
||
{% if cuda_compiler_version in (None, "None", True, False) %} | ||
{% set cuda_major = 0 %} | ||
{% else %} | ||
{% set cuda_major = environ.get("cuda_compiler_version", "11.2").split(".")[0] | int %} | ||
{% endif %} | ||
|
||
package: | ||
# Their package name is "SAM 2" pip claims it is "SAM-2" | ||
# the lower case is thus "sam-2" | ||
name: sam-2 | ||
version: {{ version }} | ||
|
||
source: | ||
url: https://github.com/facebookresearch/segment-anything-2/archive/{{ git_hash }}.tar.gz | ||
sha256: 30a317999fa4eae5f239a0ae41e464f1394ecc387d40c16047118c70c5459aa8 | ||
patches: | ||
- 216_be_compatible_with_cpu_only.patch | ||
- remove_torchvision_minimum_requirement.patch | ||
|
||
{% set build = 1 %} | ||
{% if cuda_major != 0 %} | ||
{% set build = build + 300 %} | ||
{% endif %} | ||
|
||
build: | ||
string: cpu_pyh{{ PKG_HASH }}_{{ PKG_BUILDNUM }} # [cuda_compiler_version == "None"] | ||
string: cuda{{ cuda_compiler_version | replace('.', '') }}py{{ CONDA_PY }}h{{ PKG_HASH }}_{{ PKG_BUILDNUM }} # [cuda_compiler_version != "None"] | ||
script: | ||
# Ensure to add PTX at the end, always | ||
- export TORCH_CUDA_ARCH_LIST="5.0;6.0;6.1;7.0;7.5;8.0;8.6;8.9;9.0+PTX" # [cuda_compiler_version != "None"] | ||
- export CUDA_TOOLKIT_ROOT_DIR="${PREFIX}" # [cuda_compiler_version != "None"] | ||
- export FORCE_CUDA=1 # [cuda_compiler_version != "None"] | ||
- {{ PYTHON }} -m pip install . -vv | ||
number: {{ build }} | ||
skip: true # [win] | ||
# A contributor can come and help build out 11.8 correctly | ||
skip: true # [cuda_compiler_version == "11.8"] | ||
{% if cuda_major != 0 %} | ||
skip: true # [py<310] | ||
{% else %} | ||
noarch: python | ||
{% endif %} | ||
|
||
requirements: | ||
build: | ||
- {{ stdlib('c') }} # [cuda_compiler_version != "None"] | ||
- {{ compiler('c') }} # [cuda_compiler_version != "None"] | ||
- {{ compiler('cxx') }} # [cuda_compiler_version != "None"] | ||
- {{ compiler('cuda') }} # [cuda_compiler_version != "None"] | ||
{% if cuda_major >= 12 %} | ||
- libcublas-dev # [build_platform != target_platform] | ||
- libcusolver-dev # [build_platform != target_platform] | ||
- libcusparse-dev # [build_platform != target_platform] | ||
- cuda-cudart-dev # [build_platform != target_platform] | ||
{% endif %} | ||
host: | ||
- python # [cuda_compiler_version != "None"] | ||
- python >=3.10 # [cuda_compiler_version == "None"] | ||
- pip | ||
- setuptools | ||
# For now pin to pytorch 2.4.1 to avoid 2.3.X builds that | ||
# don't have the correct run exports | ||
- pytorch 2.4.1 | ||
- libtorch *=cuda* # [cuda_compiler_version != "None"] | ||
{% if cuda_major >= 12 %} | ||
- libcublas-dev | ||
- libcusolver-dev | ||
- libcusparse-dev | ||
- cuda-cudart-dev | ||
{% endif %} | ||
- cuda-version {{ cuda_compiler_version }} # [cuda_compiler_version != "None"] | ||
run: | ||
- python # [cuda_compiler_version != "None"] | ||
- python >=3.10 # [cuda_compiler_version == "None"] | ||
- hydra-core | ||
- pytorch | ||
- torchvision >=0.18.1 | ||
# Fallback for the cuda compiled code | ||
# https://github.com/facebookresearch/segment-anything-2/pull/216 | ||
- scikit-image # [cuda_compiler_version != "None"] | ||
# It doesn't seem to be used anywhere in their code... | ||
# https://github.com/conda-forge/iopath-feedstock/pull/3 | ||
# - iopath >=0.1.10 | ||
- tqdm | ||
- pillow | ||
# avoid that people without GPUs needlessly download ~0.5-1GB | ||
- __cuda # [cuda_compiler_version != "None"] | ||
run_constrained: | ||
# Let the run_export from pytorch take effect | ||
# so put their requirement here | ||
- pytorch >=2.3.1 | ||
# There seems to be an other package on conda-forge with | ||
# a similar name that wasn't packaged from the official | ||
# source | ||
# https://github.com/conda-forge/sam2-feedstock/issues/4 | ||
# I will be working with them to ensure that | ||
# we have a cohesive version at conda-forge but the 0.4.1 | ||
# version should not be co-installed with this package | ||
- sam2 >=1.0.0 | ||
|
||
test: | ||
imports: | ||
- sam2 | ||
requires: | ||
- pip | ||
commands: | ||
- pip check | ||
# Tests that cuda compilation worked for the expected output | ||
- test -f ${SP_DIR}/sam2/_C.so # [cuda_compiler_version != "None"] | ||
|
||
about: | ||
home: https://github.com/facebookresearch/segment-anything-2 | ||
summary: 'SAM 2: Segment Anything in Images and Videos' | ||
description: | | ||
Segment Anything Model 2 (SAM 2) is a foundation model towards solving | ||
promptable visual segmentation in images and videos. We extend SAM to video | ||
by considering images as a video with a single frame. The model design is a | ||
simple transformer architecture with streaming memory for real-time video | ||
processing. We build a model-in-the-loop data engine, which improves model | ||
and data via user interaction, to collect our SA-V dataset, the largest | ||
video segmentation dataset to date. SAM 2 trained on our data provides | ||
strong performance across a wide range of tasks and visual domains. | ||
license: Apache-2.0 | ||
license_family: Apache | ||
license_file: LICENSE | ||
|
||
extra: | ||
recipe-maintainers: | ||
- hmaarrfk |
17 changes: 17 additions & 0 deletions
17
recipes/sam-2/remove_torchvision_minimum_requirement.patch
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
diff --git a/setup.py b/setup.py | ||
index ebef97c..da9e154 100644 | ||
--- a/setup.py | ||
+++ b/setup.py | ||
@@ -23,11 +23,10 @@ with open("README.md", "r", encoding="utf-8") as f: | ||
# Required dependencies | ||
REQUIRED_PACKAGES = [ | ||
"torch>=2.3.1", | ||
- "torchvision>=0.18.1", | ||
+ "torchvision", | ||
"numpy>=1.24.4", | ||
"tqdm>=4.66.1", | ||
"hydra-core>=1.3.2", | ||
- "iopath>=0.1.10", | ||
"pillow>=9.4.0", | ||
] | ||
|