When training for custom dataset, you need to prepare dataset in the appropriate format.
We provide two forms of dataset, one for offline composition and one for online composition.
## Offline composition
If the images have been composited offline or do not need to be composited, the dataset should be organized as follows.
```
dataset_root/
|--train/
| |--fg/
| |--alpha/
|
|--val/
| |--fg/
| |--alpha/
|
|--train.txt
|
|--val.txt
```
where, fg folder stores the original images. The image name in the fg folder must correspond to that in the alpha folder one by one,
and the resolution must be the same for the correspond image in the two folders.
train.txt and val.txt contents are as follows.
```
train/fg/14299313536_ea3e61076c_o.jpg
train/fg/14429083354_23c8fddff5_o.jpg
train/fg/14559969490_d33552a324_o.jpg
...
```
## Online composition
Data reading support online composition, that is, the image input network composited online by the foreground, alpha, and background images,
which like the Composition-1k dataset used in [Deep Image Matting](https://arxiv.org/pdf/1703.03872.pdf) .
The dataset should be organized as follows:
```
Composition-1k/
|--bg/
|
|--train/
| |--fg/
| |--alpha/
|
|--val/
| |--fg/
| |--alpha/
| |--trimap/ (如果存在)
|
|--train.txt
|
|--val.txt
```
where, the fg folder stores the foreground images and the bg folder stores the background images.
The contents of train.txt is as follows:
```
train/fg/fg1.jpg bg/bg1.jpg
train/fg/fg2.jpg bg/bg2.jpg
train/fg/fg3.jpg bg/bg3.jpg
...
```
where, the first column is the foreground images and the second column is the background images.
The contents of val.txt are as follows. If trimap does not exist in dataset, the third column is not needed and the code will generate trimap automatically.
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](https://www.paddlepaddle.org.cn/install/quick?docurl=/documentation/docs/zh/install/pip/linux-pip.html) for the installation tutorial.
**Note** : This dataset is only used as a tutorial demonstration and cannot be trained to produce a convergent model.
## Model selection
The Matting project supports configurable direct drive, with model config files placed in [configs](../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](../configs/quick_start/ppmattingv2-stdc1-human_512.yml) for teaching demonstrations.
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.
```shell
python tools/train.py --help
```
If you want to use multiple GPUs,please use `python -m paddle.distributed.launch` to run.
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.
**Note**: The website is developed and maintained by community developers and may be unstable. It is recommended that you based on [quick start](./quick_start_en.md).