code: def cocofy_lvis(input_filename, output_filename):
      """
      Filter LVIS instance segmentation annotations to remove all categories that are not included in
      COCO. The new json files can be used to evaluate COCO AP using `lvis-api`. The category ids in
      the output json are the incontiguous COCO dataset ids.
  
      Args:
          input_filename (str): path to the LVIS json file.
          output_filename (str): path to the COCOfied json file.
      """
  
      with open(input_filename, "r") as f:
          lvis_json = json.load(f)
  
      lvis_annos = lvis_json.pop("annotations")
      cocofied_lvis = copy.deepcopy(lvis_json)
      lvis_json["annotations"] = lvis_annos
  
      # Mapping from lvis cat id to coco cat id via synset
      lvis_cat_id_to_synset = {
          cat["id"]: cat["synset"] for cat in lvis_json["categories"]
      }
      synset_to_coco_cat_id = {
          x["synset"]: x["coco_cat_id"] for x in COCO_SYNSET_CATEGORIES
      }
      # Synsets that we will keep in the dataset
      synsets_to_keep = set(synset_to_coco_cat_id.keys())
      coco_cat_id_with_instances = defaultdict(int)
  
      new_annos = []
      ann_id = 1
      for ann in lvis_annos:
          lvis_cat_id = ann["category_id"]
          synset = lvis_cat_id_to_synset[lvis_cat_id]
          if synset not in synsets_to_keep:
              continue
          coco_cat_id = synset_to_coco_cat_id[synset]
          new_ann = copy.deepcopy(ann)
          new_ann["category_id"] = coco_cat_id
          new_ann["id"] = ann_id
          ann_id += 1
          new_annos.append(new_ann)
          coco_cat_id_with_instances[coco_cat_id] += 1
      cocofied_lvis["annotations"] = new_annos
  
      for image in cocofied_lvis["images"]:
          for key in [
              "not_exhaustive_category_ids",
              "neg_category_ids",
          ]:
              new_category_list = []
              for lvis_cat_id in image[key]:
                  synset = lvis_cat_id_to_synset[lvis_cat_id]
                  if synset not in synsets_to_keep:
                      continue
                  coco_cat_id = synset_to_coco_cat_id[synset]
                  new_category_list.append(coco_cat_id)
                  coco_cat_id_with_instances[coco_cat_id] += 1
              image[key] = new_category_list
  
      coco_cat_id_with_instances = set(
          coco_cat_id_with_instances.keys()
      )
  
      new_categories = []
      for cat in lvis_json["categories"]:
          synset = cat["synset"]
          if synset not in synsets_to_keep:
              continue
          coco_cat_id = synset_to_coco_cat_id[synset]
          if coco_cat_id not in coco_cat_id_with_instances:
              continue
          new_cat = copy.deepcopy(cat)
          new_cat["id"] = coco_cat_id
          new_categories.append(new_cat)
      cocofied_lvis["categories"] = new_categories
  
      with open(output_filename, "w") as f:
          json.dump(cocofied_lvis, f)
      print(
          "{} is COCOfied and stored in {}.".format(
              input_filename, output_filename
          )
      )
  
imports: import copy
  import json
  import os
  from collections import defaultdict
label_desc: Use image node_id 1 for calling a global function with example usage: cocofy_lvis(input_filename, output_filename) without return types