# SMD-Nets: Stereo Mixture Density Networks

**NEWS!** Quarter resolution (960×540) version of the UnrealStereo4K dataset is now available on [Hugging Face](https://huggingface.co/datasets/fabiotosi92/UnrealStereo4K)! Check below for more details.
This repository contains a Pytorch implementation of "SMD-Nets: Stereo Mixture Density Networks" (CVPR 2021) by [Fabio Tosi](https://vision.disi.unibo.it/~ftosi/), [Yiyi Liao](https://www.is.mpg.de/person/yliao), [Carolin Schmitt](https://avg.is.tuebingen.mpg.de/person/cschmitt) and [Andreas Geiger](http://www.cvlibs.net/)
**Contributions:**
* A novel learning framework for stereo matching that exploits compactly parameterized bimodal mixture densities as output representation and can be trained using a simple likelihood-based loss function. Our simple formulation lets us avoid bleeding artifacts at depth discontinuities and provides a measure for aleatoric uncertainty.
* A continuous function formulation aimed at estimating disparities at arbitrary spatial resolution with constant memory footprint.
* A new large-scale synthetic binocular stereo dataset with ground truth disparities at 3840×2160 resolution, comprising photo-realistic renderings of indoor and outdoor environments.
For more details, please check:
[[Paper]](http://www.cvlibs.net/publications/Tosi2021CVPR.pdf) [[Supplementary]](http://www.cvlibs.net/publications/Tosi2021CVPR_supplementary.pdf) [[Poster]](http://www.cvlibs.net/publications/Tosi2021CVPR_poster.pdf) [[Video]](https://www.youtube.com/watch?v=tvVGuUSe2n8&t=148s) [[Blog]](https://autonomousvision.github.io/smdnets/)
If you find this code useful in your research, please cite:
```shell
@INPROCEEDINGS{Tosi2021CVPR,
author = {Fabio Tosi and Yiyi Liao and Carolin Schmitt and Andreas Geiger},
title = {SMD-Nets: Stereo Mixture Density Networks},
booktitle = {Conference on Computer Vision and Pattern Recognition (CVPR)},
year = {2021}
}
```
## Requirements
This code was tested with Python 3.8, Pytotch 1.8, CUDA 11.2 and Ubuntu 20.04.
All our experiments were performed on a single NVIDIA Titan V100 GPU.
Requirements can be installed using the following script:
```shell
pip install -r requirements
```
## Datasets
We create our synthetic dataset, UnrealStereo4K, using the popular game engine [Unreal Engine](https://www.unrealengine.com/en-US/) combined with the open-source plugin [UnrealCV](https://unrealcv.org/).
### UnrealStereo4K
Our photo-realistic synthetic passive binocular UnrealStereo4K dataset consists of images of 8 static scenes, including indoor and outdoor environments. We rendered stereo pairs at **3840×2160** resolution for each scene with pixel-accurate **ground truth disparity maps** (aligned with both the left and the right images!) and **ground truth poses**.
You can automatically download the entire synthetic binocular stereo dataset using the ```download_data.sh``` script in the ```scripts``` folder. In alternative, you can download each scene individually:
[UnrealStereo4K_00000.zip [74 GB]](https://s3.eu-central-1.amazonaws.com/avg-projects/smd_nets/UnrealStereo4K_00000.zip)
[UnrealStereo4K_00001.zip [73 GB]](https://s3.eu-central-1.amazonaws.com/avg-projects/smd_nets/UnrealStereo4K_00001.zip)
[UnrealStereo4K_00002.zip [74 GB]](https://s3.eu-central-1.amazonaws.com/avg-projects/smd_nets/UnrealStereo4K_00002.zip)
[UnrealStereo4K_00003.zip [73 GB]](https://s3.eu-central-1.amazonaws.com/avg-projects/smd_nets/UnrealStereo4K_00003.zip)
[UnrealStereo4K_00004.zip [72 GB]](https://s3.eu-central-1.amazonaws.com/avg-projects/smd_nets/UnrealStereo4K_00004.zip)
[UnrealStereo4K_00005.zip [74 GB]](https://s3.eu-central-1.amazonaws.com/avg-projects/smd_nets/UnrealStereo4K_00005.zip)
[UnrealStereo4K_00006.zip [67 GB]](https://s3.eu-central-1.amazonaws.com/avg-projects/smd_nets/UnrealStereo4K_00006.zip)
[UnrealStereo4K_00007.zip [76 GB]](https://s3.eu-central-1.amazonaws.com/avg-projects/smd_nets/UnrealStereo4K_00007.zip)
[UnrealStereo4K_00008.zip [16 GB]](https://s3.eu-central-1.amazonaws.com/avg-projects/smd_nets/UnrealStereo4K_00008.zip) - It contains 200 stereo pairs only, used as out-of-domain test set
### UnrealStereo4K-Q (Quarter Resolution)
We now provide a quarter-resolution version of the dataset (960×540) to support applications that do not require the full 4K resolution. This version contains the same scenes but with reduced file sizes, making it more accessible for experimentation and development.
The quarter-resolution dataset is available on Hugging Face:
[https://huggingface.co/datasets/fabiotosi92/UnrealStereo4K](https://huggingface.co/datasets/fabiotosi92/UnrealStereo4K)
Key features of UnrealStereo4K-Q:
- Image resolution: 960×540 (1/4 of the original)
- High-quality JPG images instead of PNG
- NPY format disparity maps (scaled appropriately for the resolution)
- Same 9 scenes as the original dataset
This version maintains the core structure of the original dataset but with a focus on reduced storage requirements and faster loading times, making it ideal for prototyping and initial experiments.
**Warning!**: All the RGB images in the original dataset are PNG files at 8 MPx. This notably slows down the training process due to the expensive dataloading operation. Thus, we suggest compressing the images to raw binary files to speed up the process and trainings (Pay attention to edit the filenames accordingly). You can use the following code to convert (**offline**) the stereo images (Image0 and Image1 folders) to a ```raw``` format:
```shell
img_path=/path/to/the/image
out = open(img_path.replace("png", "raw"), 'wb')
img = cv2.imread(img_path, -1)
img = cv2.cvtColor(img, cv2.COLOR_RGBA2RGB)
img.tofile(out)
out.close()
```
## Training
All training and testing scripts are provided in the ```scripts``` folder.
As an example, use the following command to train SMD-Nets on our UnrealStereo4K dataset.
```shell
python apps/train.py --dataroot $dataroot \
--checkpoints_path $checkpoints_path \
--training_file $training_file \
--testing_file $testing_file \
--results_path $results_path \
--mode $mode \
--name $name \
--batch_size $batch_size \
--num_epoch $num_epoch \
--learning_rate $learning_rate \
--gamma $gamma \
--crop_height $crop_height \
--crop_width $crop_width \
--num_sample_inout $num_sample_inout \
--aspect_ratio $aspect_ratio \
--sampling $sampling \
--output_representation $output_representation \
--backbone $backbone
```
For a detailed description of training options, please take a look at ```lib/options.py```
In order to monitor and visualize the training process, you can start a tensorboard session with:
```shell
tensorboard --logdir checkpoints
```
## Evaluation
Use the following command to evaluate the trained SMD-Nets on our UnrealStereo4K dataset.
```shell
python apps/test.py --dataroot $dataroot \
--testing_file $testing_file \
--results_path $results_path \
--mode $mode \
--batch_size 1 \
--superes_factor $superes_factor \
--aspect_ratio $aspect_ratio \
--output_representation $output_representation \
--load_checkpoint_path $checkpoints_path \
--backbone $backbone
```
**Warning!** The soft edge error (SEE) on the KITTI dataset requires instance segmentation maps from the KITTI 2015 dataset.
**Stereo Ultra High-Resolution**: if you want to estimate a disparity map at arbitrary spatial resolution given a low resolution stereo pair at testing time, just use a different value for the ```superres_factor``` parameter (e.g. 2,4,8..32!). Below, a comparison of our model using the PSMNet backbone at 128Mpx resolution (top) and the original PSMNet at 0.5Mpx resolution (bottom), both taking stereo pairs at 0.5Mpx resolution as input.