Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class CustomDataset(utils.Dataset):
- def load_custom(self, dataset_dir, subset):
- """Load a subset of the Balloon dataset.
- dataset_dir: Root directory of the dataset.
- subset: Subset to load: train or val
- """
- # Add classes. We have only one class to add.
- self.add_class("damage", 1, "damage")
- # Train or validation dataset?
- assert subset in ["train", "val"]
- dataset_dir = os.path.join(dataset_dir, subset)
- # Load annotations
- # VGG Image Annotator saves each image in the form:
- # { 'filename': '28503151_5b5b7ec140_b.jpg',
- # 'regions': {
- # '0': {
- # 'region_attributes': {},
- # 'shape_attributes': {
- # 'all_points_x': [...],
- # 'all_points_y': [...],
- # 'name': 'polygon'}},
- # ... more regions ...
- # },
- # 'size': 100202
- # }
- # We mostly care about the x and y coordinates of each region
- annotations1 = json.load(open(os.path.join(dataset_dir, "via_region_data.json")))
- # print(annotations1)
- annotations = list(annotations1.values()) # don't need the dict keys
- # The VIA tool saves images in the JSON even if they don't have any
- # annotations. Skip unannotated images.
- annotations = [a for a in annotations if a['regions']]
- # Add images
- for a in annotations:
- # print(a)
- # Get the x, y coordinaets of points of the polygons that make up
- # the outline of each object instance. There are stores in the
- # shape_attributes (see json format above)
- polygons = [r['shape_attributes'] for r in a['regions'].values()]
- # load_mask() needs the image size to convert polygons to masks.
- # Unfortunately, VIA doesn't include it in JSON, so we must read
- # the image. This is only managable since the dataset is tiny.
- image_path = os.path.join(dataset_dir, a['filename'])
- image = skimage.io.imread(image_path)
- height, width = image.shape[:2]
- self.add_image(
- "damage", ## for a single class just add the name here
- image_id=a['filename'], # use file name as a unique image id
- path=image_path,
- width=width, height=height,
- polygons=polygons)
Add Comment
Please, Sign In to add comment