- Installation
- Dataset preparation
- Model selection
- Training
- Evaluation
- Prediction
- Background Replacement
- Export and Deployment
Versions
-
PaddlePaddle >= 2.0.2
-
Python >= 3.7+
Due to the high computational cost of model, PaddleSeg is recommended for GPU version PaddlePaddle. CUDA 10.0 or later is recommended. See PaddlePaddle official website for the installation tutorial.
git clone https://github.com/PaddlePaddle/PaddleSeg
cd PaddleSeg/Matting
pip install -r requirements.txt
Using MODNet's open source PPM-100 dataset as our demo dataset for the tutorial. Custom dataset refer to dataset preparation.
Download the prepared PPM-100 dataset.
mkdir data && cd data
wget https://paddleseg.bj.bcebos.com/matting/datasets/PPM-100.zip
unzip PPM-100.zip
cd ..
The dataset structure is as follows.
PPM-100/
|--train/
| |--fg/
| |--alpha/
|
|--val/
| |--fg/
| |--alpha
|
|--train.txt
|
|--val.txt
Note : This dataset is only used as a tutorial demonstration and cannot be trained to produce a convergent model.
The Matting project supports configurable direct drive, with model config files placed in configs directory. You can select a config file based on the actual situation to perform training, prediction et al.
This tutorial uses configs/quick_start/ppmattingv2-stdc1-human_512.yml for teaching demonstrations.
export CUDA_VISIBLE_DEVICES=0
python tools/train.py \
--config configs/quick_start/ppmattingv2-stdc1-human_512.yml \
--do_eval \
--use_vdl \
--save_interval 500 \
--num_workers 5 \
--save_dir output
Using --do_eval
will affect training speed and increase memory consumption, turning on and off according to needs.
If opening the --do_eval
, the historical best model will be saved to '{save_dir}/best_model' according to SAD. At the same time, 'best_sad.txt' will be generated in this directory to record the information of metrics and iter at this time.
--num_workers
Read data in multi-process mode. Speed up data preprocessing.
Run the following command to view more parameters.
python tools/train.py --help
If you want to use multiple GPUs,please use python -m paddle.distributed.launch
to run.
export CUDA_VISIBLE_DEVICES=0
python tools/val.py \
--config configs/quick_start/ppmattingv2-stdc1-human_512.yml \
--model_path output/best_model/model.pdparams \
--save_dir ./output/results \
--save_results
--save_result
The prediction results will be saved if turn on. If it is off, it will speed up the evaluation.
You can directly download the provided model for evaluation.
Run the following command to view more parameters.
python tools/val.py --help
export CUDA_VISIBLE_DEVICES=0
python tools/predict.py \
--config configs/quick_start/ppmattingv2-stdc1-human_512.yml \
--model_path output/best_model/model.pdparams \
--image_path data/PPM-100/val/fg/ \
--save_dir ./output/results \
--fg_estimate True
If the model requires trimap information, pass the trimap path through '--trimap_path'.
--fg_estimate False
can turn off foreground estimation, which improves prediction speed but reduces image quality.
You can directly download the provided model for evaluation.
Run the following command to view more parameters.
python tools/predict.py --help
export CUDA_VISIBLE_DEVICES=0
python tools/bg_replace.py \
--config configs/quick_start/ppmattingv2-stdc1-human_512.yml \
--model_path output/best_model/model.pdparams \
--image_path path/to/your/image \
--background path/to/your/background/image \
--save_dir ./output/results \
--fg_estimate True
If the model requires trimap information, pass the trimap path through --trimap_path
.
--background
can pass a path of brackground image or select one of ('r', 'g', 'b', 'w') which represent red, green, blue and white. If it is not specified, a green background is used.
--fg_Estimate False
can turn off foreground estimation, which improves prediction speed but reduces image quality.
note: --image_path
must be a image path。
You can directly download the provided model for background replacement.
Run the following command to view more parameters.
python tools/bg_replace.py --help
python tools/export.py \
--config configs/quick_start/ppmattingv2-stdc1-human_512.yml \
--model_path output/best_model/model.pdparams \
--save_dir output/export \
--input_shape 1 3 512 512
If the model requires trimap information such as DIM, --trimap
is need.
Run the following command to view more parameters.
python tools/export.py --help
python deploy/python/infer.py \
--config output/export/deploy.yaml \
--image_path data/PPM-100/val/fg/ \
--save_dir output/results \
--fg_estimate True
If the model requires trimap information, pass the trimap path through '--trimap_path'.
--fg_Estimate False
can turn off foreground estimation, which improves prediction speed but reduces image quality.
Run the following command to view more parameters.
python deploy/python/infer.py --help