class_docstr: `MS Coco Detection <https://cocodataset.org/>`_ Dataset.
  
  Args:
      root (string): Root directory where images are downloaded to.
      dataset (string): train or val.
      transforms (callable, optional): A function/transform that takes input sample and its target as entry
          and returns a transformed version.
class_docstr_tok: ["`","MS","Coco","Detection","<","https",":","\/\/cocodataset.org\/",">","`","_","Dataset",".","Args",":","root","(","string",")",":","Root","directory","where","images","are","downloaded","to",".","dataset","(","string",")",":","train","or","val",".","transforms","(","callable",",","optional",")",":","A","function\/transform","that","takes","input","sample","and","its","target","as","entry","and","returns","a","transformed","version","."]
code: def __init__(
      self, root, dataset="train", transforms=None, years="2017"
  ):
      super(CocoDetection, self).__init__()
      assert dataset in [
          "train",
          "val",
      ], 'dataset must be in ["train", "val"]'
      anno_file = f"instances_{dataset}{years}.json"
      assert os.path.exists(root), "file '{}' does not exist.".format(
          root
      )
      self.img_root = os.path.join(root, f"{dataset}{years}")
      assert os.path.exists(
          self.img_root
      ), "path '{}' does not exist.".format(self.img_root)
      self.anno_path = os.path.join(root, "annotations", anno_file)
      assert os.path.exists(
          self.anno_path
      ), "file '{}' does not exist.".format(self.anno_path)
  
      self.mode = dataset
      self.transforms = transforms
      self.coco = COCO(self.anno_path)
  
      # 获取coco数据索引与类别名称的关系
      # 注意在object80中的索引并不是连续的，虽然只有80个类别，但索引还是按照stuff91来排序的
      data_classes = dict(
          [(v["id"], v["name"]) for k, v in self.coco.cats.items()]
      )
      max_index = max(data_classes.keys())  # 90
      # 将缺失的类别名称设置成N/A
      coco_classes = {}
      for k in range(1, max_index + 1):
          if k in data_classes:
              coco_classes[k] = data_classes[k]
          else:
              coco_classes[k] = "N/A"
  
      if dataset == "train":
          json_str = json.dumps(coco_classes, indent=4)
          with open("coco91_indices.json", "w") as f:
              f.write(json_str)
  
      self.coco_classes = coco_classes
  
      ids = list(sorted(self.coco.imgs.keys()))
      if dataset == "train":
          # 移除没有目标，或者目标面积非常小的数据
          valid_ids = coco_remove_images_without_annotations(
              self.coco, ids
          )
          self.ids = valid_ids
      else:
          self.ids = ids
  
file_path: deep-learning-for-image-processing/pytorch_object_detection/mask_rcnn/my_dataset_coco.py
imports: import os
  import json
  import torch
  from PIL import Image
  import torch.utils.data
  from pycocotools.coco import COCO
  from train_utils import coco_remove_images_without_annotations, convert_coco_poly_mask
label_desc: Use image node_id 1 to create a new CocoDetection object from inherited base classes: data with example: obj = CocoDetection(root, dataset, transforms, years)