Spaces:
Paused
Paused
Zhen Ye commited on
Commit ·
6268ac2
1
Parent(s): d74c718
feat(segmentation): add YSAM2 variants and mission-aware detector mapping
Browse filesFiles changed (5)
60 unmodified lines616263646566193 unmodified lines26026126226326426526635 unmodified lines302303304305306307308130 unmodified lines43944044144244344444544644744844945045145245345460 unmodified linesfrom utils.relevance import evaluate_relevancefrom utils.enrichment import run_enrichmentfrom utils.schemas import AssessmentStatusfrom utils.mission_parser import parse_mission_text, build_broad_queries, MissionParseErrorlogging.basicConfig(level=logging.INFO)193 unmodified linesmode: Detection mode (object_detection, segmentation, drone_detection)queries: Comma-separated object classes for object_detection modedetector: Model to use (hf_yolov8, detr_resnet50, grounding_dino)segmenter: Segmentation model to use (GSAM2-S, GSAM2-B, GSAM2-L)enable_depth: Whether to run legacy depth estimation (default: False)drone_detection uses the dedicated drone_yolo model.35 unmodified linesoutput_path,query_list,segmenter_name=segmenter,detector_name="grounding_dino",num_maskmem=7,)except ValueError as exc:130 unmodified linesmission_mode = "LEGACY"detector_name = detectorif mode == "drone_detection":detector_name = "drone_yolo"elif mode == "segmentation":# Grounded-SAM2 uses Grounding DINO (open-vocabulary) for keyframe prompts.detector_name = "grounding_dino"if queries.strip():try:mission_spec = parse_mission_text(queries.strip(), detector_name, video_path=str(input_path))query_list = build_broad_queries(detector_name, mission_spec)mission_mode = "MISSION"logging.info("Mission parsed: mode=%s classes=%s broad_queries=%s domain=%s(%s)",60 unmodified lines61626364656667193 unmodified lines26126226326426526626735 unmodified lines303304305306307308130 unmodified lines43944044144244344444544644744844945045145245345445545645745845946046160 unmodified linesfrom utils.relevance import evaluate_relevancefrom utils.enrichment import run_enrichmentfrom utils.schemas import AssessmentStatusfrom models.segmenters.model_loader import get_segmenter_detectorfrom utils.mission_parser import parse_mission_text, build_broad_queries, MissionParseErrorlogging.basicConfig(level=logging.INFO)193 unmodified linesmode: Detection mode (object_detection, segmentation, drone_detection)queries: Comma-separated object classes for object_detection modedetector: Model to use (hf_yolov8, detr_resnet50, grounding_dino)segmenter: Segmentation model to use (GSAM2-S/B/L, YSAM2-S/B/L)enable_depth: Whether to run legacy depth estimation (default: False)drone_detection uses the dedicated drone_yolo model.35 unmodified linesoutput_path,query_list,segmenter_name=segmenter,num_maskmem=7,)except ValueError as exc:130 unmodified linesmission_mode = "LEGACY"detector_name = detectormission_detector = detector # detector key used for mission query parsingif mode == "drone_detection":detector_name = "drone_yolo"mission_detector = "drone_yolo"elif mode == "segmentation":# Segmenter registry owns detector selection (GSAM2→GDINO, YSAM2→YOLO).# detector_name=None so the job doesn't forward it (avoids duplicate kwarg).try:mission_detector = get_segmenter_detector(segmenter)except ValueError as exc:raise HTTPException(status_code=400, detail=str(exc))detector_name = Noneif queries.strip():try:mission_spec = parse_mission_text(queries.strip(), mission_detector, video_path=str(input_path))query_list = build_broad_queries(mission_detector, mission_spec)mission_mode = "MISSION"logging.info("Mission parsed: mode=%s classes=%s broad_queries=%s domain=%s(%s)",
77 unmodified lines78798081828377 unmodified lines<option value="GSAM2-L" data-kind="segmentation">GSAM2-L</option><option value="GSAM2-B" data-kind="segmentation">GSAM2-B</option><option value="GSAM2-S" data-kind="segmentation">GSAM2-S</option></optgroup><optgroup label="Drone Detection Models"><option value="drone_yolo" data-kind="drone">Drone</option>77 unmodified lines78798081828384858677 unmodified lines<option value="GSAM2-L" data-kind="segmentation">GSAM2-L</option><option value="GSAM2-B" data-kind="segmentation">GSAM2-B</option><option value="GSAM2-S" data-kind="segmentation">GSAM2-S</option><option value="YSAM2-L" data-kind="segmentation">YSAM2-L (Fast)</option><option value="YSAM2-B" data-kind="segmentation">YSAM2-B (Fast)</option><option value="YSAM2-S" data-kind="segmentation">YSAM2-S (Fast)</option></optgroup><optgroup label="Drone Detection Models"><option value="drone_yolo" data-kind="drone">Drone</option>
348 unmodified lines349350351352353354355348 unmodified linesself.num_maskmem = num_maskmem # None = use default (7)self._detector_name = detector_name # None = "grounding_dino"_size_suffix = {"small": "S", "base": "B", "large": "L"}self.name = f"GSAM2-{_size_suffix[model_size]}"if device:self.device = device348 unmodified lines349350351352353354355356357348 unmodified linesself.num_maskmem = num_maskmem # None = use default (7)self._detector_name = detector_name # None = "grounding_dino"_size_suffix = {"small": "S", "base": "B", "large": "L"}_det_prefix = {"hf_yolov8": "YSAM2"}_prefix = _det_prefix.get(detector_name, "GSAM2")self.name = f"{_prefix}-{_size_suffix[model_size]}"if device:self.device = device
12345678910111213141516171819import osfrom functools import lru_cachefrom typing import Callable, Dict, Optionalfrom .base import Segmenterfrom .grounded_sam2 import GroundedSAM2SegmenterDEFAULT_SEGMENTER = "GSAM2-L"_REGISTRY: Dict[str, Callable[..., Segmenter]] = {"GSAM2-S": lambda **kw: GroundedSAM2Segmenter(model_size="small", **kw),"GSAM2-B": lambda **kw: GroundedSAM2Segmenter(model_size="base", **kw),"GSAM2-L": lambda **kw: GroundedSAM2Segmenter(model_size="large", **kw),}def _create_segmenter(name: str, **kwargs) -> Segmenter:"""Create a new segmenter instance."""try:1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556import osfrom functools import lru_cachefrom typing import Callable, Dict, Optional, Tuplefrom .base import Segmenterfrom .grounded_sam2 import GroundedSAM2SegmenterDEFAULT_SEGMENTER = "GSAM2-L"_DEFAULT_SEGMENTER_DETECTOR = "grounding_dino"_SEGMENTER_SPECS: Dict[str, Tuple[str, Optional[str]]] = {"GSAM2-S": ("small", None),"GSAM2-B": ("base", None),"GSAM2-L": ("large", None),"YSAM2-S": ("small", "hf_yolov8"),"YSAM2-B": ("base", "hf_yolov8"),"YSAM2-L": ("large", "hf_yolov8"),}def _build_factory(model_size: str, detector_name: Optional[str]) -> Callable[..., Segmenter]:def _factory(**kw) -> Segmenter:if detector_name is None:return GroundedSAM2Segmenter(model_size=model_size, **kw)# YSAM2 keys own detector selection; drop accidental caller overrides.kw.pop("detector_name", None)return GroundedSAM2Segmenter(model_size=model_size,detector_name=detector_name,**kw,)return _factory_REGISTRY: Dict[str, Callable[..., Segmenter]] = {name: _build_factory(model_size, detector_name)for name, (model_size, detector_name) in _SEGMENTER_SPECS.items()}def get_segmenter_detector(segmenter_name: str) -> str:"""Return the detector key associated with a segmenter (for mission parsing)."""spec = _SEGMENTER_SPECS.get(segmenter_name)if spec is None:available = ", ".join(sorted(_REGISTRY))raise ValueError(f"Unknown segmenter '{segmenter_name}'. Available: {available}")detector_name = spec[1]return detector_name or _DEFAULT_SEGMENTER_DETECTORdef _create_segmenter(name: str, **kwargs) -> Segmenter:"""Create a new segmenter instance."""try:
23 unmodified lines2425262728297 unmodified lines37383940414223 unmodified lines"GSAM2-S": 48.0, # SAM2 small encoder"GSAM2-B": 96.0, # SAM2 base encoder"GSAM2-L": 200.0, # SAM2 large encoder"gsam2_tiny": 24.0, # SAM2 tiny encoder}7 unmodified lines"GSAM2-S": 92.0,"GSAM2-B": 180.0,"GSAM2-L": 400.0,"gsam2_tiny": 46.0,}23 unmodified lines242526272829303132337 unmodified lines41424344454647484923 unmodified lines"GSAM2-S": 48.0, # SAM2 small encoder"GSAM2-B": 96.0, # SAM2 base encoder"GSAM2-L": 200.0, # SAM2 large encoder# YSAM2 uses the same SAM2 backbone; detector differences are reflected in timing."YSAM2-S": 48.0,"YSAM2-B": 96.0,"YSAM2-L": 200.0,"gsam2_tiny": 24.0, # SAM2 tiny encoder}7 unmodified lines"GSAM2-S": 92.0,"GSAM2-B": 180.0,"GSAM2-L": 400.0,"YSAM2-S": 92.0,"YSAM2-B": 180.0,"YSAM2-L": 400.0,"gsam2_tiny": 46.0,}
app.py+13-6
frontend/index.html+3
models/segmenters/grounded_sam2.py+3-1
models/segmenters/model_loader.py+41-4
utils/roofline.py+7