omoured commited on
Commit
bf78bd5
·
verified ·
1 Parent(s): a3d3018

Update dataset.py

Browse files
Files changed (1) hide show
  1. dataset.py +23 -18
dataset.py CHANGED
@@ -13,60 +13,65 @@ class LineGraphicsDataset(GeneratorBasedBuilder):
13
  "height": Value("int32"),
14
  "instances": Sequence({
15
  "category_id": Value("int32"),
16
- "mask": Sequence(Sequence(Value("float32"))) # list of polygon(s)
17
  })
18
  }),
19
  description="Line Graphics Dataset with polygon instance masks in COCO format.",
20
  )
21
 
22
  def _split_generators(self, dl_manager):
23
- # Automatically download and extract the repo contents
24
  data_dir = dl_manager.download_and_extract(self.config.data_dir or ".")
25
 
26
  return [
27
  SplitGenerator(
28
  name=Split.TRAIN,
29
- gen_kwargs={"split_dir": os.path.join(data_dir, "train")}
30
  ),
31
  SplitGenerator(
32
  name=Split.VALIDATION,
33
- gen_kwargs={"split_dir": os.path.join(data_dir, "val")}
34
  ),
35
  SplitGenerator(
36
  name=Split.TEST,
37
- gen_kwargs={"split_dir": os.path.join(data_dir, "test")}
38
  ),
39
  ]
40
 
41
  def _generate_examples(self, split_dir):
42
- # Load annotations.json
43
- ann_path = os.path.join(split_dir, "annotations.json")
44
- with open(ann_path, "r") as f:
45
  coco = json.load(f)
46
 
 
47
  image_id_to_info = {img["id"]: img for img in coco["images"]}
48
 
 
49
  from collections import defaultdict
50
  anns_by_image = defaultdict(list)
51
  for ann in coco["annotations"]:
52
  anns_by_image[ann["image_id"]].append(ann)
53
 
54
- for img_id, info in image_id_to_info.items():
55
- image_path = os.path.join(split_dir, "images", info["file_name"])
56
- width = info["width"]
57
- height = info["height"]
58
 
59
  instances = []
60
- for ann in anns_by_image[img_id]:
 
 
 
 
61
  instances.append({
62
  "category_id": ann["category_id"],
63
- "mask": ann["segmentation"] # List of polygons
64
  })
65
 
66
- yield info["file_name"], {
67
- "image_name": info["file_name"],
68
  "image": image_path,
69
- "width": width,
70
- "height": height,
71
  "instances": instances
72
  }
 
13
  "height": Value("int32"),
14
  "instances": Sequence({
15
  "category_id": Value("int32"),
16
+ "mask": Sequence(Sequence(Value("float32"))) # List of polygons (each polygon = list of floats)
17
  })
18
  }),
19
  description="Line Graphics Dataset with polygon instance masks in COCO format.",
20
  )
21
 
22
  def _split_generators(self, dl_manager):
 
23
  data_dir = dl_manager.download_and_extract(self.config.data_dir or ".")
24
 
25
  return [
26
  SplitGenerator(
27
  name=Split.TRAIN,
28
+ gen_kwargs={"split_dir": os.path.join(data_dir, "train")},
29
  ),
30
  SplitGenerator(
31
  name=Split.VALIDATION,
32
+ gen_kwargs={"split_dir": os.path.join(data_dir, "val")},
33
  ),
34
  SplitGenerator(
35
  name=Split.TEST,
36
+ gen_kwargs={"split_dir": os.path.join(data_dir, "test")},
37
  ),
38
  ]
39
 
40
  def _generate_examples(self, split_dir):
41
+ # Load annotations
42
+ annotation_file = os.path.join(split_dir, "annotations.json")
43
+ with open(annotation_file, "r") as f:
44
  coco = json.load(f)
45
 
46
+ # Map image_id to metadata
47
  image_id_to_info = {img["id"]: img for img in coco["images"]}
48
 
49
+ # Group annotations by image
50
  from collections import defaultdict
51
  anns_by_image = defaultdict(list)
52
  for ann in coco["annotations"]:
53
  anns_by_image[ann["image_id"]].append(ann)
54
 
55
+ # Generate examples
56
+ for image_id, image_info in image_id_to_info.items():
57
+ file_name = image_info["file_name"]
58
+ image_path = os.path.join(split_dir, "images", file_name)
59
 
60
  instances = []
61
+ for ann in anns_by_image[image_id]:
62
+ # Each mask is a list of polygons
63
+ segmentation = ann["segmentation"]
64
+ if not isinstance(segmentation, list):
65
+ continue # skip if malformed
66
  instances.append({
67
  "category_id": ann["category_id"],
68
+ "mask": segmentation
69
  })
70
 
71
+ yield file_name, {
72
+ "image_name": file_name,
73
  "image": image_path,
74
+ "width": image_info["width"],
75
+ "height": image_info["height"],
76
  "instances": instances
77
  }