wutianyiRosun / OCNet

OCNet achieves the state-of-the-art scene parsing performance on both Cityscapes and ADE20K.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

OCNet: Object Context Network for Scene Parsing (pytorch)

!!!Welcome to share higher reproduced performance than the numbers reported in the paper.

Overall Framework of OCNet

Please check the paper OCNet here.

You are welcome to share our work with your friends. zhihu share

Please consider citing our work if you find it helps you,

  title={OCNet: Object Context Network for Scene Parsing},
  author={Yuhui Yuan, Jingdong Wang},
  journal={arXiv preprint arXiv:1809.00916},


Context is essential for various computer vision tasks. The state-of-the-art scene parsing methods have exploited the effectiveness of the context defined over image-level. Such context carries the mixture of objects belonging to different categories.

According to that the label of each pixel is defined as the category of the object it belongs to, we propose the Object Context that considers the objects belonging to the same category. The representation of any pixel P's object context is the aggregation of all the pixels' features that belong to the same category with P. Since it is impractical to estimate all the objects belonging to the same category in advance, we employ the self-attention method to approximate the objects by learning a pixel-wise similarity map.

We further propose the Pyramid Object Context and Atrous Spatial Pyramid Object Context to capture context of multiple scales. Based on the object context, we introduce the OCNet and show that OCNet achieves state-of-the-art performance on both Cityscapes benchmark and ADE20K benchmark.

Visualization of the learned Object Context

Object Context learned with OCNet

Experiment Results

All of our implementation is based on pytorch, OCNet can achieve competitive performance on various benchmarks such as Cityscapes and ADE20K without any bells and whistles.

The current performance on the Cityscapes test set of OCNet trained with only the fine-labeled set,

Method Conference Backbone mIoU(%)
RefineNet CVPR2017 ResNet-101 73.6
SAC ICCV2017 ResNet-101 78.1
PSPNet CVPR2017 ResNet-101 78.4
DUC-HDC WACV2018 ResNet-101 77.6
AAF ECCV2018 ResNet-101 77.1
BiSeNet ECCV2018 ResNet-101 78.9
PSANet ECCV2018 ResNet-101 80.1
DFN CVPR2018 ResNet-101 79.3
DSSPN CVPR2018 ResNet-101 77.8
DenseASPP CVPR2018 DenseNet-161 80.6
OCNet - ResNet-101 81.2

The current performance on the ADE20K validation set of the OCNet

Method Conference Backbone mIoU(%)
RefineNet CVPR2017 ResNet-152 40.70
PSPNet CVPR2017 ResNet-101 43.29
SAC ICCV2017 ResNet-101 44.30
PSANet ECCV2018 ResNet-101 43.77
EncNet CVPR2018 ResNet-101 44.65
OCNet - ResNet-101 45.08


The code is developed using python 3.5+ on Ubuntu 16.04. NVIDIA GPUs ared needed. The code is tested using 4 x NVIDIA P100 GPUS cards. All the experiments on Cityscapes are tested on pytorch0.4.1.

Quick start



Just install the offical pytorch with pip install torch torchvision is OK.

We mainly test all of our results based on the official pytorch 0.4.1.

Train/Validate/Test the OCNet (Cityscapes)

We implement training, validating, testing in one script for convenience. You can achieve all the results by runing this script.

sh run_asp_oc.sh

You are expected to achieve ~79.5 mIoU on the validation set/ ~78.3 mIoU on the testing set (single scale).

You are also recommended to try the run_base_oc.sh, which can also achieve ~79.3 mIoU on the validation set / ~78.1 mIoU on the testing set (single scale).

To further improve the performance, you can employ the CriterionOhemDSN_single by setting that


then you could expect to achieve ~80.4 mIoU on the validation set/ ~79.0 mIoU on the testing set (single scale).

To achieve the 81.2 on the testing set, you need to train the model with both the training set and validation set for 80,000 iterations first(achieve ~80.5 on the testing set), then you need to finetune this model for 100,000 iterations with fixed learning rate(1e-4). We adopt the online hard example mining accordingly.

Data preparation

For the cityscapes dataset, please download the dataset from the Cityscapes webset. Unzip all the images under the path "./OCNet/dataset/cityscapes". Ensure the path tree like below within the folder "./OCNet/dataset/cityscapes".

|-- get_cs_extra.sh
|-- gtCoarse
|   |-- README
|   |-- license.txt
|   |-- train
|   |-- train_extra
|   `-- val
|-- gtFine
|   |-- README
|   |-- license.txt
|   |-- test
|   |-- train
|   `-- val
|-- leftImg8bit
|   |-- README
|   |-- license.txt
|   |-- test
|   |-- train
|   |-- train_extra
|   `-- val
|-- license.txt
`-- tree.txt

Pretrained Models

Please put the pretrained models under the folder "./OCNet/pretrained_model"

ImageNet Pretrained ResNet-101

Docker with the enviroments


Other problems (Performance gap between the Validation set and Testing set)

We find that the mIoU of the class train is unstable sometimes. For example, we run our code for 5 times, there can exist one time the mIoU for class train is 0.42 while we can get 0.75 for other 4 times.

There also exist some problems about the validation/testing set accuracy gap. For example, if you run the base-oc method for two times, you can achieve 79.3 and 79.8 mIou on the validation set separately while the testing mIou can be 78.55 and 77.69. Thus I recommend to you to run our methods for multiple times if you want to achieve good performance on the testing set while our method performs pretty robust on the validation set as the reason of the distribution gaps between the training/validation set and the testing set.

Thanks to the Third Party Libs






OCNet achieves the state-of-the-art scene parsing performance on both Cityscapes and ADE20K.


Language:Python 75.8%Language:Cuda 9.3%Language:C++ 7.6%Language:Shell 6.2%Language:C 1.1%