Home

Awesome


<div align="center">

Refign: Align and Refine for Adaptation of Semantic Segmentation to Adverse Conditions

Paper Conference

PWC

PWC

</div>

This repository provides the official code for the WACV 2023 paper Refign: Align and Refine for Adaptation of Semantic Segmentation to Adverse Conditions. The code is organized using PyTorch Lightning.

πŸ”₯ [September 2, 2022] Applied on top of HRDA, Refign ranks #1 on both the ACDC leaderboardβ€”72.05 mIoUβ€”and the Dark Zurich leaderboardβ€”63.91 mIoU. See below for training configurations.

<img src="./docs/method.png" width="900"/>

Abstract

Due to the scarcity of dense pixel-level semantic annotations for images recorded in adverse visual conditions, there has been a keen interest in unsupervised domain adaptation (UDA) for the semantic segmentation of such images. UDA adapts models trained on normal conditions to the target adverse-condition domains. Meanwhile, multiple datasets with driving scenes provide corresponding images of the same scenes across multiple conditions, which can serve as a form of weak supervision for domain adaptation. We propose Refign, a generic extension to self-training-based UDA methods which leverages these cross-domain correspondences. Refign consists of two steps: (1) aligning the normal-condition image to the corresponding adverse-condition image using an uncertainty-aware dense matching network, and (2) refining the adverse prediction with the normal prediction using an adaptive label correction mechanism. We design custom modules to streamline both steps and set the new state of the art for domain-adaptive semantic segmentation on several adverse-condition benchmarks, including ACDC and Dark Zurich. The approach introduces no extra training parameters, minimal computational overheadβ€”during training onlyβ€”and can be used as a drop-in extension to improve any given self-training-based UDA method.

Usage

Requirements

The code is run with Python 3.8.13. To install the packages, use:

pip install -r requirements.txt

Set Data Directory

The following environment variable must be set:

export DATA_DIR=/path/to/data/dir

Download the Data

Before running the code, download and extract the corresponding datasets to the directory $DATA_DIR.

UDA

<details> <summary>Cityscapes</summary>

Download leftImg8bit_trainvaltest.zip and gt_trainvaltest.zip from here and extract them to $DATA_DIR/Cityscapes.

$DATA_DIR
β”œβ”€β”€ Cityscapes
β”‚   β”œβ”€β”€ leftImg8bit
β”‚   β”‚   β”œβ”€β”€ train
β”‚   β”‚   β”œβ”€β”€ val
β”‚   β”œβ”€β”€ gtFine
β”‚   β”‚   β”œβ”€β”€ train
β”‚   β”‚   β”œβ”€β”€ val
β”œβ”€β”€ ...

Afterwards, run the preparation script:

python tools/convert_cityscapes.py $DATA_DIR/Cityscapes
</details> <details> <summary>ACDC</summary>

Download rgb_anon_trainvaltest.zip and gt_trainval.zip from here and extract them to $DATA_DIR/ACDC.

$DATA_DIR
β”œβ”€β”€ ACDC
β”‚   β”œβ”€β”€ rgb_anon
β”‚   β”‚   β”œβ”€β”€ fog
β”‚   β”‚   β”œβ”€β”€ night
β”‚   β”‚   β”œβ”€β”€ rain
β”‚   β”‚   β”œβ”€β”€ snow
β”‚   β”œβ”€β”€ gt
β”‚   β”‚   β”œβ”€β”€ fog
β”‚   β”‚   β”œβ”€β”€ night
β”‚   β”‚   β”œβ”€β”€ rain
β”‚   β”‚   β”œβ”€β”€ snow
β”œβ”€β”€ ...
</details> <details> <summary>Dark Zurich</summary>

Download Dark_Zurich_train_anon.zip, Dark_Zurich_val_anon.zip, and Dark_Zurich_test_anon_withoutGt.zip from here and extract them to $DATA_DIR/DarkZurich.

$DATA_DIR
β”œβ”€β”€ DarkZurich
β”‚   β”œβ”€β”€ rgb_anon
β”‚   β”‚   β”œβ”€β”€ train
β”‚   β”‚   β”œβ”€β”€ val
β”‚   β”‚   β”œβ”€β”€ val_ref
β”‚   β”‚   β”œβ”€β”€ test
β”‚   β”‚   β”œβ”€β”€ test_ref
β”‚   β”œβ”€β”€ gt
β”‚   β”‚   β”œβ”€β”€ val
β”œβ”€β”€ ...
</details> <details> <summary>Nighttime Driving</summary>

Download NighttimeDrivingTest.zip from here and extract it to $DATA_DIR/NighttimeDrivingTest.

$DATA_DIR
β”œβ”€β”€ NighttimeDrivingTest
β”‚   β”œβ”€β”€ leftImg8bit
β”‚   β”‚   β”œβ”€β”€ test
β”‚   β”œβ”€β”€ gtCoarse_daytime_trainvaltest
β”‚   β”‚   β”œβ”€β”€ test
β”œβ”€β”€ ...
</details> <details> <summary>BDD100k-night</summary>

Download 10k Images and Segmentation from here and extract them to $DATA_DIR/bdd100k.

$DATA_DIR
β”œβ”€β”€ bdd100k
β”‚   β”œβ”€β”€ images
β”‚   β”‚   β”œβ”€β”€ 10k
β”‚   β”œβ”€β”€ labels
β”‚   β”‚   β”œβ”€β”€ sem_seg
β”œβ”€β”€ ...
</details> <details> <summary>RobotCar for Segmentation</summary>

Download all data from here and save them to $DATA_DIR/RobotCar. As mentioned in the corresponding README.txt, the images must be downloaded from this link.

$DATA_DIR
β”œβ”€β”€ RobotCar
β”‚   β”œβ”€β”€ images
β”‚   β”‚   β”œβ”€β”€ dawn
β”‚   β”‚   β”œβ”€β”€ dusk
β”‚   β”‚   β”œβ”€β”€ night
β”‚   β”‚   β”œβ”€β”€ night-rain
β”‚   β”‚   β”œβ”€β”€ ...
β”‚   β”œβ”€β”€ correspondence_data
β”‚   β”‚   β”œβ”€β”€ ...
β”‚   β”œβ”€β”€ segmented_images
β”‚   β”‚   β”œβ”€β”€ training
β”‚   β”‚   β”œβ”€β”€ validation
β”‚   β”‚   β”œβ”€β”€ testing
β”œβ”€β”€ ...
</details>

Alignment

<details> <summary>MegaDepth</summary>

For training, we use the version provided by the D2-Net repo. Follow their instructions for downloading and preprocessing the dataset.

For testing, we use the split provided by RANSAC-Flow here. The directories MegaDepth_Train, MegaDepth_Train_Org, and Val can be removed.

All in all, the folder structure should look as follows:

$DATA_DIR
β”œβ”€β”€ MegaDepth
β”‚   β”œβ”€β”€ Undistorted_SfM
β”‚   β”‚   β”œβ”€β”€ ...
β”‚   β”œβ”€β”€ scene_info
β”‚   β”‚   β”œβ”€β”€ ...
β”‚   β”œβ”€β”€ Test
β”‚   β”‚   β”œβ”€β”€ test1600Pairs
β”‚   β”‚   |   β”œβ”€β”€ ...
β”‚   β”‚   β”œβ”€β”€ test1600Pairs.csv
β”œβ”€β”€ ...
</details> <details> <summary>RobotCar for Matching</summary>

We use the correspondence file provided by RANSAC-Flow here. If not already downloaded for segmentation, download the images from here.

$DATA_DIR
β”œβ”€β”€ RobotCar
β”‚   β”œβ”€β”€ images
β”‚   β”‚   β”œβ”€β”€ dawn
β”‚   β”‚   β”œβ”€β”€ dusk
β”‚   β”‚   β”œβ”€β”€ night
β”‚   β”‚   β”œβ”€β”€ night-rain
β”‚   β”‚   β”œβ”€β”€ ...
β”‚   β”œβ”€β”€ test6511.csv
β”œβ”€β”€ ...
</details>

Download the Pretrained Weights

The following pretrained weights are required for Refign. Save them to ./pretrained_models/.

  1. UAWarpC checkpoint, download it here.

  2. ImageNet-pretrained MiT weights (mit_b5.pth), download them from the SegFormer repository.

  3. Cityscapes-pretrained SegFormer weights (segformer.b5.1024x1024.city.160k.pth), download them from the SegFormer repository.

Trained Models and Results

We provide trained models of both UDA and alignment networks. To facilitate qualitative segmentation comparisons, validation set predictions of Refign can be directly downloaded. Starred models use Cityscapes pretrained weights in the backbone, the others ImageNet pretrained.

UDA

ModelTaskTest SetTest ScoreConfigCheckpointPredictions
Refign-DAFormerCityscapes→ACDCACDC test65.5 mIoUconfigmodelACDC val
Refign-HRDA*Cityscapes→ACDCACDC test72.1 mIoUconfigmodelACDC val
Refign-DAFormerCityscapes→Dark ZurichDark Zurich test56.2 mIoUconfigmodelDark Zurich val
Refign-HRDA*Cityscapes→Dark ZurichDark Zurich test63.9 mIoUconfigmodelDark Zurich val
Refign-DAFormerCityscapes→RobotCarRobotCar Seg. test60.5 mIoUconfigmodelRobotCar val

Alignment

ModelTaskTest SetScoreConfigCheckpoint
UAWarpCMegaDepth Dense MatchingRobotCar Matching test36.8 PCK-5stage1, stage2model

Refign Training

Make sure to first download the necessary pretrained weights. To train Refign on ACDC (single GPU, with AMP) use the following command:

python tools/run.py fit --config configs/cityscapes_acdc/refign_hrda_star.yaml --trainer.gpus 1 --trainer.precision 16

Similar config files are available for Dark Zurich and RobotCar. We also provide the config files for reproducing the ablation study in configs/cityscapes_acdc/ablations/.

Refign Testing

Make sure to first download the necessary pretrained weights. To evaluate Refign e.g. on the ACDC validation set, use the following command:

python tools/run.py test --config configs/cityscapes_acdc/refign_hrda_star.yaml --ckpt_path /path/to/trained/model --trainer.gpus 1

We also provide trained models, which can be downloaded from the link above. To evaluate them, simply provide them as the argument --ckpt_path.

To get test set scores for ACDC and DarkZurich, predictions are evaluated on the respective evaluation servers: ACDC and DarkZurich. To create and save test predictions for e.g. ACDC, use this command:

python tools/run.py predict --config configs/cityscapes_acdc/refign_hrda_star.yaml --ckpt_path /path/to/trained/model --trainer.gpus 1

UAWarpC Training

Alignment training consists of two stages. To train stage 1 use:

python tools/run.py fit --config configs/megadepth/uawarpc_stage1.yaml --trainer.gpus 1 --trainer.precision 16

Afterwards, launch stage 2, providing the path of the last checkpoint of stage 1 as an argument:

python tools/run.py fit --config configs/megadepth/uawarpc_stage2.yaml --model.init_args.pretrained /path/to/last/ckpt/of/stage1 --trainer.gpus 1 --trainer.precision 16

Training of the alignment network takes several days on a single GPU.

UAWarpC Testing

We use a separate config file for evaluation, to avoid the lengthy sampling of MegaDepth training data at that stage. But of course, the config file used for training can be used as well.

python tools/run.py test --config configs/megadepth/uawarpc_evalonly.yaml --ckpt_path /path/to/last/ckpt/of/stage2 --trainer.gpus 1

We also provide a pretrained model, which can be downloaded from the link above. To test it, simply provide it as the argument --ckpt_path.

Local Correlation

Local correlation is implemented through this custom CUDA extension. By default the extension is built just in time. In case there are problems with this mechanism, the extension can be alternatively pre-installed in the environment (see also the README of the linked repo):

pip install spatial-correlation-sampler

How to Add Refign to your Self-Training UDA Code

Check the training_step method in models/segmentation_model.py. You will need to implement similar logic as is called when the use_refign attribute is True. In particular, you also need methods align and refine, located in the same file (and the full alignment network). Of course, the dataloader must also return a reference image for Refign to work.

Citation

If you find this code useful in your research, please consider citing the paper:

@inproceedings{bruggemann2022refign,
  title={Refign: Align and Refine for Adaptation of Semantic Segmentation to Adverse Conditions},
  author={Bruggemann, David and Sakaridis, Christos and Truong, Prune and Van Gool, Luc},
  booktitle={WACV},
  year={2023}
}

License

This repository is released under the MIT license. However, care should be taken to adopt appropriate licensing for third-party code in this repository. Third-party code is marked accordingly.