File size: 12,173 Bytes
9b812c9
092603a
2f5ffdf
092603a
 
 
2f5ffdf
092603a
 
 
 
 
 
 
 
 
9b812c9
2f5ffdf
9b812c9
2f5ffdf
18c7611
 
2f5ffdf
 
092603a
9b812c9
2f5ffdf
 
6b33a08
 
092603a
2f5ffdf
 
092603a
ba11c18
2f5ffdf
 
 
 
6b33a08
 
092603a
2f5ffdf
 
 
092603a
 
2f5ffdf
 
ba11c18
092603a
 
 
 
9911fbf
092603a
 
 
 
 
 
 
 
 
 
18c7611
2f5ffdf
 
092603a
 
9b812c9
092603a
 
 
 
2f5ffdf
092603a
2f5ffdf
 
092603a
2f5ffdf
092603a
18c7611
9b812c9
092603a
9b812c9
2f5ffdf
 
 
092603a
 
ba11c18
092603a
 
 
ba11c18
a7b7796
2f5ffdf
092603a
 
2f5ffdf
092603a
 
 
18c7611
092603a
18c7611
 
092603a
 
 
2f5ffdf
 
 
 
 
 
092603a
 
2f5ffdf
092603a
2f5ffdf
 
092603a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18c7611
2f5ffdf
092603a
 
 
 
 
 
 
2f5ffdf
092603a
 
 
 
 
 
 
 
 
 
 
 
 
 
18c7611
6b33a08
092603a
 
 
 
 
 
 
 
 
 
 
18c7611
2f5ffdf
8aea63c
2f5ffdf
092603a
2f5ffdf
 
092603a
 
 
2f5ffdf
 
092603a
8aea63c
092603a
8aea63c
092603a
8aea63c
 
 
83068db
ba11c18
2f5ffdf
092603a
 
 
 
 
6b33a08
092603a
6b33a08
 
 
 
 
 
 
092603a
 
8aea63c
092603a
8aea63c
092603a
8aea63c
7648a61
092603a
7648a61
092603a
8aea63c
 
7648a61
092603a
 
6b33a08
 
 
 
 
 
 
 
 
 
 
 
092603a
 
6b33a08
 
 
 
092603a
 
 
 
 
6b33a08
 
 
092603a
6b33a08
092603a
 
 
 
2f5ffdf
092603a
2f5ffdf
 
 
 
 
 
 
092603a
2f5ffdf
092603a
 
 
8aea63c
 
 
2f5ffdf
 
 
092603a
 
 
 
 
2f5ffdf
092603a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f5ffdf
092603a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f5ffdf
092603a
 
 
 
 
 
2f5ffdf
092603a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#!/usr/bin/env bash
# One-shot setup for this workspace.
#
# Installs packages required by every Python file under /workspace, preserves the
# project layout expected by the code, and downloads/clones the external data,
# repos, and checkpoints used at runtime.
#
# Usage:
#   ./setup.sh
#   HF_TOKEN=hf_xxx ./setup.sh -y
#   ./setup.sh --skip-models
#   ./setup.sh --skip-data
#   ./setup.sh --skip-workspace
#   ./setup.sh --with-caches
#   ./setup.sh --with-segvggt
#   ./setup.sh --force

set -euo pipefail

WORKSPACE_ROOT="${VSI_WORKSPACE_ROOT:-/workspace}"
DATA_ROOT="${VSI_DATA_ROOT:-/root/data}"
MODELS_ROOT="${VSI_MODELS_ROOT:-/root/models}"
VENV_ROOT="${VSI_VENV_ROOT:-/root/.venv}"
PYTHON_BIN="${VSI_PYTHON_BIN:-python3.11}"
WORKSPACE_REPO="${VSI_WORKSPACE_REPO:-AntonioJun/workspace}"

SKIP_MODELS=0
SKIP_DATA=0
SKIP_WORKSPACE=0
WITH_CACHES=0
WITH_SEGVGGT=0
FORCE=0
ASSUME_YES=0
HF_TOKEN="${HF_TOKEN:-${HUGGING_FACE_HUB_TOKEN:-}}"

for arg in "$@"; do
  case "$arg" in
    --skip-models) SKIP_MODELS=1 ;;
    --skip-data) SKIP_DATA=1 ;;
    --skip-workspace) SKIP_WORKSPACE=1 ;;
    --with-caches) WITH_CACHES=1 ;;
    --with-segvggt) WITH_SEGVGGT=1 ;;
    --force) FORCE=1 ;;
    -y|--yes) ASSUME_YES=1 ;;
    --token=*) HF_TOKEN="${arg#--token=}" ;;
    -h|--help) awk 'NR == 1 {next} /^#/ {sub(/^# ?/, ""); print; next} {exit}' "$0"; exit 0 ;;
    *) echo "unknown argument: $arg" >&2; exit 1 ;;
  esac
done

log() { printf '\n==> %s\n' "$1"; }
warn() { printf '!! %s\n' "$1" >&2; }
die() { printf 'XX %s\n' "$1" >&2; exit 1; }


if [ -z "$HF_TOKEN" ] && { [ "$SKIP_DATA" -eq 0 ] || [ "$SKIP_MODELS" -eq 0 ] || [ "$SKIP_WORKSPACE" -eq 0 ]; }; then
  if [ "$ASSUME_YES" -eq 1 ]; then
    warn "HF_TOKEN is empty; public downloads may work, gated downloads will fail"
  else
    log "Hugging Face token requested for gated/model/dataset downloads"
    echo "Create/read one at https://huggingface.co/settings/tokens."
    echo "You also need access to facebook/sam3 and nyu-visionx/VSI-Bench where applicable."
    read -r -s -p "HF token (input hidden, blank to continue without one): " HF_TOKEN
    echo
  fi
fi
export HF_TOKEN
export HUGGING_FACE_HUB_TOKEN="$HF_TOKEN"
export HF_HUB_ENABLE_HF_TRANSFER=1
export HF_XET_HIGH_PERFORMANCE=1

log "Checking system packages"
missing=()
for bin in git curl unzip "$PYTHON_BIN"; do
  command -v "$bin" >/dev/null 2>&1 || missing+=("$bin")
done
if [ "${#missing[@]}" -gt 0 ]; then
  if command -v apt-get >/dev/null 2>&1; then
    apt-get update -qq
    apt-get install -y -qq git curl unzip python3.11 python3.11-venv python3.11-dev build-essential ffmpeg
  else
    die "missing required system tools: ${missing[*]}"
  fi
fi
mkdir -p "$DATA_ROOT" "$DATA_ROOT/caches" "$DATA_ROOT/spatial codes" "$MODELS_ROOT"

clone_repo() {
  local url="$1" dest="$2"
  if [ -d "$dest/.git" ] && [ "$FORCE" -eq 0 ]; then
    log "Already cloned: $dest"
    return
  fi
  log "Cloning $url -> $dest"
  rm -rf "$dest"
  git clone --depth 1 "$url" "$dest"
}

create_venv() {
  if [ -d "$VENV_ROOT" ] && [ "$FORCE" -eq 0 ]; then
    log "Using existing venv: $VENV_ROOT"
  else
    log "Creating venv: $VENV_ROOT"
    rm -rf "$VENV_ROOT"
    "$PYTHON_BIN" -m venv "$VENV_ROOT"
  fi
  "$VENV_ROOT/bin/pip" install --upgrade -q pip wheel "setuptools<81"
}

install_workspace_requirements() {
  log "Installing Python packages used by every /workspace Python file"
  "$VENV_ROOT/bin/pip" install -q -r /dev/stdin <<'REQS'
numpy<2
scipy
pandas
PyYAML
loguru
datasets
Pillow
opencv-python==4.11.0.86
opencv-contrib-python-headless==4.10.0.84
torch
torchvision
accelerate==1.14.0
transformers==5.14.1
huggingface_hub[cli]>=0.24
hf_transfer
safetensors
timm
einops
sentencepiece
protobuf
av
imageio
pycocotools
hydra-core
omegaconf
pytest>=8.3.5
black
REQS
}

install_external_repos() {
  log "Cloning model source repositories under $MODELS_ROOT"
  clone_repo "https://github.com/facebookresearch/sam3.git" "$MODELS_ROOT/sam3"
  clone_repo "https://github.com/bytedance-seed/depth-anything-3.git" "$MODELS_ROOT/depth-anything-3"
  if [ "$WITH_SEGVGGT" -eq 1 ]; then
    clone_repo "https://github.com/Seed3D/SegVGGT.git" "$MODELS_ROOT/SegVGGT"
  fi

  log "Installing editable model repos where present"
  if [ -f "$MODELS_ROOT/sam3/pyproject.toml" ] || [ -f "$MODELS_ROOT/sam3/setup.py" ]; then
    "$VENV_ROOT/bin/pip" install -q -e "$MODELS_ROOT/sam3"
  else
    warn "SAM3 repo has no pyproject.toml/setup.py at $MODELS_ROOT/sam3; skipped editable install"
  fi
  if [ -f "$MODELS_ROOT/depth-anything-3/pyproject.toml" ] || [ -f "$MODELS_ROOT/depth-anything-3/setup.py" ]; then
    "$VENV_ROOT/bin/pip" install -q -e "$MODELS_ROOT/depth-anything-3"
  else
    warn "DA3 repo has no pyproject.toml/setup.py at $MODELS_ROOT/depth-anything-3; skipped editable install"
  fi
  if [ "$WITH_SEGVGGT" -eq 1 ] && [ -f "$MODELS_ROOT/SegVGGT/requirements.txt" ]; then
    "$VENV_ROOT/bin/pip" install -q -r "$MODELS_ROOT/SegVGGT/requirements.txt"
  fi
}

hf_cli() {
  if [ -x "$VENV_ROOT/bin/hf" ]; then
    printf '%s
' "$VENV_ROOT/bin/hf"
  elif [ -x "$VENV_ROOT/bin/huggingface-cli" ]; then
    printf '%s
' "$VENV_ROOT/bin/huggingface-cli"
  else
    command -v hf || command -v huggingface-cli || true
  fi
}

hf_download() {
  local repo_id="$1" repo_type="$2" dest="$3" optional="${4:-}"
  if [ -d "$dest" ] && [ "$(ls -A "$dest" 2>/dev/null)" ] && [ "$FORCE" -eq 0 ]; then
    log "Already present: $dest"
    return
  fi
  local cli
  cli="$(hf_cli)"
  [ -n "$cli" ] || die "no hf/huggingface-cli command available"
  log "Downloading $repo_id ($repo_type) -> $dest"
  mkdir -p "$dest"
  token_args=()
  [ -n "$HF_TOKEN" ] && token_args=(--token "$HF_TOKEN")
  if ! "$cli" download "$repo_id" --repo-type "$repo_type" --local-dir "$dest" "${token_args[@]}"; then
    if [ "$optional" = "optional" ]; then
      warn "download failed but marked optional: $repo_id"
    else
      die "download failed: $repo_id"
    fi
  fi
}

sync_workspace_from_backup() {
  [ "$SKIP_WORKSPACE" -eq 0 ] || return 0
  log "Syncing workspace files from Hugging Face dataset repo: $WORKSPACE_REPO"
  WITH_CACHES="$WITH_CACHES" WORKSPACE_REPO="$WORKSPACE_REPO" WORKSPACE_ROOT="$WORKSPACE_ROOT" \
    "$VENV_ROOT/bin/python" - <<'PYSYNC'
import os
import tarfile
from pathlib import Path
from huggingface_hub import HfApi, hf_hub_download

repo = os.environ["WORKSPACE_REPO"]
root = Path(os.environ["WORKSPACE_ROOT"])
token = os.environ.get("HF_TOKEN") or None
api = HfApi(token=token)
folders = ["harness", "symbolic", "analysis", "corruption", "calibration", "encoder", "inference", "tests"]
wanted_spatial = (
    "data/spatial codes/sam3+depth-anything-3/metric/tracking/selective/32/",
    "data/spatial codes/sam3+depth-anything-3/metric/tracking/uniform/32/",
    "data/spatial codes/ground truth/explicit/",
    "data/spatial codes/ground truth/compact/",
)
try:
    bundle = hf_hub_download(repo, "bundles/spatial-codes.tar.gz", repo_type="dataset", token=token)
    with tarfile.open(bundle) as tar:
        members = [m for m in tar.getmembers() if any(m.name.startswith(w) for w in wanted_spatial)]
        tar.extractall(root, members=members)
    print(f"[data/spatial codes] {len(members)} files extracted from bundle", flush=True)
except Exception as exc:
    print(f"[data/spatial codes] bundle unavailable ({exc}); using per-file fallback", flush=True)
    folders.extend(w.rstrip("/") for w in wanted_spatial)
if os.environ.get("WITH_CACHES") == "1":
    folders.append("data/caches")

def fetch(rel):
    dest = root / rel
    if dest.is_file() and dest.stat().st_size > 0:
        return 0
    hf_hub_download(repo, rel, repo_type="dataset", local_dir=str(root), token=token)
    return 1

for folder in folders:
    files = [
        e.path
        for e in api.list_repo_tree(repo, repo_type="dataset", path_in_repo=folder, recursive=True)
        if e.__class__.__name__ == "RepoFile"
    ]
    got = sum(fetch(rel) for rel in files)
    print(f"[{folder}] {len(files)} files ({got} downloaded, rest already present)", flush=True)
for rel in ["README.md", "backup.py", "selective_frame_counts.csv"]:
    try:
        fetch(rel)
    except Exception as exc:
        print(f"[{rel}] skipped: {exc}", flush=True)
print("workspace sync complete", flush=True)
PYSYNC
  chmod +x "$WORKSPACE_ROOT/setup.sh" "$WORKSPACE_ROOT/backup.py" 2>/dev/null || true
}

install_data() {
  [ "$SKIP_DATA" -eq 0 ] || return 0
  log "Cloning thinking-in-space under $DATA_ROOT"
  clone_repo "https://github.com/vision-x-nyu/thinking-in-space.git" "$DATA_ROOT/thinking-in-space"
  hf_download "nyu-visionx/VSI-Bench" dataset "$DATA_ROOT/VSI-Bench"
  log "Extracting VSI-Bench scene archives"
  for name in scannet arkitscenes scannetpp; do
    zip_path="$DATA_ROOT/VSI-Bench/${name}.zip"
    out_dir="$DATA_ROOT/VSI-Bench/${name}"
    if [ -f "$zip_path" ] && { [ ! -d "$out_dir" ] || [ "$FORCE" -eq 1 ]; }; then
      unzip -q -o "$zip_path" -d "$DATA_ROOT/VSI-Bench"
    fi
  done
}

install_models() {
  [ "$SKIP_MODELS" -eq 0 ] || return 0
  log "Downloading model checkpoints under $MODELS_ROOT"
  hf_download "facebook/sam3" model "$MODELS_ROOT/sam3/checkpoints" optional
  hf_download "depth-anything/DA3-LARGE-1.1" model "$MODELS_ROOT/depth-anything-3/checkpoints/DA3-LARGE-1.1" optional
  hf_download "depth-anything/DA3NESTED-GIANT-LARGE-1.1" model "$MODELS_ROOT/depth-anything-3/checkpoints/DA3NESTED-GIANT-LARGE-1.1" optional
  hf_download "Qwen/Qwen3.5-4B" model "$MODELS_ROOT/qwen3.5-4b"
  hf_download "Qwen/Qwen3.5-2B" model "$MODELS_ROOT/qwen3.5-2b"
  hf_download "OpenGVLab/InternVL3_5-4B-HF" model "$MODELS_ROOT/internvl3.5-4b"
  hf_download "OpenGVLab/InternVL3_5-2B-HF" model "$MODELS_ROOT/internvl3.5-2b"
  if [ "$WITH_SEGVGGT" -eq 1 ]; then
    hf_download "Seed3D/SegVGGT" model "$MODELS_ROOT/SegVGGT/checkpoint" optional
  fi
}

write_env_file() {
  log "Writing environment helper: /root/vsi-env.sh"
  cat > /root/vsi-env.sh <<ENVEOF
export VSI_WORKSPACE_ROOT="$WORKSPACE_ROOT"
export VSI_DATA_ROOT="$DATA_ROOT"
export VSI_ROOT="$DATA_ROOT/VSI-Bench"
export VSI_CACHE_ROOT="$DATA_ROOT/caches"
export VSI_CODES="$DATA_ROOT/spatial codes"
export VSI_MODELS_ROOT="$MODELS_ROOT"
export VSI_SAM3_ROOT="$MODELS_ROOT/sam3"
export VSI_DA3_ROOT="$MODELS_ROOT/depth-anything-3"
export VSI_SEGVGGT_ROOT="$MODELS_ROOT/SegVGGT"
export HARNESS_OFFICIAL_EVAL="$DATA_ROOT/thinking-in-space/lmms_eval/tasks/vsibench/utils.py"
export SYMBOLIC_OFFICIAL_EVAL="$DATA_ROOT/thinking-in-space/lmms_eval/tasks/vsibench/utils.py"
export PYTHONPATH="$WORKSPACE_ROOT:\$PYTHONPATH"
ENVEOF
  cat > /root/.venv-map.json <<MAPEOF
{
  "mode": "shared",
  "venv": "$VENV_ROOT",
  "workspace": "$WORKSPACE_ROOT",
  "data_root": "$DATA_ROOT",
  "models_root": "$MODELS_ROOT"
}
MAPEOF
}

smoke_test_imports() {
  log "Running import smoke test for workspace dependency coverage"
  "$VENV_ROOT/bin/python" - <<'PYSMOKE'
import importlib
mods = [
    "numpy", "scipy", "pandas", "yaml", "loguru", "PIL.Image", "cv2",
    "torch", "torchvision", "transformers", "huggingface_hub", "hydra",
    "pytest", "black", "pycocotools.mask",
]
failed = []
for mod in mods:
    try:
        importlib.import_module(mod)
    except Exception as exc:
        failed.append(f"{mod}: {exc}")
if failed:
    print("IMPORT_SMOKE_FAILED")
    for item in failed:
        print(" -", item)
    raise SystemExit(1)
print("IMPORT_SMOKE_OK")
PYSMOKE
}

final_checks() {
  log "Running final workspace checks"
  (cd "$WORKSPACE_ROOT" && "$VENV_ROOT/bin/python" -m black --check .)
  (cd "$WORKSPACE_ROOT" && "$VENV_ROOT/bin/python" -m compileall -q .)
  (cd "$WORKSPACE_ROOT" && "$VENV_ROOT/bin/python" -m pytest -q) || warn "pytest did not fully pass; review output above"
}

create_venv
install_workspace_requirements
install_external_repos
sync_workspace_from_backup
install_data
install_models
write_env_file
smoke_test_imports
final_checks

log "Setup complete"
cat <<DONEEOF

Workspace:  $WORKSPACE_ROOT
Data:       $DATA_ROOT
Models:     $MODELS_ROOT
Venv:       $VENV_ROOT
Env helper: /root/vsi-env.sh

Activate with:
  source $VENV_ROOT/bin/activate
  source /root/vsi-env.sh

DONEEOF