How to implement deformable convolution

does anyone know how to implement deformable convolution v1 or v2 in gloun (not c++ cuda)

The NDArray API provides a routine for deformable convolutions: https://mxnet.incubator.apache.org/api/python/symbol/contrib.html#mxnet.symbol.contrib.DeformableConvolution
To use it in Gluon, you need to wrap MXNet symbol in a Gluon SymbolBlock: https://mxnet.incubator.apache.org/api/python/gluon/gluon.html?highlight=symbolbl#mxnet.gluon.SymbolBlock

I am a newbie in mxnet, can you show me how can i do that in a simple example?

Also, should i wait till mxnet provide deformable conv v2 or i can change v1 to v2 ?

Here is a simple example:

import mxnet as mx
from mxnet import nd
from mxnet import gluon

# set context to gpu
ctx=mx.gpu()

# Define data and offset symbols
data = mx.sym.var('data')
offset = mx.sym.var('offset')

# Define the DeformbleConvolution
output = mx.symbol.contrib.DeformableConvolution(data=data, offset=offset, num_deformable_group=1, num_group=1, no_bias=True, num_filter=3,kernel=(3,3),pad=(1,1))

# Wrap DeformableConvolution into a gluon SymbolBlock
net = gluon.SymbolBlock(output, [data, offset])

# Initialize parameters
net.collect_params().initialize(ctx=ctx)

# Create random data for input and offset
input_data = mx.nd.random.normal(shape=(10,3,100,100),ctx=ctx)
offset_data = mx.nd.random.normal(shape=(10,27,100, 100),ctx=ctx)

# Feed data into the DeformableConvolution 
net(input_data,offset_data)

I assume you mean with v1,v2 1-dimensional and 2-dimensional convolutions? MXNet currently supports deformable 2-dimensional convolution: https://mxnet.incubator.apache.org/api/python/symbol/contrib.html#mxnet.symbol.contrib.DeformableConvolution

1 Like

I meant deformeble convolution v2, the paper released a couple of months ago https://arxiv.org/pdf/1811.11168.pdf

Ok I was not aware of this new type of deformable convolution. It seems the authors of the paper already provide an MXNet implementation: https://github.com/msracver/Deformable-ConvNets/tree/master/DCNv2_op

I don not know how to use DeformableConvolution api in MXNET symbol
Can i use mx.ndarray.contrib.DeformableConvolution to build a net?

You may need to clone the sources of incubator-mxnet and copy these source files into src/operator/contrib,and recompile it. There seems no way to use DeformableConvolution v2 unless you install mxnet from sources.

1 Like