(metadata, paths)
Generates Aircraft examples.
Generates Aircraft examples.
Generates Aircraft examples .
def generate_aircraft_examples(metadata, paths):
  """Generates Aircraft examples."""
  data_path = paths['fgvc-aircraft-2013b'] / 'fgvc-aircraft-2013b/data'

  # Retrieve mapping from filename to bounding box.
  # Cropping to the bounding boxes is important for two reasons:
  # 1) The dataset documentation mentions that "[the] (main) aircraft in each
  #    image is annotated with a tight bounding box [...]", which suggests
  #    that there may be more than one aircraft in some images. Cropping to
  #    the bounding boxes removes ambiguity as to which airplane the label
  #    refers to.
  # 2) Raw images have a 20-pixel border at the bottom with copyright
  #    information which needs to be removed. Cropping to the bounding boxes
  #    has the side-effect that it removes the border.
  bboxes_path = data_path / 'images_box.txt'
  with bboxes_path.open('r') as f:
    names_to_bboxes = [
        line.split('\n')[0].split(' ') for line in f.readlines()
    ]
  names_to_bboxes = dict(
      (name, tuple(map(int, (xmin, ymin, xmax, ymax))))
      for name, xmin, ymin, xmax, ymax in names_to_bboxes)

  # Retrieve mapping from filename to variant
  variant_trainval_path = data_path / 'images_variant_trainval.txt'
  with variant_trainval_path.open('r') as f:
    names_to_variants = [
        line.split('\n')[0].split(' ', 1) for line in f.readlines()
    ]

  variant_test_path = data_path / 'images_variant_test.txt'
  with variant_test_path.open('r') as f:
    names_to_variants += [
        line.split('\n')[0].split(' ', 1) for line in f.readlines()
    ]

  names_to_variants = dict(names_to_variants)

  # Build mapping from variant to filenames. "Variant" refers to the aircraft
  # model variant (e.g., A330-200) and is used as the class name in the
  # dataset. The position of the class name in the concatenated list of
  # training, validation, and test class name constitutes its class ID.
  variants_to_names = collections.defaultdict(list)
  for name, variant in names_to_variants.items():
    variants_to_names[variant].append(name)

  image_ids = itertools.count()

  for label, class_name in enumerate(metadata['class_names']):
    for filename in sorted(variants_to_names[class_name]):
      image_path = data_path / f'images/{filename}.jpg'
      bbox = names_to_bboxes[image_path.stem]
      yield _image_key(next(image_ids), metadata['total_num_examples']), {
          'image': _load_and_process_image(image_path=image_path, bbox=bbox),
          'format': 'JPEG',
          'filename': image_path.name,
          'label': label,
          'class_name': class_name,
      }
def generate_aircraft_examples ( metadata , paths ) : data_path = paths [ 'fgvc-aircraft-2013b' ] / 'fgvc-aircraft-2013b/data' # Retrieve mapping from filename to bounding box. # Cropping to the bounding boxes is important for two reasons: # 1) The dataset documentation mentions that "[the] (main) aircraft in each #    image is annotated with a tight bounding box [...]", which suggests #    that there may be more than one aircraft in some images. Cropping to #    the bounding boxes removes ambiguity as to which airplane the label #    refers to. # 2) Raw images have a 20-pixel border at the bottom with copyright #    information which needs to be removed. Cropping to the bounding boxes #    has the side-effect that it removes the border. bboxes_path = data_path / 'images_box.txt' with bboxes_path . open ( 'r' ) as f : names_to_bboxes = [ line . split ( '\n' ) [ 0 ] . split ( ' ' ) for line in f . readlines ( ) ] names_to_bboxes = dict ( ( name , tuple ( map ( int , ( xmin , ymin , xmax , ymax ) ) ) ) for name , xmin , ymin , xmax , ymax in names_to_bboxes ) # Retrieve mapping from filename to variant variant_trainval_path = data_path / 'images_variant_trainval.txt' with variant_trainval_path . open ( 'r' ) as f : names_to_variants = [ line . split ( '\n' ) [ 0 ] . split ( ' ' , 1 ) for line in f . readlines ( ) ] variant_test_path = data_path / 'images_variant_test.txt' with variant_test_path . open ( 'r' ) as f : names_to_variants += [ line . split ( '\n' ) [ 0 ] . split ( ' ' , 1 ) for line in f . readlines ( ) ] names_to_variants = dict ( names_to_variants ) # Build mapping from variant to filenames. "Variant" refers to the aircraft # model variant (e.g., A330-200) and is used as the class name in the # dataset. The position of the class name in the concatenated list of # training, validation, and test class name constitutes its class ID. variants_to_names = collections . defaultdict ( list ) for name , variant in names_to_variants . items ( ) : variants_to_names [ variant ] . append ( name ) image_ids = itertools . count ( ) for label , class_name in enumerate ( metadata [ 'class_names' ] ) : for filename in sorted ( variants_to_names [ class_name ] ) : image_path = data_path / f'images/{filename}.jpg' bbox = names_to_bboxes [ image_path . stem ] yield _image_key ( next ( image_ids ) , metadata [ 'total_num_examples' ] ) , { 'image' : _load_and_process_image ( image_path = image_path , bbox = bbox ) , 'format' : 'JPEG' , 'filename' : image_path . name , 'label' : label , 'class_name' : class_name , }