Home

Awesome

Towards Robust Classification Model by Counterfactual and Invaraint Generation (CVPR 2021)

This repository is the official implementation of our CVPR 2021 paper.

<img src="./images/fig1.png" width=600px>

Requirements

Code packages

We use the following packages:

To install requirements:

pip install -r requirements.txt

📋 Download datasets

We provide 3 shell scripts under datasets/ that download the datasets. We also provide our counterfactual inpainting images imputed by CAGAN. To run:

cd datasets/
bash fetch_in9.sh
bash fetch_wb.sh
bash fetch_cct.sh

Training

Arguments:

For example, if we want to run a CF(CAGAN) + F(Shuffle) + Sal(lambda=10) in Waterbird. We can run:

dataset='wb'
seed='5'
inpaint='cagan'
f_inpaint='shuffle'
reg='bbox_o'
reg_coeff='10'
python3 -u train_pl.py --name 0219_${dataset}_${inpaint}_f${f_inpaint}_${reg}_reg${reg_coeff}_s${seed} --dataset ${dataset} --fp16 1 --seed ${seed} --inpaint ${inpaint} --f_inpaint ${f_inpaint} --reg ${reg} --reg_coeff ${reg_coeff}

If encounter error, increase the batch_split or turn off fp16. The models will be stored in lightning_logs/$name/. And the results including test/val error are stored in results/WaterbirdLightningModel_results.tsv. Use notebook and pandas to read it.

import pandas as pd
pd.read_csv('results/WaterbirdLightningModel_results.tsv', delimiter='\t')

Other experiments

We tried several datasets that we find neither our method nor our Sal baseline improve, but just list them here for references:

  1. ImageNet-A dataset: in this paper they show some of their examples have some background confounding, but in our experiments all methods perform the same.
  2. ObjectNet: similar to ImageNet-A but does not have clear background spuriousness.
  3. Xray with different view points: it is shown in recent ICLR paper that Sal can improve on this Xray datasets when different view points are correlated with labels. We do not find any improvement for all baselines and our methods.

Citation

If you find this code helpful, please cite

@article{chang2021towards,
  title={Towards Robust Classification Model by Counterfactual and Invariant Data Generation},
  author={Chang, Chun-Hao and Adam, George Alexandru and Goldenberg, Anna},
  journal={arXiv preprint arXiv:2106.01127},
  year={2021}
}

Contributing

All content in this repository is licensed under the Apache license.