picrest / setup_models.py
concisewebsesign's picture
Upload 7 files
affeece verified
Raw History Blame Contribute Delete
1.44 kB
"""Fetch Microsoft's "Bringing Old Photos Back to Life" code and its global-stage checkpoints (~2 GB).
Runs once when the Space starts; skipped if already present."""
import os
import subprocess
import urllib.request
import zipfile
ROOT = os.path.dirname(os.path.abspath(__file__))
BOP = os.path.join(ROOT, "bop")
CKPT_URL = "https://github.com/microsoft/Bringing-Old-Photos-Back-to-Life/releases/download/v1.0/global_checkpoints.zip"
def ensure_models():
if not os.path.isdir(BOP):
subprocess.run(["git", "clone", "--depth", "1",
"https://github.com/microsoft/Bringing-Old-Photos-Back-to-Life.git", BOP], check=True)
det = os.path.join(BOP, "Global", "detection_models")
if not os.path.isdir(os.path.join(det, "sync_batchnorm")):
sb = os.path.join(det, "Synchronized-BatchNorm-PyTorch")
subprocess.run(["git", "clone", "--depth", "1",
"https://github.com/vacancy/Synchronized-BatchNorm-PyTorch", sb], check=True)
os.rename(os.path.join(sb, "sync_batchnorm"), os.path.join(det, "sync_batchnorm"))
ck = os.path.join(BOP, "Global", "checkpoints")
if not os.path.isdir(ck):
z = os.path.join(BOP, "Global", "checkpoints.zip")
urllib.request.urlretrieve(CKPT_URL, z)
with zipfile.ZipFile(z) as f:
f.extractall(os.path.join(BOP, "Global"))
os.remove(z)
if __name__ == "__main__":
ensure_models()