update
This commit is contained in:
@@ -5,7 +5,8 @@ from __future__ import annotations
|
||||
|
||||
当前支持:
|
||||
- SDXL Inpaint(diffusers AutoPipelineForInpainting)
|
||||
- ControlNet(占位,暂未统一封装)
|
||||
- ControlNet(Stable Diffusion + ControlNet Inpaint)
|
||||
- LaMa(torchscript big-lama,本地封装)
|
||||
"""
|
||||
|
||||
from dataclasses import dataclass
|
||||
@@ -26,6 +27,7 @@ from config_loader import (
|
||||
class InpaintBackend(str, Enum):
|
||||
SDXL_INPAINT = "sdxl_inpaint"
|
||||
CONTROLNET = "controlnet"
|
||||
LAMA = "lama"
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -278,11 +280,13 @@ def _make_controlnet_predictor(_: UnifiedInpaintConfig):
|
||||
image_run = image
|
||||
mask_run = mask
|
||||
|
||||
# control image:使用 canny 边缘作为约束(最通用)
|
||||
# control image:使用 Canny 边缘作为条件(更通用、更稳定)。
|
||||
# 备注:当前项目的 ControlNet 权重未必是 inpaint 专用模型,
|
||||
# 若直接把洞内置零作为条件,容易把输出“拉黑”。先回退到稳定方案。
|
||||
rgb = np.array(image_run, dtype=np.uint8)
|
||||
edges = cv2.Canny(rgb, 100, 200)
|
||||
edges3 = np.stack([edges, edges, edges], axis=-1)
|
||||
control_image = Image.fromarray(edges3)
|
||||
control_image = Image.fromarray(edges3, mode="RGB")
|
||||
|
||||
if device == "cuda":
|
||||
try:
|
||||
@@ -312,6 +316,37 @@ def _make_controlnet_predictor(_: UnifiedInpaintConfig):
|
||||
return _predict
|
||||
|
||||
|
||||
def _make_lama_predictor(cfg: UnifiedInpaintConfig):
|
||||
import torch
|
||||
|
||||
app_cfg = load_app_config()
|
||||
device = cfg.device
|
||||
if device is None:
|
||||
device = app_cfg.inpaint.device
|
||||
device = "cuda" if device.startswith("cuda") and torch.cuda.is_available() else "cpu"
|
||||
|
||||
from .lama_torchscript import LaMaTorchscript
|
||||
|
||||
lama = LaMaTorchscript(device=device)
|
||||
|
||||
def _predict(
|
||||
image: Image.Image,
|
||||
mask: Image.Image,
|
||||
_prompt: str,
|
||||
_negative_prompt: str = "",
|
||||
strength: float = 0.8,
|
||||
guidance_scale: float = 7.5,
|
||||
num_inference_steps: int = 30,
|
||||
max_side: int = 1024,
|
||||
**_kwargs,
|
||||
) -> Image.Image:
|
||||
# LaMa 不使用 prompt/negative_prompt,也不使用 strength/steps 等扩散参数
|
||||
_ = (strength, guidance_scale, num_inference_steps, max_side)
|
||||
return lama(image, mask)
|
||||
|
||||
return _predict
|
||||
|
||||
|
||||
def build_inpaint_predictor(
|
||||
cfg: UnifiedInpaintConfig | None = None,
|
||||
) -> tuple[Callable[..., Image.Image], InpaintBackend]:
|
||||
@@ -326,6 +361,9 @@ def build_inpaint_predictor(
|
||||
if cfg.backend == InpaintBackend.CONTROLNET:
|
||||
return _make_controlnet_predictor(cfg), InpaintBackend.CONTROLNET
|
||||
|
||||
if cfg.backend == InpaintBackend.LAMA:
|
||||
return _make_lama_predictor(cfg), InpaintBackend.LAMA
|
||||
|
||||
raise ValueError(f"不支持的补全后端: {cfg.backend}")
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user