Guest User

Untitled

a guest
Jul 16th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. class CustomDataset(utils.Dataset):
  2.  
  3. def load_custom(self, dataset_dir, subset):
  4. """Load a subset of the Balloon dataset.
  5. dataset_dir: Root directory of the dataset.
  6. subset: Subset to load: train or val
  7. """
  8. # Add classes. We have only one class to add.
  9. self.add_class("damage", 1, "damage")
  10.  
  11. # Train or validation dataset?
  12. assert subset in ["train", "val"]
  13. dataset_dir = os.path.join(dataset_dir, subset)
  14.  
  15. # Load annotations
  16. # VGG Image Annotator saves each image in the form:
  17. # { 'filename': '28503151_5b5b7ec140_b.jpg',
  18. # 'regions': {
  19. # '0': {
  20. # 'region_attributes': {},
  21. # 'shape_attributes': {
  22. # 'all_points_x': [...],
  23. # 'all_points_y': [...],
  24. # 'name': 'polygon'}},
  25. # ... more regions ...
  26. # },
  27. # 'size': 100202
  28. # }
  29. # We mostly care about the x and y coordinates of each region
  30. annotations1 = json.load(open(os.path.join(dataset_dir, "via_region_data.json")))
  31. # print(annotations1)
  32. annotations = list(annotations1.values()) # don't need the dict keys
  33.  
  34. # The VIA tool saves images in the JSON even if they don't have any
  35. # annotations. Skip unannotated images.
  36. annotations = [a for a in annotations if a['regions']]
  37.  
  38. # Add images
  39. for a in annotations:
  40. # print(a)
  41. # Get the x, y coordinaets of points of the polygons that make up
  42. # the outline of each object instance. There are stores in the
  43. # shape_attributes (see json format above)
  44. polygons = [r['shape_attributes'] for r in a['regions'].values()]
  45.  
  46. # load_mask() needs the image size to convert polygons to masks.
  47. # Unfortunately, VIA doesn't include it in JSON, so we must read
  48. # the image. This is only managable since the dataset is tiny.
  49. image_path = os.path.join(dataset_dir, a['filename'])
  50. image = skimage.io.imread(image_path)
  51. height, width = image.shape[:2]
  52.  
  53. self.add_image(
  54. "damage", ## for a single class just add the name here
  55. image_id=a['filename'], # use file name as a unique image id
  56. path=image_path,
  57. width=width, height=height,
  58. polygons=polygons)
Add Comment
Please, Sign In to add comment