class_docstr: The basic implementation for self-attention block/non-local block.
class_docstr_tok: ["The","basic","implementation","for","self-attention","block\/non-local","block","."]
code: def forward(self, x):
      batch_size, c, w, h = x.size()
      if self.scale > 1:
          x = self.pool(x)
  
      value = (
          self.f_value(x)
          .view(batch_size, self.value_channels, -1)
          .permute(0, 2, 1)
      )
      query = (
          self.f_query(x)
          .view(batch_size, self.key_channels, -1)
          .permute(0, 2, 1)
      )
      key = self.f_key(x).view(batch_size, self.key_channels, -1)
  
      sim_map = torch.bmm(query, key) * (self.key_channels**-0.5)
      sim_map = F.softmax(sim_map, dim=-1)
  
      context = torch.bmm(sim_map, value).permute(0, 2, 1).contiguous()
      context = context.view(
          batch_size, self.value_channels, *x.size()[2:]
      )
      context = self.W(context)
      if self.scale > 1:
          context = F.interpolate(
              context, size=(w, h), mode="bilinear", align_corners=True
          )
  
      return context
  
imports: import torch
  import torch.nn
  import torch.nn.functional
  from pywick.models.segmentation.da_basenets.segbase import SegBaseModel
  from pywick.models.segmentation.da_basenets.fcn import _FCNHead
label_desc: Use image node_id 2 for calling the BaseAttentionBlock obj's underlying member method code with example usage: obj.forward(x) and returns: context