SynergyNet Documentation

repository·main·Indexed 19 days ago

https://github.com/choyingw/synergynet

A high-performance framework for accurate 3D facial geometry estimation from single images. SynergyNet leverages the synergy between 3D Morphable Models (3DMM) and 3D landmarks to provide 3D landmarks, face meshes, and head pose estimation. The framework includes components such as FaceBoxes for detection and Sim3DR for 3D rendering, and supports generating textured artistic and real face renderings using predicted UV maps.

Tokens
1.6K
Snippets
10
Records
10
Agent score
15%

What's inside SynergyNet

  1. Build the CPU version of NMS for FaceBoxes

    main

    FaceBoxes requires a CPU-based Non-Maximum Suppression (NMS) extension. You can build it using the provided build script in the utils directory or by running the convenience shell script.

    # Option 1: Using the build script
    cd utils
    python3 build.py build_ext --inplace
    
    # Option 2: Using the convenience shell script
    sh ./build_cpu_nms.sh
  2. Train SynergyNet

    main

    To train the model from scratch:

    1. Complete the setup steps (1-4 of the Single Image Inference Demo).
    2. Download the training data from the 3DDFA repository (train_aug_120x120.zip) and extract it under the root folder.
    3. Execute the training script:
      bash train_script.sh

    Training Details:

    • Hyperparameters (learning rate, epochs, GPU device) are defined in train_script.sh.
    • Default settings require approximately 19GB of VRAM (e.g., on an RTX 3090) and take about 6 hours.
    • Scaling: If using a smaller GPU, decrease the batch size and learning rate proportionally.
    bash train_script.sh
  3. Install and Setup SynergyNet

    main

    Follow these steps to set up the environment for single image inference:

    1. Clone the repository:
      git clone https://github.com/choyingw/SynergyNet
      cd SynergyNet
    2. Create a Conda environment:
      conda create --name SynergyNet
      conda activate SynergyNet
    3. Install prerequisites: Install PyTorch 1.9 (compatible with 1.0+), torchvision, opencv, scipy, matplotlib, and cython.
    4. Download and prepare data:
      • Download the required data files from the provided Google Drive links (refer to the README for specific links).
      • Extract them under the repository root.
      • Download pretrained weights and place them in the pretrained/ directory.
    5. Compile C++ extensions:
      cd Sim3DR
      ./build_sim3dr.sh
      cd ../FaceBoxes
      ./build_cpu_nms.sh
      cd ..

    Note on CPU usage: The default inference requires a compatible GPU. To run on a CPU, you must manually comment out .cuda() calls in the code and ensure weights are loaded to the CPU.

    # Example of the sequence of commands
    git clone https://github.com/choyingw/SynergyNet
    cd SynergyNet
    conda create --name SynergyNet
    conda activate SynergyNet
    cd Sim3DR
    ./build_sim3dr.sh
    cd ../FaceBoxes
    ./build_cpu_nms.sh
    cd ..
    python singleImage.py -f img
  4. Generate Textured Artistic Face Meshes

    main

    This feature uses a UV-texture GAN to generate meshes from artistic face datasets.

    1. Complete setup steps 1-5 of the Single Image Inference Demo.
    2. Download the artistic faces data (from AF-Dataset) and the predicted UV maps. Extract them under the root folder.
    3. Run the artistic.py script:
    • For a whole folder:
      python artistic.py -f art-all --png
    • For a single image:
      python artistic.py -f art-all/122.png

    Note: Results are significantly better for images that are close to real faces compared to highly abstract samples.

    python artistic.py -f art-all --png
  5. Generate Textured Real Face Renderings

    main

    To generate 3D meshes and renderings from real face images using predicted UV maps:

    1. Complete setup steps 1-5 of the Single Image Inference Demo.
    2. Download the predicted UV maps and real face images for AFLW2000-3D. Extract them under the root folder.
    3. Run the uv_texture_realFaces.py script:
    • For a whole folder:
      python uv_texture_realFaces.py -f texture_data/real --png
    • For a single image:
      python uv_texture_realFaces.py -f texture_data/real/image00002_real_A.png

    Results (3D meshes and renderings) are saved in the inference_output directory.

    python uv_texture_realFaces.py -f texture_data/real --png
  6. Use the Simplified SynergyNet API

    main

    You can integrate SynergyNet into your own projects using a simplified API. First, install the package in editable mode:

    pip install -e .

    Requirements: The 3dmm_data folder and the pretrained/ directory containing weights must be present in your environment.

    Usage: Import SynergyNet from synergy3DMM to get 3D landmarks, face mesh, and face pose in a single call.

    Outputs:

    • lmk3d: 3D landmarks as [[y, x, z], 68 (points)].
    • mesh: 3D face mesh as [[y, x, z], 53215 (points)].
    • pose: Face pose consisting of Euler angles [yaw, pitch, roll] and translation [y, x, z].
    import cv2
    from synergy3DMM import SynergyNet
    
    model = SynergyNet()
    I = cv2.imread('<your image path>')
    
    # Returns 3D landmarks, mesh, and pose
    lmk3d, mesh, pose = model.get_all_outputs(I)
  7. Run Benchmark Evaluation

    main

    To evaluate the model performance using the benchmark script, ensure you have completed the setup steps (1-4 of the Single Image Inference Demo). Run the following command, specifying the pretrained model path with the -w flag:

    python benchmark.py -w pretrained/best.pth.tar

    Results and visualizations for the first 50 examples will be stored in the results/ directory.

    python benchmark.py -w pretrained/best.pth.tar