All Projects → Serge-weihao → Ccnet Pure Pytorch

Serge-weihao / Ccnet Pure Pytorch

Licence: mit
Criss-Cross Attention for Semantic Segmentation in pure Pytorch with a faster and more precise implementation.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Ccnet Pure Pytorch

Machine Learning
My Attempt(s) In The World Of ML/DL....
Stars: ✭ 78 (-37.1%)
Mutual labels:  attention
Captcharecognition
End-to-end variable length Captcha recognition using CNN+RNN+Attention/CTC (pytorch implementation). 端到端的不定长验证码识别
Stars: ✭ 97 (-21.77%)
Mutual labels:  attention
Leader Line
Draw a leader line in your web page.
Stars: ✭ 1,872 (+1409.68%)
Mutual labels:  attention
Attend infer repeat
A Tensorfflow implementation of Attend, Infer, Repeat
Stars: ✭ 82 (-33.87%)
Mutual labels:  attention
Cnn lstm for text classify
CNN, LSTM, NBOW, fasttext 中文文本分类
Stars: ✭ 90 (-27.42%)
Mutual labels:  attention
Lambda Networks
Implementation of LambdaNetworks, a new approach to image recognition that reaches SOTA with less compute
Stars: ✭ 1,497 (+1107.26%)
Mutual labels:  attention
Nlp Tutorial
Natural Language Processing Tutorial for Deep Learning Researchers
Stars: ✭ 9,895 (+7879.84%)
Mutual labels:  attention
Deeplab V3 Plus Cityscapes
mIOU=80.02 on cityscapes. My implementation of deeplabv3+ (also know as 'Encoder-Decoder with Atrous Separable Convolution for Semantic Image Segmentation' based on the dataset of cityscapes).
Stars: ✭ 121 (-2.42%)
Mutual labels:  cityscapes
Njunmt Tf
An open-source neural machine translation system developed by Natural Language Processing Group, Nanjing University.
Stars: ✭ 97 (-21.77%)
Mutual labels:  attention
Bertqa Attention On Steroids
BertQA - Attention on Steroids
Stars: ✭ 112 (-9.68%)
Mutual labels:  attention
Self Attention Classification
document classification using LSTM + self attention
Stars: ✭ 84 (-32.26%)
Mutual labels:  attention
Nlp Journey
Documents, papers and codes related to Natural Language Processing, including Topic Model, Word Embedding, Named Entity Recognition, Text Classificatin, Text Generation, Text Similarity, Machine Translation),etc. All codes are implemented intensorflow 2.0.
Stars: ✭ 1,290 (+940.32%)
Mutual labels:  attention
Numpy Ml
Machine learning, in numpy
Stars: ✭ 11,100 (+8851.61%)
Mutual labels:  attention
Attention Transfer
Improving Convolutional Networks via Attention Transfer (ICLR 2017)
Stars: ✭ 1,231 (+892.74%)
Mutual labels:  attention
Nlp Models Tensorflow
Gathers machine learning and Tensorflow deep learning models for NLP problems, 1.13 < Tensorflow < 2.0
Stars: ✭ 1,603 (+1192.74%)
Mutual labels:  attention
Chainer Pspnet
PSPNet in Chainer
Stars: ✭ 76 (-38.71%)
Mutual labels:  cityscapes
Multiturndialogzoo
Multi-turn dialogue baselines written in PyTorch
Stars: ✭ 106 (-14.52%)
Mutual labels:  attention
Fastpunct
Punctuation restoration and spell correction experiments.
Stars: ✭ 121 (-2.42%)
Mutual labels:  attention
Sightseq
Computer vision tools for fairseq, containing PyTorch implementation of text recognition and object detection
Stars: ✭ 116 (-6.45%)
Mutual labels:  attention
Dabnet
Depth-wise Asymmetric Bottleneck for Real-time Semantic Segmentation (BMVC2019)
Stars: ✭ 109 (-12.1%)
Mutual labels:  cityscapes

CCNet-Pure-Pytorch

Criss-Cross Attention for Semantic Segmentation in pure Pytorch with a faster and more precise implementation.

Introduction

I unofficially re-implement CCNet: Criss-Cross Attention for Semantic Segmentation in pure Pytorch for better compatibility under different versions and environments. Many previous open-source projects employ a Cuda extension for Pytorch, which suffer from problems of compatibility and precision loss. Moreover, Cuda extension may not be optimized and accelerated by Pytorch, when we set cudnn.benchmark = True. To address these issues, I design a Criss-Cross Attention operation in our CC.py based on tensor transformation in Pytorch, which is implemented in parallel and shows a faster speed and more precise in the forward result and backward gradient.

My Operation and Performances

CUDA extension is not necessary. Previous Criss-Cross Attention projects are using a Cuda extension for Pytorch. Here I design a more elegant pure Pytorch implementation for Criss-Cross Attention in CC.py. To check the correctness and compare it with CUDA cc_attention of the official one, run the check.py.

To check the correctness, I check my pure pytorch CC() and the official CUDA CrissCross(), the inputs are Query, Key and Value, respectively.

Input
The theoretical output should be 3. The output of our CC() is

CC
But the output of official CUDA CrissCross() is not exactly 3

CUDA
Then I check the gradient, the theoretical gradient of z is 1. Gradient of CC() is excatly 1, but gradient of CUDA CrissCross() is 0.9999998212.

g
As for the speed of tranning and testing, I compare my Pytorch Criss-Cross Attention and the official CUDA Criss-Cross Attention in this project. For batch size 4 at 4 2080Ti with Ohem, my Pytorch Criss-Cross Attention costs 14m32s, and the official CUDA Criss-Cross Attention costs 15m22s on Cityscapes trainning set. For evaluation with batch size 1 at 1 2080Ti using single scale, my Pytorch Criss-Cross Attention costs 28m44s, and the official CUDA Criss-Cross Attention costs 30m59s on Cityscapes val set.
Evaluatations for a same CKPT in single scale by my pure pytorch implementation and official cc_attention.
My module
em
Official CUDA cc_attention
ec
Our pure Pytorch implementation (CC.py) is faster and more precise, as well as more compatibale.

SynBN

For better compatibility under different versions and environments, I decide to use pure Pytorch implementation without using Cuda inplace-abn. I adopt Synchronized-BatchNorm-PyTorch , so it costs more GPU memory than inplace-abn. And I will try to realize an efficient inplace-abn in the future.

Requirements

CUDA extension is not necessary.
If you do not want to compare our implementation with the Cuda version, you just need Python 3, Pytorch 1.2 or 0.4, OpenCV and PIL.
If you want to compare our implementation with the Cuda version, you need Pytorch 1.1 or 1.2 and apex.

# Install **Pytorch-1.1**
$ conda install pytorch torchvision cudatoolkit=9.0 -c pytorch

# Install **Apex**
$ git clone https://github.com/NVIDIA/apex
$ cd apex
$ pip install -v --no-cache-dir --global-option="--cpp_ext" --global-option="--cuda_ext" ./

Training and Evaluation

ImageNet Pre-trained Model can be downloaded from resnet101-imagenet.pth.

$ export CUDA_VISIBLE_DEVICES=0,1,2,3
$ python train.py --data-dir /data/datasets/Cityscapes/ --random-mirror --random-scale --restore-from ./dataset/resnet101-imagenet.pth --gpu 0,1,2,3 --learning-rate 0.01 --input-size 769,769 --weight-decay 0.0001 --batch-size 4 --num-steps 60000 --recurrence 2 --ohem 1 --ohem-thres 0.7 --ohem-keep 100000 --model ccnet


$ python evaluate.py --data-dir /data/datasets/Cityscapes/  --recurrence 2 --model ccnet --restore-from ./snapshots/CS_scenes_60000.pth --whole Ture --gpu 0 --recurrence 2 --batch-size 1

Dataset

I implement this on CityScapes dataset.

Thanks to the Third Party Libs

CCNet: Criss-Cross Attention for Semantic Segmentation
Synchronized-BatchNorm-PyTorch

To do

  • [ ] Implement Inplace-abn in pure Pytorch.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].