class_docstr: Basic Non-local module.
  This model is the implementation of "Non-local Neural Networks"
  (https://arxiv.org/abs/1711.07971)
  
  Args:
      in_channels (int): Channels of the input feature map.
      reduction (int): Channel reduction ratio. Default: 2.
      use_scale (bool): Whether to scale pairwise_weight by `1/sqrt(inter_channels)` when the mode is `embedded_gaussian`. Default: True.
      sub_sample (bool): Whether to utilize max pooling after pairwise function. Default: False.
      mode (str): Options are `gaussian`, `concatenation`, `embedded_gaussian` and `dot_product`. Default: embedded_gaussian.
code: def __init__(
      self,
      in_channels,
      reduction=2,
      use_scale=True,
      sub_sample=False,
      mode="embedded_gaussian",
  ):
      super(NonLocal2D, self).__init__()
      self.in_channels = in_channels
      self.reduction = reduction
      self.use_scale = use_scale
      self.sub_sample = sub_sample
      self.mode = mode
      if mode not in [
          "gaussian",
          "embedded_gaussian",
          "dot_product",
          "concatenation",
      ]:
          raise ValueError(
              "Mode should be in 'gaussian', 'concatenation','embedded_gaussian' or 'dot_product'."
          )
  
      self.inter_channels = max(in_channels // reduction, 1)
  
      self.g = nn.Conv2D(
          in_channels=self.in_channels,
          out_channels=self.inter_channels,
          kernel_size=1,
      )
      self.conv_out = layers.ConvBNReLU(
          in_channels=self.inter_channels,
          out_channels=self.in_channels,
          kernel_size=1,
          bias_attr=False,
      )
  
      if self.mode != "gaussian":
          self.theta = nn.Conv2D(
              in_channels=self.in_channels,
              out_channels=self.inter_channels,
              kernel_size=1,
          )
          self.phi = nn.Conv2D(
              in_channels=self.in_channels,
              out_channels=self.inter_channels,
              kernel_size=1,
          )
  
      if self.mode == "concatenation":
          self.concat_project = layers.ConvBNReLU(
              in_channels=self.inter_channels * 2,
              out_channels=1,
              kernel_size=1,
              bias_attr=False,
          )
  
      if self.sub_sample:
          max_pool_layer = nn.MaxPool2D(kernel_size=(2, 2))
          self.g = nn.Sequential(self.g, max_pool_layer)
          if self.mode != "gaussian":
              self.phi = nn.Sequential(self.phi, max_pool_layer)
          else:
              self.phi = max_pool_layer
  
file_path: PaddleSeg/paddleseg/models/layers/nonlocal2d.py
imports: import paddle
  import paddle.nn
  import paddle.nn.functional
  from paddleseg.models import layers
label_desc: Use image node_id 1 to create a new NonLocal2D object from inherited base classes: nn with example: obj = NonLocal2D(in_channels, reduction, use_scale, sub_sample, mode)