Using UpSampling in HybridBlock


#1

Is there any example how to use F.UpSampling in hybribBlock.
Here is used code:

class UpSampling(nn.HybridBlock):
def init(self, scale, sample_type):
super(UpSampling, self).init()
self.scale = scale
self.sample_type = sample_type

def hybrid_forward(self, F, x):
    return F.UpSampling(x, scale=self.scale, num_filter=1,  sample_type=self.sample_type, num_args=2)

Error:

mxnet.base.MXNetError: [12:58:06] src/c_api/…/imperative/imperative_utils.h:336: Check failed: num_inputs == infered_num_inputs (1 vs. 2) Operator UpSampling expects 2 inputs, but got 1 instead.

Need help! Hope for reply!


#2

Hi, this is how I use UpSampling - for nearest interpolation. I recall there was an issue - I had trouble when I was trying to do bilinear.

class UpSample(HybridBlock):
    def __init__(self,_nfilters, factor = 2,  _norm_type='BatchNorm', **kwards):
        HybridBlock.__init__(self,**kwards)


        self.factor = factor
        self.nfilters = _nfilters // self.factor

        with self.name_scope():
            self.convup_normed = Conv2DNormed(self.nfilters,
                                              kernel_size = (3,3), # you can choose to use (1,1), but then nearby pixels will again have same value for the same channel, for interpolation "nearest". 
                                              padding = 1,
                                              _norm_type = _norm_type,
                                              prefix="_convdn_")

    def hybrid_forward(self,F,_xl):
                x = F.UpSampling(_xl, scale=self.factor, sample_type='nearest')
        x = self.convup_normed(x)

        return x

in this example I first upsample, (double the size) and then I apply a convolution operator to “smooth” out the upscaling. I am using it in place of transposed convolution.

See also stackoverflow question I asked few months back.

Hope this helps.

edit There is also a new operator (at least I just saw it) in mxnet.F.contrib.BilinearResize2D, however you need to supply the new height and width (F can be either Symbol or NDArray).