MODNet Portrait Matting

repository·master·Indexed 26 days ago

https://github.com/zhkkke/modnet

A real-time, trimap-free portrait matting model that requires only RGB image input. Designed for high-speed performance on PCs and mobile devices, MODNet supports image and video matting, including WebCam-based real-time processing. The repository provides tools for exporting models to ONNX and TorchScript formats, as well as training logic for supervised training and SOC adaptation via src/trainer.py.

Tokens
2.9K
Snippets
9
Records
23
Agent score
88%

What's inside MODNet

  1. Export MODNet to ONNX format

    master

    To convert a PyTorch checkpoint to an ONNX model, run the onnx.export_onnx module. Note that a higher version of PyTorch than the official MODNet (which uses torch==1.7.1) is recommended for this export function.

    python -m onnx.export_onnx \
        --ckpt-path=pretrained/modnet_photographic_portrait_matting.ckpt \
        --output-path=pretrained/modnet_photographic_portrait_matting.onnx
  2. Download MODNet pre-trained models

    master

    Official pre-trained models for MODNet are hosted on Google Drive. You must download these models manually and place them in the pretrained/ directory of the repository to use them for portrait matting tasks.

    https://drive.google.com/drive/folders/1umYmlCulvIFNaqPjwod1SayFmSRHziyR?usp=sharing
  3. Export MODNet to TorchScript

    master

    To convert a standard MODNet checkpoint into a TorchScript version, ensure you are in the project root directory and have PyTorch version 1.2.0 or higher installed. Use the torchscript.export_torchscript module to perform the export.

    python -m torchscript.export_torchscript \
        --ckpt-path=pretrained/modnet_photographic_portrait_matting.ckpt \
        --output-path=pretrained/modnet_photographic_portrait_matting.torchscript
  4. Run the Custom Portrait Video Matting Demo

    master

    Execute the matting process using the demo.video_matting.custom.run module. You must provide the path to your input video.

    Command Syntax:

    python -m demo.video_matting.custom.run --video YOUR_VIDEO_PATH

    Arguments:

    • --video YOUR_VIDEO_PATH: (Required) The specific path to the video file you want to process.
    • --result-type: (Optional, default=fg) Specifies what to save. Options are fg (save the alpha matte) or fg (save the foreground).
    • --fps: (Optional, default=30) Sets the frames per second for the resulting video.
  5. Optimize Video Quality for MODNet Matting

    master

    To achieve the best results with the MODNet video matting demo, ensure your input videos meet the following criteria:

    • Distinguishable Subjects: The portrait and the background should be clearly distinguishable (not similar).
    • Lighting: Videos should be captured in soft and bright ambient lighting.
    • Motion: Contents should not move too fast.

    Note on Limitations:

    • The model may make errors in portrait semantics estimation under challenging scenes due to limited labeled training data.
    • The demo does not currently support the OFD trick.
  6. Setup the WebCam Matting Demo environment

    master

    Follow these steps to set up a conda environment and install dependencies for the WebCam demo:

    1. Clone the repository and enter the directory:
      git clone https://github.com/ZHKKKe/MODNet.git
      cd MODNet
    2. Download the pre-trained model from this link and place it in the MODNet/pretrained/ folder.
    3. Create and activate a conda environment (e.g., using Python 3.6):
      conda create -n modnet python=3.6
      source activate modnet
    4. Install dependencies (ensure your CUDA version is compatible with your PyTorch installation):
      pip install -r demo/video_matting/webcam/requirements.txt
    5. Run the demo:
      python -m demo.video_matting.webcam.run
    git clone https://github.com/ZHKKKe/MODNet.git
    cd MODNet
    conda create -n modnet python=3.6
    source activate modnet
    pip install -r demo/video_matting/webcam/requirements.txt
    python -m demo.video_matting.webcam.run
  7. Run MODNet video matting offline

    master

    For higher performance (FPS) or custom video processing, use the offline demos:

    • WebCam (Ubuntu recommended): Use the offline demo located at demo/video_matting/webcam.
    • Custom Videos: Use the offline demo located at demo/video_matting/custom to process your own video files.
  8. Explore MODNet community extensions

    master

    The community has developed several optimized versions and applications of MODNet:

    • ONNX Version: Convert pre-trained models to ONNX using this code or try the ONNX Colab demo.
    • TorchScript Version: Convert models to TorchScript using this code.
    • TensorRT Version: Access TensorRT optimized versions via this repository.
    • Docker Container: Use a containerized environment via this repository.
    • Background Blur: Use this Colab demo to apply Bokeh effects using MODNet.
  9. Setup the MODNet Custom Portrait Video Matting Demo

    master

    To run the custom portrait video matting demo, follow these steps to prepare your environment and assets:

    1. Clone the repository:

      git clone https://github.com/ZHKKKe/MODNet.git
      cd MODNet
    2. Download the pre-trained model: Download the model from this link and place it in the MODNet/pretrained/ directory.

    3. Create a Conda environment:

      conda create -n modnet python=3.6
      source activate modnet
    4. Install dependencies: Ensure your CUDA version is compatible with your installed PyTorch version before running:

      pip install -r demo/video_matting/custom/requirements.txt
    git clone https://github.com/ZHKKKe/MODNet.git
    cd MODNet
    conda create -n modnet python=3.6
    source activate modnet
    pip install -r demo/video_matting/custom/requirements.txt