---
title: cv_resnet50_face-reconstruction
canonical_url: "https://www.modelscope.cn/models/iic/cv_resnet50_face-reconstruction"
md_url: "https://www.modelscope.cn/models/iic/cv_resnet50_face-reconstruction.md"
repository: iic/cv_resnet50_face-reconstruction
chinese_name: "人脸重建模型"
last_updated: 2023-11-29
license: "Apache License 2.0"
pipeline_tag: face-reconstruction
tasks:
  - face-reconstruction
model_type:
  - "face reconstruction"
library_name:
  - pytorch
frameworks:
  - pytorch
language:
  - ch
domain:
  - cv
downloads: 97507
stars: 271
tags:
  - CVPR2023
  - "face reconstruction"
  - Alibaba
  - "REALY benchmark top-1"
  - avatar
  - "人脸重建"
  - "三维重建"
  - "单图重建"
---

# cv_resnet50_face-reconstruction

> cv_resnet50_face-reconstruction - iic 在 ModelScope 开源的模型。单图人脸重建榜单REALY冠军模型，相关论文被CVPR2023收录。

iic/cv_resnet50_face-reconstruction 是 ModelScope 魔搭社区上的face-reconstruction模型，采用 Apache License 2.0 许可。

- **Repository**: iic/cv_resnet50_face-reconstruction
- **License**: Apache License 2.0
- **Tasks**: face-reconstruction
- **Tags**: CVPR2023, face reconstruction, Alibaba, REALY benchmark top-1, avatar, 人脸重建, 三维重建, 单图重建
- **Downloads**: 97507
- **Stars**: 271
- **Last updated**: 2023-11-29

Source: https://www.modelscope.cn/models/iic/cv_resnet50_face-reconstruction

---

# HRN人脸重建模型

### [论文](https://arxiv.org/abs/2302.14434) | [项目主页](https://younglbw.github.io/HRN-homepage/) | [github](https://github.com/youngLBW/HRN)

人脸重建模型以单张人像图作为输入，利用层次化表征实现快速人脸几何、纹理恢复，输出高精度3D人脸重建mesh，相关论文HRN已被CVPR2023接收。

![Output1](results/00016_0_hrn_output.gif)
![Output1](results/00034_0_hrn_output.gif)
![Output1](results/00133_0_hrn_output.gif)
![Output1](results/00437_0_hrn_output.gif)
![Output1](results/00521_0_hrn_output.gif)

## 三维视觉系列模型

| [<img src="results/example_head_reconstruction.jpg" height="200px">](https://www.modelscope.cn/models/damo/cv_HRN_head-reconstruction/summary) | [<img src="results/example_text-to-head.jpg" height="200px">](https://modelscope.cn/models/damo/cv_HRN_text-to-head/summary) |
|:--:|:--:| 
| [HRN头部重建](https://www.modelscope.cn/models/damo/cv_HRN_head-reconstruction/summary) | [文本生成3D头部](https://modelscope.cn/models/damo/cv_HRN_text-to-head/summary) |

## demo使用

可在界面右侧在线体验模块进行一键试用，需要注意的是： 

- 请输入包含人脸的图像（最好是单个人脸，模型暂无法识别主要人脸），非人脸图像可能会报错 "list index out of range"。

- 页面无法支持displacement map的渲染，因此只能展示重建纹理及中频细节，可利用下方代码范例导出高频细节的展示结果。

- 初次使用等待时间可能较长（模型初始化、排队、网络传输等因素），需要一分钟左右，请耐心等待。

- 如果渲染结果脸部有大量白点（非modelscope水印），可能与chrome浏览器版本有关，请升级到最新版后进行试用。


## 模型描述

在该模型中，我们提出了一种新颖的层次化表征网络 (HRN)，以实现单图的高精细人脸重建。 具体来说，我们将人脸几何拆解为低频部分、中频细节以及高频细节三个部分，并引入了层次化表征分别对其进行建模（如下图），从而实现精细的人脸建模。

![内容图像](assets/HRN_core_idea.jpg)

本模型在原始HRN的基础上做了少许改动，以提升效果和鲁棒性：
- 引入valid mask，缓解头发等遮挡带来的干扰。
- 重新实现了texture map的生成以及re-align，速度更快。
- 引入两个可训练参数α、β，提升模型训练初期的稳定性。

## 期望模型使用方式以及适用范围

使用方式：
- 支持GPU推理，在任意真实人脸图像上进行直接推理。

推理时间：
- 模型的纯推理时间小于1秒，pipeline中集成了较多的可视化，总体时间大概为5~10秒。

使用范围:
- 适用于包含人脸的人像照片，其中人脸分辨率大于100x100，图像整体分辨率小于5000x5000。
- 正脸或小角度侧脸图像，脸部区域无遮挡。

目标场景:
- 影视、娱乐、医美等。

### 如何使用

本模型基于pytorch进行训练和推理，在ModelScope框架上，提供输入图片，即可以通过简单的Pipeline调用来使用人脸重建模型。

#### 环境安装
安装好基础modelscope环境后，安装nvdiffrast
```
# 安装nvdiffrast
git clone https://github.com/NVlabs/nvdiffrast.git
cd nvdiffrast
pip install .

# 安装nvdiffrast所需依赖（opengl等）
apt-get install freeglut3-dev
apt-get install binutils-gold g++ cmake libglew-dev mesa-common-dev build-essential libglew1.5-dev libglm-dev
apt-get install mesa-utils
apt-get install libegl1-mesa-dev 
apt-get install libgles2-mesa-dev
apt-get install libnvidia-gl-525
pip install 'numpy<=1.22.0' 'pandas<1.4.0' 
```

#### 代码范例
当前模型依赖gpu进行3D渲染，请在gpu环境进行试用、测试
```python
import os
import cv2
from moviepy.editor import ImageSequenceClip
from modelscope.models.cv.face_reconstruction.utils import write_obj
from modelscope.outputs import OutputKeys
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks


def save_results(result, save_root):
    os.makedirs(save_root, exist_ok=True)

    # export obj and texture
    mesh = result[OutputKeys.OUTPUT]['mesh']
    texture_map = result[OutputKeys.OUTPUT_IMG]
    mesh['texture_map'] = texture_map
    write_obj(os.path.join(save_root, 'hrn_mesh_mid.obj'), mesh)

    # export rotation video
    frame_list = result[OutputKeys.OUTPUT]['frame_list']
    video = ImageSequenceClip(sequence=frame_list, fps=30)
    video.write_videofile(
        os.path.join(save_root, 'rotate.mp4'), fps=30, audio=False)
    del frame_list

    # save visualization image
    vis_image = result[OutputKeys.OUTPUT]['vis_image']
    cv2.imwrite(os.path.join(save_root, 'vis_image.jpg'), vis_image)

    print(f'Output written to {os.path.abspath(save_root)}')

face_reconstruction = pipeline(Tasks.face_reconstruction, model='damo/cv_resnet50_face-reconstruction', model_revision='v2.0.3')
result = face_reconstruction('https://modelscope.oss-cn-beijing.aliyuncs.com/test/images/face_reconstruction.jpg')

save_results(result, './face_reconstruction_results')
```

### 模型局限性以及可能的偏差
- 在人脸分辨率大于100×100的图像上可取得期望效果，分辨率过小时会导致重建结果精度较差、纹理不清晰。
- 对于脸部存在遮挡的情况，重建效果可能不理想。

## 训练数据介绍
- 本模型可通过自监督的方式进行训练，训练数据仅需2D人脸图像，可使用人脸公开数据集如CeleA、300W-LP等。
- 可在自监督训练的基础上引入3D数据辅助训练，提升细节的准确性和真实性，具体可参考论文及补充材料。

### 预处理
- 人脸区域裁剪、resize到224x224分辨率作为网络输入。

### 后处理
- 将顶点坐标、三角面片、贴图等数据转化为obj等模型文件。

## 引用
如果你觉得这个该模型对有所帮助，请考虑引用下面的相关的论文：

```BibTeX
@misc{lei2023hierarchical,
      title={A Hierarchical Representation Network for Accurate and Detailed Face Reconstruction from In-The-Wild Images}, 
      author={Biwen Lei and Jianqiang Ren and Mengyang Feng and Miaomiao Cui and Xuansong Xie},
      year={2023},
      eprint={2302.14434},
      archivePrefix={arXiv},
      primaryClass={cs.CV}
}
```
