Ruffle Flash Player 扩展:3 个选项让老 Flash 游戏重新动起来
2026/10/19 1:30:30
本文旨在为使用GPEN人像修复增强模型的开发者和研究人员提供一套完整、可落地的数据准备流程。通过本教程,您将掌握:
完成本教程后,您即可基于自定义或公开数据集开展GPEN模型的微调或再训练任务。
建议读者具备以下基础:
本教程基于CSDN提供的“GPEN人像修复增强模型镜像”环境展开,已预装所有必要依赖,无需额外配置。
GPEN采用监督学习范式进行训练,其核心是输入一对图像:
模型目标是从LQ图像中恢复出接近HQ的细节,因此每张LQ必须有对应的HQ作为监督信号。
关键点:LQ与HQ必须严格对齐(同一人物、相同姿态、无裁剪偏移),否则会导致训练不稳定或伪影生成。
| 特征 | 说明 |
|---|---|
| 语义一致性 | LQ与HQ应为同一张原始图像的不同退化版本 |
| 空间对齐性 | 人脸关键点(眼、鼻、嘴)需精确对齐 |
| 退化多样性 | 包含多种模糊、噪声、下采样组合以提升泛化能力 |
| 分辨率匹配 | 推荐统一为512×512或1024×1024,符合GPEN默认输入尺寸 |
import cv2 import os from facexlib.detection import RetinaFaceDetector def align_and_crop_face(image_path, output_dir, target_size=512): # 初始化人脸检测器 detector = RetinaFaceDetector() img = cv2.imread(image_path) bboxes, landmarks = detector.detect_faces(img) if len(landmarks) == 0: print(f"No face detected in {image_path}") return # 使用五点对齐(两眼、鼻尖、嘴角) aligned_face = detector.align_multi(img, landmarks[0], target_size=target_size) filename = os.path.basename(image_path) cv2.imwrite(os.path.join(output_dir, filename), aligned_face) # 批量处理示例 hq_source_dir = "/path/to/hq_images" output_hq_dir = "/path/to/aligned_hq" os.makedirs(output_hq_dir, exist_ok=True) for file in os.listdir(hq_source_dir): align_and_crop_face(os.path.join(hq_source_dir, file), output_hq_dir)提示:
facexlib已在镜像中预装,可直接调用RetinaFaceDetector实现精准对齐。
BSRGAN 模拟真实相机退化过程(模糊+噪声+JPEG压缩),比简单双三次下采样更贴近实际场景。
# 克隆 BSRGAN 项目 git clone https://github.com/cszn/BSRGAN.git cd BSRGAN # 激活环境并运行降质脚本 conda activate torch25 python inference_bsrgan.py --input /path/to/aligned_hq --output /path/to/lq_images --scale 4参数说明:
--scale 4:生成1/4尺寸的低质图像(如512→128)--noise 10添加高斯噪声强度RealESRGAN 提供realesrgan-dataset-preprocess工具,支持多尺度退化。
# 安装 RealESRGAN(镜像中已包含) pip install realesrgan # 执行降质 realesrgan-dataset-preprocess \ -i /path/to/aligned_hq \ -o /path/to/lq_images \ --scale 4 \ --crop_size 512 \ --suffix "_lq"该工具会自动应用随机模糊核、噪声和压缩,生成更具多样性的LQ图像。
训练时需保证HQ与LQ文件名一致,便于按名称匹配读取。
dataset/ ├── train/ │ ├── hq/ │ │ ├── img001.png │ │ ├── img002.png │ │ └── ... │ └── lq/ │ ├── img001.png │ ├── img002.png │ └── ... └── val/ ├── hq/ └── lq/import os import shutil def sync_filenames(hq_dir, lq_dir, output_lq_dir): """确保LQ图像与HQ同名""" os.makedirs(output_lq_dir, exist_ok=True) hq_files = {f.split('.')[0]: f for f in os.listdir(hq_dir) if f.lower().endswith(('.png', '.jpg', '.jpeg'))} lq_files = {f.split('_lq')[0]: f for f in os.listdir(lq_dir) if '_lq' in f} for base_name, hq_file in hq_files.items(): if base_name in lq_files: src = os.path.join(lq_dir, lq_files[base_name]) dst = os.path.join(output_lq_dir, hq_file) # 使用HQ的文件名 shutil.copy(src, dst) else: print(f"Warning: No LQ found for {hq_file}") sync_filenames("/path/to/hq", "/path/to/lq_raw", "/path/to/lq")编写简单脚本检查对齐效果:
import cv2 import numpy as np def visualize_pair(hq_path, lq_path): hq = cv2.imread(hq_path) lq = cv2.imread(lq_path) # 上采样LQ以便对比 lq_up = cv2.resize(lq, (hq.shape[1], hq.shape[0]), interpolation=cv2.INTER_CUBIC) # 拼接显示 comparison = np.hstack([lq_up, hq]) cv2.imshow("LQ (left) vs HQ (right)", comparison) cv2.waitKey(0) cv2.destroyAllWindows() visualize_pair("dataset/train/hq/img001.png", "dataset/train/lq/img001.png")进入镜像环境,使用预训练模型测试LQ→HQ恢复效果:
cd /root/GPEN python inference_gpen.py --input dataset/train/lq/img001.png --output test_restore.png观察输出图像是否保留自然肤色、纹理细节,避免过度平滑或伪影。
修改options/train/GPEN_512.yml中的数据路径:
datasets: train: name: FFHQ type: PairedImageDataset dataroot_gt: /path/to/dataset/train/hq dataroot_lq: /path/to/dataset/train/lq filename_tmpl: '{}' io_backend: type: disk| 参数 | 推荐值 | 说明 |
|---|---|---|
lr_g | 2e-4 | 生成器学习率 |
lr_d | 1e-4 | 判别器学习率 |
warmup_iter | 3000 | 学习率预热步数 |
total_iter | 300000 | 总迭代次数 |
batch_size | 8~16 | 根据GPU显存调整 |
size | 512 | 输入图像尺寸 |
启动训练:
python train.py -opt options/train/GPEN_512.ymlfacexlib实现精准对齐,inference_gpen.py快速验证效果。获取更多AI镜像
想探索更多AI镜像和应用场景?访问 CSDN星图镜像广场,提供丰富的预置镜像,覆盖大模型推理、图像生成、视频生成、模型微调等多个领域,支持一键部署。