How to use ndarray.contrib.DeformableConvolution?


#1

I define a module performing deformable convolution as follows, but I do not know how to save the parameters (self.weight, self.bias) of this module using “net.save_parameters()”

class DeformConv2D(nn.Block):
def init(self,in_channel,num_filter,kernel=(5,5),pad=(2,2),stride=(1,1)):
super(DeformConv2D, self).init()
self.in_channel = in_channel
self.num_filter = num_filter
self.kernel = kernel
self.pad = pad
self.stride = stride
self.weight = nd.normal(loc=0.,scale=1.,shape=(num_filter,in_channel,kernel[0],kernel[1]),ctx=gpu())
self.bias = nd.zeros((num_filter,),ctx=gpu())
self.weight.attach_grad()
self.bias.attach_grad()

def forward(self, inputs, offset):
if self.weight.context != inputs.context:
self.weight = self.weight.copyto(inputs.context)
self.bias = self.bias.copyto(inputs.context)
outputs = ndarray.contrib.DeformableConvolution(data=inputs, offset=offset,weight=self.weight,bias=self.bias, kernel=self.kernel, num_filter=self.num_filter,pad=self.pad,no_bias=self.no_bias)

return outputs


#2

I think net.save_parameters() should be enough.


#3

However, it doesn’t work. ‘save_parameters()’ don’t save weight and bias. How to make the weight and bias able to be saved ?


#4

You’re not defining your parameters as an actual Parameter object. The correct way to define parameters is to use self.params.get() function. Here is an example from MXNet source code.