Package: mit-semseg
Semantic Segmentation on MIT ADE20K dataset in PyTorchThis is a PyTorch implementation of semantic segmentation models on MIT ADE20K scene parsing dataset (http://sceneparsing.csail.mit.edu/).ADE20K is the largest open source dataset for semantic segmentation and scene parsing, released by MIT Computer Vision team. Follow the link below to find the repository for our dataset and implementations on Caffe and Torch7:https://github.com/CSAILVision/sceneparsingIf you simply want to play with our demo, please try this link:http://scenesegmentation.csail.mit.eduYou can upload your own photo and parse it!You can also use this colab notebook playground hereto tinker with the code for segmenting an image.All pretrained models can be found at:http://sceneparsing.csail.mit.edu/model/pytorch[From left to right: Test Image, Ground Truth, Predicted Result]Color encoding of semantic categories can be found here:https://docs.google.com/spreadsheets/d/1se8YEtb2detS7OuPE86fXGyD269pMycAWe2mtKUj2W8/edit?usp=sharingUpdatesHRNet model is now supported.We use configuration files to store most options which were in argument parser. The definitions of options are detailed inconfig/defaults.py.We conform to Pytorch practice in data preprocessing (RGB [0, 1], substract mean, divide std).HighlightsSyncronized Batch Normalization on PyTorchThis module computes the mean and standard-deviation across all devices during training. We empirically find that a reasonable large batch size is important for segmentation. We thankJiayuan Maofor his kind contributions, please refer toSynchronized-BatchNorm-PyTorchfor details.The implementation is easy to use as:It is pure-python, no C++ extra extension libs.It is completely compatible with PyTorch's implementation. Specifically, it uses unbiased variance to update the moving average, and use sqrt(max(var, eps)) instead of sqrt(var + eps).It is efficient, only 20% to 30% slower than UnsyncBN.Dynamic scales of input for training with multiple GPUsFor the task of semantic segmentation, it is good to keep aspect ratio of images during training. So we re-implement theDataParallelmodule, and make it support distributing data to multiple GPUs in python dict, so that each gpu can process images of different sizes. At the same time, the dataloader also operates differently.Now the batch size of a dataloader always equals to the number of GPUs, each element will be sent to a GPU. It is also compatible with multi-processing. Note that the file index for the multi-processing dataloader is stored on the master process, which is in contradict to our goal that each worker maintains its own file list. So we use a trick that although the master process still gives dataloader an index for__getitem__function, we just ignore such request and send a random batch dict. Also,the multiple workers forked by the dataloader all have the same seed, you will find that multiple workers will yield exactly the same data, if we use the above-mentioned trick directly. Therefore, we add one line of code which sets the defaut seed fornumpy.randombefore activating multiple worker in dataloader.State-of-the-Art modelsPSPNetis scene parsing network that aggregates global representation with Pyramid Pooling Module (PPM). It is the winner model of ILSVRC'16 MIT Scene Parsing Challenge. Please refer tohttps://arxiv.org/abs/1612.01105for details.UPerNetis a model based on Feature Pyramid Network (FPN) and Pyramid Pooling Module (PPM). It doesn't need dilated convolution, an operator that is time-and-memory consuming.Without bells and whistles, it is comparable or even better compared with PSPNet, while requiring much shorter training time and less GPU memory. Please refer tohttps://arxiv.org/abs/1807.10221for details.HRNetis a recently proposed model that retains high resolution representations throughout the model, without the traditional bottleneck design. It achieves the SOTA performance on a series of pixel labeling tasks. Please refer tohttps://arxiv.org/abs/1904.04514for details.Supported modelsWe split our models into encoder and decoder, where encoders are usually modified directly from classification networks, and decoders consist of final convolutions and upsampling. We have provided some pre-configured models in theconfigfolder.Encoder:MobileNetV2dilatedResNet18/ResNet18dilatedResNet50/ResNet50dilatedResNet101/ResNet101dilatedHRNetV2 (W48)Decoder:C1 (one convolution module)C1_deepsup (C1 + deep supervision trick)PPM (Pyramid Pooling Module, seePSPNetpaper for details.)PPM_deepsup (PPM + deep supervision trick)UPerNet (Pyramid Pooling + FPN head, seeUperNetfor details.)Performance:IMPORTANT: The base ResNet in our repository is a customized (different from the one in torchvision). The base models will be automatically downloaded when needed.ArchitectureMultiScale TestingMean IoUPixel Accuracy(%)Overall ScoreInference Speed(fps)MobileNetV2dilated + C1_deepsupNo34.8475.7554.0717.2Yes33.8476.8055.3210.3MobileNetV2dilated + PPM_deepsupNo35.7677.7756.2714.9Yes36.2878.2657.276.7ResNet18dilated + C1_deepsupNo33.8276.0554.9413.9Yes35.3477.4156.385.8ResNet18dilated + PPM_deepsupNo38.0078.6458.3211.7Yes38.8179.2959.054.2ResNet50dilated + PPM_deepsupNo41.2679.7360.508.3Yes42.1480.1361.142.6ResNet101dilated + PPM_deepsupNo42.1980.5961.396.8Yes42.5380.9161.722.0UperNet50No40.4479.8060.128.4Yes41.5580.2360.892.9UperNet101No42.0080.7961.407.8Yes42.6681.0161.842.3HRNetV2No42.0380.7761.405.8Yes43.2081.4762.341.9The training is benchmarked on a server with 8 NVIDIA Pascal Titan Xp GPUs (12GB GPU memory), the inference speed is benchmarked a single NVIDIA Pascal Titan Xp GPU, without visualization.EnvironmentThe code is developed under the following configurations.Hardware: >=4 GPUs for training, >=1 GPU for testing (set[--gpus GPUS]accordingly)Software: Ubuntu 16.04.3 LTS,CUDA>=8.0, Python>=3.5, PyTorch>=0.4.0Dependencies: numpy, scipy, opencv, yacs, tqdmQuick start: Test on an image using our trained modelHere is a simple demo to do inference on a single image:chmod+xdemo_test.sh
./demo_test.shThis script downloads a trained model (ResNet50dilated + PPM_deepsup) and a test image, runs the test script, and saves predicted segmentation (.png) to the working directory.To test on an image or a folder of images ($PATH_IMG), you can simply do the following:python3 -u test.py --imgs $PATH_IMG --gpu $GPU --cfg $CFGTrainingDownload the ADE20K scene parsing dataset:chmod+xdownload_ADE20K.sh
./download_ADE20K.shTrain a model by selecting the GPUs ($GPUS) and configuration file ($CFG) to use. During training, checkpoints by default are saved in folderckpt.python3train.py--gpus$GPUS--cfg$CFGTo choose which gpus to use, you can either do--gpus 0-7, or--gpus 0,2,4,6.For example, you can start with our provided configurations:Train MobileNetV2dilated + C1_deepsuppython3train.py--gpusGPUS--cfgconfig/ade20k-mobilenetv2dilated-c1_deepsup.yamlTrain ResNet50dilated + PPM_deepsuppython3train.py--gpusGPUS--cfgconfig/ade20k-resnet50dilated-ppm_deepsup.yamlTrain UPerNet101python3train.py--gpusGPUS--cfgconfig/ade20k-resnet101-upernet.yamlYou can also override options in commandline, for examplepython3 train.py TRAIN.num_epoch 10.EvaluationEvaluate a trained model on the validation set. AddVAL.visualize Truein argument to output visualizations as shown in teaser.For example:Evaluate MobileNetV2dilated + C1_deepsuppython3eval_multipro.py--gpusGPUS--cfgconfig/ade20k-mobilenetv2dilated-c1_deepsup.yamlEvaluate ResNet50dilated + PPM_deepsuppython3eval_multipro.py--gpusGPUS--cfgconfig/ade20k-resnet50dilated-ppm_deepsup.yamlEvaluate UPerNet101python3eval_multipro.py--gpusGPUS--cfgconfig/ade20k-resnet101-upernet.yamlIntegration with other projectsThis library can be installed viapipto easily integrate with another codebasepipinstallgit+https://github.com/CSAILVision/semantic-segmentation-pytorch.git@masterNow this library can easily be consumed programmatically. For examplefrommit_semseg.configimportcfgfrommit_semseg.datasetimportTestDatasetfrommit_semseg.modelsimportModelBuilder,SegmentationModuleReferenceIf you find the code or pre-trained models useful, please cite the following papers:Semantic Understanding of Scenes through ADE20K Dataset. B. Zhou, H. Zhao, X. Puig, T. Xiao, S. Fidler, A. Barriuso and A. Torralba. International Journal on Computer Vision (IJCV), 2018. (https://arxiv.org/pdf/1608.05442.pdf)@article{zhou2018semantic,
  title={Semantic understanding of scenes through the ade20k dataset},
  author={Zhou, Bolei and Zhao, Hang and Puig, Xavier and Xiao, Tete and Fidler, Sanja and Barriuso, Adela and Torralba, Antonio},
  journal={International Journal on Computer Vision},
  year={2018}
}Scene Parsing through ADE20K Dataset. B. Zhou, H. Zhao, X. Puig, S. Fidler, A. Barriuso and A. Torralba. Computer Vision and Pattern Recognition (CVPR), 2017. (http://people.csail.mit.edu/bzhou/publication/scene-parse-camera-ready.pdf)@inproceedings{zhou2017scene,
    title={Scene Parsing through ADE20K Dataset},
    author={Zhou, Bolei and Zhao, Hang and Puig, Xavier and Fidler, Sanja and Barriuso, Adela and Torralba, Antonio},
    booktitle={Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition},
    year={2017}
}