1defload_mask(self, image_id):2"""Load instance masks for the given image.
3 Different datasets use different ways to store masks. This
4 function converts the different mask format to one format
5 in the form of a bitmap [height, width, instances].
6 Returns:
7 masks: A bool array of shape [height, width, instance count] with
8 one mask per instance.
9 class_ids: a 1D array of class IDs of the instance masks.
10 """11# If not a COCO image, delegate to parent class.12 image_info = self.image_info[image_id]13if image_info["source"]!="coco":14returnsuper(CocoDataset, self).load_mask(image_id)1516 instance_masks =[]17 class_ids =[]18 annotations = self.image_info[image_id]["annotations"]19# Build mask of shape [height, width, instance_count] and list20# of class IDs that correspond to each channel of the mask.21for annotation in annotations:22 class_id = self.map_source_class_id(23"coco.{}".format(annotation['category_id']))24if class_id:25 m = self.annToMask(annotation, image_info["height"],26 image_info["width"])27# Some objects are so small that they're less than 1 pixel area28# and end up rounded out. Skip those objects.29if m.max()<1:30continue31# Is it a crowd? If so, use a negative class ID.32if annotation['iscrowd']:33# Use negative class ID for crowds34 class_id *=-135# For crowd masks, annToMask() sometimes returns a mask36# smaller than the given dimensions. If so, resize it.37if m.shape[0]!= image_info["height"]or m.shape[1]!= image_info["width"]:38 m = np.ones([image_info["height"], image_info["width"]], dtype=bool)39 instance_masks.append(m)40 class_ids.append(class_id)4142# Pack instance masks into an array43if class_ids:44 mask = np.stack(instance_masks, axis=2).astype(np.bool)45 class_ids = np.array(class_ids, dtype=np.int32)46return mask, class_ids
47else:48# Call super class to return an empty mask49returnsuper(CocoDataset, self).load_mask(image_id)