text: import os
  import numpy as np
  import cv2
  import matplotlib.pyplot as plt
  from mrcnn.config import Config
  #import utils
  from mrcnn import model as modellib
  from mrcnn import utils
  from mrcnn import visualize
  import yaml
  from mrcnn.model import log
  from PIL import Image
  ROOT_DIR = os.path.abspath("..")
  print(ROOT_DIR)
  # Directory to save logs and trained model
  MODEL_DIR = os.path.join(ROOT_DIR, "logs")
  print(MODEL_DIR)
  iter_num=0
  
  # Local path to trained weights file
  COCO_MODEL_PATH = os.path.join(ROOT_DIR, "2018.11.21.h5")
  # Download COCO trained weights from Releases if needed
  if not os.path.exists(COCO_MODEL_PATH):
      utils.download_trained_weights(COCO_MODEL_PATH)
  class ShapesConfig(Config):
      """Configuration for training on the toy shapes dataset.
      Derives from the base Config class and overrides values specific
      to the toy shapes dataset.
      """
      # Give the configuration a recognizable name
      NAME = "shape"
  
      # Train on 1 GPU and 8 images per GPU. We can put multiple images on each
      # GPU because the images are small. Batch size is 8 (GPUs * images/GPU).
      GPU_COUNT =1
      IMAGES_PER_GPU = 8
  
      # Number of classes (including background)
      NUM_CLASSES = 1 + 4  # background + 6 shapes
  
      # Use small images for faster training. Set the limits of the small side
      # the large side, and that determines the image shape.
      IMAGE_MIN_DIM = 320
      IMAGE_MAX_DIM = 384
  
      # Use smaller anchors because our image and objects are small
      RPN_ANCHOR_SCALES = (8 * 6, 16 * 6, 32 * 6, 64 * 6, 128 * 6)  # anchor side in pixels
  
      # Reduce training ROIs per image because the images are small and have
      # few objects. Aim to allow ROI sampling to pick 33% positive ROIs.
      TRAIN_ROIS_PER_IMAGE = 100
  
      # Use a small epoch since the data is simple
      STEPS_PER_EPOCH = 100
  
      # use small validation steps since the epoch is small
      VALIDATION_STEPS = 50
  
  
  config = ShapesConfig()
  # config.display()
  class DrugDataset(utils.Dataset):
      # 得到该图中有多少个实例（物体）
      def get_obj_index(self, image):
          n = np.max(image)
          return n
  
      # 解析labelme中得到的yaml文件，从而得到mask每一层对应的实例标签
      def from_yaml_get_class(self, image_id):
          info = self.image_info[image_id]
          with open(info['yaml_path']) as f:
              temp = yaml.load(f.read())
              labels = temp['label_names']
              del labels[0]
          return labels
  
      # 重新写draw_mask
      def draw_mask(self, num_obj, mask, image,image_id):
          #print("draw_mask-->",image_id)
          #print("self.image_info",self.image_info)
          info = self.image_info[image_id]
          #print("info-->",info)
          #print("info[width]----->",info['width'],"-info[height]--->",info['height'])
          for index in range(num_obj):
              for i in range(info['width']):
                  for j in range(info['height']):
                      #print("image_id-->",image_id,"-i--->",i,"-j--->",j)
                      #print("info[width]----->",info['width'],"-info[height]--->",info['height'])
                      at_pixel = image.getpixel((i, j))
                      if at_pixel == index + 1:
                          mask[j, i, index] = 1
          return mask
  
      # 重新写load_shapes，里面包含自己的类别,可以任意添加
      # 并在self.image_info信息中添加了path、mask_path 、yaml_path
      # yaml_pathdataset_root_path = "/tongue_dateset/"
      # img_floder = dataset_root_path + "rgb"
      # mask_floder = dataset_root_path + "mask"
      # dataset_root_path = "/tongue_dateset/"
      def load_shapes(self, count, img_floder, mask_floder, imglist, dataset_root_path):
          """Generate the requested number of synthetic images.
          count: number of images to generate.
          height, width: the size of the generated images.
          """
          # Add classes,可通过这种方式扩展多个物体
          self.add_class("shapes", 1, "edge")
          self.add_class("shapes", 2, "quilt")
          self.add_class("shapes", 3, "corner")
          self.add_class("shapes", 4, "head")
  
          for i in range(count):
              # 获取图片宽和高
  
              filestr = imglist[i].split(".")[0]
              #print(imglist[i],"-->",cv_img.shape[1],"--->",cv_img.shape[0])
              #print("id-->", i, " imglist[", i, "]-->", imglist[i],"filestr-->",filestr)
              #filestr = filestr.split("_")[1]
              mask_path = mask_floder + "/" + filestr + ".png"
              yaml_path = dataset_root_path + "labelme_json/rgb_" + filestr + "_json/info.yaml"
              # print(dataset_root_path + "labelme_json/" + filestr + "_json/img.png")
              cv_img = cv2.imread(dataset_root_path + "pic/" + filestr + ".png")
  
              self.add_image("shapes", image_id=i, path=img_floder + "/" + imglist[i],
                             width=cv_img.shape[1], height=cv_img.shape[0], mask_path=mask_path, yaml_path=yaml_path)
  
      # 重写load_mask
      def load_mask(self, image_id):
          """Generate instance masks for shapes of the given image ID.
          """
          global iter_num
          print("image_id",image_id)
          info = self.image_info[image_id]
          count = 1  # number of object
          img = Image.open(info['mask_path'])
          num_obj = self.get_obj_index(img)
          mask = np.zeros([info['height'], info['width'], num_obj], dtype=np.uint8)
          mask = self.draw_mask(num_obj, mask, img,image_id)
          occlusion = np.logical_not(mask[:, :, -1]).astype(np.uint8)
          for i in range(count - 2, -1, -1):
              mask[:, :, i] = mask[:, :, i] * occlusion
  
              occlusion = np.logical_and(occlusion, np.logical_not(mask[:, :, i]))
          labels = []
          labels = self.from_yaml_get_class(image_id)
          labels_form = []
          for i in range(len(labels)):
              if labels[i].find("corner") != -1:
                  labels_form.append("corner")
              elif labels[i].find("quilt") != -1:
                  labels_form.append("quilt")
              elif labels[i].find("edge") != -1:
                  labels_form.append("edge")
              elif labels[i].find("head") != -1:
                  labels_form.append("head")
  
          class_ids = np.array([self.class_names.index(s) for s in labels_form])
          return mask, class_ids.astype(np.int32)
  
  dataset_root_path=ROOT_DIR+"/new_data/"
  img_floder = dataset_root_path + "pic"
  # print(img_floder)
  mask_floder = dataset_root_path + "cv2_mask"
  #yaml_floder = dataset_root_path
  imglist = os.listdir(img_floder)
  count = len(imglist)
  print(imglist)
  print(count)
  
  #train与val数据集准备
  dataset_train = DrugDataset()
  dataset_train.load_shapes(count, img_floder, mask_floder, imglist, dataset_root_path)
  dataset_train.prepare()
  dataset_val = DrugDataset()
  dataset_val.load_shapes(10, img_floder, mask_floder, imglist, dataset_root_path)
  dataset_val.prepare()
  print("dataset_val-->",dataset_val._image_ids)
  model = modellib.MaskRCNN(mode="training", config=config,
                            model_dir=MODEL_DIR)
  print(COCO_MODEL_PATH)
  init_with = "coco"  # imagenet, coco, or last
  
  
  
  if init_with == "imagenet":
      model.load_weights(model.get_imagenet_weights(), by_name=True)
  elif init_with == "coco":
      # Load weights trained on MS COCO, but skip layers that
      # are different due to the different number of classes
      # See README for instructions to download the COCO weights
      model.load_weights(COCO_MODEL_PATH, by_name=True,
                         exclude=["mrcnn_class_logits", "mrcnn_bbox_fc",
                                  "mrcnn_bbox", "mrcnn_mask"])
  elif init_with == "last":
      # Load the last model you trained and continue training
      model.load_weights(model.find_last()[1], by_name=True)
  
  model.train(dataset_train, dataset_val,
              learning_rate=config.LEARNING_RATE,
              epochs=80,
              layers='all')