Skip to main content
Skip table of contents

Detector BYOD: Pascal VOC Format Example

Bring Your Own Pascal VOC Formatted Data

Training with a Pascal VOC formatted dataset is very similar to the using COCO formatted data. Perform the following to train your model with Pascal VOC formatted data:

Find the Pascal-like dataset template file in the Docker container located at

/latentai/custom-dataset-configs/data/pascal-like-template.yaml

It will look like this:

CODE
defaults:
  - /data/transforms@module.train_transforms: resize.yaml
  - /data/transforms@module.valid_transforms: resize.yaml
  - _self_

nclasses: 0
module:
  _target_: af.core.data.modules.adaptermodule.AdaptorDataModule
  batch_sizes: ${task.batch_sizes}
  num_workers: ${task.num_workers}
  adaptors: ${model.adaptors}
  dataset_generator:
    _target_: af.core.data.generic.pascal_like.PASCALlike
    root_path: null # when downloading a dataset, a good default is to use ${paths.cache_dir}
    images_dir: JPEGImages
    annotations_dir: Annotations
    type: detection
    is_split: false
    trainval_split_ratio: 0.75
    trainval_split_seed: 42
    train_set: ImageSets/Main/train.txt
    val_set: ImageSets/Main/val.txt
    labelmap_file: pascal_label_map.pbtxt
    download_from_url: false
    label_indexing: 0-indexed-no-background # one of: "0-indexed-no-background", "1-indexed-no-background", "0-indexed-with-background". if dataset contains a background class, it must be at index 0. 
    dataset_name: my-custom-pascal-like-data

The fields you will need to change are for the number of classes in your dataset, and the dataset_generator shown here from line 14 on. Changing those fields with your dataset’s information is enough to load and use your PascalVOC-formatted dataset.

  • nclasses: The number of classes in your dataset.

  • root_path: The absolute path to the root directory of the dataset.

  • images_dir: The path to folder containing only images, relative to root_path.

  • annotations_dir: The path to folder containing xml files, relative to root_path.

  • type: detection or segmentation. For this detection recipe, we can leave the value as detection

  • is_split: true or false.

    • If set to true, text files containing the list of samples for training and validation should be specified using train_set and val_set

    • If set to false, data will be split by the ingestor given the trainval_split_ratio and trainval_split_seed

  • trainval_split_ratio: The ratio to use to split the dataset. Used only if is_split: false

  • trainval_split_seed: The seed to use to pseudo randomly split the dataset. Used only if is_split: false

  • train_set: The path to text file containing names (no extensions) to the training samples. Used only if is_split: true

  • val_set: The path to text file containing names (no extensions) to the validation samples. Used only if is_split: true

  • labelmap_file: (optional) the path to the file containing a map from class index (int) to class name (string), relative to root_path. If you dont have this file, it will be created automatically and stored in the specified directory.

  • download_from_url: If your data is compressed to a single file and resides in the cloud (public S3 bucket, Drive, etc.), the ingestor can download it and place it inside root_path

  • label_indexing: Are the labels 0 indexed or is the first class at index 1? Is there a background class? One of 0-indexed-no-background, 1-indexed-no-background, 0-indexed-with-background. The AF needs this information to decide if it needs to label shift to ensure that if a background class is present it must be at index 0, and other classes start at index 1.

  • dataset_name: A string. Will be used to name any generated artifacts.

A Concrete BYOD Pascal VOC Format Example

For an example of the usage with a real dataset, see custom-configs/data/smoke-pascal-like.yaml.

This example has a download_from_url defined with the path to the data, so the data will get downloaded automatically into root_path the first time the dataset is requested by any command.

Lets see what a few samples of the data look like with command=vizdata:

CODE
af --config-name=yolov5_S_RT data=smoke-pascal-like command=vizdata

The AF will check if the data already exists on the root_path and download the dataset from the internet if necessary. The vizdata command will read the annotations and draw bounding boxes over some of the samples. The sample images will be stored in /latentai/artifacts/vizdata/smoke_pascal_like/*.

Train

You can train one of the YOLO models using command=train:

CODE
af --config-name=yolov5_S_RT data=smoke-pascal-like \
 command=train task.moniker="BYOD_recipe"

Note where the checkpoint is stored at the end of the training run. The checkpoint will be stored in a path of the form:/latentai/artifacts/train/{date}_{time}_BYOD_recipe/{epoch}.ckpt Find that file and store it for ease of use in the next steps:

CODE
export SMOKE_CHECK=<path to .ckpt file>

# Example:
# export SMOKE_CHECK=/latentai/artifacts/train/2022-08-23_17-25-29_task_BYOD_recipe/epoch-19_step-1840.ckpt

Evaluate the Model on Host Using the AF

You can now use your newly trained model by passing the checkpoint to the commands you used previously with the pre-trained model. For example, to evaluate:

CODE
af --config-name=yolov5_S_RT data=smoke-pascal-like \
  command=evaluate task.moniker="BYOD_recipe" \
  +checkpoint=$SMOKE_CHECK

Export the Model

CODE
af --config-name=yolov5_S_RT data=smoke-pascal-like \
  command=export task.moniker="BYOD_recipe" \
  +checkpoint=$SMOKE_CHECK

Your exported model will be saved in:
/latentai/artifacts/export/BYOD_recipe_batch1_640-640/traced_model.pt

Evaluating the Smoke Detection Example on the Target

Before you can optimize your trained model, you will need to identify a calibration dataset from your BYOD data. Identifying an ideal calibration dataset is beyond the scope of this tutorial, but usually entails selecting a few representative images for the quantizer. If you are targeting a CUDA device, you will need to provide 2N images, where N is your batch size. You will need to provide a path to these images in the file rep_dataset.txt that is in the model recipe directory. We will use the last two images in the validation dataset for the Smoke dataset.

For YOLOv5 Small, the representative dataset is in /latentai/recipes/yolov5_S_RT/rep_dataset.txt. The default rep_dataset has been chosen for MSCOCO optimization. Copy this file to a safe place, and change the content of the file to contain the following paths to images in the smoke dataset:

CODE
/latentai/workspace/datasets/smoke-pascal-like/smoke_dataset/JPEGImages/ck0ukkz8tytsn0721y70sud46_jpeg.rf.18cbd5352f166266b09e38e04afa7394.jpg
/latentai/workspace/datasets/smoke-pascal-like/smoke_dataset/JPEGImages/ck0uklx72wly70701px5wl6fw_jpeg.rf.cb1683418c777e1d072763d02d84465b.jpg

If you find that your optimized (Int8) model has substantial accuracy loss from the compiled (Float32) model, try different calibration images. Some image sets will produce outlier results that may adversely affect accuracy.

Please bear in mind that the optimal training dataset may change each time you retrain the model. The above is an example dataset, but you may need to test out different datasets to yield the best results after you have trained your model.

If you do not have the utils directory in your PYTHONPATH from Step Two, you will need to set it now before running the leip pipeline command:
export PYTHONPATH=/latentai/recipes/yolov5_L_RT/evaluation/utils:$PYTHONPATH

Once you have exported your trained model and determined a calibration dataset, simply compile and optimize it as before, providing the traced model as the input path:

CODE
leip pipeline \
  --input_path /latentai/artifacts/export/BYOD_recipe_batch1_640-640/traced_model.pt \
  --output_path /latentai/workspace/recipes_output/yolov5_S_RT/x86_64_cuda \
  --config_path /latentai/recipes/yolov5_S_RT/pipeline_x86_64_cuda.yaml

If you would like to use leip evaluate to evaluate your model, you will need to convert the Pascal VOC formated data to COCO format. You can do this via the af command=export_data function:

CODE
# Export the Pascal data into COCO format:
af data=smoke-pascal-like command=export_data export_data=coco

# Some processes will look for the instances file in the data directory:
cp /latentai/artifacts/export_data/COCO/smoke_pascal_like/instances_val2017.json \
  /latentai/artifacts/export_data/COCO/smoke_pascal_like/val2017/

# Data will be available here in COCO format:
# /latentai/artifacts/export_data/COCO/smoke_pascal_like

You can now evaluate your compiled, optimized model against the smoke dataset:

CODE
# Evaluate Float32:
leip evaluate \
  --input_path /latentai/workspace/recipes_output/yolov5_S_RT/x86_64_cuda/Float32-compile/ \
  --test_path /latentai/artifacts/export_data/COCO/smoke_pascal_like/val2017/instances_val2017.json \
  --dataset_type coco --output_format yolov5

# Evaluate Int8:
leip evaluate \
  --input_path /latentai/workspace/recipes_output/yolov5_S_RT/x86_64_cuda/Int8-optimize/ \
  --test_path /latentai/artifacts/export_data/COCO/smoke_pascal_like/val2017/instances_val2017.json \
  --dataset_type coco --output_format yolov5

If you would like to evaluate your BYOD model on the target device, follow these additional steps.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.