Python开源项目PGDiff——人脸重建(Face Restoration),模糊清晰、划痕修复及黑白上色的实践

python ansconda 等的下载、安装等请参阅:

Python开源项目CodeFormer——人脸重建(Face Restoration),模糊清晰、划痕修复及黑白上色的实践icon-default.png?t=N7T8https://blog.csdn.net/beijinghorn/article/details/134334021

友情提示:
(1)这是2023年的论文;

(2)必须有 CUDA !

(3)运行速度慢!效果一般!

(4)有2个bug;后面会介绍一下。

1 PGDiff

https://github.com/pq-yang/PGDiff

1.1 论文Paper


《PGDiff: Guiding Diffusion Models for Versatile Face Restoration via Partial Guidance》
Peiqing Yang1  Shangchen Zhou1  Qingyi Tao2  Chen Change Loy1
S-Lab, Nanyang Technological University  SenseTime Research, Singapore 
Accepted to NeurIPS 2023


PGDiff builds a versatile framework that is applicable to a broad range of face restoration tasks.

If you find PGDiff helpful to your projects, please consider ⭐ this repo. Thanks! 

Supported Applications
Blind Restoration
Colorization
Inpainting
Reference-based Restoration
Old Photo Restoration (w/ scratches)
[TODO] Natural Image Restoration

1.2 进化史 Updates

2021.10.10: Release our codes and models. Have fun! 😋
2021.08.16: This repo is created.

1.3 安装 Installation

Codes and Environment

# git clone this repository
git clone https://github.com/pq-yang/PGDiff.git
cd PGDiff

# create new anaconda env
conda create -n pgdiff python=1.8 -y
conda activate pgdiff

# install python dependencies
conda install mpi4py
pip3 install -r requirements.txt
pip install -e .

Pretrained Model
Download the pretrained face diffusion model from [Google Drive | BaiduPan (pw: pgdf)] to the models folder (credit to DifFace).
https://pan.baidu.com/share/init?surl=VHv48RDUXI8onMEodVFZkw

1.4 功能 Applications

1.4.1 Blind Restoration


To extract smooth semantics from the input images, download the pretrained restorer from [Google Drive | BaiduPan (pw: pgdf)] to the models/restorer folder. The pretrained restorer provided here is modified from the 
 1 generator of Real-ESRGAN. Note that the pretrained restorer can also be flexibly replaced with other restoration models by modifying the create_restorer function and specifying your own --restorer_path accordingly.
https://pan.baidu.com/share/init?surl=IkEnPGDJqFcg4dKHGCH9PQ

Commands
Guidance scale 
 for BFR is generally taken from [0.05, 0.1]. Smaller 
 tends to produce a higher-quality result, while larger 
 yields a higher-fidelity result.

For cropped and aligned faces (512x512):

python inference_pgdiff.py --task restoration --in_dir [image folder] --out_dir [result folder] --restorer_path [restorer path] --guidance_scale [s]

Example:

python inference_pgdiff.py --task restoration --in_dir testdata/cropped_faces --out_dir results/blind_restoration --guidance_scale 0.05


1.4.2 Colorization


We provide a set of color statistics in the adaptive_instance_normalization function as the default colorization style. One may change the colorization style by running the script scripts/color_stat_calculation.py to obtain target statistics (avg mean & avg std) and replace those in the adaptive_instance_normalization function.

Commands
For cropped and aligned faces (512x512):

python inference_pgdiff.py --task colorization --in_dir [image folder] --out_dir [result folder] --lightness_weight [w_l] --color_weight [w_c] --guidance_scale [s]

Example:

Try different color styles for various outputs!

# style 0 (default)
python inference_pgdiff.py --task colorization --in_dir testdata/grayscale_faces --out_dir results/colorization --guidance_scale 0.01

# style 1 (uncomment line 272-273 in `guided_diffusion/script_util.py`)
python inference_pgdiff.py --task colorization --in_dir testdata/grayscale_faces --out_dir results/colorization_style1 --guidance_scale 0.01

# style 3 (uncomment line 278-279 in `guided_diffusion/script_util.py`)
python inference_pgdiff.py --task colorization --in_dir testdata/grayscale_faces --out_dir results/colorization_style3 --guidance_scale 0.01

1.4.3 Inpainting


A folder for mask(s) mask_dir must be specified with each mask image name corresponding to each input (masked) image. Each input mask shoud be a binary map with white pixels representing masked regions (refer to testdata/append_masks). We also provide a script scripts/irregular_mask_gen.py to randomly generate irregular stroke masks on input images.

Note: If you don't specify mask_dir, we will automatically treat the input image as if there are no missing pixels.

Commands
For cropped and aligned faces (512x512):

python inference_pgdiff.py --task inpainting --in_dir [image folder] --mask_dir [mask folder] --out_dir [result folder] --unmasked_weight [w_um] --guidance_scale [s]

Example:

Try different seeds for various outputs!

python inference_pgdiff.py --task inpainting --in_dir testdata/masked_faces --mask_dir testdata/append_masks --out_dir results/inpainting --guidance_scale 0.01 --seed 4321

1.4.4 Reference-based Restoration


To extract identity features from both the reference image and the intermediate results, download the pretrained ArcFace model from [Google Drive | BaiduPan (pw: pgdf)] to the models folder.

A folder for reference image(s) ref_dir must be specified with each reference image name corresponding to each input image. A reference image is suggested to be a high-quality image from the same identity as the input low-quality image. Test image pairs we provided here are from the CelebRef-HQ dataset.
https://pan.baidu.com/share/init?surl=Ku-d57YYavAuScTFpvaP3Q

Commands
Similar to blind face restoration, reference-based restoration requires to tune the guidance scale 
 according to the input quality, which is generally taken from [0.05, 0.1].

For cropped and aligned faces (512x512):

python inference_pgdiff.py --task ref_restoration --in_dir [image folder] --ref_dir [reference folder] --out_dir [result folder] --ss_weight [w_ss] --ref_weight [w_ref] --guidance_scale [s]

Example:

# Choice 1: MSE Loss (default)
python inference_pgdiff.py --task ref_restoration --in_dir testdata/ref_cropped_faces --ref_dir testdata/ref_faces --out_dir results/ref_restoration_mse --guidance_scale 0.05 --ref_weight 25

# Choice 2: Cosine Similarity Loss (uncomment line 71-72)
python inference_pgdiff.py --task ref_restoration --in_dir testdata/ref_cropped_faces --ref_dir testdata/ref_faces --out_dir results/ref_restoration_cos --guidance_scale 0.05 --ref_weight 1e4


1.4.5 Old Photo Restoration


If scratches exist, a folder for mask(s) mask_dir must be specified with the name of each mask image corresponding to that of each input image. Each input mask shoud be a binary map with white pixels representing masked regions. To obtain a scratch map automatically, we recommend using the scratch detection model from Bringing Old Photo Back to Life. One may also generate or adjust the scratch map with an image editing app (e.g., Photoshop).

If scratches don't exist, set the mask_dir augment as None (default). As a result, if you don't specify mask_dir, we will automatically treat the input image as if there are no missing pixels.

Commands

For cropped and aligned faces (512x512):

python inference_pgdiff.py --task old_photo_restoration --in_dir [image folder] --mask_dir [mask folder] --out_dir [result folder] --op_lightness_weight [w_op_l] --op_color_weight [w_op_c] --guidance_scale [s]

Demos
Similar to blind face restoration, old photo restoration is a more complex task (restoration + colorization + inpainting) that requires to tune the guidance scale 
 according to the input quality. Generally,$s$ is taken from [0.0015, 0.005]. Smaller 
 tends to produce a higher-quality result, while larger 
 yields a higher-fidelity result.

Degradation: Light
# no scratches (don't specify mask_dir)
python inference_pgdiff.py --task old_photo_restoration --in_dir testdata/op_cropped_faces/lg --out_dir results/op_restoration/lg --guidance_scale 0.004 --seed 4321

Degradation: Medium
# no scratches (don't specify mask_dir)
python inference_pgdiff.py --task old_photo_restoration --in_dir testdata/op_cropped_faces/med --out_dir results/op_restoration/med --guidance_scale 0.002 --seed 1234

# with scratches
python inference_pgdiff.py --task old_photo_restoration --in_dir testdata/op_cropped_faces/med_scratch --mask_dir testdata/op_mask --out_dir results/op_restoration/med_scratch --guidance_scale 0.002 --seed 1111

Degradation: Heavy
python inference_pgdiff.py --task old_photo_restoration --in_dir testdata/op_cropped_faces/hv --mask_dir testdata/op_mask --out_dir results/op_restoration/hv --guidance_scale 0.0015 --seed 4321

Customize your results with different color styles!

# style 1 (uncomment line 272-273 in `guided_diffusion/script_util.py`)
python inference_pgdiff.py --task old_photo_restoration --in_dir testdata/op_cropped_faces/hv --mask_dir testdata/op_mask --out_dir results/op_restoration/hv_style1 --guidance_scale 0.0015 --seed 4321

# style 2 (uncomment line 275-276 in `guided_diffusion/script_util.py`)
python inference_pgdiff.py --task old_photo_restoration --in_dir testdata/op_cropped_faces/hv --mask_dir testdata/op_mask --out_dir results/op_restoration/hv_style2 --guidance_scale 0.0015 --seed 4321

1.5 引用Citation


If you find our work useful for your research, please consider citing:

@inproceedings{yang2023pgdiff,
  title={{PGDiff}: Guiding Diffusion Models for Versatile Face Restoration via Partial Guidance},
  author={Yang, Peiqing and Zhou, Shangchen and Tao, Qingyi and Loy, Chen Change},
  booktitle={NeurIPS},
  year={2023}
}

1.6 权利 License


This project is licensed under NTU S-Lab License 1.0. Redistribution and use should follow this license.

1.7 知识 Acknowledgement


This study is supported under the RIE2020 Industry Alignment Fund – Industry Collaboration Projects (IAF-ICP) Funding Initiative, as well as cash and in-kind contribution from the industry partner(s).

This implementation is based on guided-diffusion. We also adopt the pretrained face diffusion model from DifFace, the pretrained identity feature extraction model from ArcFace, and the restorer backbone from Real-ESRGAN. Thanks for their awesome works!

1.8 联系 Contact


If you have any questions, please feel free to reach out at peiqingyang99@outlook.com.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/134919.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

建行广东省江门市分行走进农村地区开展反假货币宣传

人民对美好生活的向往,涉及方方面面,小至“钱袋子”安全。建行广东省江门市分行落实当地监管部门部署,积极扛起维护国家金融安全的重要政治责任,深入农村地区开展反假货币宣传工作,助力构建农村反假货币工作长效机制。…

递归和master公式

前置知识:无 1)从思想上理解递归:对于新手来说,递归去画调用图是非常重要的,有利于分析递归 2)从实际上理解递归:递归不是玄学,底层是利用系统栈来实现的 3)任何递归函…

什么是AI算子开发

今天在某离职群里看到前同事聊天,说到国内某大厂的一个面试,本来求职面试的岗位是通信库,类似于英伟达的 nccl, 但是却被问到了很多与算子开发相关的问题。 看来算子开发岗位依然很稀缺。 联想到之前写过的一篇关于AI算子开发的文…

【JAVA进阶篇】与数据结构结合?这些知识你应该知道

个人主页:【😊个人主页】 系列专栏:【❤️JAVA进阶】 文章目录 前言关与JAVA中的数据结构Java中的数据结构 枚举位集合创建一个初始大小的位集合设置特定的位从另一个位集合中复制位迭代位集合中设置为1的位将位集合转换为字节数组将字节数组…

如何在 Azure 中使用自动机器学习进行模型训练

自动机器学习(Automated Machine Learning,简称为AutoML)是一种通过自动化流程来简化模型训练和调优的技术。在Azure机器学习平台中,AutoML提供了丰富的功能和工具,使我们能够快速地训练和优化机器学习模型。本文将介绍…

nodejs多版本管理

背景 在开发过程中经常会用到不同的nodejs版本,程序在不同版本之间又可能不兼容的情况。一般的做法就是卸载nodejs然后安装需要的版本,这样太过于麻烦。实际上跟conda一样,可以做多版本的管理 解决方法 安装nvm管理nodejs版本,…

windows上运行yolov3代码详解(小白)

batch_normalize1 # 是否做BN 代码链接 环境配置 没有Anaconda的话可以安装下 首先创建虚拟环境,名称随意,版本3.9.我觉得挺好的 激活虚拟环境 conda activate 刚刚创建的环境名称 切换到requirements.txt目录下,直接vscode打开yolov3文件…

Linux应用开发基础知识——字符文字编码(五)

前言: TXT 文件中保存的是字符的核心:它的编码值。而 Notepad 上显示时, 这些字符对应什么样的形状态,这是由字符文件决定的。编码值,字体是两个不一样的东西,比如 A 的编码值是 0x41,但是在屏幕…

2.2 CE修改器:未知数值扫描

本关需要扫描未知数只扫描,要在不知道初始值的情况下找到一个在0到500之间的数值。首先,选择“未知的初始值”扫描方式,在数值类型中选择 4 字节,并点击“首次扫描”以开始扫描。扫描结束后,点击“打我”按钮进行一些操…

CS224W5.3——信念传播

此文中,我们介绍信念传播,这是一种回答图中概率查询的动态规划方法。通过迭代传递消息给邻居节点,如果达成共识,则计算最终的信念值。然后,我们通过示例和泛化树结构展示消息传递。最后讨论了循环信念传播算法及其优缺…

建行驻江门市分行纪检组以政治谈话压责任促发展

开展政治谈话,是加强“一把手”和领导班子监督、严肃党内政治生活、加强对党员领导干部日常教育管理的有效手段。 为督促“一把手”和领导班子成员依法依规履行职责、行使权力,推动党中央重大决策部署以及建设银行总行、广东省分行党委的决策部署在本单…

数据结构之红黑树

红黑树的概念 红黑树(Red-Black Tree)同AVL树一样, 也是一种自平衡的二叉搜索树, 但在每个结点上增加一个存储位表示结点的颜色, 可以是Red或Black, 通过对任何一条从根到叶子的路径上各个结点着色方式的限制, 红黑树确保没有一条路径会比其他路径长出俩…

Vue23组件自定义事件 和 解绑事件

Vue2&3组件自定义事件 和 解绑事件 Vue2组件自定义事件 功能:父组件绑定数据,子组件触发事件。(父绑子触发) 实现步骤(前三步在父组件实现,第四步在子组件实现): 第一步&#…

测试用例的设计方法(黑盒)

1.基于需求的设计方法 比如针对网易邮箱进行测试:分为功能相关和非功能相关两大类 但是这么设计的话,有无数多个测试用例,我们现在看到的只是一些大概的测试用例,要想设计具体的测试用例,需要用到下面测试用例的方法…

盘点双11!阿里妈妈助这些品牌短视频赢增长!

刚刚!一年一度的双11落下帷幕,很多新变化值得回味。 尽管天气在变凉,但市场出现了逐渐回暖的迹象。在此背景下,大量商家特别关心如何在双11打一场漂亮的胜仗。 卖方如何行动,关键在于买方的变化。在阿里妈妈发布的《…

神经网络(第二周)

一、简介 1.1 需求预测示例 1.1.1 逻辑回归算法 根据价格预测商品是否畅销。特征:T恤的价格;分类:销售量高1/销售量低0;使用逻辑回归算法进行分类,拟合效果如下图所示: 1.1.2 神经元和神经网络 将逻辑回…

【LeetCode刷题-二分查找】--162.寻找峰值

162.寻找峰值 方法一:寻找最大值 题目保证了nums[i]≠nums[i1],所以数组nums中最大值两侧的元素一定严格小于最大值本身,因此最大值所在的位置就是一个可行的峰值位置 class Solution {public int findPeakElement(int[] nums) {int idx 0…

分类网络搭建示例

搭建CNN网络 本章我们来学习一下如何搭建网络,初始化方法,模型的保存,预训练模型的加载方法。本专栏需要搭建的是对分类性能的测试,所以这里我们只以VGG为例。 请注意,这里定义的只是一个简陋的版本,后续一…

什么是数据库事务、事务的ACID、怎么设置/禁止自动提交?

数据库事务及ACID 数据库事务是指作为单个逻辑工作单元执行的一组操作。这组操作要么全部成功地执行,要么全部不执行,不允许出现部分执行的情况。数据库事务通常需要满足ACID属性,即原子性(Atomicity)、一致性&#x…

第2关:还原键盘输入(list)

题目&#xff1a; 知识点&#xff1a; 列表list相较于数组&#xff1a; 优势&#xff1a;可在任意指定位置插入或者删除元素而不影响列表其他地方 。 劣势&#xff1a;无法直接进行下标索引&#xff0c;需要迭代器it逐个遍历。 代码&#xff1a; #include <iostream>…