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-bin
    In [ ]
%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。

-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 的方法:

  1. 文本引导的风格向量优化,使用 CLIP 模型作为损失网络对现有风格向量进行多次迭代更新,但该方法对每次处理都需要重新训练。
  2. 训练 风格向量映射器,使CLIP文本特征向量映射至StyleGAN 风格向量空间,避免(1)方法的训练问题,但可控性较差,经论文对比其生成质量也不如(3)。
  3. 在 StyleGAN 的 StyleSpace 中,把文本描述映射到输入图像的全局方向 (Global Direction),进而运行自由控制图像操作强度以及分离程度,实现类似于StyleGAN Editing 模块的使用体验。

本次仅复现论文中效果最好的 (3)Global Direction 方法。

StyleCLIP Global Direction 训练过程分两步:

  1. 提取风格向量并统计
python styleclip_getf.py
       
  1. 结合CLIP模型计算转换矩阵
python ppgan/apps/styleganv2clip_predictor.py extract
    In [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]


# python  # git  # ai  # red  # 重构  # 不受  # 图像处理  # 几个  # 最好的  # 有可能  # 则是  # 他不  # 所需  # 请在 


相关栏目: 【 Google疑问12 】 【 Facebook疑问10 】 【 网络优化91478 】 【 技术知识72672 】 【 云计算0 】 【 GEO优化84317 】 【 优选文章0 】 【 营销推广36048 】 【 网络运营41350 】 【 案例网站102563 】 【 AI智能45237


相关推荐: 宝可梦化石精灵大揭秘:晶灿钻石视角下的精灵演化  教你用AI将一篇长文自动拆解成社交媒体帖子,实现一文多发  教你用AI把照片变成动漫风格,3个简单步骤刷爆朋友圈  打造AI Jarvis:停止功能、联网、中文与人脸集成  AI学习秘籍:3个高效黑科技,解锁智能学习新时代  掌握写作技巧:小说情节设计的核心要素解析  图像分割技术详解:定义、类型、技术与应用  宝可梦朱紫:如何高效刷闪异色宝可梦,提升游戏体验  MediCa AI:AI赋能的智能医疗保健平台全面解析  提升效率:使用AI代理自动生成视频标题的实用指南  秀米AI排版如何自动生成模板_秀米AI排版模板生成入口与风格选择【攻略】  豆包AI怎么查看个人主页_管理账号信息与偏好设置  ChatGPT新手指南:大学生如何高效利用AI工具?  AI电子书写作终极指南:ChatGPT和Canva实战教程  AI Buildr: 构建 AI 应用的终极指南  Mootion AI视频生成器:一键创作动画故事!  2025年AI招聘大师班:初学者友好且功能强大  怎么用ai制作表情包 AI个性化动态表情包教程【方法】  Removebg怎样快速抠图_Removebg上传图片与自动抠图步骤【教程】  豆包 AI 辅助进行初级绘本创作的剧情构思  豆包 AI 辅助进行精简版个人周报撰写技巧  AI心理测试生成工具有哪些_一键生成趣味测评的AI工具推荐  掌握解方程技巧:4.2家庭作业难题精讲与分数系数处理  即梦ai能否生成国风插画_即梦ai国风元素调用与文化符号添加【技巧】  Descript音频编辑终极指南:技巧、AI工具与专业效果  AI赋能招聘:高级策略助你领先猎头行业  AI助手高效获取谷歌评论:提升本地商家曝光率的终极指南  AI 播客脚本写作工具:提升内容创作效率的终极指南  夸克AI怎样搜索医疗健康_夸克AI医疗频道与症状自查【技巧】  播客数据深度解析:揭秘全球听众分布和增长策略  Notta AI: 提升效率的智能会议纪要工具  DeepSeek分析Excel怎么用_DeepSeek分析Excel使用方法详细指南【教程】  Claude怎样用提示词控制输出长度_Claude输出长度设置【教程】  AI伴侣:连接还是孤独?真实对话揭秘AI伦理困境  千问如何切换回答风格_千问风格选择正式口语等【实操】  AI游戏革命:文本驱动,无限可能  eBookWriter AI:无需写作也能创作专业电子书  教你用AI帮你写出有说服力的众筹项目文案  百度ai助手工具栏怎么关 百度ai助手状态栏隐藏  使用AI简化多机位播客视频编辑:Eddie AI全面指南  Midjourney怎么用一键生成壁纸_Midjourney壁纸生成教程【教程】  百度AI对话助手入口 智能聊天机器人入口  利用AI赋能教育:学习方式的未来之路  ChatGPT 4o图像生成器:免费AI绘画技巧与应用  豆包 AI 在英语单词高效背诵中的趣味应用  智谱AI绘画怎么用_智谱AI绘画使用方法详细指南【教程】  一键生成PPT工具怎么用_一键生成PPT工具使用方法详细指南【教程】  利用 ChatGPT 进行复杂数学公式的推导教程  律师视角下的生成式AI:信息爆炸时代的法律实践与未来展望  AI驱动的自动化工作流:Zapier、Perplexity和Claude集成指南 

 2025-07-31

了解您产品搜索量及市场趋势,制定营销计划

同行竞争及网站分析保障您的广告效果

点击免费数据支持

提交您的需求,1小时内享受我们的专业解答。

南京市珐之弘网络技术有限公司


南京市珐之弘网络技术有限公司

南京市珐之弘网络技术有限公司专注海外推广十年,是谷歌推广.Facebook广告全球合作伙伴,我们精英化的技术团队为企业提供谷歌海外推广+外贸网站建设+网站维护运营+Google SEO优化+社交营销为您提供一站式海外营销服务。

 87067657

 13565296790

 87067657@qq.com

Notice

We and selected third parties use cookies or similar technologies for technical purposes and, with your consent, for other purposes as specified in the cookie policy.
You can consent to the use of such technologies by closing this notice, by interacting with any link or button outside of this notice or by continuing to browse otherwise.