SaulLu commited on
Commit
a51a586
1 Parent(s): 159348b

simplify script

Browse files
Files changed (1) hide show
  1. FGVC-Aircraft.py +26 -20
FGVC-Aircraft.py CHANGED
@@ -265,6 +265,17 @@ _VARIANTS = [
265
  ]
266
 
267
 
 
 
 
 
 
 
 
 
 
 
 
268
  class FGVCAircraft(datasets.GeneratorBasedBuilder):
269
 
270
  VERSION = datasets.Version("1.0.0")
@@ -341,37 +352,32 @@ class FGVCAircraft(datasets.GeneratorBasedBuilder):
341
  ]
342
 
343
  def _generate_examples(self, images_dir, annotations_dir, split):
344
- filenames = open(annotations_dir / f"images_{split}.txt").readlines()
345
- filenames = [filename.strip() + ".jpg" for filename in filenames]
346
 
347
  families = open(annotations_dir / f"images_family_{split}.txt").readlines()
348
- families = [" ".join(family.strip().split()[1:]) for family in families]
349
 
350
  manufacturers = open(
351
  annotations_dir / f"images_manufacturer_{split}.txt"
352
  ).readlines()
353
-
354
- manufacturers = [
355
- " ".join(manufacturer.strip().split()[1:]) for manufacturer in manufacturers
356
- ]
357
 
358
  variants = open(annotations_dir / f"images_variant_{split}.txt").readlines()
359
- variants = [" ".join(variant.strip().split()[1:]) for variant in variants]
360
 
361
  bounding_boxes = open(
362
  os.path.join(annotations_dir, "images_box.txt")
363
  ).readlines()
364
- bounding_boxes = [box.strip().split() for box in bounding_boxes]
365
- bboxes = {}
366
- for box in bounding_boxes:
367
- bboxes[box[0]] = box[1:]
368
-
369
- for i, filename in enumerate(filenames):
370
- full_path = images_dir / filename
371
- family = families[i]
372
- manufacturer = manufacturers[i]
373
- variant = variants[i]
374
- xmin, ymin, xmax, ymax = list(map(int, bboxes[filename.split(".")[0]]))
375
 
376
  record = {
377
  "image": str(full_path),
@@ -380,4 +386,4 @@ class FGVCAircraft(datasets.GeneratorBasedBuilder):
380
  "manufacturer": manufacturer,
381
  "variant": variant,
382
  }
383
- yield filename, record
 
265
  ]
266
 
267
 
268
+ def parse_annotations(annotations, join_labels=True):
269
+ annotations = [annotation.strip().split() for annotation in annotations]
270
+ if join_labels:
271
+ return {
272
+ annotation[0].strip(): " ".join(annotation[1:])
273
+ for annotation in annotations
274
+ }
275
+ else:
276
+ return {annotation[0].strip(): annotation[1:] for annotation in annotations}
277
+
278
+
279
  class FGVCAircraft(datasets.GeneratorBasedBuilder):
280
 
281
  VERSION = datasets.Version("1.0.0")
 
352
  ]
353
 
354
  def _generate_examples(self, images_dir, annotations_dir, split):
355
+ image_ids = open(annotations_dir / f"images_{split}.txt").readlines()
356
+ image_ids = [image_id.strip() for image_id in image_ids]
357
 
358
  families = open(annotations_dir / f"images_family_{split}.txt").readlines()
359
+ families = parse_annotations(families)
360
 
361
  manufacturers = open(
362
  annotations_dir / f"images_manufacturer_{split}.txt"
363
  ).readlines()
364
+ manufacturers = parse_annotations(manufacturers)
 
 
 
365
 
366
  variants = open(annotations_dir / f"images_variant_{split}.txt").readlines()
367
+ variants = parse_annotations(variants)
368
 
369
  bounding_boxes = open(
370
  os.path.join(annotations_dir, "images_box.txt")
371
  ).readlines()
372
+
373
+ bounding_boxes = parse_annotations(bounding_boxes, join_labels=False)
374
+
375
+ for image_id in image_ids:
376
+ full_path = images_dir / f"{image_id}.jpg"
377
+ family = families[image_id]
378
+ manufacturer = manufacturers[image_id]
379
+ variant = variants[image_id]
380
+ xmin, ymin, xmax, ymax = list(map(int, bounding_boxes[image_id]))
 
 
381
 
382
  record = {
383
  "image": str(full_path),
 
386
  "manufacturer": manufacturer,
387
  "variant": variant,
388
  }
389
+ yield image_id, record