feevos removed a comment on issue #9288: Get HybridBlock layer shape on runtime
URL: 
https://github.com/apache/incubator-mxnet/issues/9288#issuecomment-409782762
 
 
   PS And this is an (hybrid-able) implementation of the pyramid scene parsing 
module, users need to write their own Conv2DNormed HybridBlock layer 
(basically, convolution + NormLayer (Batch/Instance etc). Here I assume that 
input is the format (Batch,NChannels, H, W), with H == W. Trivial to modify for 
H != W
   
   
   ```Python
   from mxnet import gluon
   from mxnet.gluon import  HybridBlock
   from mxnet.ndarray import NDArray
   from phaino.nn.layers.conv2Dnormed import * # You need to define your own 
conv2Dnormed HybridBlock
   
   class PSP_Pooling(HybridBlock):
   
       """
       Pyramid Scene Parsing pooling layer, as defined in Zhao et al. 2017 
(https://arxiv.org/abs/1612.01105)        
       This is only the pyramid pooling module. 
       INPUT:
           layer of size Nbatch, Nchannel, H, W
       OUTPUT:
           layer of size Nbatch,  Nchannel, H, W. 
   
       """
   
       def __init__(self, _nfilters, _norm_type = 'BatchNorm', **kwards):
           HybridBlock.__init__(self,**kwards)
   
           self.nfilters = _nfilters
           self.layer_size = None
           # This is used as a container (list) of layers
           self.convs = gluon.nn.HybridSequential()
           with self.name_scope():
   
               
self.convs.add(Conv2DNormed(self.nfilters//4,kernel_size=(1,1),padding=(0,0), 
prefix="_conv1_"))
               
self.convs.add(Conv2DNormed(self.nfilters//4,kernel_size=(1,1),padding=(0,0), 
prefix="_conv2_"))
               
self.convs.add(Conv2DNormed(self.nfilters//4,kernel_size=(1,1),padding=(0,0), 
prefix="_conv3_"))
               
self.convs.add(Conv2DNormed(self.nfilters//4,kernel_size=(1,1),padding=(0,0), 
prefix="_conv4_"))
   
   
           self.conv_norm_final = Conv2DNormed(channels = self.nfilters,
                                               kernel_size=(1,1),
                                               padding=(0,0),
                                               _norm_type=_norm_type)
   
       def forward(self,_input):
           self.layer_size = _input.shape
           
           return HybridBlock.forward(self,_input)
   
       
       def hybrid_forward(self,F,_input):
           p = [_input]
           for i in range(4):
   
               pool_size = self.layer_size[-1] // (2**i) # Need this to be 
integer 
               x = 
F.Pooling(_input,kernel=[pool_size,pool_size],stride=[pool_size,pool_size],pool_type='max')
               x = F.UpSampling(x,sample_type='nearest',scale=pool_size)
               x = self.convs[i](x)
               p += [x]
   
           out = F.concat(p[0],p[1],p[2],p[3],p[4],dim=1)
   
           out = self.conv_norm_final(out)
   
           return out
   
   
   ```
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to