File size: 7,051 Bytes
40980f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env bash
# =============================================================================
# setup.sh — one-shot environment setup for the SMAL dog dataset render script.
#
# What it does:
#   1. (optional) creates the conda env from ../environment.yml  [--make-env]
#   2. clones the BITE renderer code        github.com/camenduru/bite_gradio-hf @ dev
#   3. clones the BITE/SMAL model weights    huggingface.co/camenduru/bite
#      and moves  data/ checkpoint/ datasets/  into the code checkout, so that
#      bite_gradio-hf/data/smal_data/new_dog_models/my_smpl_39dogsnorm_Jr_4_dog.pkl
#      (the '39dogs_norm' model the renderer needs) is in place.
#   4. patches the cloned BITE code (idempotent) for the renderer:
#        - adds the keyp_conf=='all' branch (47-keypoint output) to smal_torch_new.py
#        - makes torch.cross() pass dim=-1 in geometry_utils.py (future-proof)
#   5. installs chumpy into the env (BITE imports it; it needs --no-build-isolation,
#      so it is not listed in environment.yml).
#
# We do NOT redistribute any model weights — this reproduces the public
# camenduru colab (https://github.com/camenduru/bite-colab) flow. You remain
# bound by the upstream SMAL/MPI license terms (see docs/SMAL_SETUP.md).
#
# Usage:
#   bash scripts/setup.sh [--bite-root DIR] [--env-name NAME] [--make-env]
#
#   --bite-root DIR   where to place the BITE checkout   (default: <repo>/bite_gradio-hf)
#   --env-name NAME   conda env name to create           (default: eccv26_bdoped)
#   --make-env        also create the conda env from ../environment.yml
#
# Requirements: git, git-lfs (for the weights), and conda (only with --make-env).
# =============================================================================
set -euo pipefail

HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"   # repo/scripts
REPO_DIR="$(dirname "$HERE")"                            # repo/
# Default the BITE checkout into the repo itself so the repo is self-contained
# (the renderer resolves <repo>/bite_gradio-hf regardless of the working dir).
BITE_ROOT="$REPO_DIR/bite_gradio-hf"
ENV_NAME="eccv26_bdoped"
MAKE_ENV=0

while [[ $# -gt 0 ]]; do
  case "$1" in
    --bite-root) BITE_ROOT="$2"; shift 2 ;;
    --env-name)  ENV_NAME="$2";  shift 2 ;;
    --make-env)  MAKE_ENV=1;     shift 1 ;;
    -h|--help)   sed -n '2,30p' "$0"; exit 0 ;;
    *) echo "Unknown arg: $1" >&2; exit 2 ;;
  esac
done

echo ">> BITE_ROOT = $BITE_ROOT"
echo ">> ENV_NAME  = $ENV_NAME  (make-env=$MAKE_ENV)"

# --- 0. conda env (optional) -------------------------------------------------
if [[ "$MAKE_ENV" == "1" ]]; then
  echo ">> Creating conda env '$ENV_NAME' from $REPO_DIR/environment.yml"
  conda env create -n "$ENV_NAME" -f "$REPO_DIR/environment.yml"
  echo ">> Done. Activate with:  conda activate $ENV_NAME"
fi

command -v git >/dev/null || { echo "git not found" >&2; exit 1; }
git lfs version >/dev/null 2>&1 || { echo "git-lfs not found (needed for weights)" >&2; exit 1; }

# --- 1. BITE renderer code ---------------------------------------------------
if [[ -d "$BITE_ROOT/src/smal_pytorch" ]]; then
  echo ">> BITE code already present at $BITE_ROOT (skipping clone)"
else
  echo ">> Cloning BITE code -> $BITE_ROOT"
  git clone -b dev https://github.com/camenduru/bite_gradio-hf "$BITE_ROOT"
fi

# --- 2. BITE/SMAL weights ----------------------------------------------------
SMAL_PKL="$BITE_ROOT/data/smal_data/new_dog_models/my_smpl_39dogsnorm_Jr_4_dog.pkl"
if [[ -f "$SMAL_PKL" ]]; then
  echo ">> SMAL weights already in place ($SMAL_PKL) — skipping weights download"
else
  TMP_W="$(mktemp -d)"
  echo ">> Cloning weights huggingface.co/camenduru/bite -> $TMP_W/bite (~2 GB)"
  GIT_LFS_SKIP_SMUDGE=0 git clone https://huggingface.co/camenduru/bite "$TMP_W/bite"
  echo ">> Moving data/ checkpoint/ datasets/ into $BITE_ROOT"
  for d in data checkpoint datasets; do
    if [[ -d "$TMP_W/bite/$d" ]]; then
      mkdir -p "$BITE_ROOT/$d"
      cp -rn "$TMP_W/bite/$d/." "$BITE_ROOT/$d/"
    fi
  done
  rm -rf "$TMP_W"
  [[ -f "$SMAL_PKL" ]] || { echo "ERROR: expected $SMAL_PKL after setup" >&2; exit 1; }
fi

# --- 3. patch BITE code for the renderer (idempotent) ------------------------
echo ">> Patching BITE code for the renderer (idempotent)"
SMAL_PY="$BITE_ROOT/src/smal_pytorch/smal_model/smal_torch_new.py"
GEOM_PY="$BITE_ROOT/src/lifting_to_3d/utils/geometry_utils.py"
python - "$SMAL_PY" "$GEOM_PY" <<'PYEOF'
import sys
smal_py, geom_py = sys.argv[1], sys.argv[2]

# (a) smal_torch_new.py: add the keyp_conf == 'all' branch (returns all 47 joints/keypoints)
s = open(smal_py).read()
if "keyp_conf == 'all'" in s:
    print("   - smal_torch_new.py: 'all' branch already present")
else:
    lines = s.split("\n")
    out, done = [], False
    for i, ln in enumerate(lines):
        # insert just before the first 8-space `else:` that follows the 'dict' branch
        if (not done and ln.startswith("        else:")
                and any("elif keyp_conf == 'dict':" in p for p in lines[:i])):
            out.append("        elif keyp_conf == 'all':")
            out.append("            relevant_joints = joints")
            done = True
        out.append(ln)
    if not done:
        sys.exit("ERROR: could not locate insertion point for keyp_conf=='all' in " + smal_py)
    open(smal_py, "w").write("\n".join(out))
    print("   - smal_torch_new.py: inserted keyp_conf=='all' branch")

# (b) geometry_utils.py: torch.cross(b1, b2) -> torch.cross(b1, b2, dim=-1)
g = open(geom_py).read()
if "torch.cross(b1, b2, dim" in g:
    print("   - geometry_utils.py: torch.cross dim already set")
elif "torch.cross(b1, b2)" in g:
    open(geom_py, "w").write(g.replace("torch.cross(b1, b2)", "torch.cross(b1, b2, dim=-1)"))
    print("   - geometry_utils.py: pinned torch.cross dim=-1")
else:
    print("   - geometry_utils.py: torch.cross(b1, b2) not found (skipped)")
PYEOF

# --- 4. chumpy (required by BITE; needs --no-build-isolation) -----------------
echo ">> Ensuring chumpy is installed (BITE imports it)"
# Target the env's own python explicitly. `conda run -n NAME python` is unreliable
# here (it can resolve a python outside the env), so derive the interpreter path.
ENV_PY="$(conda info --base 2>/dev/null)/envs/$ENV_NAME/bin/python"
if [[ -x "$ENV_PY" ]]; then
  PY="$ENV_PY"                       # named env (created via environment.yml / --make-env)
elif command -v python >/dev/null 2>&1; then
  PY="$(command -v python)"          # fall back to the active python (env already activated)
else
  echo "WARN: no python found to install chumpy; activate the env then re-run." >&2
  PY=""
fi
if [[ -n "$PY" ]]; then
  echo ">> chumpy target python: $PY"
  if "$PY" -m pip show chumpy >/dev/null 2>&1; then
    echo ">> chumpy already installed"
  else
    "$PY" -m pip install -U pip setuptools wheel
    "$PY" -m pip install --no-build-isolation chumpy==0.70
  fi
fi

echo ">> Setup complete. SMAL model: $SMAL_PKL"
echo ">> Render with:  python scripts/render_smal_multiview.py --bite-root $BITE_ROOT ..."