(metadata, paths)
Generates Fungi examples.
Generates Fungi examples.
Generates Fungi examples .
def generate_fungi_examples(metadata, paths):
  """Generates Fungi examples."""
  original_train = json.loads(
      (paths['train_val_annotations'] / 'train.json').read_text())
  original_val = json.loads(
      (paths['train_val_annotations'] / 'val.json').read_text())

  image_list = original_train['images'] + original_val['images']
  image_id_dict = {}
  for image in image_list:
    # assert this image_id was not previously added
    assert image['id'] not in image_id_dict
    image_id_dict[image['id']] = image

  # Add a class annotation to every image in image_id_dict.
  annotations = original_train['annotations'] + original_val['annotations']
  for annotation in annotations:
    # assert this images_id was not previously annotated
    assert 'class' not in image_id_dict[annotation['image_id']]
    image_id_dict[annotation['image_id']]['class'] = annotation['category_id']

  class_filepaths = collections.defaultdict(list)
  for image in image_list:
    class_filepaths[image['class']].append(
        paths['train_val'] / image['file_name'])

  image_ids = itertools.count()

  for label, class_name in enumerate(metadata['class_names']):
    for image_path in class_filepaths[int(class_name.split('.')[0])]:
      yield _image_key(next(image_ids), metadata['total_num_examples']), {
          'image': _load_and_process_image(image_path=image_path),
          'format': 'JPEG',
          'filename': image['file_name'].split('/')[-1],
          'label': label,
          'class_name': class_name,
      }
def generate_fungi_examples ( metadata , paths ) : original_train = json . loads ( ( paths [ 'train_val_annotations' ] / 'train.json' ) . read_text ( ) ) original_val = json . loads ( ( paths [ 'train_val_annotations' ] / 'val.json' ) . read_text ( ) ) image_list = original_train [ 'images' ] + original_val [ 'images' ] image_id_dict = { } for image in image_list : # assert this image_id was not previously added assert image [ 'id' ] not in image_id_dict image_id_dict [ image [ 'id' ] ] = image # Add a class annotation to every image in image_id_dict. annotations = original_train [ 'annotations' ] + original_val [ 'annotations' ] for annotation in annotations : # assert this images_id was not previously annotated assert 'class' not in image_id_dict [ annotation [ 'image_id' ] ] image_id_dict [ annotation [ 'image_id' ] ] [ 'class' ] = annotation [ 'category_id' ] class_filepaths = collections . defaultdict ( list ) for image in image_list : class_filepaths [ image [ 'class' ] ] . append ( paths [ 'train_val' ] / image [ 'file_name' ] ) image_ids = itertools . count ( ) for label , class_name in enumerate ( metadata [ 'class_names' ] ) : for image_path in class_filepaths [ int ( class_name . split ( '.' ) [ 0 ] ) ] : yield _image_key ( next ( image_ids ) , metadata [ 'total_num_examples' ] ) , { 'image' : _load_and_process_image ( image_path = image_path ) , 'format' : 'JPEG' , 'filename' : image [ 'file_name' ] . split ( '/' ) [ - 1 ] , 'label' : label , 'class_name' : class_name , }