au-automatic人脸修复技术:GFPGAN与CodeFormer集成
【免费下载链接】automatic 项目地址: https://gitcode.com/GitHub_Trending/au/automatic
引言
在计算机视觉领域,人脸修复技术(Face Restoration)旨在恢复低质量人脸图像的细节特征,广泛应用于老照片修复、视频增强、虚拟形象生成等场景。au-automatic(SD.Next)作为开源的AI图像生成与处理平台,深度集成了当前主流的两种人脸修复算法——GFPGAN(Generative Facial Prior GAN)和CodeFormer,为用户提供了高效、可配置的人脸修复解决方案。本文将从技术实现、参数配置、性能对比和实战应用四个维度,全面解析这两种算法在au-automatic中的集成方案。
技术架构与集成原理
整体架构
au-automatic采用模块化设计实现人脸修复功能,核心架构包含三个层级:
- 用户交互层:通过WebUI提供参数调节界面(如修复强度、权重系数)
- 核心调度层:通过
face_restoration.py实现模型注册与调度逻辑 - 算法实现层:分别通过
gfpgan_model.py和codeformer_model.py实现具体修复算法
动态调度机制
核心调度逻辑位于modules/face_restoration.py,系统根据用户配置自动选择修复模型:
def restore_faces(np_image, p=None):
# 根据配置筛选可用修复器
face_restorers = [x for x in shared.face_restorers
if x.name() == shared.opts.face_restoration_model]
if len(face_restorers) == 0:
return np_image
# 执行修复
return face_restorers[0].restore(np_image, p)
这种设计允许用户在运行时动态切换修复模型,无需重启服务,提升了开发与测试效率。
GFPGAN集成方案
算法原理
GFPGAN由腾讯ARC实验室提出,通过引入生成人脸先验(Generative Facial Prior)解决传统方法中人脸细节模糊的问题。其核心创新点包括:
- 两阶段修复流程:先修复面部关键区域(眼睛、嘴巴等),再进行全局优化
- 预训练生成器:使用StyleGAN2作为生成先验,提供高质量人脸特征参考
- 面部解析辅助:结合FaceParse模块实现语义感知修复
代码实现
在au-automatic中,GFPGAN的集成通过modules/postprocess/gfpgan_model.py实现,核心步骤包括:
1. 模型加载与初始化
def setup_model(dirname):
# 创建模型目录
if not os.path.exists(model_path):
os.makedirs(model_path)
# 安装依赖
install('facexlib')
install('gfpgan')
# 注册修复器
class FaceRestorerGFPGAN(modules.detailer.Detailer):
def name(self):
return "GFPGAN"
def restore(self, np_image, p=None):
return gfpgan_fix_faces(np_image)
shared.face_restorers.append(FaceRestorerGFPGAN())
2. 修复执行流程
def gfpgan_fix_faces(np_image):
model = gfpgann() # 获取模型实例
if model is None:
return np_image
# 图像格式转换(RGB->BGR)
np_image_bgr = np_image[:, :, ::-1]
# 执行修复
_cropped_faces, _restored_faces, gfpgan_output_bgr = model.enhance(
np_image_bgr, has_aligned=False, only_center_face=False, paste_back=True
)
# 结果转换(BGR->RGB)
return gfpgan_output_bgr[:, :, ::-1]
参数配置
GFPGAN提供强度调节参数(gfpgan_visibility),取值范围为0.0~1.0,通过scripts/postprocessing_gfpgan.py实现UI集成:
def ui(self):
with gr.Accordion('Restore faces: GFPGan', open=False):
with gr.Row():
gfpgan_visibility = gr.Slider(
minimum=0.0, maximum=1.0, step=0.001,
label="Strength", value=0, elem_id="extras_gfpgan_visibility"
)
return { "gfpgan_visibility": gfpgan_visibility }
参数影响:
- 0.0:禁用修复
- 0.3~0.5:轻度修复,保留更多原始细节
- 0.8~1.0:深度修复,适合严重模糊图像
CodeFormer集成方案
算法原理
CodeFormer由香港中文大学提出,采用基于Transformer的编解码架构,通过代码本(codebook)实现人脸特征的精确重建。相比GFPGAN,其核心优势在于:
- 更精细的细节控制:通过
w参数调节修复强度与保真度平衡 - 鲁棒的姿态适应性:对侧脸、大角度人脸修复效果更稳定
- 语义一致性优化:引入语义感知损失函数,减少面部结构扭曲
代码实现
CodeFormer的集成通过modules/postprocess/codeformer_model.py实现,关键模块包括:
1. 模型初始化
def setup_model(dirname):
# 定义修复器类
class FaceRestorerCodeFormer(modules.detailer.Detailer):
def name(self):
return "CodeFormer"
def create_models(self):
# 加载CodeFormer网络
net = CodeFormer(
dim_embd=512, codebook_size=1024, n_head=8, n_layers=9
).to(devices.device)
# 加载预训练权重
checkpoint = torch.load(ckpt_path)['params_ema']
net.load_state_dict(checkpoint)
# 初始化人脸辅助工具
face_helper = FaceRestoreHelper(
1, face_size=512, det_model='retinaface_resnet50'
)
return net, face_helper
2. 修复执行流程
def restore(self, np_image, p=None, w=None):
# 图像预处理
np_image = np_image[:, :, ::-1] # RGB转BGR
original_resolution = np_image.shape[0:2]
# 人脸检测与对齐
self.face_helper.read_image(np_image)
self.face_helper.get_face_landmarks_5(only_center_face=False)
self.face_helper.align_warp_face()
# 人脸修复
for cropped_face in self.face_helper.cropped_faces:
# 图像转张量
cropped_face_t = img2tensor(cropped_face / 255., bgr2rgb=True)
normalize(cropped_face_t, (0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True)
# 模型推理
with devices.inference_context():
output = self.net(
cropped_face_t.unsqueeze(0),
w=w if w is not None else shared.opts.code_former_weight
)[0]
restored_face = tensor2img(output, rgb2bgr=True, min_max=(-1, 1))
self.face_helper.add_restored_face(restored_face)
# 结果融合
restored_img = self.face_helper.paste_faces_to_input_image()
return restored_img[:, :, ::-1] # BGR转RGB
参数配置
CodeFormer提供两个关键参数,通过scripts/postprocessing_codeformer.py集成到UI:
def ui(self):
with gr.Accordion('Restore faces: CodeFormer', open=False):
with gr.Row():
codeformer_visibility = gr.Slider(
minimum=0.0, maximum=1.0, step=0.01,
label="Strength", value=0.0
)
codeformer_weight = gr.Slider(
minimum=0.0, maximum=1.0, step=0.01,
label="Weight", value=0.2
)
return {
"codeformer_visibility": codeformer_visibility,
"codeformer_weight": codeformer_weight
}
参数说明:
codeformer_visibility:修复强度(0.0~1.0)codeformer_weight:保真度权重(0.0~1.0),值越高越接近原图
技术对比与选型指南
性能对比
| 指标 | GFPGAN | CodeFormer |
|---|---|---|
| 模型大小 | ~300MB | ~500MB |
| 推理速度(单人脸) | 较快(~0.2s/张) | 较慢(~0.5s/张) |
| 模糊图像修复 | ★★★★☆ | ★★★★★ |
| 侧脸修复 | ★★★☆☆ | ★★★★☆ |
| 细节保留 | ★★★☆☆ | ★★★★☆ |
| 卡通人脸适应性 | ★★★★☆ | ★★☆☆☆ |
| 资源占用 | 较低 | 较高 |
适用场景推荐
-
优先选择GFPGAN:
- 追求处理速度的场景(如视频批量处理)
- 卡通/动漫风格人脸修复
- 资源受限设备(如低显存GPU)
-
优先选择CodeFormer:
- 高保真度要求的老照片修复
- 复杂姿态人脸处理
- 需要精细控制修复效果的场景
实战应用指南
环境配置
au-automatic已内置GFPGAN和CodeFormer的依赖管理,首次使用时会自动安装所需库:
# GFPGAN依赖安装(自动触发)
from installer import install
install("facexlib")
install("gfpgan")
# CodeFormer依赖通过requirements.txt管理
# requirements.txt关键依赖项
torch>=2.8.0
torchvision>=0.23.0
facexlib
gfpgan
basicsr
快速上手示例
1. WebUI使用流程
- 在"Extra"选项卡中找到"Face Restoration"区域
- 选择修复模型(GFPGAN/CodeFormer)
- 调节对应参数(强度/权重)
- 上传图像并点击"Restore Faces"
2. API调用示例
通过cli/api-faceid.py可实现批量人脸修复:
# 设置修复参数
options['face'] = {
'mode': 'FaceID',
'ip_model': 'FaceID XL',
'source_images': [encode(args.face)],
'restoration_model': 'CodeFormer', # 指定修复模型
'restoration_strength': 0.8, # 修复强度
'codeformer_weight': 0.5 # CodeFormer权重
}
# 调用API
data = post('/sdapi/v1/txt2img', options)
高级调优技巧
-
参数组合优化:
- 轻度修复:CodeFormer(visibility=0.5, weight=0.7)
- 深度修复:CodeFormer(visibility=0.8, weight=0.3)
- 快速修复:GFPGAN(visibility=0.6)
-
多模型融合策略: 对复杂图像可先使用GFPGAN修复整体结构,再用CodeFormer优化细节:
# 伪代码示例:多模型级联修复
def cascade_restore(image):
# 第一步:GFPGAN快速修复
image = gfpgan_fix_faces(image, visibility=0.5)
# 第二步:CodeFormer细节优化
image = codeformer_fix_faces(image, visibility=0.3, weight=0.8)
return image
- 性能优化:
- 对视频序列处理,可启用模型缓存:
shared.opts.detailer_unload = False - 低显存设备可降低人脸检测分辨率:
face_size=256
- 对视频序列处理,可启用模型缓存:
结论与展望
au-automatic通过模块化设计实现了GFPGAN与CodeFormer的无缝集成,为用户提供了灵活高效的人脸修复解决方案。两种算法各有侧重,用户可根据具体场景选择或组合使用。未来发展方向包括:
- 模型轻量化:通过量化技术减小模型体积,提升推理速度
- 多模型融合:自动根据图像特征选择最优修复策略
- 实时视频修复:优化帧间连贯性,降低视频处理延迟
- 交互式修复:结合用户涂鸦实现指定区域精细修复
通过持续优化与创新,au-automatic将进一步提升人脸修复技术的易用性与处理质量,为数字内容创作提供更强有力的工具支持。
【免费下载链接】automatic 项目地址: https://gitcode.com/GitHub_Trending/au/automatic
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考



