google-automl

repository·master·Indexed 27 days ago

https://github.com/google/automl

Implementation of EfficientDet, a family of scalable and efficient object detection models using EfficientNet backbones and BiFPN. The repository provides tools for converting COCO and Pascal VOC datasets to TFRecord, training on TPUs, finetuning, and exporting models to SavedModel, Frozen Graph, TensorRT, and TFLite formats. It includes utilities for performing inference on images and videos, benchmarking network and end-to-end latency, and visualizing TFRecords.

Tokens
15K
Snippets
38
Records
71
Agent score
91%

What's inside google-automl

  1. Overview of the Lion Optimizer

    master
    Lion is an optimization algorithm discovered via symbolic program search. It is designed to be simple, memory-efficient, and fast. Unlike AdamW or other adaptive optimizers that require saving both first and second moments, Lion only requires the momentum, which halves the additional memory footprint. This makes it particularly beneficial for training large models or using large batch sizes. In practice, Lion often provides a 2-15% runtime speedup compared to AdamW and Adafactor.
  2. Export EfficientDet to ONNX

    master

    To export a SavedModel to ONNX format:

    1. Install tf2onnx: pip install tf2onnx.
    2. Configure NMS (Non-Maximum Suppression) settings. Note that ONNX does not support soft NMS, so use method: 'hard'.
    3. Run the conversion command using tf2onnx.convert.

    Example NMS configuration:

    nms_configs:
      method: 'hard'
      iou_thresh: 0.5
      score_thresh: 0.
      sigma: 0.0
      pyfunc: False
      max_nms_inputs: 0
      max_output_size: 100
  3. Train EfficientDet on Cloud TPU

    master

    To train on Cloud TPU, you need a GCE VM with an associated Cloud TPU resource and a GCS bucket for checkpoints.

    Set PYTHONPATH to include the models directory and use the --tpu flag with your TPU_NAME and --strategy=tpu.

    export PYTHONPATH="$PYTHONPATH:/path/to/models"
    python train.py --tpu=TPU_NAME --train_file_pattern=DATA_DIR/*.tfrecord --model_dir=MODEL_DIR --strategy=tpu
  4. Format code for Pull Requests

    master

    Use yapf with the following style option: --style='{based_on_style: yapf}'.

    Alternatively, you can save this configuration to ~/.config/yapf/style:

    [style]
    based_on_style = yapf

    To check formatting with lint, run: !pylint --rcfile=../.pylintrc your_file.py

  5. Convert COCO validation set to tfrecord

    master

    To convert a COCO validation dataset into the tfrecord format, you must first download the COCO images and annotations, unzip them, and then run the dataset/create_coco_tfrecord.py script.

    Important Note: The source_id (or image_id) must be an integer to comply with official COCO library requirements.

    # Download coco data.
    !wget http://images.cocodataset.org/zips/val2017.zip
    !wget http://images.cocodataset.org/annotations/annotations_trainval2017.zip
    !unzip val2017.zip
    !unzip annotations_trainval2017.zip
    
    # convert coco data to tfrecord.
    !mkdir tfrecord
    !PYTHONPATH=".:$PYTHONPATH"  python dataset/create_coco_tfrecord.py \
      --image_dir=val2017 \
      --object_annotations_file=annotations/instances_val2017.json \
      --output_file_prefix=tfrecord/val \
      --num_shards=32
  6. Finetune EfficientDet on PASCAL VOC 2012

    master

    To finetune an EfficientDet model using a COCO checkpoint on the PASCAL VOC 2012 dataset, follow these steps:

    1. Convert PASCAL VOC data to TFRecord using dataset/create_pascal_tfrecord.py.
    2. Create a voc_config.yaml file containing the dataset configuration (classes, learning rate, etc.).
    3. Run train.py providing the --pretrained_ckpt flag to point to your COCO checkpoint.

    Note: num_epochs is a maximum; to continue training, re-run the command with the same settings.

    # Convert pascal data.
    !PYTHONPATH=".:$PYTHONPATH"  python dataset/create_pascal_tfrecord.py  \
        --data_dir=VOCdevkit --year=VOC2012  --output_path=tfrecord/pascal
    
    # Finetune.
    !python train.py \
        --train_file_pattern=tfrecord/pascal*.tfrecord \
        --val_file_pattern=tfrecord/pascal*.tfrecord \
        --val_file_pattern=tfrecord/*.json \
        --model_name=efficientdet-d0 \
        --model_dir=/tmp/efficientdet-d0-finetune  \
        --pretrained_ckpt=efficientdet-d0  \
        --batch_size=64 \
        --eval_samples=1024 \
        --num_examples_per_epoch=5717 --num_epochs=50  \
        --hparams=voc_config.yaml --val_json_file=tfrecord/json_pascal.json
  7. Train EfficientDet on single or multi-node GPUs

    master

    Single Node GPUs

    Add the --strategy=gpus flag to your training command.

    Multi-Node GPUs

    Use python -m tf2.train with --strategy=multi-gpus. You must specify --worker addresses for all nodes and assign a unique --worker_index (starting from 0) to each node.

  8. Run all tests

    master

    To run all tests in the repository, export the current directory to PYTHONPATH and use find with parallel to execute all files matching *_test.py.

    !export PYTHONPATH="`pwd`:$PYTHONPATH"
    !find . -name "*_test.py" | parallel python &> /tmp/test.log \
      && echo "All passed" || echo "Failed! Search keyword FAILED in /tmp/test.log"
  9. Perform inference on videos with EfficientDet

    master

    To run inference on a video, use model_inspect.py with the --runmode=saved_model_video flag. You can either display the results online or save the output to a video file.

    Required steps:

    1. Export the model using --runmode=saved_model.
    2. Run inference using --runmode=saved_model_video providing the --input_video path.
    # step 1: export saved model
    python model_inspect.py --runmode=saved_model \
      --model_name=efficientdet-d0 --ckpt_path=efficientdet-d0 \
      --saved_model_dir=/tmp/savedmodel --hparams=voc_config.yaml
    
    # step 2: inference video and save the result
    python model_inspect.py --runmode=saved_model_video \
      --model_name=efficientdet-d0   \
      --saved_model_dir=/tmp/savedmodel --input_video=input.mov  \
      --output_video=output.mov
  10. Train EfficientDet on TPUs

    master

    To train on Cloud TPU, you need a GCE VM with an associated Cloud TPU resource and a GCS bucket for checkpoints.

    Run the training command with the --tpu and --strategy=tpu flags:

    • --tpu: The name of the TPU node.
    • --train_file_pattern: GCS location (gs://...) for training TFRecords.
    • --model_dir: GCS location for checkpoints.
    • --strategy: Set to tpu.
    export PYTHONPATH="$PYTHONPATH:/path/to/models"
    python main.py --tpu=TPU_NAME --train_file_pattern=DATA_DIR/*.tfrecord --model_dir=MODEL_DIR --strategy=tpu