File size: 1,443 Bytes
affeece
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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()