本文介绍StyleCLIP:文本驱动的图像处理。它结合StyleGAN V2与CLIP模型,通过语言描述编辑图像,不受预标注属性限制。复现用PaddleGAN的预训练模型,包括StyleGAN V2生成器、Pixel2Style2Pixel转换风格向量,依赖Paddle-CLIP和dlib。还说明安装、生成图片、风格向量及训练等步骤与参数。
☞☞☞AI 智能聊天, 问答助手, AI 智能搜索, 免费无限量使用 DeepSeek R1 模型☜☜☜

StyleCLIP: 文本驱动的图像处理
1. 简介
StyleGAN V2 的任务是使用风格向量进行image generation,而Clip guided Editing 则是利用CLIP (Contrastive Language-Image Pre-training ) 多模态预训练模型计算文本输入对应的风格向量变化,用文字表述来对图像进行编辑操纵风格向量进而操纵生成图像的属性。相比于Editing 模块,StyleCLIP不受预先统计的标注属性限制,可以通过语言描述自由控制图像编辑。
原论文中使用 Pixel2Style2Pixel 的 升级模型 Encode4Editing 计算要编辑的代表图像的风格向量,为尽量利用PaddleGAN提供的预训练模型本次复现中仍使用Pixel2Style2Pixel计算得到风格向量进行实验,重构效果略有下降,期待PaddleGAN跟进e4e相关工作。
准备代码
#!git clone --depth 1 https://github.com/ultranity/PaddleGAN
安装
StyleCLIP 模型 需要使用简介中对应提到的几个预训练模型, 本次复现使用PPGAN 提供的 在FFHQ数据集上进行预训练的StyleGAN V2 模型作为生成器,并使用Pixel2Style2Pixel模型将待编辑图像转换为对应风格向量。
CLIP模型依赖Paddle-CLIP实现。 pSp模型包含人脸检测步骤,依赖dlib框架。 除本repo外还需要安装 Paddle-CLIP 和 dlib 依赖。
整体安装方法如下。
pip install -e . pip install paddleclip pip install dlib-bin
%cd ~/PaddleGAN/ !pip install -e .
!pip install paddleclip dlib-bin
生成随机图片
%cd ~/PaddleGAN/
!python -u applications/tools/styleganv2.py \
--n_row 1 --n_col 1 \
--output_path '/home/aistudio/output_dir' --model_type ffhq-config-f --seed 1
/home/aistudio/PaddleGAN [06/16 23:30:17] ppgan INFO: Found /home/aistudio/.cache/ppgan/stylegan2-ffhq-config-f.pdparams W0616 23:30:20.268061 2162 gpu_context.cc:278] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 11.2, Runtime API Version: 10.1 W0616 23:30:20.272033 2162 gpu_context.cc:306] device: 0, cuDNN Version: 7.6.
生成风格向量
%cd ~/PaddleGAN/
!python -u applications/tools/pixel2style2pixel.py \
--input_image '/home/aistudio/output_dir/sample.png' \
--output_path '/home/aistudio/output_dir' --model_type ffhq-inversion --seed 2333
/home/aistudio/PaddleGAN [06/16 23:30:34] ppgan INFO: Found /home/aistudio/.cache/ppgan/pSp-ffhq-inversion.pdparams W0616 23:30:48.542668 2221 gpu_context.cc:278] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 11.2, Runtime API Version: 10.1 W0616 23:30:48.546530 2221 gpu_context.cc:306] device: 0, cuDNN Version: 7.6.
| @@##@@ | @@##@@ |
|---|---|
| 原图 | 重构 |
StyleCLIP 生成
参数说明:
- latent: 要编辑的代表图像的风格向量的路径。可来自于Pixel2Style2Pixel生成的dst.npy或StyleGANv2 Fitting模块生成的dst.fitting.npy
- output_path: 生成图片存放的文件夹
- weight_path: 或StyleGANv2 预训练模型路径
- model_type: 模型类型,当前使用: ffhq-config-f
- direction_path: 存放CLIP统计向量的文件路径
- stats_path: 存放向量统计数据的文件路径
- neutral: 对原图像的中性描述,如 face
- target: 为对目标图像的描述,如 young face
- beta_threshold: 向量调整阈值
- direction_offset: 属性的偏移强度
- cpu: 是否使用cpu推理,若不使用,请在命令中去除
!以下 参数需与StyleGAN 预训练模型保持一致
- size: 模型参数,输出图片的分辨率
- style_dim: 模型参数,风格z的维度
- n_mlp: 模型参数,风格z所输入的多层感知层的层数
- channel_multiplier: 模型参数,通道乘积,影响模型大小和生成图片质量
变换对举例
| Edit | Neutral Text | Target Text |
|---|---|---|
| Smile | face | smiling face |
| Gender | female face | male face |
| Blonde hair | face with hair | face with blonde hair |
| Hi-top fade | face with hair | face with Hi-top fade hair |
| Blue eyes | face with eyes | face with blue eyes |
目前有两套不同随机种子训练所得模型权重可用,生成结果细节有不同
--direction_path='stylegan2-ffhq-config-f-styleclip-global-directions.pdparams' \--stat_path='stylegan2-ffhq-config-f-styleclip-stats.pdparams'
--direction_path='stylegan2-ffhq-config-f-styleclip-global-directions0.pdparams' \--stat_path='stylegan2-ffhq-config-f-styleclip-stats0.pdparams'
%cd ~ !python PaddleGAN/ppgan/apps/styleganv2clip_predictor.py generate --latent 'output_dir/dst.npy' \ --neutral='face' --target='short hair face' \ --beta_threshold 0.12 --direction_offset 5 \ --direction_path='stylegan2-ffhq-config-f-styleclip-global-directions.pdparams' \ --stat_path='stylegan2-ffhq-config-f-styleclip-stats.pdparams'
/home/aistudio [06/16 23:35:40] ppgan INFO: Found /home/aistudio/.cache/ppgan/stylegan2-ffhq-config-f.pdparams W0616 23:35:43.785570 3708 gpu_context.cc:278] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 11.2, Runtime API Version: 10.1 W0616 23:35:43.789443 3708 gpu_context.cc:306] device: 0, cuDNN Version: 7.6. max delta_s is 0.2481601983308792 112 channels will be manipulated under the beta threshold 0.12
| @@##@@ | @@##@@ |
|---|---|
| 原图 | 生成 |
StyleCLIP 训练
在StyleCLIP论文中作者研究了 3 种结合 StyleGAN 和 CLIP 的方法:
- 文本引导的风格向量优化,使用 CLIP 模型作为损失网络对现有风格向量进行多次迭代更新,但该方法对每次处理都需要重新训练。
- 训练 风格向量映射器,使CLIP文本特征向量映射至StyleGAN 风格向量空间,避免(1)方法的训练问题,但可控性较差,经论文对比其生成质量也不如(3)。
- 在 StyleGAN 的 StyleSpace 中,把文本描述映射到输入图像的全局方向 (Global Direction),进而运行自由控制图像操作强度以及分离程度,实现类似于StyleGAN Editing 模块的使用体验。
本次仅复现论文中效果最好的 (3)Global Direction 方法。
StyleCLIP Global Direction 训练过程分两步:
- 提取风格向量并统计
python styleclip_getf.py
- 结合CLIP模型计算转换矩阵
python ppgan/apps/styleganv2clip_predictor.py extract
!python PaddleGAN/tools/styleclip_getf.py
[06/16 23:36:27] ppgan INFO: Found /home/aistudio/.cache/ppgan/stylegan2-ffhq-config-f.pdparams W0616 23:36:31.398654 3895 gpu_context.cc:278] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 11.2, Runtime API Version: 10.1 W0616 23:36:31.402468 3895 gpu_context.cc:306] device: 0, cuDNN Version: 7.6. 100%|███████████████████████████████████████| 1000/1000 [00:48<00:00, 20.51it/s] 100%|██████████████████████████████████████████| 20/20 [00:00<00:00, 106.77it/s] 100%|███████████████████████████████████████████| 20/20 [00:00<00:00, 35.95it/s] 100%|█████████████████████████████████████████| 980/980 [00:17<00:00, 56.01it/s] Done.
!python PaddleGAN/ppgan/apps/styleganv2clip_predictor.py extract
[05/19 19:35:44] ppgan INFO: Downloading stylegan2-ffhq-config-f.pdparams from https://paddlegan.bj.bcebos.com/models/stylegan2-ffhq-config-f.pdparams to /home/aistudio/.cache/ppgan/stylegan2-ffhq-config-f.pdparams 100%|████████████████████████████████| 194006/194006 [00:02<00:00, 72709.93it/s] W0519 19:35:51.973740 1169 gpu_context.cc:278] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 10.1, Runtime API Version: 10.1 W0519 19:35:51.977663 1169 gpu_context.cc:306] device: 0, cuDNN Version: 7.6. total channels to manipulate: 6048 Style manipulation in layer "0" 100%|███████████████████████████████████████| 512/512 [1:46:22<00:00, 12.40s/it] Style manipulation in layer "2" 100%|███████████████████████████████████████| 512/512 [1:45:44<00:00, 12.39s/it] Style manipulation in layer "3" 100%|███████████████████████████████████████| 512/512 [1:45:41<00:00, 12.38s/it] Style manipulation in layer "5" 100%|███████████████████████████████████████| 512/512 [1:45:41<00:00, 12.39s/it] Style manipulation in layer "6" 100%|███████████████████████████████████████| 512/512 [1:45:41<00:00, 12.39s/it] Style manipulation in layer "8" 100%|███████████████████████████████████████| 512/512 [1:45:39<00:00, 12.39s/it] Style manipulation in layer "9" 100%|███████████████████████████████████████| 512/512 [1:45:39<00:00, 12.37s/it] Style manipulation in layer "11" 100%|███████████████████████████████████████| 512/512 [1:45:39<00:00, 12.38s/it] Style manipulation in layer "12" 100%|███████████████████████████████████████| 512/512 [1:45:39<00:00, 12.39s/it] Style manipulation in layer "14" 100%|███████████████████████████████████████| 512/512 [1:45:45<00:00, 12.37s/it] Style manipulation in layer "15" 100%|█████████████████████████████████████████| 256/256 [52:48<00:00, 12.37s/it] Style manipulation in layer "17" 100%|█████████████████████████████████████████| 256/256 [53:00<00:00, 12.44s/it] Style manipulation in layer "18" 100%|█████████████████████████████████████████| 128/128 [26:32<00:00, 12.45s/it] Style manipulation in layer "20" 100%|█████████████████████████████████████████| 128/128 [26:34<00:00, 12.45s/it] Style manipulation in layer "21" 100%|███████████████████████████████████████████| 64/64 [13:16<00:00, 12.46s/it] Style manipulation in layer "23" 100%|███████████████████████████████████████████| 64/64 [13:17<00:00, 12.46s/it] Style manipulation in layer "24" 100%|███████████████████████████████████████████| 32/32 [06:38<00:00, 12.44s/it]













