workspace / setup.sh
AntonioJun's picture
Add files using upload-large-folder tool
092603a verified
Raw
History Blame Contribute Delete
12.2 kB
#!/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