Object Detection datasets

This guide describes how the TensorFlow object detection services read a dataset: the supported annotation formats, the dataset section attributes, and how the training, validation, test, quantization, and prediction sets are built. For the PyTorch pipeline, see the PyTorch training guide: it reads COCO-format datasets, and Pascal VOC for SSD models only.

The annotations are parsed directly in their original format when the data loaders are created and kept in memory. No conversion step is needed and no intermediate file is written on disk.

1. Supported formats

Set dataset.format to the annotation format of your dataset:

format

Annotations

Layout

coco

One JSON file per split

An image folder and an annotation JSON file per split, for example train2017/ and annotations/instances_train2017.json.

voc

One XML file per image

The VOCdevkit layout: JPEGImages/, Annotations/, and ImageSets/Main/*.txt split files listing the image names.

darknet_yolo

One .txt file per image

class_id x_center y_center width height on each line, normalized to [0, 1]. Images and labels can be in the same folder or in two folders.

General rules:

  • Images: for darknet_yolo and voc, images must be .jpg files. For coco, the images are looked up by the file_name of the JSON file in the image folder; images listed in the annotations but missing from the folder are skipped. Folders of images for quantization and prediction must contain .jpg files, directly in the folder (no sub-folders).

  • Classes: dataset.class_names is required for training and evaluation. For coco and voc, the classes are selected by name and ordered as in class_names; annotations of other classes are ignored. For darknet_yolo, the class_id of the label files is the index in class_names.

  • Images without objects: images without annotation are kept as background images. Set exclude_unlabeled: True to remove them.

  • Maximum number of boxes: at most max_gt boxes (default 20) are kept per image; the others are dropped.

2. The dataset section

dataset:
  format: darknet_yolo                  # coco, voc or darknet_yolo
  dataset_name: darknet_yolo            # Selects the dataset wrapper, see section 3
  dataset_subsetname: person            # Optional. With dataset_name, selects the pretrained weights
  class_names: [person]                 # Names of the classes
  exclude_unlabeled: True               # Optional, default False
  max_gt: 20                            # Optional, default 20
  train_images_path: <train-images-directory>
  train_annotations_path: <train-annotations>
  val_images_path: <validation-images-directory>        # Optional
  val_annotations_path: <validation-annotations>        # Optional
  # validation_split: 0.2               # Used when no validation set is given
  test_images_path: <test-images-directory>             # Optional for training, required for evaluation
  test_annotations_path: <test-annotations>
  quantization_path: <quantization-images-directory>    # Optional
  quantization_split:                                   # Optional
  prediction_path: <prediction-images-directory>        # Required for prediction
  seed: 123

Attribute

Description

format

Annotation format. Required for training, evaluation and the chains that include them. It can be omitted for quantization, prediction, chain_qb, and chain_qd, which only read images (unless an advanced quantization mode that evaluates the model is used).

dataset_name

Selects the dataset wrapper (see section 3). Required for training and evaluation; it defaults to custom for quantization, prediction, chain_qb, and chain_qd. It is also used with dataset_subsetname to select the pretrained weights, for example coco and person select the coco_person weights.

class_names

List of the class names. Required for training, evaluation, prediction, and deployment.

exclude_unlabeled

If True, images without annotation are dropped. Default: False.

max_gt

Maximum number of boxes kept per image. Default: 20.

validation_split

Fraction of the training set used for validation when no validation set is given. Must be between 0 and 1 (excluded). Default: 0.2.

quantization_path

Folder of calibration images. No annotations are needed.

quantization_split

Fraction of the calibration images to use, between 0 (excluded) and 1. If it is set to 0, the quantization uses random data.

prediction_path

Folder of the images to predict.

seed

Seed used to shuffle the data and to split the training set. Default: 123.

3. Dataset wrappers

The dataset_name value selects a registered dataset wrapper: coco, voc, darknet_yolo, custom_dataset, or custom. Any other value raises an error when the data loaders are created.

All the wrappers behave the same way: the loader is chosen with dataset.format, not with dataset_name. A dataset named coco can therefore be in darknet_yolo format, as in the example configuration files. Use custom_dataset for your own data if you do not want to reuse the name of a public dataset.

dataset_name  ->  dataset wrapper (coco, voc, darknet_yolo, custom_dataset, custom)
                          |
                    dataset.format
        +-----------------+-----------------+
      coco               voc            darknet_yolo
  parse the JSON    parse the XML     parse the .txt
   annotations       annotations        labels
        +-----------------+-----------------+
                          |
          samples kept in memory, then split in
        train / validation / test (training, evaluation)
              quantization and prediction sets

4. Attributes per format

The attributes below are for the training mode. For the evaluation mode alone, only the test_* attributes are used, and they are all required.

4.1 format: coco

Attribute

Training

Evaluation

train_images_path

Required: folder of the training images

train_annotations_path

Required: COCO JSON file of the training set

val_images_path, val_annotations_path

Optional. Both are needed to use a validation set.

test_images_path, test_annotations_path

Optional. Both are needed to use a test set.

Required

If download_data: True is set, the loader ignores the train_* and val_* paths and expects the official COCO layout in data_dir: the train2017 and val2017 image folders and annotations/instances_train2017.json and annotations/instances_val2017.json. The files are not downloaded by the loader: download and extract the dataset beforehand. The train_images_path and train_annotations_path attributes must still be set.

4.2 format: voc

Attribute

Training

Evaluation

train_images_path

Required: JPEGImages folder

train_annotations_path

Required: folder of the XML annotation files

train_split

Required: ImageSets/Main/train.txt file

val_images_path, val_annotations_path, val_split

Optional. All three are needed to use a validation set.

test_images_path, test_annotations_path, test_split

Optional. All three are needed to use a test set.

Required

The split files list one image name per line, without extension. The image <name>.jpg and the annotation <name>.xml are read from the images and annotations folders.

download_data and data_dir are not used by the Pascal VOC loader.

4.3 format: darknet_yolo

Attribute

Training

Evaluation

train_images_path

Required: folder of the .jpg images

train_annotations_path

Required: folder of the .txt label files. It can be the same folder as the images.

val_images_path, val_annotations_path

Optional. Both are needed to use a validation set.

test_images_path, test_annotations_path

Optional. Both are needed to use a test set.

Required

For each image <name>.jpg, the labels are read from <name>.txt in the annotations folder. An image without label file is a background image. download_data: True is not supported for this format.

5. How the sets are built

  • Validation set: if no validation set is given, validation_split of the training set is used for validation (80:20 by default). Use the same validation_split value in the training and evaluation services.

  • Test set: if no test set is given, the validation set is used as the test set. Using a separate test set gives a more reliable estimate of the accuracy.

  • Quantization set: the images of quantization_path are used. If it is empty, the images of train_images_path are used. If neither is set, or if quantization_split is 0, the model is quantized with random data: the accuracy is meaningless, but the footprints on target can be estimated. quantization_split selects a fraction of the images.

  • Preprocessing: the bounding boxes are updated together with the images when they are resized or augmented.

6. Required attributes per operation mode

Operation mode

Required

training, chain_tqe, chain_tqeb

format, class_names, the train_* attributes of the format

evaluation, chain_eqe, chain_eqeb

format, class_names, the test_* attributes of the format

quantization, chain_qb

Nothing: quantization_path is recommended

chain_qd

class_names; quantization_path is recommended

prediction

prediction_path, class_names

benchmarking

Nothing

deployment

class_names