(self)
Implements DatasetConverter.create_dataset_specification_and_records.
Implements DatasetConverter.create_dataset_specification_and_records.
Implements DatasetConverter . create_dataset_specification_and_records .
def create_dataset_specification_and_records(self):
    """Implements DatasetConverter.create_dataset_specification_and_records."""

    splits = self.get_splits()
    # Get the names of the classes assigned to each split
    train_classes = splits['train']
    valid_classes = splits['valid']
    test_classes = splits['test']

    self.classes_per_split[learning_spec.Split.TRAIN] = len(train_classes)
    self.classes_per_split[learning_spec.Split.VALID] = len(valid_classes)
    self.classes_per_split[learning_spec.Split.TEST] = len(test_classes)

    with tf.io.gfile.GFile(os.path.join(self.data_root, 'train.json')) as f:
      original_train = json.load(f)
    with tf.io.gfile.GFile(os.path.join(self.data_root, 'val.json')) as f:
      original_val = json.load(f)

    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']

    # dict where the class is the key.
    class_filepaths = collections.defaultdict(list)
    for image in image_list:
      class_filepaths[image['class']].append(
          os.path.join(self.data_root, image['file_name']))

    all_classes = list(
        itertools.chain(train_classes, valid_classes, test_classes))
    for class_id, class_label in enumerate(all_classes):
      logging.info('Creating record for class ID %d (%s)...', class_id,
                   class_label)
      # Extract the "category_id" information from the class label
      category_id = int(class_label[:4])
      # Check that the key is actually in `class_filepaths`, so that an empty
      # list is not accidentally used.
      if category_id not in class_filepaths:
        raise ValueError('class_filepaths does not contain paths to any '
                         'image for category %d. Existing categories are: %s.' %
                         (category_id, class_filepaths.keys()))
      class_paths = class_filepaths[category_id]
      class_records_path = os.path.join(
          self.records_path, self.dataset_spec.file_pattern.format(class_id))
      self.class_names[class_id] = class_label
      self.images_per_class[class_id] = len(class_paths)

      # Create and write the tf.Record of the examples of this class
      write_tfrecord_from_image_files(class_paths, class_id, class_records_path)
def create_dataset_specification_and_records ( self ) : splits = self . get_splits ( ) # Get the names of the classes assigned to each split train_classes = splits [ 'train' ] valid_classes = splits [ 'valid' ] test_classes = splits [ 'test' ] self . classes_per_split [ learning_spec . Split . TRAIN ] = len ( train_classes ) self . classes_per_split [ learning_spec . Split . VALID ] = len ( valid_classes ) self . classes_per_split [ learning_spec . Split . TEST ] = len ( test_classes ) with tf . io . gfile . GFile ( os . path . join ( self . data_root , 'train.json' ) ) as f : original_train = json . load ( f ) with tf . io . gfile . GFile ( os . path . join ( self . data_root , 'val.json' ) ) as f : original_val = json . load ( f ) 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' ] # dict where the class is the key. class_filepaths = collections . defaultdict ( list ) for image in image_list : class_filepaths [ image [ 'class' ] ] . append ( os . path . join ( self . data_root , image [ 'file_name' ] ) ) all_classes = list ( itertools . chain ( train_classes , valid_classes , test_classes ) ) for class_id , class_label in enumerate ( all_classes ) : logging . info ( 'Creating record for class ID %d (%s)...' , class_id , class_label ) # Extract the "category_id" information from the class label category_id = int ( class_label [ : 4 ] ) # Check that the key is actually in `class_filepaths`, so that an empty # list is not accidentally used. if category_id not in class_filepaths : raise ValueError ( 'class_filepaths does not contain paths to any ' 'image for category %d. Existing categories are: %s.' % ( category_id , class_filepaths . keys ( ) ) ) class_paths = class_filepaths [ category_id ] class_records_path = os . path . join ( self . records_path , self . dataset_spec . file_pattern . format ( class_id ) ) self . class_names [ class_id ] = class_label self . images_per_class [ class_id ] = len ( class_paths ) # Create and write the tf.Record of the examples of this class write_tfrecord_from_image_files ( class_paths , class_id , class_records_path )