How to feed output of a Gluon Layer into Symbol inside a Hybrid Block?

Greetings everyone.

I am trying to obtain the output of a hybrid block, and feed it into a symbol block; but I am unsure as to how should i implement it.

More specifically, I am looking to modify the ConvPredictor in GluonCV’s Single Shot Detection Model by adding a Deformable Convolution layer in [1]. The Deformable Convolution is presented as a symbol, but requires offset data generated from a Convolution Layer.

The source code from GluonCV’s repo is provided below:

class ConvPredictor(HybridBlock):
    def __init__(self, num_channel, kernel=(3, 3), pad=(1, 1), stride=(1, 1),
           activation=None, use_bias=True, in_channels=0, **kwargs):
        super(ConvPredictor, self).__init__(**kwargs)
        with self.name_scope():
            self.predictor = nn.Conv2D(
                num_channel, kernel, strides=stride, padding=pad,
                activation=activation, use_bias=use_bias, in_channels=in_channels,
                weight_initializer=mx.init.Xavier(magnitude=2),
                bias_initializer='zeros')

    def hybrid_forward(self, F, x):
        return self.predictor(x)

I was thinking of feeding the convolution layer’s output into the symbol inside its hybrid_forward() method, but I don’t see a way to do so without re-initializing the symbol repeatedly inside the method. I would like to do so in it’s init() method but I am not sure how to link its output to the input of the symbol.

[1] https://mxnet.incubator.apache.org/api/python/symbol/contrib.html#mxnet.symbol.contrib.DeformableConvolution

  1. Create a SymbolBlock from your symbol
  2. Create a HybridSequental block and add the conv layer and the symbolblock.
  3. In the hybrid_forward() call the hybridsequental instead of the conv block.

This will pipe the output of the conv block into your symbol

Hi @ifeherva,

Thank you very much for your reply! :slight_smile:

The solution worked, and i will be closing this thread.

1 Like