text: """RefineNet-CRP-RCU-blocks in PyTorch
  
  RefineNet-PyTorch for non-commercial purposes
  
  Copyright (c) 2018, Vladimir Nekrasov (vladimir.nekrasov@adelaide.edu.au)
  All rights reserved.
  
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:
  
  * Redistributions of source code must retain the above copyright notice, this
    list of conditions and the following disclaimer.
  
  * Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.
  
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  """
  
  import numpy as np
  import torch
  import torch.nn as nn
  import torch.nn.functional as F
  
  def batchnorm(in_planes):
      "batch norm 2d"
      return nn.BatchNorm2d(in_planes, affine=True, eps=1e-5, momentum=0.1)
  
  def conv3x3(in_planes, out_planes, stride=1, bias=False):
      "3x3 convolution with padding"
      return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
                       padding=1, bias=bias)
  
  def conv1x1(in_planes, out_planes, stride=1, bias=False):
      "1x1 convolution"
      return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride,
                       padding=0, bias=bias)
  
  def convbnrelu(in_planes, out_planes, kernel_size, stride=1, groups=1, act=True):
      "conv-batchnorm-relu"
      if act:
          return nn.Sequential(nn.Conv2d(in_planes, out_planes, kernel_size, stride=stride, padding=int(kernel_size / 2.), groups=groups, bias=False),
                               batchnorm(out_planes),
                               nn.ReLU6(inplace=True))
      else:
          return nn.Sequential(nn.Conv2d(in_planes, out_planes, kernel_size, stride=stride, padding=int(kernel_size / 2.), groups=groups, bias=False),
                               batchnorm(out_planes))
  
  class CRPBlock(nn.Module):
  
      def __init__(self, in_planes, out_planes, n_stages):
          super(CRPBlock, self).__init__()
          for i in range(n_stages):
              setattr(self, '{}_{}'.format(i + 1, 'outvar_dimred'),
                      conv3x3(in_planes if (i == 0) else out_planes,
                              out_planes, stride=1,
                              bias=False))
          self.stride = 1
          self.n_stages = n_stages
          self.maxpool = nn.MaxPool2d(kernel_size=5, stride=1, padding=2)
  
      def forward(self, x):
          top = x
          for i in range(self.n_stages):
              top = self.maxpool(top)
              top = getattr(self, '{}_{}'.format(i + 1, 'outvar_dimred'))(top)
              x = top + x
          return x
      
  stages_suffixes = {0 : '_conv',
                     1 : '_conv_relu_varout_dimred'}
      
  class RCUBlock(nn.Module):
      
      def __init__(self, in_planes, out_planes, n_blocks, n_stages):
          super(RCUBlock, self).__init__()
          for i in range(n_blocks):
              for j in range(n_stages):
                  setattr(self, '{}{}'.format(i + 1, stages_suffixes[j]),
                          conv3x3(in_planes if (i == 0) and (j == 0) else out_planes,
                                  out_planes, stride=1,
                                  bias=(j == 0)))
          self.stride = 1
          self.n_blocks = n_blocks
          self.n_stages = n_stages
      
      def forward(self, x):
          for i in range(self.n_blocks):
              residual = x
              for j in range(self.n_stages):
                  x = F.relu(x)
                  x = getattr(self, '{}{}'.format(i + 1, stages_suffixes[j]))(x)
              x += residual
          return x
  
  class MFBlock(nn.Upsample):
      def __init__(self, *args, mode='bilinear', **kwds):
          super(MFBlock, self).__init__(*args, mode=mode, **kwds)
      
      def forward(self, *xs):
          try:
              outputs = torch.stack([super(MFBlock, self).forward(x) for x in xs])
          except ValueError:
              self.size = tuple(np.stack([x.shape[2:] for x in xs]).max(0))
              outputs = torch.stack([super(MFBlock, self).forward(x) for x in xs])
          except Exception as e:
              assert False, e
          finally:
              return torch.sum(outputs, 0)
  