code: def pyramid_roi_align(
      feature_maps, rois, pool_size, pyramid_levels, dim
  ):
      """
      Implements ROI Pooling on multiple levels of the feature pyramid.
      :param feature_maps: list of feature maps, each of shape (b, c, y, x , (z))
      :param rois: proposals (normalized coords.) as returned by RPN. contain info about original batch element allocation.
      (n_proposals, (y1, x1, y2, x2, (z1), (z2), batch_ixs)
      :param pool_size: list of poolsizes in dims: [x, y, (z)]
      :param pyramid_levels: list. [0, 1, 2, ...]
      :return: pooled: pooled feature map rois (n_proposals, c, poolsize_y, poolsize_x, (poolsize_z))
  
      Output:
      Pooled regions in the shape: [num_boxes, height, width, channels].
      The width and height are those specific in the pool_shape in the layer
      constructor.
      """
      boxes = rois[:, : dim * 2]
      batch_ixs = rois[:, dim * 2]
  
      # Assign each ROI to a level in the pyramid based on the ROI area.
      if dim == 2:
          y1, x1, y2, x2 = boxes.chunk(4, dim=1)
      else:
          y1, x1, y2, x2, z1, z2 = boxes.chunk(6, dim=1)
  
      h = y2 - y1
      w = x2 - x1
  
      # Equation 1 in https://arxiv.org/abs/1612.03144. Account for
      # the fact that our coordinates are normalized here.
      # divide sqrt(h*w) by 1 instead image_area.
      roi_level = (
          (4 + mutils.log2(torch.sqrt(h * w)))
          .round()
          .int()
          .clamp(pyramid_levels[0], pyramid_levels[-1])
      )
      # if Pyramid contains additional level P6, adapt the roi_level assignemnt accordingly.
      if len(pyramid_levels) == 5:
          roi_level[h * w > 0.65] = 5
  
      # Loop through levels and apply ROI pooling to each.
      pooled = []
      box_to_level = []
      for level_ix, level in enumerate(pyramid_levels):
          ix = roi_level == level
          if not ix.any():
              continue
          ix = torch.nonzero(ix)[:, 0]
          level_boxes = boxes[ix, :]
          # re-assign rois to feature map of original batch element.
          ind = batch_ixs[ix].int()
  
          # Keep track of which box is mapped to which level
          box_to_level.append(ix)
  
          # Stop gradient propogation to ROI proposals
          level_boxes = level_boxes.detach()
  
          # Crop and Resize
          # From Mask R-CNN paper: "We sample four regular locations, so
          # that we can evaluate either max or average pooling. In fact,
          # interpolating only a single value at each bin center (without
          # pooling) is nearly as effective."
          #
          # Here we use the simplified approach of a single value per bin,
          # which is how is done in tf.crop_and_resize()
          #
          # Also fixed a bug from original implementation, reported in:
          # https://hackernoon.com/how-tensorflows-tf-image-resize-stole-60-days-of-my-life-aba5eb093f35
  
          if len(pool_size) == 2:
              pooled_features = ra2D(pool_size[0], pool_size[1], 0)(
                  feature_maps[level_ix], level_boxes, ind
              )
          else:
              pooled_features = ra3D(
                  pool_size[0], pool_size[1], pool_size[2], 0
              )(feature_maps[level_ix], level_boxes, ind)
  
          pooled.append(pooled_features)
  
      # Pack pooled features into one tensor
      pooled = torch.cat(pooled, dim=0)
  
      # Pack box_to_level mapping into one array and add another
      # column representing the order of pooled boxes
      box_to_level = torch.cat(box_to_level, dim=0)
  
      # Rearrange pooled features to match the order of the original boxes
      _, box_to_level = torch.sort(box_to_level)
      pooled = pooled[box_to_level, :, :]
  
      return pooled
  
imports: import utils.model_utils
  import utils.exp_utils
  from cuda_functions.nms_2D.pth_nms import nms_gpu
  from cuda_functions.nms_3D.pth_nms import nms_gpu
  from cuda_functions.roi_align_2D.roi_align.crop_and_resize import CropAndResizeFunction
  from cuda_functions.roi_align_3D.roi_align.crop_and_resize import CropAndResizeFunction
  import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  import torch.utils
label_desc: Use image node_id 7 for calling a global function with example usage: pyramid_roi_align(feature_maps, rois, pool_size, pyramid_levels, dim) and returns: pooled