google-automl
repository·master·Indexed 27 days ago
https://github.com/google/automlImplementation 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.
What's inside google-automl
- 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.
Export EfficientDet to ONNX
masterTo export a SavedModel to ONNX format:
- Install
tf2onnx:pip install tf2onnx. - Configure NMS (Non-Maximum Suppression) settings. Note that ONNX does not support soft NMS, so use
method: 'hard'. - 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- Install
Train EfficientDet on Cloud TPU
masterTo train on Cloud TPU, you need a GCE VM with an associated Cloud TPU resource and a GCS bucket for checkpoints.
Set
PYTHONPATHto include the models directory and use the--tpuflag with yourTPU_NAMEand--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=tpuFormat code for Pull Requests
masterUse
yapfwith the following style option:--style='{based_on_style: yapf}'.Alternatively, you can save this configuration to
~/.config/yapf/style:[style] based_on_style = yapfTo check formatting with lint, run:
!pylint --rcfile=../.pylintrc your_file.pyConvert COCO validation set to tfrecord
masterTo convert a COCO validation dataset into the
tfrecordformat, you must first download the COCO images and annotations, unzip them, and then run thedataset/create_coco_tfrecord.pyscript.Important Note: The
source_id(orimage_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=32Finetune EfficientDet on PASCAL VOC 2012
masterTo finetune an EfficientDet model using a COCO checkpoint on the PASCAL VOC 2012 dataset, follow these steps:
- Convert PASCAL VOC data to TFRecord using
dataset/create_pascal_tfrecord.py. - Create a
voc_config.yamlfile containing the dataset configuration (classes, learning rate, etc.). - Run
train.pyproviding the--pretrained_ckptflag to point to your COCO checkpoint.
Note:
num_epochsis 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- Convert PASCAL VOC data to TFRecord using
Train EfficientDet on single or multi-node GPUs
masterSingle Node GPUs
Add the
--strategy=gpusflag to your training command.Multi-Node GPUs
Use
python -m tf2.trainwith--strategy=multi-gpus. You must specify--workeraddresses for all nodes and assign a unique--worker_index(starting from 0) to each node.Quick start with EfficientDet tutorial
masterFor a guided introduction to using EfficientDet, including visualization and COCO evaluation, you can use the provided Jupyter notebook tutorial.
[tutorial.ipynb](tutorial.ipynb)Run all tests
masterTo run all tests in the repository, export the current directory to
PYTHONPATHand usefindwithparallelto 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"Perform inference on videos with EfficientDet
masterTo run inference on a video, use
model_inspect.pywith the--runmode=saved_model_videoflag. You can either display the results online or save the output to a video file.Required steps:
- Export the model using
--runmode=saved_model. - Run inference using
--runmode=saved_model_videoproviding the--input_videopath.
# 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- Export the model using
Train EfficientDet on TPUs
masterTo 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
--tpuand--strategy=tpuflags:--tpu: The name of the TPU node.--train_file_pattern: GCS location (gs://...) for training TFRecords.--model_dir: GCS location for checkpoints.--strategy: Set totpu.
export PYTHONPATH="$PYTHONPATH:/path/to/models" python main.py --tpu=TPU_NAME --train_file_pattern=DATA_DIR/*.tfrecord --model_dir=MODEL_DIR --strategy=tpuDebug EfficientDet models
masterTo debug the model, append the--debugflag to your command. This enables eager execution and deterministic operations, allowing you to usepdbfor debugging.