yakhyo Face Parsing

repository·main·Indexed 43 days ago

https://github.com/yakhyo/face-parsing

A high-precision face parsing model based on BiSeNet for real-time semantic segmentation of facial features. It supports ResNet18 and ResNet34 backbones, provides pre-trained weights in PyTorch (.pt) and ONNX formats, and includes tools for training on datasets like CelebAMask-HQ, PyTorch inference, and ONNX export.

Tokens
2.6K
Snippets
11
Records
12
Agent score
6%

What's inside yakhyo-face-parsing

  1. Prepare the dataset for training

    main

    The model expects a specific directory structure for training datasets. You should organize your images and their corresponding segmentation masks as follows:

    • dataset/images/: Contains the input face images (e.g., .jpg).
    • dataset/labels/: Contains the corresponding segmentation masks (e.g., .png).

    Note: The model was originally trained on the CelebAMask-HQ dataset.

    dataset/
    ├── images/           # Input face images
    │   ├── image1.jpg
    │   ├── image2.jpg
    │   └── ...
    └── labels/           # Corresponding segmentation masks
        ├── image1.png
        ├── image2.png
        └── ...
  2. Train the face parsing model

    main

    Use train.py to train the BiSeNet model on your dataset. Ensure your dataset is prepared according to the project's dataset requirements before starting. You can specify the backbone (e.g., resnet18 or resnet34), batch size, epochs, and other hyperparameters via CLI arguments.

    # Start training with default parameters
    python train.py --data-root /path/to/your/dataset
    
    # Custom training example
    python train.py --data-root /path/to/dataset --backbone resnet34 --batch-size 8 --epochs 200
  3. Download pre-trained model weights

    main

    You can download pre-trained weights for ResNet18 and ResNet34 in both PyTorch (.pt) and ONNX formats from the official releases. These weights are required for inference and can be placed in a weights/ directory.

    | Model    | PT                                                                                         | ONNX                                                                                           |
    | -------- | ------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------- |
    | ResNet18 | [resnet18.pt](https://github.com/yakhyo/face-parsing/releases/download/weights/resnet18.pt) | [resnet18.onnx](https://github.com/yakhyo/face-parsing/releases/download/weights/resnet18.onnx) |
    | ResNet34 | [resnet34.pt](https://github.com/yakhyo/face-parsing/releases/download/weights/resnet34.pt) | [resnet34.onnx](https://github.com/yakhyo/face-parsing/releases/download/weights/resnet34.onnx) |
  4. Export PyTorch models to ONNX

    main

    Use onnx_export.py to convert trained PyTorch .pt models into ONNX format for cross-platform deployment. The exported .onnx file will be created in the same directory as the source weight file.

    # Export ResNet18 model
    python onnx_export.py --model resnet18 --weight ./weights/resnet18.pt
    
    # Export ResNet34 model
    python onnx_export.py --model resnet34 --weight ./weights/resnet34.pt
  5. Run PyTorch inference

    main

    Use inference.py to perform face parsing on a single image or a folder of images using PyTorch weights.

    # Single image inference
    python inference.py --model resnet18 --weight ./weights/resnet18.pt --input ./assets/images/1.jpg --output ./results
    
    # Batch processing (folder of images)
    python inference.py --model resnet18 --weight ./weights/resnet18.pt --input ./assets/images --output ./assets/results
  6. Install the Face Parsing Model

    main

    To use the Face Parsing Model, clone the repository and install the necessary Python dependencies using pip.

    git clone https://github.com/yakhyo/face-parsing.git
    cd face-parsing
    pip install -r requirements.txt
  7. Run ONNX inference

    main

    Use onnx_inference.py to perform face parsing using an exported ONNX model. This is useful for deployment in environments where PyTorch is not available.

    python onnx_inference.py --model ./weights/resnet18.onnx --input ./assets/images --output ./assets/results/resnet18onnx
  8. Reference: Training CLI arguments

    main

    Arguments for train.py to configure the training process.

    options:
      -h, --help            show this help message and exit
      --num-classes NUM_CLASSES
                            Number of classes in the dataset (default: 19)
      --batch-size BATCH_SIZE
                            Batch size for training (default: 16)
      --num-workers NUM_WORKERS
                            Number of workers for data loading (default: 4)
      --image-size IMAGE_SIZE IMAGE_SIZE
                            Size of input images (default: 512 512)
      --data-root DATA_ROOT
                            Root directory of the dataset
      --momentum MOMENTUM   Momentum for optimizer (default: 0.9)
      --weight-decay WEIGHT_DECAY
                            Weight decay for optimizer (default: 5e-4)
      --lr-start LR_START   Initial learning rate (default: 1e-2)
      --max-iter MAX_ITER   Maximum number of iterations (default: 80000)
      --power POWER         Power for learning rate policy (default: 0.9)
      --lr-warmup-epochs LR_WARMUP_EPOCHS
                            Number of warmup epochs (default: 10)
      --warmup-start-lr WARMUP_START_LR
                            Warmup starting learning rate (default: 1e-5)
      --score-thres SCORE_THRES
                            Score threshold (default: 0.7)
      --epochs EPOCHS       Number of epochs for training (default: 150)
      --backbone BACKBONE   Backbone architecture (default: resnet18)
      --print-freq PRINT_FREQ
                            Print frequency during training (default: 10)
      --resume              Resume training from checkpoint
  9. Reference: PyTorch Inference CLI arguments

    main

    Arguments for inference.py.

    options:
      -h, --help       show this help message and exit
      --model MODEL    model name, i.e resnet18, resnet34
      --weight WEIGHT  path to trained model, i.e resnet18/34
      --input INPUT    path to an image or a folder of images
      --output OUTPUT  path to save model outputs
  10. Reference: ONNX Inference CLI arguments

    main

    Arguments for onnx_inference.py.

    options:
      -h, --help       show this help message and exit
      --model MODEL    path to ONNX model file
      --input INPUT    path to an image or a folder of images
      --output OUTPUT  path to save model outputs
  11. Reference: ONNX Export CLI arguments

    main

    Arguments for onnx_export.py.

    options:
      -h, --help       show this help message and exit
      --model MODEL    model name, i.e resnet18, resnet34 (default: resnet18)
      --weight WEIGHT   path to trained PyTorch model (default: ./weights/resnet18.pt)
  12. Compare ResNet18 and ResNet34 model performance

    main

    The repository provides two backbone models for face parsing. Choose between them based on your requirements for parameter count and model size:

    ModelParametersModel Size
    ResNet18~11.2M~43MB
    ResNet34~21.3M~82MB