{"id":19510709,"url":"https://github.com/goruck/edge-tpu-train","last_synced_at":"2025-04-26T03:32:19.902Z","repository":{"id":39725887,"uuid":"280039996","full_name":"goruck/edge-tpu-train","owner":"goruck","description":"Use the TensorFlow Object Detection API to train models for the Google Coral Edge TPU.","archived":false,"fork":false,"pushed_at":"2022-12-08T11:09:42.000Z","size":32,"stargazers_count":13,"open_issues_count":8,"forks_count":10,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-04T07:35:58.764Z","etag":null,"topics":["coral-tpu","object-detection","tensorflow"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/goruck.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-07-16T03:10:47.000Z","updated_at":"2024-04-18T17:30:18.000Z","dependencies_parsed_at":"2023-01-25T15:16:41.151Z","dependency_job_id":null,"html_url":"https://github.com/goruck/edge-tpu-train","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goruck%2Fedge-tpu-train","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goruck%2Fedge-tpu-train/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goruck%2Fedge-tpu-train/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goruck%2Fedge-tpu-train/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/goruck","download_url":"https://codeload.github.com/goruck/edge-tpu-train/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250926905,"owners_count":21509057,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["coral-tpu","object-detection","tensorflow"],"created_at":"2024-11-10T23:16:48.905Z","updated_at":"2025-04-26T03:32:19.603Z","avatar_url":"https://github.com/goruck.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TensorFlow Object Detection API with Coral Edge TPU\n\nThis project uses the TensorFlow Object Detection API to train models suitable for the Google Coral Edge TPU. Follow the steps below to install the required programs and to train your own models for use on the Edge TPU.\n\n## Installation\n\n### Install TensorFlow Object Detection API\nFollow these installation [steps](https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/install.html).\n\n### Install edge-tpu-train\n```bash\n$ git clone https://github.com/goruck/models/tree/edge-tpu-train\n```\n\nCheck the ```requirements.txt``` file to ensure you have the necessary Python packages installed on your system or virtual environment. \n\n### Setup Data Set Directories\n\nUnder the ```annotations```, ```images```, ```tf-record``` and ```tflite-models``` directories in ```edge-tpu-train``` place a sub-directory named after your data set(s).\n\n### Install labelImg\nFollow these [steps](https://github.com/tzutalin/labelImg) to install labelImg, a great tool you can use to label your images that you'll use for training. \n\n## Collect Images\n\nPlace your data set images in the ```images/\u003cnamed-data-set\u003e``` directory you created in the step above.\n\nAbout 200 images per class is sufficient to re-train most models in my experience.\n\n## Label Images\n\nUse labelImg to label the images you collected. Store the xml annotation files in ```annotations/\u003cnamed-data-set\u003e```.\n\n## Create Label Map (.pbtxt)\n\nClasses need to be listed in the label map. Since in the case I am detecting the members of my family (including pets) the label map looks like this:\n\n```protobuf\nitem {\n    id: 1\n    name: 'lindo'\n}\nitem {\n    id: 2\n    name: 'nikki'\n}\nitem {\n    id: 3\n    name: 'eva'\n}\nitem {\n    id: 4\n    name: 'nico'\n}\nitem {\n    id: 5\n    name: 'polly'\n}\nitem {\n    id: 6\n    name: 'rebel'\n}\nitem {\n    id: 7\n    name: 'unknown'\n}\n```\n\nNote that id 0 is reserved. Store this file in the ```annotations/\u003cnamed-data-set\u003e``` folder with the name ```label_map.pbtxt```.\n\n## Create TFRecord (.record)\n\nTFRecord is an important data format designed for Tensorflow. (Read more about it [here](https://www.tensorflow.org/tutorials/load_data/tf_records)). Before you can train your custom object detector, you must convert your data into the TFRecord format.\n\nSince you need to train as well as validate your model, the data set will be split into training (```train.record```) and validation sets (```val.record```). The purpose of training set is straightforward - it is the set of examples the model learns from. The validation set is a set of examples used DURING TRAINING to iteratively assess model accuracy.\n\nUse the program [create_tf_record.py](./create_tf_record.py) to convert the data set into train.record and val.record.\n\nThis program is preconfigured to do 80–20 train-val split. Execute it by running:\n\n```bash\n$ python3 ./create_tf_record.py --dataset_name \u003cnamed-data-set\u003e\n```\n\nAs configured above the program will store the ``.record`` files to the ```tf_record/\u003cnamed-data-set\u003e``` folder. \n\n## Download pre-trained model\n\nThere are many pre-trained object detection models available in the model zoo but you need to limit your selection to those that can be converted to quantized TensorFlow Lite (object detection) models. (You must use [quantization-aware training](https://www.tensorflow.org/model_optimization/guide/quantization/training), so the model must be designed with fake quantization nodes.)\n\nIn order to train them using your custom data set, the models need to be restored in Tensorflow using their checkpoints (```.ckpt``` files), which are records of previous model states.\n\nFor this example download ```ssd_mobilenet_v2_quantized_coco``` from [here](http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v2_quantized_300x300_coco_2019_01_03.tar.gz) and save its model checkpoint files (```model.ckpt.meta```, ```model.ckpt.index```, ```model.ckpt.data-00000-of-00001```) to the ```checkpoints``` directory.\n\n## Modify Config (.config) File\n\nIf required (for example you are changing the number of classes from 7 used in this example to something else) modify the files in the ```config/\u003cnamed-data-set\u003e``` directory as needed. There should not be many changes required if using the scripts above as directed except for the name of your data set. \n\n## Re-train model\n\nFollow the steps below to re-train the model replacing the values for ```pipline_config_path``` and ```num_training_steps``` as needed. I found 1400 training steps to be sufficient in this example. \n\n```bash\n$ train.sh \\\n--pipeline_config_path ./configs/pipeline_mobilenet_v2_ssd_retrain_last_few_layers.config \\\n--num_training_steps 1400\n```\n\n## Monitor training progress\n\nStart tensorboard in a new terminal:\n\n```bash\n$ tensorboard --logdir ./train\n```\n\n## Convert model to TF Lite and compile it for the edge tpu\n\nRun the following script to export the model to a frozen graph, convert it to a TF Lite model and compile it to run on the edge TPU. Replace the pipeline configuration path as required and make sure the checkpoint number matches the last training step used in training the model.\n\nNB: this assumes the [Edge TPU Compiler](https://coral.withgoogle.com/docs/edgetpu/compiler/) has been installed on your system.\n\n```bash\n$ convert_checkpoint_to_edgetpu.sh \\\n--pipeline_config_path ./configs/pipeline_mobilenet_v2_ssd_retrain_last_few_layers.config \\\n--checkpoint_num 1400\n```\n\n## Run the model\n\nYou can now use the retrained and compiled model with the [Edge TPU Python API](https://coral.withgoogle.com/docs/edgetpu/api-intro/).\n\n## License\n\n[MIT](./LICENSE)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgoruck%2Fedge-tpu-train","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgoruck%2Fedge-tpu-train","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgoruck%2Fedge-tpu-train/lists"}