text: import torch
  import torch.nn.functional as F
  
  from tkdet.layers import paste_masks_in_image
  from tkdet.structures import Instances
  from tkdet.utils.memory import retry_if_cuda_oom
  
  __all__ = ["detector_postprocess", "sem_seg_postprocess"]
  
  
  def detector_postprocess(results, output_height, output_width, mask_threshold=0.5):
      if isinstance(output_width, torch.Tensor):
          output_width_tmp = output_width.float()
      else:
          output_width_tmp = output_width
  
      if isinstance(output_height, torch.Tensor):
          output_height_tmp = output_height.float()
      else:
          output_height_tmp = output_height
  
      scale_x, scale_y = (
          output_width_tmp / results.image_size[1],
          output_height_tmp / results.image_size[0]
      )
      results = Instances((output_height, output_width), **results.get_fields())
  
      if results.has("pred_boxes"):
          output_boxes = results.pred_boxes
      elif results.has("proposal_boxes"):
          output_boxes = results.proposal_boxes
  
      output_boxes.scale(scale_x, scale_y)
      output_boxes.clip(results.image_size)
  
      results = results[output_boxes.nonempty()]
  
      if results.has("pred_masks"):
          results.pred_masks = retry_if_cuda_oom(paste_masks_in_image)(
              results.pred_masks[:, 0, :, :],
              results.pred_boxes,
              results.image_size,
              threshold=mask_threshold,
          )
  
      if results.has("pred_keypoints"):
          results.pred_keypoints[:, :, 0] *= scale_x
          results.pred_keypoints[:, :, 1] *= scale_y
  
      return results
  
  
  def sem_seg_postprocess(result, img_size, output_height, output_width):
      result = result[:, : img_size[0], : img_size[1]].expand(1, -1, -1, -1)
      result = F.interpolate(
          result,
          size=(output_height, output_width),
          mode="bilinear",
          align_corners=False
      )[0]
      return result
  