新闻中心
StyleClip
本文介绍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相关工作。
准备代码
In [ ]#!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-binIn [ ]
%cd ~/PaddleGAN/ !pip install -e .In [ ]
!pip install paddleclip dlib-bin
生成随机图片
In [14]%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.
生成风格向量
In [15]%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: 模型参数,通道乘积,影响模型大小和生成图片质量
-direction_offset- 沿目标属性方向移动的修改强度,参考值为5。
Motiff妙多
Motiff妙多是一款AI驱动的界面设计工具,定位为“AI时代设计工具”
334
查看详情
-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'In [26]
%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 extractIn [27]
!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.In [6]
!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的详细内容,更多请关注其它相关文章!
# git
# 查询百度seo
# seo原创文章发布技巧
# 南充如何建设企业网站
# 包头怎样优化网站
# 则是
# 有可能
# 最好的
# 几个
# 官网
# 图像处理
# 不受
# 重构
# 一言
# 中文网
# type
# fig
# udio
# red
# ai
# python
# 福建长尾关键词排名
# 深圳神马seo推广外包
# 丹东网站建设优化
# 淄博网站优化活动
# 长沙县快手营销推广品牌
# 肇庆岳阳整合推广营销
相关栏目:
【
行业资讯67740 】
【
技术百科0 】
【
网络运营39195 】
相关推荐:
电脑5G怎么上传手机
迅达热水器显示power是什么意思
j*a二数组怎么创建
j*a中怎么截取数组
笔记本电脑多少钱
j*a数组怎么取元素
锤子手机怎么不出5g
如何用命令打开光驱
什么是base64
typescript和nodejs哪个好
油烟机上的power是什么意思
固态硬盘如何迁移系统
软件命令行参数如何设置
电瓶车充电器power是什么意思
怎么自学typescript
eraser是什么意思
电脑如何查看固态硬盘
苹果16更新了哪些功能
苹果16有哪些系统
如何看固态硬盘型号
抖音GMV是什么_抖音GMV是什么意思
animal是什么意思
怎么确定手机是5g
学typescript要求什么
j*a怎么让数组倒换
索尼type-c接口是什么
折叠屏有哪些手机
typescript是什么软件
单片机显存怎么设置最佳
市盈率回落是什么意思
苹果16更新了哪些版本
为什么夸克书架书单没了
系统如何装进固态硬盘
苹果16会有哪些更新
输入命令如何换行
夸克链信有什么用
单片机怎么连接电路图
折叠手机内屏为什么会坏
如何查看网站域名解析
固态硬盘如何启动
如何安装大华固态硬盘
如何检测固态硬盘温度
固态硬盘4k如何看
如何以命令符运行程序
cmd如何定时执行命令
type-c接口接地是什么意思
typescript怎么设置滚动条
如何知道固态硬盘
春运辅助抢票怎么抢
三星固态硬盘如何安装


2025-07-31
浏览次数:次
返回列表
: 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.