class_docstr: The MODNet implementation based on PaddlePaddle.
  
  The original article refers to
  Zhanghan Ke, et, al. "Is a Green Screen Really Necessary for Real-Time Portrait Matting?"
  (https://arxiv.org/pdf/2011.11961.pdf).
  
  Args:
      backbone: backbone model.
      hr(int, optional): The channels of high resolutions branch. Defautl: None.
      pretrained(str, optional): The path of pretrianed model. Defautl: None.
class_docstr_tok: ["The","MODNet","implementation","based","on","PaddlePaddle",".","The","original","article","refers","to","Zhanghan","Ke",",","et",",","al",".","``","Is","a","Green","Screen","Really","Necessary","for","Real-Time","Portrait","Matting","?","''","(","https",":","\/\/arxiv.org\/pdf\/2011.11961.pdf",")",".","Args",":","backbone",":","backbone","model",".","hr","(","int",",","optional",")",":","The","channels","of","high","resolutions","branch",".","Defautl",":","None",".","pretrained","(","str",",","optional",")",":","The","path","of","pretrianed","model",".","Defautl",":","None","."]
code: def loss(self, logit_dict, label_dict, loss_func_dict=None):
      if loss_func_dict is None:
          if self.loss_func_dict is None:
              self.loss_func_dict = defaultdict(list)
              self.loss_func_dict["semantic"].append(
                  paddleseg.models.MSELoss()
              )
              self.loss_func_dict["detail"].append(
                  paddleseg.models.L1Loss()
              )
              self.loss_func_dict["fusion"].append(
                  paddleseg.models.L1Loss()
              )
              self.loss_func_dict["fusion"].append(
                  paddleseg.models.L1Loss()
              )
      else:
          self.loss_func_dict = loss_func_dict
  
      loss = {}
      # semantic loss
      semantic_gt = F.interpolate(
          label_dict["alpha"],
          scale_factor=1 / 16,
          mode="bilinear",
          align_corners=False,
      )
      semantic_gt = self.blurer(semantic_gt)
      #         semantic_gt.stop_gradient=True
      loss["semantic"] = self.loss_func_dict["semantic"][0](
          logit_dict["semantic"], semantic_gt
      )
  
      # detail loss
      trimap = label_dict["trimap"]
      mask = (trimap == 128).astype("float32")
      logit_detail = logit_dict["detail"] * mask
      label_detail = label_dict["alpha"] * mask
      loss_detail = self.loss_func_dict["detail"][0](
          logit_detail, label_detail
      )
      loss_detail = loss_detail / (mask.mean() + 1e-6)
      loss["detail"] = 10 * loss_detail
  
      # fusion loss
      matte = logit_dict["matte"]
      alpha = label_dict["alpha"]
      transition_mask = label_dict["trimap"] == 128
      matte_boundary = paddle.where(transition_mask, matte, alpha)
      # l1 loss
      loss_fusion_l1 = self.loss_func_dict["fusion"][0](
          matte, alpha
      ) + 4 * self.loss_func_dict["fusion"][0](matte_boundary, alpha)
      # composition loss
      loss_fusion_comp = self.loss_func_dict["fusion"][1](
          matte * label_dict["img"], alpha * label_dict["img"]
      ) + 4 * self.loss_func_dict["fusion"][1](
          matte_boundary * label_dict["img"], alpha * label_dict["img"]
      )
      # consisten loss with semantic
      transition_mask = F.interpolate(
          label_dict["trimap"],
          scale_factor=1 / 16,
          mode="nearest",
          align_corners=False,
      )
      transition_mask = transition_mask == 128
      matte_con_sem = F.interpolate(
          matte,
          scale_factor=1 / 16,
          mode="bilinear",
          align_corners=False,
      )
      matte_con_sem = self.blurer(matte_con_sem)
      logit_semantic = logit_dict["semantic"].clone()
      logit_semantic.stop_gradient = True
      matte_con_sem = paddle.where(
          transition_mask, logit_semantic, matte_con_sem
      )
      if False:
          import cv2
  
          matte_con_sem_num = matte_con_sem.numpy()
          matte_con_sem_num = matte_con_sem_num[0].squeeze()
          matte_con_sem_num = (matte_con_sem_num * 255).astype("uint8")
          semantic = logit_dict["semantic"].numpy()
          semantic = semantic[0].squeeze()
          semantic = (semantic * 255).astype("uint8")
          transition_mask = transition_mask.astype("uint8")
          transition_mask = transition_mask.numpy()
          transition_mask = (transition_mask[0].squeeze()) * 255
          cv2.imwrite("matte_con.png", matte_con_sem_num)
          cv2.imwrite("semantic.png", semantic)
          cv2.imwrite("transition.png", transition_mask)
      mse_loss = paddleseg.models.MSELoss()
      loss_fusion_con_sem = mse_loss(
          matte_con_sem, logit_dict["semantic"]
      )
      loss_fusion = (
          loss_fusion_l1 + loss_fusion_comp + loss_fusion_con_sem
      )
      loss["fusion"] = loss_fusion
      loss["fusion_l1"] = loss_fusion_l1
      loss["fusion_comp"] = loss_fusion_comp
      loss["fusion_con_sem"] = loss_fusion_con_sem
  
      loss["all"] = loss["semantic"] + loss["detail"] + loss["fusion"]
  
      return loss
  
imports: from collections import defaultdict
  import paddle
  import paddle.nn
  import paddle.nn.functional
  import numpy
  import scipy
  import paddleseg
  from paddleseg.models import layers, losses
  from paddleseg import utils
  from paddleseg.cvlibs import manager, param_init
label_desc: Use image node_id 3 for calling the MODNet obj's underlying member method code with example usage: obj.loss(logit_dict, label_dict, loss_func_dict) and returns: loss