6 Star 37 Fork 12

Gitee 极速下载 / facechain

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/modelscope/facechain
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0



FaceChain

最新消息

  • 🚀🚀🚀 正在推出FACT,10秒写真出图,兼容现成的LoRa与ControlNet,并且具备更强的指令跟随能力!(2024-05-28)
  • FaceChain两项算法创新工作FaceChain-ImagineIDFaceChain-SuDe被CVPR 2024接收录用! (2024-02-27)

介绍

FaceChain是一个可以用来打造个人数字形象的深度学习模型工具。用户仅需要提供最低一张照片即可获得独属于自己的个人形象数字替身。FaceChain支持在gradio的界面中使用模型训练和推理能力、支持资深开发者使用python脚本进行训练推理,也支持在sd webui中安装插件使用;同时,我们也欢迎开发者对本Repo进行继续开发和贡献。 FaceChain的模型由ModelScope开源模型社区提供支持。

ModelScope Studio 🤖&nbsp |API 🔥&nbsp | API's Example App 🔥&nbsp | HuggingFace Space 🤗&nbsp


image

News

  • 🚀🚀🚀 正在推出FACT,10秒写真出图,兼容现成的LoRa与ControlNet,并且具备更强的指令跟随能力!(2024-05-28)
  • FaceChain两项算法创新工作FaceChain-ImagineIDFaceChain-SuDe被CVPR 2024接收录用! (2024-02-27)
  • 🏆🏆🏆阿里巴巴年度开源新锐项目、阿里巴巴年度开源先锋人物-技术贡献(刘洋)、阿里巴巴年度开源先锋人物-生态贡献(孙佰贵). (2024-01-20)
  • 与NUS团队合作的InfoBatch 被ICLR 2024(Oral)录用! pytorch中只用3行代码的版本PR中. (2024-01-16)
  • 🏆开放原子2023快速成长开源项目奖项. (2023-12-20)
  • 支持SDXL模块🔥🔥🔥,出图细腻度大幅提升. (November 22th, 2023 UTC)
  • 支持超分模块🔥🔥🔥,目前多种分辨率可选 (512512, 768768, 10241024, 20482048). (November 13th, 2023 UTC)
  • 🏆FaceChain入选BenchCouncil Open100 (2022-2023) 开源榜单. (2023-11-08)
  • 增加虚拟试衣模块,可基于包含给定服饰的模特图或人台图进行重绘. (2023-10-27)
  • 增加万相版本在线免费应用. (2023-10-26)
  • 🏆1024程序员节AIGC应用工具最具商业价值奖. (2023-10-24)
  • stable-diffusion-webui支持🔥🔥🔥. (2023-10-13)
  • 高性能的(单人&双人)模版重绘功能,简化用户界面. (2023-09-09)
  • 更多技术细节可以在 论文 里查看. (2023-08-30)
  • 为Lora训练添加验证和根据face_id的融合,并添加InpaintTab(目前在Gradio界面上暂时默认隐藏). (2023-08-28)
  • 增加姿势控制模块,可一键体验模版pose复刻. (2023-08-27)
  • 增加鲁棒性人脸lora训练,提升单图训练&风格lora融合的效果. (2023-08-27)
  • 支持在HuggingFace Space中体验FaceChain ! 🤗 (2023-08-25)
  • 新增高质量提示词模板,欢迎大家一起贡献! 参考 awesome-prompts-facechain (2023-08-18)
  • 支持即插即用的风格LoRA模型! (2023-08-16)
  • 新增个性化prompt模块! (2023-08-16)
  • Colab notebook安装已支持,您可以直接打开链接体验FaceChain: Open In Colab (2023-08-15)

待办事项

  • 研发免训练模块,达成CPU运行的目标
  • 研发RLHF模块,进一步提升上限
  • 增加风格lora的训练接口
  • 现成风格模型即插即用(以C站风格模型为例)
  • 增加更多美肤功能
  • 开发更多好玩的app

Citation

如果FaceChain对您的研究有所帮助,请在您的出版物中引用FaceChain

@article{liu2023facechain,
  title={FaceChain: A Playground for Identity-Preserving Portrait Generation},
  author={Liu, Yang and Yu, Cheng and Shang, Lei and Wu, Ziheng and 
          Wang, Xingjun and Zhao, Yuze and Zhu, Lin and Cheng, Chen and 
          Chen, Weitao and Xu, Chao and Xie, Haoyu and Yao, Yuan and 
          Zhou,  Wenmeng and Chen Yingda and Xie, Xuansong and Sun, Baigui},
  journal={arXiv preprint arXiv:2308.14256},
  year={2023}

环境准备

兼容性验证

FaceChain是一个组合模型,基于PyTorch机器学习框架,以下是已经验证过的主要环境依赖:

  • python环境: py3.8, py3.10
  • pytorch版本: torch2.0.0, torch2.0.1
  • CUDA版本: 11.7
  • CUDNN版本: 8+
  • 操作系统版本: Ubuntu 20.04, CentOS 7.9
  • GPU型号: Nvidia-A10 24G

资源要求

  • GPU: 显存占用约19G
  • 磁盘: 推荐预留50GB以上的存储空间

安装指南

支持以下几种安装方式,任选其一:

1. 使用ModelScope提供的notebook环境【推荐】

ModelScope(魔搭社区)提供给新用户初始的免费计算资源,参考ModelScope Notebook

如果初始免费计算资源无法满足要求,您还可以从上述页面开通付费流程,以便创建一个准备就绪的ModelScope(GPU) DSW镜像实例。

Notebook环境使用简单,您只需要按以下步骤操作(注意:目前暂不提供永久存储,实例重启后数据会丢失):

# Step1: 我的notebook -> PAI-DSW -> GPU环境

# Step2: 进入Notebook cell,执行下述命令从github clone代码:
!GIT_LFS_SKIP_SMUDGE=1 git clone https://github.com/modelscope/facechain.git --depth 1

# Step3: 切换当前工作路径,安装依赖
import os
os.chdir('/mnt/workspace/facechain')    # 注意替换成上述clone后的代码文件夹主路径
print(os.getcwd())

!pip3 install gradio==3.50.2
!pip3 install controlnet_aux==0.0.6
!pip3 install python-slugify
!pip3 install onnxruntime==1.15.1
!pip3 install edge-tts
!pip3 install modelscope==1.10.0

# Step4: 启动服务,点击生成的URL即可访问web页面,上传照片开始训练和预测
!python3 app.py

除了ModelScope入口以外,您也可以前往PAI-DSW 直接购买带有ModelScope镜像的计算实例(推荐使用A10资源),这样同样可以使用如上的最简步骤运行起来。

2. docker镜像

如果您熟悉docker,可以使用我们提供的docker镜像,其包含了模型依赖的所有组件,无需复杂的环境安装:

# Step1: 机器资源
您可以使用本地或云端带有GPU资源的运行环境。
如需使用阿里云ECS,可访问: https://www.aliyun.com/product/ecs,推荐使用”镜像市场“中的CentOS 7.9 64位(预装NVIDIA GPU驱动)

# Step2: 将镜像下载到本地 (前提是已经安装了docker engine并启动服务,具体可参考: https://docs.docker.com/engine/install/)
# For China Mainland users:
docker pull registry.cn-hangzhou.aliyuncs.com/modelscope-repo/modelscope:ubuntu22.04-cuda11.8.0-py310-torch2.1.0-tf2.14.0-1.10.0
# For users outside China Mainland:
docker pull registry.us-west-1.aliyuncs.com/modelscope-repo/modelscope:ubuntu22.04-cuda11.8.0-py310-torch2.1.0-tf2.14.0-1.10.0

# Step3: 拉起镜像运行
docker run -it --name facechain -p 7860:7860 --gpus all registry.cn-hangzhou.aliyuncs.com/modelscope-repo/modelscope:ubuntu22.04-cuda11.8.0-py310-torch2.1.0-tf2.14.0-1.10.0 /bin/bash  # 注意 your_xxx_image_id 替换成你的镜像id
# 注意: 如果提示无法使用宿主机GPU的错误,可能需要安装nvidia-container-runtime
# 1. 安装nvidia-container-runtime:https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html
# 2. 重启docker服务:sudo systemctl restart docker

# Step4: 在容器中安装gradio
pip3 install gradio==3.50.2
pip3 install controlnet_aux==0.0.6
pip3 install python-slugify
pip3 install onnxruntime==1.15.1
pip3 install edge-tts
pip3 install modelscope==1.10.0

# Step5: 获取facechain源代码
GIT_LFS_SKIP_SMUDGE=1 git clone https://github.com/modelscope/facechain.git --depth 1
cd facechain
python3 app.py
# Note: FaceChain目前支持单卡GPU,如果您的环境有多卡,请使用如下命令
# CUDA_VISIBLE_DEVICES=0 python3 app.py

# Step6: 点击 "public URL", 形式为 https://xxx.gradio.live

3. conda虚拟环境

使用conda虚拟环境,参考Anaconda来管理您的依赖,安装完成后,执行如下命令: (提示: mmcv对环境要求较高,可能出现不适配的情况,推荐使用docker方式)

conda create -n facechain python=3.8    # 已验证环境:3.8 和 3.10
conda activate facechain

GIT_LFS_SKIP_SMUDGE=1 git clone https://github.com/modelscope/facechain.git --depth 1
cd facechain

pip3 install -r requirements.txt
pip3 install -U openmim 

# 参考mmcv官方文档 https://mmcv.readthedocs.io/en/latest/get_started/installation.html
mim install mmcv-full==1.7.2

# 进入facechain文件夹,执行:
python3 app.py
# Note: FaceChain目前支持单卡GPU,如果您的环境有多卡,请使用如下命令
# CUDA_VISIBLE_DEVICES=0 python3 app.py

# 最后点击log中生成的URL即可访问页面。

如果您想要使用"人物说话视频生成"标签页的功能,请参考installation_for_talkinghead_ZH里的安装使用教程。

4. colab运行

Colab Info
Open In Colab FaceChain Installation on Colab

5. stable-diffusion-webui中运行

  1. 选择Extensions Tab, 选择Install From URL(官方插件集成中,先从URL安装) image

  2. 切换到Installed,勾选FaceChain插件,点击Apply and restart UI image

  3. 页面刷新后,出现FaceChain Tab说明安装成功 image

备注:app服务成功启动后,在log中访问页面URL,进入”形象定制“tab页,点击“选择图片上传”,并最少选1张包含人脸的图片;点击“开始训练”即可训练模型。训练完成后日志中会有对应展示,之后切换到“形象体验”标签页点击“开始生成”即可生成属于自己的数字形象。

脚本运行

如果不想启动服务,而是直接在命令行进行开发调试等工作,FaceChain也支持在python环境中直接运行脚本进行训练和推理。在克隆后的文件夹中直接运行如下命令来进行训练:

PYTHONPATH=. sh train_lora.sh "ly261666/cv_portrait_model" "v2.0" "film/film" "./imgs" "./processed" "./output"

参数含义:

ly261666/cv_portrait_model: ModelScope模型仓库的stable diffusion基模型,该模型会用于训练,可以不修改
v2.0: 该基模型的版本号,可以不修改
film/film: 该基模型包含了多个不同风格的子目录,其中使用了film/film目录中的风格模型,可以不修改
./imgs: 本参数需要用实际值替换,本参数是一个本地文件目录,包含了用来训练和生成的原始照片
./processed: 预处理之后的图片文件夹,这个参数需要在推理中被传入相同的值,可以不修改
./output: 训练生成保存模型weights的文件夹,可以不修改

等待5-20分钟即可训练完成。用户也可以调节其他训练超参数,训练支持的超参数可以查看train_lora.sh的配置,或者facechain/train_text_to_image_lora.py中的完整超参数列表。

进行推理时,请编辑run_inference.py中的代码:

# 使用深度控制,默认False,仅在使用姿态控制时生效
use_depth_control = False
# 使用姿态控制,默认False
use_pose_model = False
# 姿态控制图片路径,仅在使用姿态控制时生效
pose_image = 'poses/man/pose1.png'
# 填入上述的预处理之后的图片文件夹,需要和训练时相同
processed_dir = './processed'
# 推理生成的图片数量
num_generate = 5
# 训练时使用的stable diffusion基模型,可以不修改
base_model = 'ly261666/cv_portrait_model'
# 该基模型的版本号,可以不修改
revision = 'v2.0'
# 该基模型包含了多个不同风格的子目录,其中使用了film/film目录中的风格模型,可以不修改
base_model_sub_dir = 'film/film'
# 训练生成保存模型weights的文件夹,需要保证和训练时相同
train_output_dir = './output'
# 指定一个保存生成的图片的文件夹,本参数可以根据需要修改
output_dir = './generated'
# 使用凤冠霞帔风格模型,默认False
use_style = False

之后执行:

python run_inference.py

即可在output_dir中找到生成的个人数字形象照片。

算法介绍

基本原理

个人写真模型的能力来源于Stable Diffusion模型的文生图功能,输入一段文本或一系列提示词,输出对应的图像。我们考虑影响个人写真生成效果的主要因素:写真风格信息,以及用户人物信息。为此,我们分别使用线下训练的风格LoRA模型和线上训练的人脸LoRA模型以学习上述信息。LoRA是一种具有较少可训练参数的微调模型,在Stable Diffusion中,可以通过对少量输入图像进行文生图训练的方式将输入图像的信息注入到LoRA模型中。因此,个人写真模型的能力分为训练与推断两个阶段,训练阶段生成用于微调Stable Diffusion模型的图像与文本标签数据,得到人脸LoRA模型;推断阶段基于人脸LoRA模型和风格LoRA模型生成个人写真图像。

image

训练阶段

输入:用户上传的包含清晰人脸区域的图像

输出:人脸LoRA模型

描述:首先,我们分别使用基于朝向判断的图像旋转模型,以及基于人脸检测和关键点模型的人脸精细化旋转方法处理用户上传图像,得到包含正向人脸的图像;接下来,我们使用人体解析模型和人像美肤模型,以获得高质量的人脸训练图像;随后,我们使用人脸属性模型和文本标注模型,结合标签后处理方法,产生训练图像的精细化标签;最后,我们使用上述图像和标签数据微调Stable Diffusion模型得到人脸LoRA模型。

推断阶段

输入:训练阶段用户上传图像,预设的用于生成个人写真的输入提示词

输出:个人写真图像

描述:首先,我们将人脸LoRA模型和风格LoRA模型的权重融合到Stable Diffusion模型中;接下来,我们使用Stable Diffusion模型的文生图功能,基于预设的输入提示词初步生成个人写真图像;随后,我们使用人脸融合模型进一步改善上述写真图像的人脸细节,其中用于融合的模板人脸通过人脸质量评估模型在训练图像中挑选;最后,我们使用人脸识别模型计算生成的写真图像与模板人脸的相似度,以此对写真图像进行排序,并输出排名靠前的个人写真图像作为最终输出结果。

模型列表

附(流程图中模型链接)

[1] 人脸检测+关键点模型DamoFD:https://modelscope.cn/models/damo/cv_ddsar_face-detection_iclr23-damofd

[2] 图像旋转模型:创空间内置模型

[3] 人体解析模型M2FP:https://modelscope.cn/models/damo/cv_resnet101_image-multiple-human-parsing

[4] 人像美肤模型ABPN:https://www.modelscope.cn/models/damo/cv_unet_skin_retouching_torch

[5] 人脸属性模型FairFace:https://modelscope.cn/models/damo/cv_resnet34_face-attribute-recognition_fairface

[6] 文本标注模型Deepbooru:https://github.com/KichangKim/DeepDanbooru

[7] 模板脸筛选模型FQA:https://modelscope.cn/models/damo/cv_manual_face-quality-assessment_fqa

[8] 人脸融合模型:https://www.modelscope.cn/models/damo/cv_unet_face_fusion_torch

[9] 人脸识别模型RTS:https://modelscope.cn/models/damo/cv_ir_face-recognition-ood_rts

[10] 人脸说话模型:https://modelscope.cn/models/wwd123/sadtalker

更多信息

License

This project is licensed under the Apache License (Version 2.0).

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

FaceChain是一款可以用于打造个人数字形象的深度学习模型工具 展开 收起
Python 等 2 种语言
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Python
1
https://gitee.com/mirrors/facechain.git
git@gitee.com:mirrors/facechain.git
mirrors
facechain
facechain
main

搜索帮助