code: def convert_to_coco_gt(
      data,
      outpath,
      caption_key,
      sample_id_key,
      split,
      load_gt_from_file=False,
      img_ids=[],
  ):
      gt_data = {"annotations": [], "images": []}
      if load_gt_from_file:
          print(
              f"Generating ground truth file for evaluation from {load_gt_from_file}...."
          )
          data = load_gt_file(load_gt_from_file)
          for ann in data:
              captions = ann[caption_key]
              img_id = (
                  int(ann[sample_id_key])
                  if is_convertible_to_int(ann[sample_id_key])
                  else ann[sample_id_key]
              )
              if (
                  img_ids and img_id not in img_ids
              ):  # only include specified img_ids if specified
                  continue
              gt_data["images"].append({"id": img_id})
              if isinstance(captions, str):
                  gt_data["annotations"].append(
                      {
                          "image_id": img_id,
                          "caption": captions,
                          "id": img_id,
                      }
                  )
              else:
                  gt_data["annotations"].extend(
                      [
                          {
                              "image_id": img_id,
                              "caption": c,
                              "id": img_id,
                          }
                          for c in captions
                      ]
                  )
      else:
          print(f"Generating ground truth file for evaluation....")
          for i, ann in tqdm(enumerate(data[split])):
              captions = data[split].annotation[i][caption_key]
              img_id = (
                  int(ann[sample_id_key])
                  if is_convertible_to_int(ann[sample_id_key])
                  else ann[sample_id_key]
              )
              if (
                  img_ids and img_id not in img_ids
              ):  # only include specified img_ids if specified
                  continue
              gt_data["images"].append({"id": img_id})
              if isinstance(captions, str):
                  gt_data["annotations"].append(
                      {
                          "image_id": img_id,
                          "caption": captions,
                          "id": img_id,
                      }
                  )
              else:
                  gt_data["annotations"].extend(
                      [
                          {
                              "image_id": img_id,
                              "caption": c,
                              "id": img_id,
                          }
                          for c in captions
                      ]
                  )
      json.dump(gt_data, open(outpath, "w"))
      print(f"Saved annotations at {outpath}")
  
imports: import json
  import os
  import pandas
  from tqdm import tqdm
  from lavis.common.dist_utils import main_process, get_rank
  from lavis.common.registry import registry
  from lavis.tasks.base_task import BaseTask
  from lavis.common.utils import is_convertible_to_int, is_url, cache_url
  from pycocoevalcap.eval import COCOEvalCap
  from pycocotools.coco import COCO
  from torchvision.datasets.utils import download_url
label_desc: Use image node_id 2 for calling a global function with example usage: convert_to_coco_gt(data, outpath, caption_key, sample_id_key, split, load_gt_from_file, img_ids) without return types