class_docstr: LinkNet as described in `"LinkNet: Exploiting Encoder Representations for Efficient Semantic Segmentation"
  <https://arxiv.org/pdf/1707.03718.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
class_docstr_tok: ["LinkNet","as","described","in","`","``","LinkNet",":","Exploiting","Encoder","Representations","for","Efficient","Semantic","Segmentation","''","<","https",":","\/\/arxiv.org\/pdf\/1707.03718.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 linknet loss, BCE with boosted box edges or focal loss. Focal loss implementation based on
  <https://github.com/tensorflow/addons/>`_.
  
  Args:
  ----
      out_map: output feature map of the model of shape N x H x W x 1
      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","linknet","loss",",","BCE","with","boosted","box","edges","or","focal","loss",".","Focal","loss","implementation","based","on","<","https",":","\/\/github.com\/tensorflow\/addons\/",">","`","_",".","Args",":","--","--","out_map",":","output","feature","map","of","the","model","of","shape","N","x","H","x","W","x","1","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,
      target: List[Dict[str, np.ndarray]],
      gamma: float = 2.0,
      alpha: float = 0.5,
      eps: float = 1e-8,
  ) -> tf.Tensor:
      """Compute linknet loss, BCE with boosted box edges or focal loss. Focal loss implementation based on
      <https://github.com/tensorflow/addons/>`_.
  
      Args:
      ----
          out_map: output feature map of the model of shape N x H x W x 1
          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
      """
      seg_target, seg_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)
  
      bce_loss = tf.keras.losses.binary_crossentropy(
          seg_target[..., None], out_map[..., None], from_logits=True
      )
      proba_map = tf.sigmoid(out_map)
  
      # Focal loss
      if gamma < 0:
          raise ValueError(
              "Value of gamma should be greater than or equal to zero."
          )
      # Convert logits to prob, compute gamma factor
      p_t = (seg_target * proba_map) + (
          (1 - seg_target) * (1 - proba_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))
  
      # Dice loss
      inter = tf.math.reduce_sum(
          seg_mask * proba_map * seg_target, (0, 1, 2, 3)
      )
      cardinality = tf.math.reduce_sum(
          (proba_map + seg_target), (0, 1, 2, 3)
      )
      dice_loss = 1 - 2 * (inter + eps) / (cardinality + eps)
  
      return 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 Model, Sequential, layers
  from doctr.file_utils import CLASS_NAME
  from doctr.models.classification import resnet18, resnet34, resnet50
  from doctr.models.utils import IntermediateLayerGetter, _bf16_to_float32, conv_sequence, load_pretrained_params
  from doctr.utils.repr import NestedObject
  from .base import LinkNetPostProcessor, _LinkNet
label_desc: Use image node_id 2 for calling the LinkNet obj's underlying member method code with example usage: obj.compute_loss(out_map, target, gamma, alpha, eps) and returns: unknown