class_docstr: The class for implementing PANet loss. This was partially adapted from
  https://github.com/whai362/pan_pp.pytorch and
  https://github.com/WenmuZhou/PAN.pytorch.
  
  PANet: `Efficient and Accurate Arbitrary-
  Shaped Text Detection with Pixel Aggregation Network
  <https://arxiv.org/abs/1908.05900>`_.
  
  Args:
      loss_text (dict) The loss config for text map. Defaults to
          dict(type='MaskedSquareDiceLoss').
      loss_kernel (dict) The loss config for kernel map. Defaults to
          dict(type='MaskedSquareDiceLoss').
      loss_embedding (dict) The loss config for embedding map. Defaults to
          dict(type='PANEmbLossV1').
      weight_text (float): The weight of text loss. Defaults to 1.
      weight_kernel (float): The weight of kernel loss. Defaults to 0.5.
      weight_embedding (float): The weight of embedding loss.
          Defaults to 0.25.
      ohem_ratio (float): The negative/positive ratio in ohem. Defaults to 3.
      shrink_ratio (tuple[float]) : The ratio of shrinking kernel. Defaults
          to (1.0, 0.5).
      max_shrink_dist (int or float): The maximum shrinking distance.
          Defaults to 20.
      reduction (str): The way to reduce the loss. Available options are
          "mean" and "sum". Defaults to 'mean'.
docstr: Sample the top-k maximal negative samples and all positive samples.
  
  Args:
      text_score (Tensor): The text score of size :math:`(H, W)`.
      gt_text (Tensor): The ground truth text mask of size
          :math:`(H, W)`.
      gt_mask (Tensor): The effective region mask of size :math:`(H, W)`.
  
  Returns:
      Tensor: The sampled pixel mask of size :math:`(H, W)`.
docstr_tok: ["Sample","the","top-k","maximal","negative","samples","and","all","positive","samples",".","Args",":","text_score","(","Tensor",")",":","The","text","score","of","size",":","math",":","`","(","H",",","W",")","`",".","gt_text","(","Tensor",")",":","The","ground","truth","text","mask","of","size",":","math",":","`","(","H",",","W",")","`",".","gt_mask","(","Tensor",")",":","The","effective","region","mask","of","size",":","math",":","`","(","H",",","W",")","`",".","Returns",":","Tensor",":","The","sampled","pixel","mask","of","size",":","math",":","`","(","H",",","W",")","`","."]
code: def _ohem_single(
      self,
      text_score: torch.Tensor,
      gt_text: torch.Tensor,
      gt_mask: torch.Tensor,
  ) -> torch.Tensor:
      """Sample the top-k maximal negative samples and all positive samples.
  
      Args:
          text_score (Tensor): The text score of size :math:`(H, W)`.
          gt_text (Tensor): The ground truth text mask of size
              :math:`(H, W)`.
          gt_mask (Tensor): The effective region mask of size :math:`(H, W)`.
  
      Returns:
          Tensor: The sampled pixel mask of size :math:`(H, W)`.
      """
      assert isinstance(text_score, torch.Tensor)
      assert isinstance(gt_text, torch.Tensor)
      assert isinstance(gt_mask, torch.Tensor)
      assert len(text_score.shape) == 2
      assert text_score.shape == gt_text.shape
      assert gt_text.shape == gt_mask.shape
  
      pos_num = (int)(torch.sum(gt_text > 0.5).item()) - (int)(
          torch.sum((gt_text > 0.5) * (gt_mask <= 0.5)).item()
      )
      neg_num = (int)(torch.sum(gt_text <= 0.5).item())
      neg_num = (int)(min(pos_num * self.ohem_ratio, neg_num))
  
      if pos_num == 0 or neg_num == 0:
          warnings.warn("pos_num = 0 or neg_num = 0")
          return gt_mask.bool()
  
      neg_score = text_score[gt_text <= 0.5]
      neg_score_sorted, _ = torch.sort(neg_score, descending=True)
      threshold = neg_score_sorted[neg_num - 1]
      sampled_mask = (
          ((text_score >= threshold) + (gt_text > 0.5)) > 0
      ) * (gt_mask > 0.5)
      return sampled_mask
  
imports: import warnings
  from typing import Dict, Sequence, Tuple, Union
  import numpy
  import torch
  import torch.nn.functional
  from mmdet.models.utils import multi_apply
  from torch import nn
  from mmocr.registry import MODELS
  from mmocr.structures import TextDetDataSample
  from .seg_based_module_loss import SegBasedModuleLoss
label_desc: Use image node_id 6 for calling the PANModuleLoss obj's underlying member method code with example usage: obj._ohem_single(text_score, gt_text, gt_mask) and returns: sampled_mask, gt_mask