Git Product home page Git Product logo

photometric_optimization's Introduction

Photometric FLAME Fitting

This repository provides an analysis-by-synthesis framework to fit a textured FLAME model to an image. FLAME is a lightweight generic 3D head model learned from over 33,000 head scans, but it does not come with an appearance space (see the scientific publication for details).

Variations of the texture space for the first five principal components. Each column shows the variation for ±2 standard deviations along one axis.

This repository

  1. describes how to build a texture space for FLAME from in-the-wild images, and provides
  2. code to fit a textured FLAME model to in-the-wild images, optimizing for FLAME's parameters, appearance, and lighting, and
  3. code to optimize for the FLAME texture to match an in-the-wild image.

The FLAME model and the texture space can be downloaded from the FLAME project website. You need to sign up and agree to the license for access.

The demos will be released soon.

Build FLAME texture space from in-the-wild images

The goal is to build a texture space from in-the-wild images in order to cover large range of ethnicities, age groups, etc. We therefore randomly select 1500 images from the FFHQ dataset in order to build a texture space. This is done in following steps

1. Initialization

Building a texture space from in-the-wild images is a chicken-and-egg problem. Given a texture space, it can be used in an analysis-by-synthesis fashion to fit the 3D model to images, where these fits then can be used to build a texture space. To get an initial texture space, we fit FLAME to the Basel Face Model (BFM) template, and project the BFM vertex colors onto the FLAME mesh, to get an initial texture basis.

2. Model fitting

We then fit FLAME to the FFHQ images, optimizing for the FLAME shape, pose, and expression parameters, the parameters of the initial texture space, the parameters for Spherical Harmonics (SH) lighting (we optimize for 9 SH coefficient only, shared across all three color channels), and a texture offset to capture texture details deviating from the initial texture space. The fitting minimizes a landmark loss, a photometric loss, and diverse regularizers for shape, pose, expression, appearance, and the texture offset.

The landmark loss minimizes the difference between the landmarks projected from the face model's surface, and predicted 2D landmarks (predicted using the FAN landmark predictor). The photometric loss is optimized for the skin region only (provided by the face segmentation network) to gain robustness to partial occlusions. See the provided code for details how to fit a textured FLAME model to an image.

3. Texture completion

After fitting, the computed texture offsets capture for each image the facial appearance of the non-occluded skin region. To complete the texture maps, we train an inpainting network adapted from GMCNN (across all texture maps) supervisely by adding random strokes (i.e. strokes of random size and location) in the visible face region(visibility obtained from the fitted reconstruction) and learning to inpaint these strokes. Once trained, we inpaint all missing regions with the resulting inpainting network.

4. Texture space computation

After completing these 1500 texture maps, we use principal component analysis (PCA) to compute a texture space.

Demos

The single image photometric fitting demo is implemented and tested in a conda environment with PyTorch 1.5 and PyTorch3D 0.2 in Python 3.8.3. For better CUDA supports, we recommend you to install PyTorch3D 0.2 via conda,

conda create -n pytorch3d python=3.8
conda activate pytorch3d
conda install -c pytorch pytorch=1.5.0 torchvision cudatoolkit=10.2
conda install -c conda-forge -c fvcore fvcore
conda install pytorch3d -c pytorch3d

ATTENTION: The pip and conda packages of PyTorch3D have different dependencies, please follow their installation guide.

Run this demo with specified FFHQ image name and computing device,

python photometric_fitting.py 00000 cuda

Another simple demo to sample the texture space can be found here.

License

The code is available for non-commercial scientific research purposes. The texture model is available under Creative Commons BY-NC-SA 4.0 license. For details see the Texture license.

Notes

We use the FLAME.py from FLAME_PyTorch and the renderer.py from DECA.

Citation

When using this code or the texture model in a scientific publication, please cite this GitHub repository and the FFHQ dataset. When using the FLAME geometry model, please cite the model (you find the up-to-date bibtex here).

Contact

For questions regarding the provided fitting code please contact [email protected], for FLAME related questions please contact [email protected].

photometric_optimization's People

Contributors

havenfeng avatar timobolkart 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

photometric_optimization's Issues

What are "Full Landmarks"?

Hi,

I looked through the code and tried to comprehend how the dynamic landmarks are calculated. Additionally to the indices and the barycentric coordinates of the dynamic and static landmarks I found full_lmk_faces_idx and full_lmk_bary_coords in the code.
What are these landmarks and why are they needed in addition to the dynamic and static landmarks?

Best,
Dominik

Releasing the code

Hey,
first of all thanks for your great work!

I was reading how you built the texture space from the FFHQ dataset in the Readme-file.
Is it possible that you release the code you used for building the texture space? I would be especially interested in seeing how you formulated the photometric loss.

Multiple file missing and wanting for an library list

When I am running the demo python photometric_fitting.py 00000 cuda
First problem:
FileNotFoundError: [Errno 2] No such file or directory: './data/generic_model.pkl'
I downloaded flame2023.pkl and rename to it. I am not sure this is the right thing to do.
Further another problem appeared.

/home/guest/anaconda3/envs/smart_class/lib/python3.10/site-packages/pytorch3d/io/obj_io.py:548: UserWarning: Mtl file does not exist: ./data/template.mtl
  warnings.warn(f"Mtl file does not exist: {f}")
making ./test_results//00000
----iter: 0, time: 2023-06-20-20:31:41
landmark: 0.12438790500164032, all_loss: 0.12438790500164032,
Traceback (most recent call last):
  File "/data/share/FLAME/Photometric_fitting/photometric_optimization/photometric_fitting.py", line 265, in <module>
    fitting.run(imagepath, landmarkpath)
  File "/data/share/FLAME/Photometric_fitting/photometric_optimization/photometric_fitting.py", line 218, in run
    single_params = self.optimize(images, landmarks, image_masks, savefolder)
  File "/data/share/FLAME/Photometric_fitting/photometric_optimization/photometric_fitting.py", line 91, in optimize
    util.tensor_vis_landmarks(images[visind], landmarks[visind]))
  File "/data/share/FLAME/Photometric_fitting/photometric_optimization/util.py", line 209, in tensor_vis_landmarks
    image_landmarks = plot_kpts(image, predicted_landmark, color)
  File "/data/share/FLAME/Photometric_fitting/photometric_optimization/util.py", line 250, in plot_kpts
    image = cv2.circle(image,(st[0], st[1]), 1, c, 2)
cv2.error: OpenCV(4.7.0) :-1: error: (-5:Bad argument) in function 'circle'
> Overload resolution failed:
>  - Can't parse 'center'. Sequence item with index 0 has a wrong type
>  - Can't parse 'center'. Sequence item with index 0 has a wrong type

In addition, I have to install different versions of libraries to fit the codes?
Can you please update an library list so we can install the correct versions?

About convergence rate

Thanks for your great work!
It can be seen from the log that Adam is very stable, but the convergence time is longer. I tried to torch.optim.lbfgs to speed up the convergence, but I don’t understand why nan always appears in training. Can you provide some solutions? Have been troubled for a long time, looking forward to your reply!

About the cam

thanks for your work!
I want to know why cam[:,0]=5?

Custom image

Nice work!

I wonder how can I apply this for a custom image?

I guess I need both

  • a landmark file ( landmarkpath = os.path.sep.join([input_folder, image_name + '.npy']) )
  • image mask file ( image_mask_path = os.path.sep.join([image_mask_folder, image_name + '.npy']) )

for the custom image, right? Do I need more than this? How can I obtain these files for a custom image?

Thanks!

How to customize landmark

hi, I need to replace the customized landmark for fitting. How to make the new ‘landmark_embedding.npy' file?

More detailed head reconstruction

Hello, I am a beginner, I want better head reconstruction results, how should I do it, can you give me some detailed suggestions?
Thank you.

projection

hi, many thanks for your great work! I wonder why there mul -1 here

What is the difference between 3d lmks and 2d lmks?

Hi!
From the code, it is not clear what is the difference between 3d lmks and 2d lmks? Both have 68 points and both have an effective dimension of 3. As I can judge from the code, 3d lmks are only used for visualization. But what is the actual difference between them, and why 2dlmks have the prefix "2d" even if they are actually 3d?
Thanks

About dynamic_lmk_faces_idx

Hi
Could you tell me how to get the file "landmark_embedding.npy", especially, 'dynamic_lmk_faces_idx', THX

Perspective projection

Are there any specific reasons for you using orthogonal projection instead of perspective? I have tried reconstruction of the head from the photo with your code, and it doesn't align well with the ground truth point cloud obtained through photogrammetry. I suspect the problem is with the orthogonal projection. So, my question is, can I expect it to work after replacing an ortho projection with the perspective one.
Thanks for the great work!

Basel Face Model (BFM) to FLAME

How did you fit BFM model to FLAME? I have tried fitting a Flame mean shape to BFM mean shape, but couldn't find good shape parameters for FLAME model.

If you can give me some info on this it would be helpful.

uv maps

Hi,
I am trying to create a UV map from a tracked face model using the world2uv function, but the values are in a weird range [-0.22; 0.13]. When I scale the values into [0,255], the picture looks like this:
Screenshot 2021-01-12 at 19 31 44

Is this correct?

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.