code: def extract_region_feature(
      self,
      region_feature_map,
      region_masks,
      original_dtype,
      return_dtype,
  ):
      all_region_features = []
      assert len(region_feature_map) == len(region_masks)
      for region_feature_map_i, region_masks_list_i in zip(
          region_feature_map, region_masks
      ):
          if len(region_masks_list_i) == 0:
              all_region_features.append(None)
          else:
              # (w, h)
              ori_image_wh = torch.tensor(
                  [
                      region_masks_list_i[0].shape[0],
                      region_masks_list_i[0].shape[1],
                  ],
                  device=region_masks_list_i[0].device,
              )[
                  None,
              ]
              # list of elements of shape [num_sample_point, 2]
              non_zero_pos = [
                  rand_sample(
                      (m.nonzero() / ori_image_wh),
                      self.get_model().max_sample_point,
                  )
                  for m in region_masks_list_i
              ]
              # [num_mask, num_sample_point(padded), 2]
              non_zero_pos = nn.utils.rnn.pad_sequence(
                  non_zero_pos, padding_value=-1, batch_first=True
              )
              non_zero_pos_mask = ~(non_zero_pos.sum(dim=-1) < 0)
              # [HxW, C] -> [H, W, C] -> [C, H, W] -> [N, C, H, W]
              h = w = int(math.sqrt(region_feature_map_i.shape[0]))
              c = region_feature_map_i.shape[-1]
              dup_region_feature_map_i = region_feature_map_i.reshape(
                  h, w, c
              ).permute(2, 0, 1)
              dup_region_feature_map_i = (
                  dup_region_feature_map_i.unsqueeze(0).repeat(
                      non_zero_pos.shape[0], 1, 1, 1
                  )
              )
              # [num_mask, C, H, W] x [num_mask, num_sample_point(padded), 2] -> [num_mask, C, num_sample_point(padded)]
              # F.grid_sample doesn't support BF16. Need to tranform into float32 then transform back.
              dup_region_feature_map_i_ori_type = (
                  dup_region_feature_map_i.to(original_dtype)
              )
              # pdb.set_trace()
              region_feature_i = point_sample(
                  dup_region_feature_map_i_ori_type,
                  non_zero_pos.flip(dims=(2,)).type(original_dtype),
                  return_dtype,
                  align_corners=True,
              )
              region_feature_i = region_feature_i.to(
                  dup_region_feature_map_i.dtype
              )
              # [num_mask, C]
              region_feature_i = torch.stack(
                  [
                      x[m].mean(dim=0)
                      for x, m in zip(
                          region_feature_i.transpose(1, 2),
                          non_zero_pos_mask,
                      )
                  ]
              ).nan_to_num()
              all_region_features.append(region_feature_i)
  
      return all_region_features
  
imports: from abc import ABC, abstractmethod
  import torch
  import torch.nn
  import torch.nn.functional
  import math
  from .multimodal_encoder.builder import build_vision_tower
  import pdb
  from ferret.constants import IGNORE_INDEX, IMAGE_TOKEN_INDEX, DEFAULT_IMAGE_PATCH_TOKEN, DEFAULT_IM_START_TOKEN, DEFAULT_IM_END_TOKEN
label_desc: Use image node_id 4 for calling the FERRETMetaForCausalLM obj's underlying member method code with example usage: obj.extract_region_feature(region_feature_map, region_masks, original_dtype, return_dtype) and returns: all_region_features