File size: 4,382 Bytes
532466c
 
 
18196ae
 
 
532466c
 
 
 
18196ae
 
 
 
 
 
 
 
 
 
 
532466c
 
 
 
 
 
 
 
 
 
1003449
 
 
 
532466c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""Mutable global model state and semantic class definitions."""

import os
import threading
from contextlib import nullcontext

import torch

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# Serialises model inference on the single GPU while letting the CPU-bound
# pipeline stages of concurrent jobs overlap. Re-entrant so nested calls
# (e.g. warm-up -> run_segmentation) are safe.
_gpu_inference_lock = threading.RLock()


def gpu_inference():
    """Context manager guarding GPU model calls; a no-op on CPU, where the
    pipeline deliberately runs segmentation and geometry in parallel."""
    return _gpu_inference_lock if device.type == "cuda" else nullcontext()

# Model references (populated lazily by models/loader.py)
seg_processor = None
seg_model = None
segmentation_backend = "oneformer"
depth_processor = None
depth_model = None
moge_model = None
sam2_predictor = None
intrinsic_models = None

# ONNX Runtime sessions (populated lazily by models/onnx_loader.py)
seg_onnx_session = None
depth_onnx_session = None

ADE20K_CLASSES = [
    "wall", "building", "sky", "floor", "tree", "ceiling", "road", "bed",
    "window", "grass", "cabinet", "sidewalk", "person", "ground", "door",
    "table", "mountain", "plant", "curtain", "chair", "car", "water",
    "painting", "sofa", "shelf", "house", "sea", "mirror", "rug", "field",
    "armchair", "seat", "fence", "desk", "rock", "wardrobe", "lamp",
    "bathtub", "railing", "cushion", "base", "box", "column", "signboard",
    "chest of drawers", "counter", "sand", "sink", "skyscraper", "fireplace",
    "refrigerator", "stairs", "runway", "bookcase", "blind", "coffee table",
    "toilet", "flower", "book", "hill", "bench", "countertop", "stove",
    "palm", "kitchen island", "computer", "swivel chair", "boat", "bar",
    "arcade machine", "hovel", "bus", "towel", "light", "truck", "tower",
    "chandelier", "awning", "streetlight", "booth", "television", "airplane",
    "dirt track", "apparel", "pole", "land", "bannister", "escalator",
    "ottoman", "bottle", "buffet", "poster", "stage", "van", "ship",
    "fountain", "conveyer belt", "canopy", "washer", "plaything",
    "swimming pool", "stool", "barrel", "basket", "waterfall", "tent",
    "bag", "minibike", "cradle", "oven", "ball", "food", "step", "tank",
    "trade name", "microwave", "pot", "animal", "bicycle", "lake",
    "dishwasher", "screen", "blanket", "sculpture", "hood", "sconce",
    "vase", "traffic light", "tray", "ashcan", "fan", "pier", "crt screen",
    "plate", "monitor", "bulletin board", "shower", "radiator", "glass",
    "clock", "flag",
]

PRIMARY_FLOOR_CLASSES = {"floor", "rug", "carpet", "mat"}
REPLACEABLE_SOFT_FLOOR_CLASSES = {"rug", "carpet", "mat"}
FLOOR_SURFACE_CLASSES = {
    "floor", "road", "sidewalk", "ground", "field", "grass", "sand",
    "runway", "dirt track", "land", "stairs", "step", "rug", "carpet", "mat",
}
REJECT_SURFACE_CLASSES = {"wall", "ceiling", "building", "sky", "window"}
# TODO: This set is empty — all downstream soft_covering_mask logic in floor/mask.py is currently a no-op.
# Populate with class names (e.g. {"rug", "carpet"}) if soft-covering removal is needed.
SOFT_FLOOR_COVERING_CLASSES: set[str] = set()
OCCLUDER_CLASSES = {
    "bed", "cabinet", "person", "door", "table", "plant", "curtain", "chair",
    "car", "painting", "sofa", "shelf", "mirror", "armchair", "seat", "desk",
    "wardrobe", "lamp", "bathtub", "railing", "cushion", "base", "box",
    "column", "chest of drawers", "counter", "sink", "fireplace",
    "refrigerator", "bookcase", "blind", "coffee table", "toilet", "bench",
    "countertop", "stove", "kitchen island", "computer", "swivel chair",
    "bar", "ottoman", "bottle", "buffet", "poster", "towel", "television",
    "washer", "plaything", "stool", "basket", "bag", "cradle", "oven",
    "ball", "food", "microwave", "pot", "dishwasher", "blanket", "sculpture",
    "vase", "tray", "fan", "plate", "monitor", "shower", "radiator", "clock",
}


def hf_offline() -> bool:
    return os.getenv("HF_HUB_OFFLINE") == "1" or os.getenv("TRANSFORMERS_OFFLINE") == "1"


def class_name_for_id(class_id: int) -> str:
    return ADE20K_CLASSES[class_id] if class_id < len(ADE20K_CLASSES) else f"class_{class_id}"


def class_ids(names: set[str]) -> list[int]:
    return [idx for idx, name in enumerate(ADE20K_CLASSES) if name in names]