class_docstr: refer: https://github.com/xuebinqin/DIS/blob/main/IS-Net/basics.py
class_docstr_tok: ["refer",":","https",":","\/\/github.com\/xuebinqin\/DIS\/blob\/main\/IS-Net\/basics.py"]
code: def update(self, pred: torch.Tensor, gt: torch.Tensor):
      batch_size, c, h, w = gt.shape
      assert (
          batch_size == 1
      ), f"validation mode batch_size must be 1, but got batch_size: {batch_size}."
      resize_pred = F.interpolate(
          pred, (h, w), mode="bilinear", align_corners=False
      )
      gt_num = torch.sum(torch.gt(gt, self.threshold).float())
  
      pp = resize_pred[torch.gt(gt, self.threshold)]  # 对应预测map中GT为前景的区域
      nn = resize_pred[torch.le(gt, self.threshold)]  # 对应预测map中GT为背景的区域
  
      pp_hist = torch.histc(pp, bins=255, min=0.0, max=1.0)
      nn_hist = torch.histc(nn, bins=255, min=0.0, max=1.0)
  
      # Sort according to the prediction probability from large to small
      pp_hist_flip = torch.flipud(pp_hist)
      nn_hist_flip = torch.flipud(nn_hist)
  
      pp_hist_flip_cum = torch.cumsum(pp_hist_flip, dim=0)
      nn_hist_flip_cum = torch.cumsum(nn_hist_flip, dim=0)
  
      precision = pp_hist_flip_cum / (
          pp_hist_flip_cum + nn_hist_flip_cum + 1e-4
      )
      recall = pp_hist_flip_cum / (gt_num + 1e-4)
  
      if self.precision_cum is None:
          self.precision_cum = torch.full_like(
              precision, fill_value=0.0
          )
  
      if self.recall_cum is None:
          self.recall_cum = torch.full_like(recall, fill_value=0.0)
  
      if self.num_cum is None:
          self.num_cum = torch.zeros(
              [1], dtype=gt.dtype, device=gt.device
          )
  
      self.precision_cum += precision
      self.recall_cum += recall
      self.num_cum += batch_size
  
imports: from collections import defaultdict, deque
  import datetime
  import time
  import torch
  import torch.distributed
  import torch.nn.functional
  import errno
  import os
label_desc: Use image node_id 2 for calling the F1Score obj's underlying member method code with example usage: obj.update(pred, gt) without return types