图像修复控制网络方案怎么测:可复现评测指南

作者:Nicky2026.11.13 03:37浏览量:0

简介:本文给出一套可复现的图像修复控制网络方案评测流程,涵盖测试环境、版本记录、功能与质量指标、显存与延迟测量及异常输入检查。读者可据此在自己的硬件上测出可比较的数据,再做选型判断。本文不预设性能结论,只提供方法与记录模板。

一、为什么先定测试口径

图像修复控制网络方案的表现受模型版本、采样参数、掩码质量和硬件影响很大。同一套权重换一组采样器,边缘效果可能明显不同。因此评测前要先固定口径,保证别人能复现你的数字。本文给出的是测量方法和记录模板,不包含未经验证的性能数字。

二、测试环境与版本记录

每次测试至少记录以下信息:

  • GPU 型号、显存容量、驱动版本与 CUDA 版本
  • Python 与 PyTorch 版本
  • diffusers 版本,ControlNet 权重与基础模型的仓库 ID 及提交哈希
  • 推理精度(fp16 或 fp32)与批大小(batch size)
  • 操作系统与内核版本

可用 pip show diffusers torch 与 nvidia-smi 导出这些信息,并保存到测试报告附录中。

三、最小可运行示例

下面的脚本使用 Hugging Face 的 diffusers 库,加载 ControlNet 的 inpaint 权重。运行前请在 Hub 页面核对仓库 ID 与许可证,仓库可能调整。

  1. import numpy as np
  2. import torch
  3. from PIL import Image
  4. from diffusers import ControlNetModel, StableDiffusionControlNetInpaintPipeline
  5. def make_inpaint_condition(image, mask):
  6. img = np.array(image.convert("RGB")).astype(np.float32) / 255.0
  7. m = np.array(mask.convert("L")).astype(np.float32) / 255.0
  8. img[m > 0.5] = -1.0 # 待修复像素置为 -1
  9. return torch.from_numpy(img.transpose(2, 0, 1)[None])
  10. controlnet = ControlNetModel.from_pretrained(
  11. "lllyasviel/control_v11p_sd15_inpaint", torch_dtype=torch.float16)
  12. pipe = StableDiffusionControlNetInpaintPipeline.from_pretrained(
  13. "stable-diffusion-v1-5/stable-diffusion-v1-5",
  14. controlnet=controlnet, torch_dtype=torch.float16).to("cuda")
  15. size = (512, 512)
  16. image = Image.open("input.png").convert("RGB").resize(size)
  17. mask = Image.open("mask.png").convert("L").resize(size) # 白色为待修复区域
  18. control = make_inpaint_condition(image, mask)
  19. gen = torch.Generator("cuda").manual_seed(42)
  20. out = pipe(prompt="修复提示词", image=image, mask_image=mask,
  21. control_image=control, num_inference_steps=30,
  22. guidance_scale=7.5, generator=gen).images[0]
  23. out.save("output.png")

代码要点如下。掩码中白色区域为待修复区域,原图与掩码的尺寸必须一致。控制图由 make_inpaint_condition 生成。固定 seed 后,同一输入可以复现同一输出。

四、评测维度与指标

维度 指标 测量方法 说明
修复区域质量 LPIPS、人工评分 有参考图时计