Official repository for Natural Image Matting via Guided Contextual Attention

Overview

GCA-Matting: Natural Image Matting via Guided Contextual Attention

The source codes and models of Natural Image Matting via Guided Contextual Attention which will appear in AAAI-20.

Matting results on test data from alphamatting.com with trimap-user.

Requirements

Packages:

  • torch >= 1.1
  • tensorboardX
  • numpy
  • opencv-python
  • toml
  • easydict
  • pprint

GPU memory >= 8GB for inference on Adobe Composition-1K testing set

Models

The models pretrained on Adobe Image Matting Dataset are covered by Adobe Deep Image Mattng Dataset License Agreement and can only be used and distributed for noncommercial purposes.

Model Name Training Data File Size MSE SAD Grad Conn
ResNet34_En_nomixup ISLVRC 2012 166 MB N/A N/A N/A N/A
gca-dist Adobe Matting Dataset 96.5 MB 0.0091 35.28 16.92 32.53
gca-dist-all-data Adobe Matting Dataset
+ Composition-1K
96.5 MB - - - -
  • ResNet34_En_nomixup: Model of the customized ResNet-34 backbone trained on ImageNet. Save to ./pretrain/. The training codes of ResNet34_En_nomixup and more variants will be released as an independent repository later. You need this checkpoint only if you want to train your own matting model.
  • gca-dist: Model of the GCA Matting in Table 2 in the paper. Save to ./checkpoints/gca-dist/.
  • gca-dist-all-data: Model of the GCA Matting trained on both Adobe Image Matting Dataset and the Composition-1K testing set for alphamatting.com online benchmark. Save to ./checkpoints/gca-dist-all-data/.

(We removed optimizer state_dict from gca-dist.pth and gca-dist-all-data.pth to save space. So you cannot resume the training from these two models.)

Run a Demo on alphamatting.com Testing Set

python demo.py \
--config=config/gca-dist-all-data.toml \
--checkpoint=checkpoints/gca-dist-all-data/gca-dist-all-data.pth \
--image-dir=demo/input_lowres \
--trimap-dir=demo/trimap_lowres/Trimap3 \
--output=demo/pred/Trimap3/

This will load the configuration from config and save predictions in output/config_checkpoint/*. You can reproduce our alphamatting.com submission by this command.

Train and Evaluate on Adobe Image Matting Dataset

Data Preparation

Since each ground truth alpha image in Composition-1K is shared by 20 merged images, we first copy and rename these alpha images to have the same name as their trimaps. If your ground truth images are in ./Combined_Dataset/Test_set/Adobe-licensed images/alpha, run following command:

./copy_testing_alpha.sh Combined_Dataset/Test_set/Adobe-licensed\ images

New alpha images will be generated in Combined_Dataset/Test_set/Adobe-licensed images/alpha_copy

Configuration

TOML files are used as configurations in ./config/. You can find the definition and options in ./utils/config.py.

Training

Default training requires 4 GPUs with 11GB memory, and the batch size is 10 for each GPU. First, you need to set your training and validation data path in configuration and dataloader will merge training images on-the-fly:

[data]
train_fg = ""
train_alpha = ""
train_bg = ""
test_merged = ""
test_alpha = ""
test_trimap = ""

You can train the model by

./train.sh

or

OMP_NUM_THREADS=2 python -m torch.distributed.launch \
--nproc_per_node=4 main.py \
--config=config/gca-dist.toml

For single GPU training, set dist=false in your *.toml and run

python main.py --config=config/*.toml

Evaluation

To evaluate our model or your own model on Composition-1K, set the path of Composition-1K testing and model name in the configuration file *.toml:

[test]
merged = "./data/test/merged"
alpha = "./data/test/alpha_copy"
trimap = "./data/test/trimap"
# this will load ./checkpoint/*/gca-dist.pth
checkpoint = "gca-dist" 

and run the command:

./test.sh

or

python main.py \
--config=config/gca-dist.toml \
--phase=test

The predictions will be save to** ./prediction by default, and you can evaluate the results by the MATLAB file ./DIM_evaluation_code/evaluate.m in which the evaluate functions are provided by Deep Image Matting. Please do not report the quantitative results calculated by our python code like ./utils/evaluate.py or this test.sh in your paper or project. The Grad and Conn functions of our reimplementation are not exactly the same as MATLAB version.

Citation

If you find this work or code useful for your research, please cite:

@inproceedings{li2020natural,
  title={Natural image matting via guided contextual attention},
  author={Li, Yaoyi and Lu, Hongtao},
  booktitle={Proceedings of the AAAI Conference on Artificial Intelligence},
  volume={34},
  pages={11450--11457},
  year={2020}
}
Comments
  • training with new dataset

    training with new dataset

    Good day I want to try to train the model with a new dataset I´m creating,

    In this new dataset, for each input image, I have a ground truth alpha mask.

    In here: train_image_file = ImageFileTrain(alpha_dir=CONFIG.data.train_alpha, fg_dir=CONFIG.data.train_fg, bg_dir=CONFIG.data.train_bg)

    It seems to ask for 3 folders, one for the ground truth alpha it seems, and then two for fg and bg? what are these?

    if I have input images and ground truth alphas, how can I adapt them to work with the input of your model? thank you very much

    opened by javismiles 11
  • About sample unknown point in the RandomCrop function

    About sample unknown point in the RandomCrop function

    Thanks for the great work and code :) Your implementation is really elegant and impressive. However, I found that some part in the RandomCrop function is a little bit confusing:

    https://github.com/Yaoyi-Li/GCA-Matting/blob/92c40b02f3f7c5dced6aed0eaf4de6eae6546c2d/dataloader/data_generator.py#L296

    Based on this line, I assume that you are sampling unknown point as the center of the crop patch (based on that you deal with the border at 4 sides)

    However, the later code using the sampled unknown point seems to treat it as the left-top point of the crop patch: https://github.com/Yaoyi-Li/GCA-Matting/blob/92c40b02f3f7c5dced6aed0eaf4de6eae6546c2d/dataloader/data_generator.py#L306

    Am I misunderstanding something? Thanks!

    opened by yucornetto 2
  • Can not reproduce 35 SAD by your pretrained model

    Can not reproduce 35 SAD by your pretrained model

    Hi Yaoyi,

    Thank you very much for this extremely powerful work!

    I would like to reproduce your result using the provided pretrained model. I use your code Composition_code.py to generate the test set. After running your test.sh, however, I only get SAD 39.

    The only clue is that it has warnings by libpng like:

    libpng warning: iCCP: known incorrect sRGB profile

    But I think this warning should not influence the matting result.

    I note that you have emphassis that matlab evaluate code is different from your python. So what's the SAD should it be for python evaluation?

    opened by kfeng123 2
  • More specific description on train and test datasets

    More specific description on train and test datasets

    Thanks for your awesome work.Now i want use my own datasets to train the model.When i am working on making datasets,i am confused on the datasets structure.When i read your code,i find these paths:

    train_fg = "/home/liyaoyi/dataset/Adobe/all/fg"
    train_alpha = "/home/liyaoyi/dataset/Adobe/all/alpha"
    train_bg = "/home/liyaoyi/dataset/coco_bg"
    test_merged = "/home/liyaoyi/dataset/Adobe/Combined_Dataset/Test_set/merged"
    test_alpha = "/home/liyaoyi/dataset/Adobe/Combined_Dataset/Test_set/alpha_copy"
    test_trimap = "/home/liyaoyi/dataset/Adobe/Combined_Dataset/Test_set/trimaps"
    

    What are these paths mean?Could your upload some demo pictures to make these clearer?Hope for your kindly reply!@Yaoyi-Li

    opened by lfxx 2
  • GPU out of memory when testing on composite-1k

    GPU out of memory when testing on composite-1k

    Hi Yaoyi,

    Thank you for your remarkable work!

    I noticed in the README it says that 8GB is needed when testing on conposite-1k dataset. However, when I run tests with a GPU with 11GB memory (RTX2080Ti), the program raised an out of memory error. So I want to ask that do I need to resize the image first before running or change the 'scale' option in the toml file? Thank you!

    My testing dataset is generated by the Composition_code.py provided with the Adobe Image Matting dataset.

    opened by ntuLC 2
  • Improving the results on real world images

    Improving the results on real world images

    Thanks for this great work!

    Any suggestions on how we can improve the performance of the model on real world images? I have seen that almost all of the image matting models perform pretty well on the data which is present in the example images, but don't give good results on actual real world data.

    opened by fhdbbk 0
  • AIM dataset

    AIM dataset

    AIM testset, one fg has mutiple trimaps, and this is the reason I can't load test dataloader because your code would find alpha, fg, trimaps with the same name, how could I fix it instead of renaming trimaps?

    opened by Dreamer312 0
  • about training data

    about training data

    Thangs for your great job!

    I have a question.I see that ‘’gca-dist-all-data: Model of the GCA Matting trained on both Adobe Image Matting Dataset and the Composition-1K testing set for alphamatting.com online benchmark. Save to ./checkpoints/gca-dist-all-data/.''

    So you trained the model on the Composition-1K testing set rather than training set? Where is the ground truth of testing set?

    opened by volcanoxs 0
  • about the config

    about the config

    hi,dear In the config/gca-dist.toml,how to set the params down if i use the dataset from the link?

    train_fg = "/home/liyaoyi/dataset/Adobe/train/fg"
    train_alpha = "/home/liyaoyi/dataset/Adobe/train/alpha"
    train_bg = "/home/liyaoyi/dataset/coco_bg"
    test_merged = "/home/liyaoyi/dataset/Adobe/Combined_Dataset/Test_set/merged"
    test_alpha = "/home/liyaoyi/dataset/Adobe/Combined_Dataset/Test_set/alpha_copy"
    test_trimap = "/home/liyaoyi/dataset/Adobe/Combined_Dataset/Test_set/trimaps"
    

    if I use the dataset ,how to modify the config?

    image

    thx

    opened by ucasiggcas 1
  • another encoder

    another encoder

    It is possible to use another encoder (with resnet101 base for example) with gca matting network? How to pretrain (in Imagenet) another encoder that would be suitable for your model?

    opened by andriyrizhiy 0
Owner
Li Yaoyi
Li Yaoyi
Github project for Attention-guided Temporal Coherent Video Object Matting.

Attention-guided Temporal Coherent Video Object Matting This is the Github project for our paper Attention-guided Temporal Coherent Video Object Matti

null 71 Dec 19, 2022
[IJCAI'21] Deep Automatic Natural Image Matting

Deep Automatic Natural Image Matting [IJCAI-21] This is the official repository of the paper Deep Automatic Natural Image Matting. Introduction | Netw

Jizhizi_Li 316 Jan 6, 2023
Official implementation of NeurIPS 2021 paper "Contextual Similarity Aggregation with Self-attention for Visual Re-ranking"

CSA: Contextual Similarity Aggregation with Self-attention for Visual Re-ranking PyTorch training code for CSA (Contextual Similarity Aggregation). We

Hui Wu 19 Oct 21, 2022
Code for 'Self-Guided and Cross-Guided Learning for Few-shot segmentation. (CVPR' 2021)'

SCL Introduction Code for 'Self-Guided and Cross-Guided Learning for Few-shot segmentation. (CVPR' 2021)' We evaluated our approach using two baseline

null 34 Oct 8, 2022
Official Pytorch implementation for Deep Contextual Video Compression, NeurIPS 2021

Introduction Official Pytorch implementation for Deep Contextual Video Compression, NeurIPS 2021 Prerequisites Python 3.8 and conda, get Conda CUDA 11

null 51 Dec 3, 2022
Repo for CVPR2021 paper "QPIC: Query-Based Pairwise Human-Object Interaction Detection with Image-Wide Contextual Information"

QPIC: Query-Based Pairwise Human-Object Interaction Detection with Image-Wide Contextual Information by Masato Tamura, Hiroki Ohashi, and Tomoaki Yosh

null 105 Dec 23, 2022
ICCV2021 - Mining Contextual Information Beyond Image for Semantic Segmentation

Introduction The official repository for "Mining Contextual Information Beyond Image for Semantic Segmentation". Our full code has been merged into ss

null 55 Nov 9, 2022
I-SECRET: Importance-guided fundus image enhancement via semi-supervised contrastive constraining

I-SECRET This is the implementation of the MICCAI 2021 Paper "I-SECRET: Importance-guided fundus image enhancement via semi-supervised contrastive con

null 13 Dec 2, 2022
Official codebase for running the small, filtered-data GLIDE model from GLIDE: Towards Photorealistic Image Generation and Editing with Text-Guided Diffusion Models.

GLIDE This is the official codebase for running the small, filtered-data GLIDE model from GLIDE: Towards Photorealistic Image Generation and Editing w

OpenAI 2.9k Jan 4, 2023
Sound-guided Semantic Image Manipulation - Official Pytorch Code (CVPR 2022)

?? Sound-guided Semantic Image Manipulation (CVPR2022) Official Pytorch Implementation Sound-guided Semantic Image Manipulation IEEE/CVF Conference on

CVLAB 58 Dec 28, 2022
PyMatting: A Python Library for Alpha Matting

Given an input image and a hand-drawn trimap (top row), alpha matting estimates the alpha channel of a foreground object which can then be composed onto a different background (bottom row).

PyMatting 1.4k Dec 30, 2022
Robust Video Matting in PyTorch, TensorFlow, TensorFlow.js, ONNX, CoreML!

Robust Video Matting in PyTorch, TensorFlow, TensorFlow.js, ONNX, CoreML!

Peter Lin 6.5k Jan 4, 2023
Robust Video Matting in PyTorch, TensorFlow, TensorFlow.js, ONNX, CoreML!

Robust Video Matting (RVM) English | 中文 Official repository for the paper Robust High-Resolution Video Matting with Temporal Guidance. RVM is specific

flow-dev 2 Aug 21, 2022
MODNet: Trimap-Free Portrait Matting in Real Time

MODNet is a model for real-time portrait matting with only RGB image input.

Zhanghan Ke 2.8k Dec 30, 2022
Real-Time High-Resolution Background Matting

Real-Time High-Resolution Background Matting Official repository for the paper Real-Time High-Resolution Background Matting. Our model requires captur

Peter Lin 6.1k Jan 3, 2023
Video Matting Refinement For Python

Video-matting refinement Library (use pip to install) scikit-image numpy av matplotlib Run Static background python path_to_video.mp4 Moving backgroun

null 3 Jan 11, 2022
Rethinking Portrait Matting with Privacy Preserving

Rethinking Portrait Matting with Privacy Preserving This is the official repository of the paper Rethinking Portrait Matting with Privacy Preserving.

null 184 Jan 3, 2023
Official Pytorch Implementation of Relational Self-Attention: What's Missing in Attention for Video Understanding

Relational Self-Attention: What's Missing in Attention for Video Understanding This repository is the official implementation of "Relational Self-Atte

mandos 43 Dec 7, 2022