code: def __init__(
      self,
      voc_root,
      year="2012",
      txt_name: str = "train.txt",
      transforms=None,
  ):
      super().__init__()
      if isinstance(year, int):
          year = str(year)
      assert year in [
          "2007",
          "2012",
      ], "year must be in ['2007', '2012']"
      if "VOCdevkit" in voc_root:
          root = os.path.join(voc_root, f"VOC{year}")
      else:
          root = os.path.join(voc_root, "VOCdevkit", f"VOC{year}")
      assert os.path.exists(root), "path '{}' does not exist.".format(
          root
      )
      image_dir = os.path.join(root, "JPEGImages")
      xml_dir = os.path.join(root, "Annotations")
      mask_dir = os.path.join(root, "SegmentationObject")
  
      txt_path = os.path.join(
          root, "ImageSets", "Segmentation", txt_name
      )
      assert os.path.exists(
          txt_path
      ), "file '{}' does not exist.".format(txt_path)
      with open(os.path.join(txt_path), "r") as f:
          file_names = [
              x.strip() for x in f.readlines() if len(x.strip()) > 0
          ]
  
      # read class_indict
      json_file = "pascal_voc_indices.json"
      assert os.path.exists(json_file), "{} file not exist.".format(
          json_file
      )
      with open(json_file, "r") as f:
          idx2classes = json.load(f)
          self.class_dict = dict(
              [(v, k) for k, v in idx2classes.items()]
          )
  
      self.images_path = []  # 存储图片路径
      self.xmls_path = []  # 存储xml文件路径
      self.xmls_info = []  # 存储解析的xml字典文件
      self.masks_path = []  # 存储SegmentationObject图片路径
      self.objects_bboxes = []  # 存储解析的目标boxes等信息
      self.masks = []  # 存储读取的SegmentationObject图片信息
  
      # 检查图片、xml文件以及mask是否都在
      images_path = [
          os.path.join(image_dir, x + ".jpg") for x in file_names
      ]
      xmls_path = [
          os.path.join(xml_dir, x + ".xml") for x in file_names
      ]
      masks_path = [
          os.path.join(mask_dir, x + ".png") for x in file_names
      ]
      for idx, (img_path, xml_path, mask_path) in enumerate(
          zip(images_path, xmls_path, masks_path)
      ):
          assert os.path.exists(img_path), f"not find {img_path}"
          assert os.path.exists(xml_path), f"not find {xml_path}"
          assert os.path.exists(mask_path), f"not find {mask_path}"
  
          # 解析xml中bbox信息
          with open(xml_path) as fid:
              xml_str = fid.read()
          xml = etree.fromstring(xml_str)
          obs_dict = parse_xml_to_dict(xml)["annotation"]  # 将xml文件解析成字典
          obs_bboxes = parse_objects(
              obs_dict, xml_path, self.class_dict, idx
          )  # 解析出目标信息
          num_objs = obs_bboxes["boxes"].shape[0]
  
          # 读取SegmentationObject并检查是否和bboxes信息数量一致
          instances_mask = Image.open(mask_path)
          instances_mask = np.array(instances_mask)
          instances_mask[
              instances_mask == 255
          ] = 0  # 255为背景或者忽略掉的地方，这里为了方便直接设置为背景(0)
  
          # 需要检查一下标注的bbox个数是否和instances个数一致
          num_instances = instances_mask.max()
          if num_objs != num_instances:
              print(
                  f"warning: num_boxes:{num_objs} and num_instances:{num_instances} do not correspond. "
                  f"skip image:{img_path}"
              )
              continue
  
          self.images_path.append(img_path)
          self.xmls_path.append(xml_path)
          self.xmls_info.append(obs_dict)
          self.masks_path.append(mask_path)
          self.objects_bboxes.append(obs_bboxes)
          self.masks.append(instances_mask)
  
      self.transforms = transforms
      self.coco = convert_to_coco_api(self)
  
file_path: deep-learning-for-image-processing/pytorch_object_detection/mask_rcnn/my_dataset_voc.py
imports: import os
  import json
  from lxml import etree
  import numpy
  from PIL import Image
  import torch
  from torch.utils.data import Dataset
  from train_utils import convert_to_coco_api
label_desc: Use image node_id 1 to create a new VOCInstances object from inherited base classes: Dataset with example: obj = VOCInstances(voc_root, year, txt_name, transforms)