Giter Club home page Giter Club logo

3dmm-fitting-pytorch's Introduction

3DMM model fitting using Pytorch

This is a fitting framework implemented in Pytorch for reconstructing the face in an image or a video using a 3DMM model.

The framework only uses Pytorch modules and a differentiable renderer from pytorch3d. The whole module is differentiable and can be integrated into other systems for the gradient propagation.

Updates

  • 🌟 Refactored the code to make future extension easier (other 3DMM models will be supported). The hyper-parameters are also re-organized.
  • 🌟 Added support for monocular video reconstruction.
  • 🌟 Added support for multi-gpu when processing long videos.

demo

demo

Installation

Requirements

  • pytorch3d It might require a specific version of Pytorch to make pytorch3d run succussfully on gpus, please follow the official instructions.
  • Please refer to "requirements.txt" for other dependences.
  • Basel Face Model 2009 (BFM09)
  • Expression Basis extra expression basis.

How to use

Installation

  1. Clone the repo:
git clone https://github.com/ascust/3DMM-Fitting-Pytorch
cd 3DMM-Fitting-Pytorch
  1. Download the Basel Face Model and put "01_MorphableModel.mat" and put it into "BFM".

  2. Download the Expression Basis. Go to the repo, download the "CoarseData" and put "Exp_Pca.bin" into "BFM".

  3. Convert the BFM parameters by:

python convert_bfm09_data.py

Single Image Reconstruction

python fit_single_img.py --img_path data/000002.jpg --res_folder results 

The results are stored in "results" folder.

Monocular Video Reconstruction

To fit a video, simply run:

python fit_video.py --v_path data/sample_video.mp4 --res_folder results 

The script will extract frames, detact features and fit all frames.

Fitting a video is a bit different from fitting an image, because frames are not isolated. In this implementation, we first estimate shape and texture of the target face using some of the frames (indicated by --nframes_shape). Then we estimate the other coefficients (expression, rotatation etc.) for each frame and keep the shape and texture coefficients fixed.

For the first frame, we use much more iterations to get a good starting point. For remaining frames, each is initialized from the previous estimation.Pose regulerizers are also imposed to increase temporal consistency.

Please check the reconstructed video in the result folder.

Multi-gpu and -process support

It could take quite a while to process a long video, so multi-gpu and multi-process are also supported to accelerate fitting. To use this feature, simply run:

python fit_video.py --v_path data/sample_video.mp4 --res_folder results --ngpus 4 --nworkers 4

Here we use 4 gpus and 4 workers. We can also use more workers to assign each gpu with multiple workers. The video will be evenly split and each clip will be fit respectively.

Hyperparameters

There are bunch of parameters that might require further tuning. The iteration numbers for non-rigid fitting "--first_nrf_iters" and "--rest_nrf_iters" affect the fitting speed a lot since we have to render the 3d mesh in each iteration. Try to change it to find a trade-off between speed and accuracy. If the result is not good, try to play with the parameters.

Acknowledgement

The code is partially borrowed from Deep3DFaceReconstrution, which is a Tensorflow-based deep reconstruction method using CNNs. Please note that our framework does not require any pretrained deep models. We estimate the parameters directly using the landmarks and photometric loss as the supervision.

3dmm-fitting-pytorch's People

Contributors

ascust avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

3dmm-fitting-pytorch's Issues

libcudart.so error

While executing python3 fit.py --img data/000002.jpg, I receive the following error. The installed version of cuda on my system is 11. What should I do?

Traceback (most recent call last):
  File "fit.py", line 6, in <module>
    from core.models import ReconModel
  File "/home/tface/Projects/torch/3DMM-Fitting-Pytorch/core/models.py", line 5, in <module>
    from pytorch3d.renderer import (
  File "/home/tface/.virtualenvs/torch/lib/python3.6/site-packages/pytorch3d/renderer/__init__.py", line 3, in <module>
    from .blending import (
  File "/home/tface/.virtualenvs/torch/lib/python3.6/site-packages/pytorch3d/renderer/blending.py", line 9, in <module>
    from pytorch3d import _C
ImportError: libcudart.so.10.1: cannot open shared object file: No such file or directory


$ nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2020 NVIDIA Corporation
Built on Wed_Jul_22_19:09:09_PDT_2020
Cuda compilation tools, release 11.0, V11.0.221
Build cuda_11.0_bu.TC445_37.28845127_0


How many bases are in the expression model?

exBase = expPC*np.reshape(expEV, [-1, 79])

hi,
Dose this line of code mean that there are 79 bases contained in the FaceWarehouse expression model?
In some other projects, I find that they just use 29 expression bases.
I want make sure that how many bases are in the FaceWarehouse expression model, since I can not get the origin model from the FaceWarehouse website.
I am Looking forward for your reply.
Thanks.

How to get the face texture?

Thank you for your awesome work!
After running your project, I get the reconstructed face image and *.obj file, can you tell me how to get the rendered image just like these?
2

Texture refering to vertex colors ?

Hello,
I am trying to use my own statistical model with your code.
What is exactly is face_texture in BFM09ReconModel.forward ? I have dimensions incompatibility with a tensor of texture so I suspect it actually refers to vertex colors. correct ?

Also, what is BFM09ReconModel.point_buf ?

Can not close eye.

Thanks for your repo first.
I find the closed eye can not be fit.
Input :
image
Output:
image

The question is how to make the eyes close?

Error:convert_bfm09_data.py

I try to use colab to run the code
I use the same data as readme.md
When i execute “!python convert_bfm09_data.py”,I get the following error:

Traceback (most recent call last):
File "convert_bfm09_data.py", line 102, in
transferBFM09()
File "convert_bfm09_data.py", line 38, in transferBFM09
expPC, expEV = LoadExpBasis()
File "convert_bfm09_data.py", line 15, in LoadExpBasis
expMU.fromfile(Expbin, 3*n_vertex)
ValueError: bytes length not a multiple of item size

found at least two devices: cuda:0 and cpu

Hello,

first of all thank you for the project.
I have an issue running your python script fit_single_img.py and Im getting this error :

RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!
Are there any commands to specify the device?

Thank you in advance.

some question about code

` vs = torch.matmul(vs, self.reverse_z.repeat((batchsize, 1, 1))) + self.camera_pos

aug_projection = torch.matmul(vs, self.p_mat.repeat((batchsize, 1, 1)).permute((0, 2, 1)))`
Hi, many thanks for your great work! I don’t know if I understand it right, the first line represents the orthographic projection? The second line indicates that the picture coordinate system is converted to the pixel coordinate system?

How to Train Our Own Data

Hi, ascust, this repository is great. I am wondering how to prepare and train my own data if I want to not only reconstrct face but other components of a head?

How to get 'facemodel_info.mat'

Hi~

Thanks for this wonderful repo.
I notice that there is file named 'facemodel_info.mat', and we have to build the bfm model using this file. I'm wondering how to get this file. Especially I don't understand what 'point_buf' means. It seems 'point_buf' is used to compute the surface norm. But could you provide more information/explanation about this file? Thank you.

some questions about reg_loss

q1: why ex_coeff use sum(1).mean() ,and other two use .sum()
`def reg_loss(id_coeff, ex_coeff, tex_coeff):

loss = torch.square(id_coeff).sum() + \
        torch.square(tex_coeff).sum() * 1.7e-3 + \
        torch.square(ex_coeff).sum(1).mean() * 0.8

return loss`

q2:why pose coeff(rot and tans) not include in reg_loss ?

q3:why illumination coeff(gamma)not include in reg_loss ?

How to output a face with ears and neck?

I used the bfm model with 53215 vertices and rewritten the point_ buf and skinmask in facemodel_info.mat run directly after that, but the results are not good. Here are some results. The texture seems to be fitted, but the shape and posture are very bad.
image
What can I do to rebuild a face with ears and neck? I would be very grateful if you could help me.

how to fit to rgbd data

Hi, ascust, this repository is great. I am wondering how to modify the code to fit rgb+depth data using the real camera parameters?

question about fov

In the code set focal=1015, Could you explain how to calculate the fov's value ?
And how to use the real fov's value of the whole picture not the crop's picture ?

RuntimeError: tensor.H is only supported on matrices (2-D tensors). Got 4-D tensor.

I am going to render the face by BFM09ReconModel, using the following code,
pred_dict = self.recon_model(D3D_coeff, render=True)
where D3D_coeff is a tensor whose size is (1,257).
However, pred_dict['rendered_img'] that I finally get is a tensor whose elements are all 0, with H traceback "RuntimeError: tensor.H is only supported on matrices (2-D tensors). Got 4-D tensor."
I wonder what mistake I might make. I'd appreciate it if you could give me some advice...

Can I put an image folder as input?

Hello, thank you for this great work

I was wondering if there is anyway to make this code take an image folder as an input instead of one image at a time

mat file on load create problem

Traceback (most recent call last):
File "C:\Users\user\Downloads\3DMM-Fitting-Pytorch-master\3DMM-Fitting-Pytorch-master\3DMM-Fitting-Pytorch\convert_bfm09_data.py", line 105, in <m transferBFM09()
File "C:\Users\user\Downloads\3DMM-Fitting-Pytorch-master\3DMM-Fitting-Pytorch-master\3DMM-Fitting-Pytorch\convert_bfm09_data.py", line 32, in transferBFM09
original_BFM = loadmat('BFM/01_MorphableModel.mat')#loadmat
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\scipy\io\matlab\mio.py", line 223, in loadmat
MR, _ = mat_reader_factory(f, **kwargs)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\scipy\io\matlab\mio.py", line 72, in mat_reader_factory
mjv, mnv = get_matfile_version(byte_stream)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\scipy\io\matlab\miobase.py", line 231, in get_matfile_version
raise ValueError('Unknown mat file type, version %s, %s' % ret)
ValueError: Unknown mat file type, version 0, 0
PS C:\Users\user\Downloads\3DMM-Fitting-Pytorch-master\3DMM-Fitting-Pytorch-master\3DMM-Fitting-Pytorch> python convert_bfm09_data.py
Traceback (most recent call last):
File "C:\Users\user\Downloads\3DMM-Fitting-Pytorch-master\3DMM-Fitting-Pytorch-master\3DMM-Fitting-Pytorch\convert_bfm09_data.py", line 105, in
transferBFM09()
File "C:\Users\user\Downloads\3DMM-Fitting-Pytorch-master\3DMM-Fitting-Pytorch-master\3DMM-Fitting-Pytorch\convert_bfm09_data.py", line 32, in transferBFM09
original_BFM = loadmat('01_MorphableModel.mat')#loadmat
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\scipy\io\matlab\mio.py", line 223, in loadmat
MR, _ = mat_reader_factory(f, **kwargs)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\scipy\io\matlab\mio.py", line 72, in mat_reader_factory
mjv, mnv = get_matfile_version(byte_stream)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\scipy\io\matlab\miobase.py", line 231, in get_matfile_version
raise ValueError('Unknown mat file type, version %s, %s' % ret)
ValueError: Unknown mat file type, version 0, 0

UV coordinates for BFM09?

Hello there!

First of all, I want to express my gratitude for your excellent work.

I have a question regarding the use of UV coordinates for BFM 09 models.

I have found a resource for UVs at https://github.com/anilbas/3DMMasSTN/blob/master/util/BFM_UV.mat, but I have encountered an issue.

The vertex numbers in the BFM 09 model and the UV resource don't seem to match.

I was wondering if you or anyone else has any suggestions on how to resolve this issue?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.