code: def make_depth_image(
      depths: torch.Tensor,
      masks: torch.Tensor,
      max_quantile: float = 0.98,
      min_quantile: float = 0.02,
      min_out_depth: float = 0.1,
      max_out_depth: float = 0.9,
  ) -> torch.Tensor:
      """
      Convert a batch of depth maps to a grayscale image.
  
      Args:
          depths: A tensor of shape `(B, 1, H, W)` containing a batch of depth maps.
          masks: A tensor of shape `(B, 1, H, W)` containing a batch of foreground masks.
          max_quantile: The quantile of the input depth values which will
              be mapped to `max_out_depth`.
          min_quantile: The quantile of the input depth values which will
              be mapped to `min_out_depth`.
          min_out_depth: The minimal value in each depth map will be assigned this color.
          max_out_depth: The maximal value in each depth map will be assigned this color.
  
      Returns:
          depth_image: A tensor of shape `(B, 1, H, W)` a batch of grayscale
              depth images.
      """
      normfacs = []
      for d, m in zip(depths, masks):
          ok = (d.view(-1) > 1e-6) * (m.view(-1) > 0.5)
          if ok.sum() <= 1:
              logger.info("empty depth!")
              normfacs.append(torch.zeros(2).type_as(depths))
              continue
          dok = d.view(-1)[ok].view(-1)
          _maxk = max(int(round((1 - max_quantile) * (dok.numel()))), 1)
          _mink = max(int(round(min_quantile * (dok.numel()))), 1)
          normfac_max = dok.topk(k=_maxk, dim=-1).values[-1]
          normfac_min = dok.topk(k=_mink, dim=-1, largest=False).values[
              -1
          ]
          normfacs.append(torch.stack([normfac_min, normfac_max]))
      normfacs = torch.stack(normfacs)
      _min, _max = (
          normfacs[:, 0].view(-1, 1, 1, 1),
          normfacs[:, 1].view(-1, 1, 1, 1),
      )
      depths = (depths - _min) / (_max - _min).clamp(1e-4)
      depths = (
          (depths * (max_out_depth - min_out_depth) + min_out_depth)
          * masks.float()
      ).clamp(0.0, 1.0)
      return depths
  
imports: import logging
  from typing import Any, Dict, Optional, Tuple, TYPE_CHECKING
  import torch
label_desc: Use image node_id 4 for calling a global function with example usage: make_depth_image(depths, masks, max_quantile, min_quantile, min_out_depth, max_out_depth) and returns: depths