Official implementation of the paper: Unifying Nonlocal Blocks for Neural Networks (ICCV'21)
Our work provide a novel perspective for the model design of non-local blocks called the Spectral View of Non-local. In this view, the non-local block can be seen as operating a set of graph filters on a fully connected weighted graph. Our spectral view can help to therorotivally anaylize exsiting non-local blocks and design novel non-local block with the help of graph signal processing (e.g. the graph neural networks).
This repository gives the implementation of Spectral Nonlocal Block (SNL) that is theoreotically designed with the help of first-order chebyshev graph convolution. The structure of the SNL is given below:
Two main differences between SNL and exisiting nonlocals, which make SNL can concern the graph spectral:
- The SNL using a symmetrical affinity matrix to ensure that the graph laplacian of the fully connected weighted graph is diagonalizable.
- The SNL using the normalized laplacian to conform the upper bound of maximum eigenvalue (equal to 2) for arbitrary graph structure.
More novel nonlocal blocks defined with other type graph filters will release soon, for example Cheby Filter, Amma Filter, and the Cayley Filter.
PyTorch >= 0.4.1
Python >= 3.5
torchvision >= 0.2.1
termcolor >= 1.1.0
tensorboardX >= 1.9
opencv >= 3.4
To train the SNL:
- install the conda environment using "env.yml"
- Setting --data_dir as the root directory of the dataset in "train_snl.sh"
- Setting --dataset as the train/val dataset (cifar10/cifar100/imagenet)
- Setting --backbone as the backbone type (we suggest using preresnet for CIFAR and resnet for ImageNet)
- Setting --arch as the backbone deepth (we suggest using 20/56 for preresnet and 50 for resnet)
- Other parameter such as learning rate, batch size can be found/set in "train_val.py"
- run the code by: "sh train_snl.sh"
- the training log and checkpoint are saving in "save_model"
We also give the module/config implementated for semantic segmentation based on mmsegmentation framework, one can regist our SNL block and train our SNL for semantic segmentation (Cityscape) followed their steps:
- preparing environment of mmsegmentation framework
- Putting ''snl_head.py'' into ''mmsegmentation/mmseg/models/decode_heads''
- registering our ''SNLHead'' by editing ''mmsegmentation/mmseg/models/decode_heads/__init__.py'':
addingfrom .snl_head import SNLHead
adding ''SNLHead'' at the end of the list ''__all__.py'' - putting ''configs/snl'' into ``mmsegmentation/configs''
- putting ''configs/base/models/snl_r50-d8.py'' into ''mmsegmentation/configs/models''
- run:
python tools/train_flops.py configs/snl/snl_r50-d8_512x1024_40k_cityscapes.py --work-dir result_snl
- the training log and checkpoint are saving in "result_snl"
Note that our experiments is done with only a Nvidia 3090 GPU, if user want to use multi-gpu for training, please refer to mmsegmentation framework.
@InProceedings{Lei_2021_ICCV,
title = {Unifying Nonlocal Blocks for Neural Networks},
author = {Zhu, Lei and She, Qi and Li, Duo and Lu, Yanye and Kang, Xuejing and Hu, Jie and Wang, Changhu},
booktitle = {IEEE International Conference on Computer Vision (ICCV)},
month = {October},
year = {2021}
}
This code and our experiments are conducted based on the release code of CGNL / mmsegmentation framework / 3D-ResNet framework. Here we thank for their remarkable works.