Upload encoder/code/k3_kaggle_driver.py with huggingface_hub
Browse files- encoder/code/k3_kaggle_driver.py +117 -0
encoder/code/k3_kaggle_driver.py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""K3 on Kaggle: the driver the notebook cell downloads and runs.
|
| 2 |
+
|
| 3 |
+
The notebook itself is six lines (fetch this file from HF, run it). This file
|
| 4 |
+
holds the sequence so a change is one upload, not one edited cell:
|
| 5 |
+
|
| 6 |
+
1. restore inputs.tgz + the three checkpoints from idefinity/flip-student-2step
|
| 7 |
+
2. setup,prep clone upstream, 4.46 GB teachers, cache 200 images
|
| 8 |
+
3. matte 7 -> 20 (reads k3/weights/matte.pt, which is matte_full.pt)
|
| 9 |
+
4. export fp32 ONNX + parity, writes encoder_meta.json
|
| 10 |
+
5. e2e teacher DiT 2-step, student vs teacher conditions
|
| 11 |
+
|
| 12 |
+
Everything lands in /kaggle/working/k3, which Kaggle saves as the version output.
|
| 13 |
+
|
| 14 |
+
Env: FLIP_NB_STAGES (default "restore,setup,prep,train,export,e2e"),
|
| 15 |
+
FLIP_EPOCHS (matte target, default 20), HF_ENDPOINT (default the mirror).
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
import importlib.util
|
| 19 |
+
import os
|
| 20 |
+
import pathlib
|
| 21 |
+
import shutil
|
| 22 |
+
import subprocess
|
| 23 |
+
import sys
|
| 24 |
+
import tarfile
|
| 25 |
+
import time
|
| 26 |
+
|
| 27 |
+
WORK = pathlib.Path(os.environ.get("FLIP_WORK", "/kaggle/working"))
|
| 28 |
+
OUT = WORK / "k3"
|
| 29 |
+
REPO_ID = "idefinity/flip-student-2step"
|
| 30 |
+
K3 = WORK / "k3_encoder_distill.py"
|
| 31 |
+
STAGES = [s.strip() for s in os.environ.get(
|
| 32 |
+
"FLIP_NB_STAGES", "restore,setup,prep,train,export,e2e").split(",") if s.strip()]
|
| 33 |
+
EPOCHS = os.environ.get("FLIP_EPOCHS", "20")
|
| 34 |
+
|
| 35 |
+
os.environ.setdefault("HF_ENDPOINT", "https://hf-mirror.com")
|
| 36 |
+
os.environ.setdefault("PYTHONUNBUFFERED", "1")
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def p(*a):
|
| 40 |
+
print(f"[{time.strftime('%H:%M:%S')}]", *a, flush=True)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def fetch(path):
|
| 44 |
+
from huggingface_hub import hf_hub_download # noqa: PLC0415
|
| 45 |
+
p("fetch", path)
|
| 46 |
+
return hf_hub_download(REPO_ID, path, local_dir=str(WORK / "dl"))
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def restore():
|
| 50 |
+
W = OUT / "weights"
|
| 51 |
+
W.mkdir(parents=True, exist_ok=True)
|
| 52 |
+
shutil.copy(fetch("encoder/code/k3_encoder_distill.py"), K3)
|
| 53 |
+
p("k3 script", K3.stat().st_size, "B")
|
| 54 |
+
if not (WORK / "inputs").is_dir():
|
| 55 |
+
with tarfile.open(fetch("encoder/checkpoints/inputs.tgz")) as tf:
|
| 56 |
+
tf.extractall(WORK)
|
| 57 |
+
for src, dst in (("matte_full.pt", "matte.pt"),
|
| 58 |
+
("dino_model.pt", "dino.pt"),
|
| 59 |
+
("vae_model.pt", "vae.pt")):
|
| 60 |
+
shutil.copy(fetch(f"encoder/checkpoints/{src}"), W / dst)
|
| 61 |
+
p("weights", dst, (W / dst).stat().st_size)
|
| 62 |
+
imgs = sorted((WORK / "inputs").rglob("*.jp*g"))
|
| 63 |
+
p("inputs", len(imgs), "->", imgs[0].name if imgs else None)
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def run_k3(stages):
|
| 67 |
+
env = dict(os.environ, FLIP_WORK=str(WORK), FLIP_IMAGE_DIR=str(WORK / "inputs"),
|
| 68 |
+
FLIP_N_IMG="200", FLIP_EPOCHS=EPOCHS)
|
| 69 |
+
cmd = [sys.executable, str(K3), "--stages", stages]
|
| 70 |
+
p("$", " ".join(cmd))
|
| 71 |
+
subprocess.run(cmd, env=env, check=True)
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def load_k3():
|
| 75 |
+
os.environ.update(
|
| 76 |
+
FLIP_WORK=str(WORK), FLIP_DINO_ARCH="conv", FLIP_DINO_DIM="512",
|
| 77 |
+
FLIP_DINO_DEPTH="8", FLIP_FP16="0",
|
| 78 |
+
PYTORCH_CUDA_ALLOC_CONF="expandable_segments:True")
|
| 79 |
+
spec = importlib.util.spec_from_file_location("k3", str(K3))
|
| 80 |
+
mod = importlib.util.module_from_spec(spec)
|
| 81 |
+
spec.loader.exec_module(mod)
|
| 82 |
+
mod.EPOCHS = int(EPOCHS)
|
| 83 |
+
return mod
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def main():
|
| 87 |
+
p("stages", STAGES, "epochs", EPOCHS)
|
| 88 |
+
if "restore" in STAGES:
|
| 89 |
+
restore()
|
| 90 |
+
if "setup" in STAGES or "prep" in STAGES:
|
| 91 |
+
run_k3(",".join(s for s in STAGES if s in ("setup", "prep")))
|
| 92 |
+
k3 = None
|
| 93 |
+
if "train" in STAGES:
|
| 94 |
+
k3 = load_k3()
|
| 95 |
+
p("train matte on cuda; resumes from epoch in weights/matte.pt")
|
| 96 |
+
k3.train_matte("cuda")
|
| 97 |
+
import torch # noqa: PLC0415
|
| 98 |
+
if torch.cuda.is_available():
|
| 99 |
+
torch.cuda.empty_cache()
|
| 100 |
+
if "export" in STAGES:
|
| 101 |
+
k3 = k3 or load_k3()
|
| 102 |
+
p("export")
|
| 103 |
+
meta = k3.export()
|
| 104 |
+
p("meta", meta)
|
| 105 |
+
if "e2e" in STAGES:
|
| 106 |
+
k3 = k3 or load_k3()
|
| 107 |
+
p("e2e")
|
| 108 |
+
k3.e2e()
|
| 109 |
+
p((OUT / "e2e.json").read_text())
|
| 110 |
+
p("outputs")
|
| 111 |
+
for f in sorted(OUT.rglob("*")):
|
| 112 |
+
if f.is_file() and f.suffix in (".onnx", ".json", ".pt", ".txt"):
|
| 113 |
+
p(" ", f.relative_to(WORK), f.stat().st_size)
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
if __name__ == "__main__":
|
| 117 |
+
main()
|