class_docstr: DBNet as described in `"Real-time Scene Text Detection with Differentiable Binarization"
  <https://arxiv.org/pdf/1911.08947.pdf>`_.
  
  Args:
  ----
      feature extractor: the backbone serving as feature extractor
      fpn_channels: number of channels each extracted feature maps is mapped to
      assume_straight_pages: if True, fit straight bounding boxes only
      exportable: onnx exportable returns only logits
      cfg: the configuration dict of the model
      class_names: list of class names
docstr: Compute a batch of gts, masks, thresh_gts, thresh_masks from a list of boxes
  and a list of masks for each image. From there it computes the loss with the model output
  
  Args:
  ----
      out_map: output feature map of the model of shape (N, H, W, C)
      thresh_map: threshold map of shape (N, H, W, C)
      target: list of dictionary where each dict has a `boxes` and a `flags` entry
      gamma: modulating factor in the focal loss formula
      alpha: balancing factor in the focal loss formula
      eps: epsilon factor in dice loss
  
  Returns:
  -------
      A loss tensor
docstr_tok: ["Compute","a","batch","of","gts",",","masks",",","thresh_gts",",","thresh_masks","from","a","list","of","boxes","and","a","list","of","masks","for","each","image",".","From","there","it","computes","the","loss","with","the","model","output","Args",":","--","--","out_map",":","output","feature","map","of","the","model","of","shape","(","N",",","H",",","W",",","C",")","thresh_map",":","threshold","map","of","shape","(","N",",","H",",","W",",","C",")","target",":","list","of","dictionary","where","each","dict","has","a","`","boxes","`","and","a","`","flags","`","entry","gamma",":","modulating","factor","in","the","focal","loss","formula","alpha",":","balancing","factor","in","the","focal","loss","formula","eps",":","epsilon","factor","in","dice","loss","Returns",":","--","--","--","-","A","loss","tensor"]
code: def compute_loss(
      self,
      out_map: tf.Tensor,
      thresh_map: tf.Tensor,
      target: List[Dict[str, np.ndarray]],
      gamma: float = 2.0,
      alpha: float = 0.5,
      eps: float = 1e-8,
  ) -> tf.Tensor:
      """Compute a batch of gts, masks, thresh_gts, thresh_masks from a list of boxes
      and a list of masks for each image. From there it computes the loss with the model output
  
      Args:
      ----
          out_map: output feature map of the model of shape (N, H, W, C)
          thresh_map: threshold map of shape (N, H, W, C)
          target: list of dictionary where each dict has a `boxes` and a `flags` entry
          gamma: modulating factor in the focal loss formula
          alpha: balancing factor in the focal loss formula
          eps: epsilon factor in dice loss
  
      Returns:
      -------
          A loss tensor
      """
      if gamma < 0:
          raise ValueError(
              "Value of gamma should be greater than or equal to zero."
          )
  
      prob_map = tf.math.sigmoid(out_map)
      thresh_map = tf.math.sigmoid(thresh_map)
  
      (
          seg_target,
          seg_mask,
          thresh_target,
          thresh_mask,
      ) = self.build_target(target, out_map.shape[1:], True)
      seg_target = tf.convert_to_tensor(seg_target, dtype=out_map.dtype)
      seg_mask = tf.convert_to_tensor(seg_mask, dtype=tf.bool)
      seg_mask = tf.cast(seg_mask, tf.float32)
      thresh_target = tf.convert_to_tensor(
          thresh_target, dtype=out_map.dtype
      )
      thresh_mask = tf.convert_to_tensor(thresh_mask, dtype=tf.bool)
  
      # Focal loss
      focal_scale = 10.0
      bce_loss = tf.keras.losses.binary_crossentropy(
          seg_target[..., None], out_map[..., None], from_logits=True
      )
  
      # Convert logits to prob, compute gamma factor
      p_t = (seg_target * prob_map) + (
          (1 - seg_target) * (1 - prob_map)
      )
      alpha_t = seg_target * alpha + (1 - seg_target) * (1 - alpha)
      # Unreduced loss
      focal_loss = alpha_t * (1 - p_t) ** gamma * bce_loss
      # Class reduced
      focal_loss = tf.reduce_sum(
          seg_mask * focal_loss, (0, 1, 2, 3)
      ) / tf.reduce_sum(seg_mask, (0, 1, 2, 3))
  
      # Compute dice loss for approx binary_map
      binary_map = 1.0 / (1.0 + tf.exp(-50 * (prob_map - thresh_map)))
      inter = tf.reduce_sum(
          seg_mask * binary_map * seg_target, (0, 1, 2, 3)
      )
      cardinality = tf.reduce_sum(
          (binary_map + seg_target), (0, 1, 2, 3)
      )
      dice_loss = 1 - 2 * (inter + eps) / (cardinality + eps)
  
      # Compute l1 loss for thresh_map
      if tf.reduce_any(thresh_mask):
          thresh_mask = tf.cast(thresh_mask, tf.float32)
          l1_loss = tf.reduce_sum(
              tf.abs(thresh_map - thresh_target) * thresh_mask
          ) / (tf.reduce_sum(thresh_mask) + eps)
      else:
          l1_loss = tf.constant(0.0)
  
      return l1_loss + focal_scale * focal_loss + dice_loss
  
imports: from copy import deepcopy
  from typing import Any, Dict, List, Optional, Tuple
  import numpy
  import tensorflow
  from tensorflow import keras
  from tensorflow.keras import layers
  from tensorflow.keras.applications import ResNet50
  from doctr.file_utils import CLASS_NAME
  from doctr.models.utils import IntermediateLayerGetter, _bf16_to_float32, conv_sequence, load_pretrained_params
  from doctr.utils.repr import NestedObject
  from ...classification import mobilenet_v3_large
  from .base import DBPostProcessor, _DBNet
label_desc: Use image node_id 2 for calling the DBNet obj's underlying member method code with example usage: obj.compute_loss(out_map, thresh_map, target, gamma, alpha, eps) and returns: unknown