Spaces:
Running on Zero
Running on Zero
Fix missing Kornia import in GHOST 2.0 blender
Browse files- ghost_runtime.py +28 -12
ghost_runtime.py
CHANGED
|
@@ -30,9 +30,35 @@ _LOCK = threading.Lock()
|
|
| 30 |
_ENGINE: "Ghost2Engine | None" = None
|
| 31 |
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
def _prepare_source() -> Path:
|
| 34 |
marker = SOURCE_DIR / ".ready"
|
| 35 |
if marker.exists():
|
|
|
|
| 36 |
return SOURCE_DIR
|
| 37 |
shutil.rmtree(SOURCE_DIR, ignore_errors=True)
|
| 38 |
SOURCE_DIR.mkdir(parents=True, exist_ok=True)
|
|
@@ -49,18 +75,8 @@ def _prepare_source() -> Path:
|
|
| 49 |
with archive.open(member) as source, destination.open("wb") as target:
|
| 50 |
shutil.copyfileobj(source, target)
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
|
| 54 |
-
crops = SOURCE_DIR / "src" / "utils" / "crops.py"
|
| 55 |
-
text = crops.read_text()
|
| 56 |
-
text = text.replace(
|
| 57 |
-
"from repos.emoca.gdl.datasets.ImageDatasetHelpers import bbox2point\n", ""
|
| 58 |
-
)
|
| 59 |
-
crops.write_text(text)
|
| 60 |
-
embedder = SOURCE_DIR / "src" / "aligner" / "embedder.py"
|
| 61 |
-
text = embedder.read_text()
|
| 62 |
-
text = text.replace("weights='DEFAULT'", "weights=None")
|
| 63 |
-
embedder.write_text(text)
|
| 64 |
marker.write_text("ready")
|
| 65 |
return SOURCE_DIR
|
| 66 |
|
|
|
|
| 30 |
_ENGINE: "Ghost2Engine | None" = None
|
| 31 |
|
| 32 |
|
| 33 |
+
def _patch_source(root: Path) -> None:
|
| 34 |
+
"""Apply small inference fixes that are missing from the upstream release."""
|
| 35 |
+
crops = root / "src" / "utils" / "crops.py"
|
| 36 |
+
text = crops.read_text()
|
| 37 |
+
text = text.replace(
|
| 38 |
+
"from repos.emoca.gdl.datasets.ImageDatasetHelpers import bbox2point\n", ""
|
| 39 |
+
)
|
| 40 |
+
crops.write_text(text)
|
| 41 |
+
|
| 42 |
+
embedder = root / "src" / "aligner" / "embedder.py"
|
| 43 |
+
text = embedder.read_text()
|
| 44 |
+
text = text.replace("weights='DEFAULT'", "weights=None")
|
| 45 |
+
embedder.write_text(text)
|
| 46 |
+
|
| 47 |
+
# Upstream BlenderGenerator calls kornia_morphology but relies on an import
|
| 48 |
+
# in the training module. Direct inference imports the generator itself, so
|
| 49 |
+
# make the dependency explicit in that module.
|
| 50 |
+
generator = root / "src" / "blender" / "generator.py"
|
| 51 |
+
text = generator.read_text()
|
| 52 |
+
import_line = "import src.utils.kornia_morphology as kornia_morphology\n"
|
| 53 |
+
if import_line not in text:
|
| 54 |
+
text = text.replace("import torch.nn.functional as F\n", f"import torch.nn.functional as F\n{import_line}")
|
| 55 |
+
generator.write_text(text)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
def _prepare_source() -> Path:
|
| 59 |
marker = SOURCE_DIR / ".ready"
|
| 60 |
if marker.exists():
|
| 61 |
+
_patch_source(SOURCE_DIR)
|
| 62 |
return SOURCE_DIR
|
| 63 |
shutil.rmtree(SOURCE_DIR, ignore_errors=True)
|
| 64 |
SOURCE_DIR.mkdir(parents=True, exist_ok=True)
|
|
|
|
| 75 |
with archive.open(member) as source, destination.open("wb") as target:
|
| 76 |
shutil.copyfileobj(source, target)
|
| 77 |
|
| 78 |
+
# Remove optional training-only imports and patch the upstream inference path.
|
| 79 |
+
_patch_source(SOURCE_DIR)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
marker.write_text("ready")
|
| 81 |
return SOURCE_DIR
|
| 82 |
|