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

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.
| @@##@@ | @@##@@ |
|---|---|
| 原图 | 重构 |
参数说明:
!以下 参数需与StyleGAN 预训练模型保持一致
-direction_offset- 沿目标属性方向移动的修改强度,参考值为5。
-beta_threshold- 较大的值意味着更强的解纠缠,而使得更少通道被编辑,以使得只有目标属性被更改,但如果beta_threshold太大则不足以执行所需的编辑。较小的值意味着更多的通道被编辑,有可能使其他不相关属性也被更改。
每次操作都会打印被操作的通道数,通常10-20个通道就足够了。对于较大的结构变化,通常需要100-200个通道,需要对应调整beta_threshold和direction_offset的值。
变换对举例
| 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论文中作者研究了 3 种结合 StyleGAN 和 CLIP 的方法:
本次仅复现论文中效果最好的 (3)Global Direction 方法。
StyleCLIP Global Direction 训练过程分两步:
python styleclip_getf.py
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]




以上就是StyleClip的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号