wkcn commented on issue #11254: add blocklist
URL: https://github.com/apache/incubator-mxnet/pull/11254#issuecomment-416561560
 
 
   @chinakook Thanks.
   I don't know it.
   `mxnet.gluon.nn.Sequential` is a serial structure.
   
[python/mxnet/gluon/nn/basic_layers.py#L51](https://github.com/apache/incubator-mxnet/blob/master/python/mxnet/gluon/nn/basic_layers.py#L51)
   ```python
   class Sequential(Block):
       [...]
       def forward(self, x):
           for block in self._children.values():
               x = block(x)
           return x
   ```
   How to use `Sequential` to implement a parallel structure like multi-column 
CNN?
   [Single-Image Crowd Counting via Multi-Column Convolutional Neural Network 
[Page:4]](https://www.cv-foundation.org/openaccess/content_cvpr_2016/papers/Zhang_Single-Image_Crowd_Counting_CVPR_2016_paper.pdf)
   
   I think `BlockList` is useful.
   e.g.
   ```python
   class MCCNN(nn.HybridBlock):
       def __init__(self, num_filter, columns, aggregator, **kwargs):
           super(MCCNN, self).__init__(**kwargs)
           with self.name_scope():
               self.column_blocks = BlockList(self, [get_column(num_filter, 
kernel_size, dilations) for kernel_size, dilations in columns])
               self.aggregator = get_aggregator(num_filter, aggregator[0], 
aggregator[1]) 
               self.conv1x1 = nn.Conv2D(channels = 1, kernel_size = (1, 1), 
activation = 'relu')
       def hybrid_forward(self, F, x):
           column_outputs = [c(x) for c in self.column_blocks]
           x = F.concat(*column_outputs, dim = 1)
           x = self.aggregator(x)
           x = self.conv1x1(x)
           x = F.squeeze(x, axis = 1)
           return x
   ```
   
   And here is my BlockList implementation.
   ```python
   def BlockList(self, block_list):
       block_names = []
       for block in block_list:
           assert not hasattr(self, block.name)
           setattr(self, block.name, block)
           block_names.append(block.name)
       class _BlockList:
           def __init__(self, block_names, parent):
               self.block_names = block_names
               self.parent = parent
           def __getitem__(self, i):
               return getattr(self.parent, self.block_names[i])
       return _BlockList(block_names, self)
   ```

----------------------------------------------------------------
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