The official implementation of the CVPR2021 paper: Decoupled Dynamic Filter Networks

Overview

Decoupled Dynamic Filter Networks

This repo is the official implementation of CVPR2021 paper: "Decoupled Dynamic Filter Networks".

Introduction

DDF is an alternative of convolution which decouples dynamic filters into spatial and channel filters.

DDF operation

We illustrate the DDF operation and the DDF module. The orange color denotes spatial dynamic filters / branch, and the green color denotes channel dynamic filters / branch. The filter application means applying the convolution operation at a single position. ‘GAP’ means the global average pooling and ‘FC’ denotes the fully connected layer.

Please refer to our project page and paper for more details.

Model zoo

Will be avaliable soon.

Usage

Install

  • Clone this repo:
git clone https://github.com/theFoxofSky/ddfnet.git
cd ddfnet
  • Create a conda virtual environment and activate it:
conda create -n ddfnet python=3.7 -y
conda activate ddfnet
conda install pytorch==1.7.1 torchvision==0.8.2 cudatoolkit=10.1 -c pytorch
  • Install timm==0.4.5:
pip install timm==0.4.5
  • Install Apex:
git clone https://github.com/NVIDIA/apex
cd apex
pip install -v --disable-pip-version-check --no-cache-dir --global-option="--cpp_ext" --global-option="--cuda_ext" ./
  • Install other requirements:
pip install pyyaml ipdb
  • Build the ddf operation:
cd ddf
python setup.py install
mv build/lib*/* .
  • Verify the ddf operation:
cd <path_to_ddfnet>
python grad_check.py

Data preparation

We use standard ImageNet dataset, you can download it from http://image-net.org/. Please prepare it under the following file structure:

$ tree data
imagenet
├── train
│   ├── class1
│   │   ├── img1.jpeg
│   │   ├── img2.jpeg
│   │   └── ...
│   ├── class2
│   │   ├── img3.jpeg
│   │   └── ...
│   └── ...
└── val
    ├── class1
    │   ├── img4.jpeg
    │   ├── img5.jpeg
    │   └── ...
    ├── class2
    │   ├── img6.jpeg
    │   └── ...
    └── ...

Training from scratch

To train a model, for example ddf_mul_resnet50, on ImageNet from scratch with 8 RTX 2080Ti, run:

./distributed_train.sh 8 <path_to_imagenet> --model ddf_mul_resnet50 --lr 0.4 \
--warmup-epochs 5 --epochs 120 --sched cosine -b 128 -j 6 --amp --dist-bn reduce

Evaluation

To evaluate a pre-trained model, for example ddf_mul_resnet50, on ImageNet val, run:

python validate.py <path_to_imagenet> --model ddf_mul_resnet50 --checkpoint <path_to_checkpoint>

Inference time

To measure the inference time, run:

python test_time.py

Use ddf in other places as a basic building layer

Please directly copy the ddf folder to your repo and rebuild the ddf operation following the instructions above. Then, you can easily import the ddf operation, the DDFPack, and the DDFUpPack.

You can design your own module with the ddf operation.

For example, you can get a carafe/involution-like module by fixing all values in the channel filter to 1 for 'mul' combination or 0 for 'add' combination.

channel_filter = torch.ones(filter_size)
output = ddf(input, channel_filter, spatial_filter,
             kernel_size, dilation, stride, head, 'mul')

or

channel_filter = torch.zeros(filter_size)
output = ddf(input, channel_filter, spatial_filter,
             kernel_size, dilation, stride, head, 'add')

Similarly, you can get a WeightNet-like depthwise filter by fixing all values in the spatial filter to 1 for 'mul' combination or 0 for 'add' combination.

spatial_filter = torch.ones(filter_size)
output = ddf(input, channel_filter, spatial_filter,
             kernel_size, dilation, stride, head, 'mul')

or

spatial_filter = torch.zeros(filter_size)
output = ddf(input, channel_filter, spatial_filter,
             kernel_size, dilation, stride, head, 'add')

Almost all exisitng weight-dynamic depthwise operation (not grid-dynamic like deformable convolution) can be implemented with our ddf operation. Have fun exploring.

Acknowledgement

Codebase from pytorch-image-models.

Citation

If you find this code useful for your research, please cite our paper.

@inproceedings{zhou_ddf_cvpr_2021,
               title = {Decoupled Dynamic Filter Networks},
               author = {Zhou, Jingkai and Jampani, Varun and Pi, Zhixiong and Liu, Qiong and Yang, Ming-Hsuan},
               booktitle = {IEEE/CVF Conf. on Computer Vision and Pattern Recognition (CVPR)},
               month = jun,
               year = {2021}
               }
Comments
  • Get feature map filled with zeros in FilterNorm

    Get feature map filled with zeros in FilterNorm

    Hi, in build_channel_branch function, FilterNorm with filter_type='channel' is a subsequent module after AdaptiveAvgPool2d, which is expected to get a N*C*1*1 feature map as input. But in FilterNorm, you calculate mean in the 2nd dim, which will return the feature map itself because this dim is a singleton dim.

    https://github.com/theFoxofSky/ddfnet/blob/b3be7fa2d77939b6beae9f49ae5a48b41169dba5/ddf/ddf.py#L158-L170

    So line 162 will always return a zero feature map, and line 163 return an NaN map.

    Is it a potential bug?

    opened by QiuJueqin 8
  • About the dilation setting

    About the dilation setting

    Hi author, thanks for your wonderful work. Today I noticed a problem in the dilation setting about the DDF model that "dilation" cannot set to 8 or more (like 16), otherwise the core will be dumped. I wonder how to solve this problem, even after checking the .cuda files... I will be very grateful if you can let me know how to solve this problem. Thank you very much.

    opened by SudongCAI 5
  • question about DDF-Up

    question about DDF-Up

    Hi. Thanks for nice paper.

    1. In DDF-Up module, does the size of guidance feature have to be same as that of input feature?

    2. Can you release the code for "joint depth upsampling with DDF-UP" experiment in Sec. 6?

    thx.

    opened by sujyQ 3
  • About “running_std”

    About “running_std”

    In the function "build_channel_branch", the running_std is True, while it is False in the the function "build_spatial_branch". Is it a good setting after experimental verification?

    opened by Deep-imagelab 2
  • Issue about the FLOPs presented in the paper

    Issue about the FLOPs presented in the paper

    Thank you for your excellent work! But I'm wondering how you obtain the 2.3B FLOPs as claimed in your paper. I've tested the FLOPs of the ResNet50 without your ddf module and it already takes 2.3B. As there are some conv2d with channel interaction in your spatial branch, I think the whole FLOPs of your ddfnet is more than 2.3B, which should be about 2.7B under rough estimation.

    opened by melohux 2
  • Something wrong with the grad_check.py

    Something wrong with the grad_check.py

    After installing the ddfnet, I run the code grad_check.py, and get the output as follows:

    True
    True
    True
    True
    True
    True
    All forward pass
    True
    True
    True
    True
    Traceback (most recent call last):
      File "grad_check.py", line 137, in <module>
        test = gradcheck(ddf, (feat, channel_att, spatial_att, 3, 1, 2, 1, 'mul'), atol=1e-5, eps=1e-3)
      File "/home/user/miniconda3/envs/ddfnet/lib/python3.7/site-packages/torch/autograd/gradcheck.py", line 323, in gradcheck
        func_out = func(*tupled_inputs)
      File "/home/user/ddfnet/ddf/ddf.py", line 65, in forward
        'only support mul or add combination, instead of {}'.format(kernel_combine)
    AssertionError: only support mul or add combination, instead of 1
    

    Could you please help us find what is happening? Thx.

    opened by Reply1999 2
  • Error about grad_check.py

    Error about grad_check.py

    Thanks for your excellent work! I have already installed the ddf pkg with python==3.8, cuda=11.1. And when I run the grad_check.py, the error accured as: Traceback (most recent call last): File "grad_check.py", line 40, in out_ddf_o = ddf(feat, channel_att, spatial_att, 3, 2, 1, 'mul', 'o') File "/mnt/lustre/chenlin.vendor/code/mmclassification/mmcls/models/utils/ddf/ddf.py", line 86, in forward OP_DICT[op_type].forward(features, channel_filter, spatial_filter, RuntimeError: ddf operation is not compiled with GPU support

    What should I do to solve this error?

    opened by xiaoachen98 1
  • Also questions about the kernel size

    Also questions about the kernel size

    The kernel size can not be larger than 13x13? I set the kernel size to 15x15 and encounter the error "CUDA error: an illegal memory access was encountered". But everything is ok when the kernel size is smaller than 13x13 or equals to it. Besides, I note that the speed is obviously lower as the kernel size goes up, more obvious than common depthwise convolutions. Is that because the implementation is not enough efficient?

    opened by ydhongHIT 1
  • Questions about the kernel size

    Questions about the kernel size

    Thank you for the open-source code. In your code, the kernel of ddf is set to square (i.e. 3x3), if I want to set the kernel to a normal rectangle (e.g. 2x5), how should I change it? Looking forward to your reply!

    opened by dangf15 1
  • Channel size

    Channel size

    DDFPack and DDFUpPack currently assume that the in_channels and out_channels are the same. Can you make short changes to the modules to allow them to work for different in_channels and out_channels?

    Also, I had an issue when I tried using DDFPack for input with single-channel (eg. input with dimensions [10,1,256,256] ) Can you please let me know if there is something I'm missing out on?

    opened by vkothapally 1
  • ImportError: cannot import name 'ddf_mul_ext'

    ImportError: cannot import name 'ddf_mul_ext'

    Hello,I met this question,can you help me?

    python grad_check.py Traceback (most recent call last): File "grad_check.py", line 8, in from ddf import ddf File "/media/omnisky/34B22D6336AC1687/JSS/ddfnet-main/ddf/init.py", line 1, in from .ddf import * File "/media/omnisky/34B22D6336AC1687/JSS/ddfnet-main/ddf/ddf.py", line 17, in from .ddf import ddf_mul_ext, ddf_mul_faster_ext, ddf_add_ext, ddf_add_faster_ext ImportError: cannot import name 'ddf_mul_ext'

    opened by jia-604 1
Owner
F.S.Fire
A CS student. Now I am working at Alibaba DAMO Academy.
F.S.Fire
An implementation for the loss function proposed in Decoupled Contrastive Loss paper.

Decoupled-Contrastive-Learning This repository is an implementation for the loss function proposed in Decoupled Contrastive Loss paper. Requirements P

Ramin Nakhli 71 Dec 4, 2022
Implement Decoupled Neural Interfaces using Synthetic Gradients in Pytorch

disclaimer: this code is modified from pytorch-tutorial Image classification with synthetic gradient in Pytorch I implement the Decoupled Neural Inter

Andrew 114 Dec 22, 2022
The implementation of CVPR2021 paper Temporal Query Networks for Fine-grained Video Understanding, by Chuhan Zhang, Ankush Gupta and Andrew Zisserman.

Temporal Query Networks for Fine-grained Video Understanding ?? This repository contains the implementation of CVPR2021 paper Temporal_Query_Networks

null 55 Dec 21, 2022
PyTorch implementation of our Adam-NSCL algorithm from our CVPR2021 (oral) paper "Training Networks in Null Space for Continual Learning"

Adam-NSCL This is a PyTorch implementation of Adam-NSCL algorithm for continual learning from our CVPR2021 (oral) paper: Title: Training Networks in N

Shipeng Wang 34 Dec 21, 2022
Towards Rolling Shutter Correction and Deblurring in Dynamic Scenes (CVPR2021)

RSCD (BS-RSCD & JCD) Towards Rolling Shutter Correction and Deblurring in Dynamic Scenes (CVPR2021) by Zhihang Zhong, Yinqiang Zheng, Imari Sato We co

null 81 Dec 15, 2022
This is the repository for CVPR2021 Dynamic Metric Learning: Towards a Scalable Metric Space to Accommodate Multiple Semantic Scales

Intro This is the repository for CVPR2021 Dynamic Metric Learning: Towards a Scalable Metric Space to Accommodate Multiple Semantic Scales Vehicle Sam

null 39 Jul 21, 2022
Official implementation of our CVPR2021 paper "OTA: Optimal Transport Assignment for Object Detection" in Pytorch.

OTA: Optimal Transport Assignment for Object Detection This project provides an implementation for our CVPR2021 paper "OTA: Optimal Transport Assignme

null 217 Jan 3, 2023
[PyTorch] Official implementation of CVPR2021 paper "PointDSC: Robust Point Cloud Registration using Deep Spatial Consistency". https://arxiv.org/abs/2103.05465

PointDSC repository PyTorch implementation of PointDSC for CVPR'2021 paper "PointDSC: Robust Point Cloud Registration using Deep Spatial Consistency",

null 153 Dec 14, 2022
The official repo of the CVPR2021 oral paper: Representative Batch Normalization with Feature Calibration

Representative Batch Normalization (RBN) with Feature Calibration The official implementation of the CVPR2021 oral paper: Representative Batch Normali

Open source projects of ShangHua-Gao 76 Nov 9, 2022
Official code of paper "PGT: A Progressive Method for Training Models on Long Videos" on CVPR2021

PGT Code for paper PGT: A Progressive Method for Training Models on Long Videos. Install Run pip install -r requirements.txt. Run python setup.py buil

Bo Pang 27 Mar 30, 2022
Dynamic View Synthesis from Dynamic Monocular Video

Towards Robust Monocular Depth Estimation: Mixing Datasets for Zero-shot Cross-dataset Transfer This repository contains code to compute depth from a

Intelligent Systems Lab Org 2.3k Jan 1, 2023
Dynamic View Synthesis from Dynamic Monocular Video

Dynamic View Synthesis from Dynamic Monocular Video Project Website | Video | Paper Dynamic View Synthesis from Dynamic Monocular Video Chen Gao, Ayus

Chen Gao 139 Dec 28, 2022
Dynamic vae - Dynamic VAE algorithm is used for anomaly detection of battery data

Dynamic VAE frame Automatic feature extraction can be achieved by probability di

null 10 Oct 7, 2022
Official implementation of Rethinking Graph Neural Architecture Search from Message-passing (CVPR2021)

Rethinking Graph Neural Architecture Search from Message-passing Intro The GNAS can automatically learn better architecture with the optimal depth of

Shaofei Cai 48 Sep 30, 2022
Official implementation of "Towards Good Practices for Efficiently Annotating Large-Scale Image Classification Datasets" (CVPR2021)

Towards Good Practices for Efficiently Annotating Large-Scale Image Classification Datasets This is the official implementation of "Towards Good Pract

Sanja Fidler's Lab 52 Nov 22, 2022
[CVPR 2021] Official PyTorch Implementation for "Iterative Filter Adaptive Network for Single Image Defocus Deblurring"

IFAN: Iterative Filter Adaptive Network for Single Image Defocus Deblurring Checkout for the demo (GUI/Google Colab)! The GUI version might occasional

Junyong Lee 173 Dec 30, 2022
Official PyTorch Implementation of paper "Deep 3D Mask Volume for View Synthesis of Dynamic Scenes", ICCV 2021.

Deep 3D Mask Volume for View Synthesis of Dynamic Scenes Official PyTorch Implementation of paper "Deep 3D Mask Volume for View Synthesis of Dynamic S

Ken Lin 17 Oct 12, 2022
The implementation of the CVPR2021 paper "Structure-Aware Face Clustering on a Large-Scale Graph with 10^7 Nodes"

STAR-FC This code is the implementation for the CVPR 2021 paper "Structure-Aware Face Clustering on a Large-Scale Graph with 10^7 Nodes" ?? ?? . ?? Re

Shuai Shen 87 Dec 28, 2022
Pytorch implementation of CVPR2021 paper "MUST-GAN: Multi-level Statistics Transfer for Self-driven Person Image Generation"

MUST-GAN Code | paper The Pytorch implementation of our CVPR2021 paper "MUST-GAN: Multi-level Statistics Transfer for Self-driven Person Image Generat

TianxiangMa 46 Dec 26, 2022