code: def roi_align_common(
      a_np,
      b_np,
      rois_np,
      channel,
      pooled_size_h,
      pooled_size_w,
      spatial_scale,
      sample_ratio,
      avg_mode,
      max_mode,
      height,
      width,
      layout,
  ):
      """Common code used by roi align NCHW and NHWC"""
      num_roi = rois_np.shape[0]
  
      for i in range(num_roi):
          roi = rois_np[i]
          batch_index = int(roi[0])
          roi_start_w, roi_start_h, roi_end_w, roi_end_h = (
              roi[1:] * spatial_scale
          )
          roi_h = max(roi_end_h - roi_start_h, 1.0)
          roi_w = max(roi_end_w - roi_start_w, 1.0)
  
          bin_h = roi_h / pooled_size_h
          bin_w = roi_w / pooled_size_w
  
          if sample_ratio > 0:
              roi_bin_grid_h = roi_bin_grid_w = int(sample_ratio)
          else:
              roi_bin_grid_h = int(math.ceil(roi_h / pooled_size_h))
              roi_bin_grid_w = int(math.ceil(roi_w / pooled_size_w))
  
          count = roi_bin_grid_h * roi_bin_grid_w
  
          for c in range(channel):
              for ph in range(pooled_size_h):
                  for pw in range(pooled_size_w):
                      if avg_mode:
                          total = 0.0
                      if max_mode:
                          total = float("-inf")
                      for iy in range(roi_bin_grid_h):
                          for ix in range(roi_bin_grid_w):
                              y = (
                                  roi_start_h
                                  + ph * bin_h
                                  + (iy + 0.5) * bin_h / roi_bin_grid_h
                              )
                              x = (
                                  roi_start_w
                                  + pw * bin_w
                                  + (ix + 0.5) * bin_w / roi_bin_grid_w
                              )
                              if avg_mode:
                                  total += (
                                      _bilinear(
                                          a_np,
                                          batch_index,
                                          c,
                                          y,
                                          x,
                                          height,
                                          width,
                                          layout,
                                      )
                                      / count
                                  )
                              if max_mode:
                                  total = max(
                                      total,
                                      _bilinear(
                                          a_np,
                                          batch_index,
                                          c,
                                          y,
                                          x,
                                          height,
                                          width,
                                          layout,
                                      ),
                                  )
  
                      if layout == "NCHW":
                          b_np[i, c, ph, pw] = total
                      else:
                          b_np[i, ph, pw, c] = total
      return b_np
  
imports: import math
  import numpy
label_desc: Use image node_id 2 for calling a global function with example usage: roi_align_common(a_np, b_np, rois_np, channel, pooled_size_h, pooled_size_w, spatial_scale, sample_ratio, avg_mode, max_mode, height, width, layout) and returns: b_np