def _load_instances_items(self): items = {} instances_dir = osp.join(self._annotations_dir, MapillaryVistasPath.INSTANCES_DIR) for instance_path in find_images(instances_dir, recursive=True): item_id = osp.splitext(osp.relpath(instance_path, instances_dir))[0] mask = load_image(instance_path, dtype=np.uint32) annotations = [] for uval in np.unique(mask): label_id, instance_id = uval >> 8, uval & 255 annotations.append( Mask( image=self._lazy_extract_mask(mask, uval), label=label_id, id=instance_id ) ) items[item_id] = DatasetItem(id=item_id, subset=self._subset, annotations=annotations) class_dir = osp.join(self._annotations_dir, MapillaryVistasPath.CLASS_DIR) for class_path in find_images(class_dir, recursive=True): item_id = osp.splitext(osp.relpath(class_path, class_dir))[0] if item_id in items: continue from PIL import Image as PILImage class_mask = np.array(PILImage.open(class_path)) classes = np.unique(class_mask) annotations = [] for label_id in classes: annotations.append(Mask(label=label_id, image=self._lazy_extract_mask(class_mask, label_id)) ) items[item_id] = DatasetItem(id=item_id, subset=self._subset, annotations=annotations) for image_path in find_images(self._images_dir, recursive=True): item_id = osp.splitext(osp.relpath(image_path, self._images_dir))[0] image = Image(path=image_path) if item_id in items: items[item_id].image = image else: items[item_id] = DatasetItem(id=item_id, subset=self._subset, image=image) self._load_polygons(items) return items.values()
def __call__( self, tfds_example: Any, item: DatasetItem, state: namespace, ) -> None: if self.filename_feature_name: filename = tfds_example[self.filename_feature_name].numpy() \ .decode('UTF-8') else: filename = None item.image = ByteImage(data=tfds_example[self.feature_name].numpy(), path=filename)