{"id":39069728,"url":"https://github.com/monocongo/tfod_ssd","last_synced_at":"2026-01-17T18:19:39.020Z","repository":{"id":97693398,"uuid":"231991208","full_name":"monocongo/tfod_ssd","owner":"monocongo","description":"Usage of the TensorFlow object detection API for training an SSD model using transfer learning with a custom dataset ","archived":false,"fork":false,"pushed_at":"2020-12-01T17:54:22.000Z","size":70,"stargazers_count":0,"open_issues_count":4,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-06-11T19:52:49.093Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/monocongo.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,"governance":null}},"created_at":"2020-01-05T23:45:16.000Z","updated_at":"2020-12-01T17:54:24.000Z","dependencies_parsed_at":"2023-06-02T18:00:12.886Z","dependency_job_id":null,"html_url":"https://github.com/monocongo/tfod_ssd","commit_stats":{"total_commits":36,"total_committers":1,"mean_commits":36.0,"dds":0.0,"last_synced_commit":"c6e1fbd5296524be2d31d7d83b4988341a9fac27"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/monocongo/tfod_ssd","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/monocongo%2Ftfod_ssd","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/monocongo%2Ftfod_ssd/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/monocongo%2Ftfod_ssd/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/monocongo%2Ftfod_ssd/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/monocongo","download_url":"https://codeload.github.com/monocongo/tfod_ssd/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/monocongo%2Ftfod_ssd/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28515293,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-17T17:57:59.192Z","status":"ssl_error","status_checked_at":"2026-01-17T17:57:52.527Z","response_time":85,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":[],"created_at":"2026-01-17T18:19:38.912Z","updated_at":"2026-01-17T18:19:39.003Z","avatar_url":"https://github.com/monocongo.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# tfod_ssd\nUsage of the TensorFlow object detection API for training an SSD model using transfer learning with a custom dataset \n\n### Install the TensorFlow Object Detection API\n1. Clone the [TensorFlow Object Detection API](https://github.com/tensorflow/models/tree/master/research/object_detection) \nfrom GitHub and set an environment variable to facilitate referencing the location:\n    ```\n    $ git clone git@github.com:tensorflow/models.git\n    $ cd models\n    $ export TFOD=`pwd`\n    ```\n\n2. The TensorFlow Object Detection API uses [protocol buffer](https://developers.google.com/protocol-buffers) \nfiles to configure model and training parameters. Before the framework can be used, \nthe Protobuf libraries must be compiled: \n    ```\n    $ sudo apt-get install protobuf-compiler\n    $ cd $TFOD/research\n    $ protoc object_detection/protos/*.proto --python_out=.\n    ```\n\n3. When running locally, the `research` and `research/slim` \ndirectories should be appended to the `PYTHONPATH` environment variable: \n    ```\n    $ cd $TFOD/research\n    $ export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim\n    ```\n4. In order to utilize COCO evaluation metrics to evaluate the accuracy of our model \nduring training we'll need to install the COCO Python API into the TensorFlow API:\n    ```\n    $ git clone https://github.com/cocodataset/cocoapi.git\n    $ cd cocoapi/PythonAPI\n    $ sudo make\n    $ cp -r pycocotools $TFOD/research/\n    ```\n\n### Create Python virtual environment\n1. Create a Python virtual environment containing all necessary TensorFlow packages. \nIn this example we'll use an environment created and managed using \n[Anaconda](https://www.anaconda.com/distribution/) but it's also possible to use \na [standard Python virtual environment](https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/).\n    ```\n    $ conda create -n tfod_ssd python=3.7 --yes\n    $ conda activate tfod_ssd\n    ```\n2. Install this project:\n    ```\n    $ git clone git@github.com:monocongo/tfod_ssd.git\n    $ cd tfod_ssd\n    $ pip install -e .\n    ```\n   \n### Create an experiment directory\nCreate a directory to contain the files used in this \"experiment\" (dataset, model \nconfiguration, class labels, etc.)\n```\n$ mkdir tfod_ssd\n$ cd tfod_ssd\n$ export EXPERIMENT=`pwd`\n```\n\n### Build a custom dataset\n\nAcquire a dataset in TFRecord format. Datasets with annotation files in other formats \ncan be converted/translated to TFRecord format using the [cvdata](https://github.com/monocongo/cvdata) \nPython package. We will create a directory in our experiment directory named `tfrecord`\nfor the TFRecord file(s).\n\nAs well as the TFRecord file(s) we should also have a labels file in protobuf text \nformat that lists items containing class IDs and their corresponding labels. \nFor example, if the TFRecord files for an animals dataset uses the IDs/labels (1: cat, \n2: dog, and 3: panda) then the labels file will look like so:\n```\nitem {\n id: 1\n name: 'cat'\n}\nitem {\n id: 2\n name: 'dog'\n}\nitem {\n id: 3\n name: 'panda'\n}\n```\n\n### Model configuration\nThe model we’ll in this example is the Single Shot Detector(SSD) with MobileNet \nmodel (optimized for inference on mobile) pretrained on the COCO dataset called \n[ssd_mobilenet_v2_quantized_coco](http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v2_quantized_300x300_coco_2019_01_03.tar.gz) \ntaken from the [TensorFlow Detection Model Zoo](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md).\n\n1. Download and unpack the model archive\n    ```\n    $ wget http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v2_quantized_300x300_coco_2019_01_03.tar.gz\n    $ tar -xzf ssd_mobilenet_v2_quantized_300x300_coco_2019_01_03.tar.gz\n    ```\n\n2. Configure the model training configuration file by copying an example configuration \nfrom the TensorFlow OD API into another location (in our case a directory named \n`training`) and modify it for our training dataset.\n    ```\n    $ mkdir $EXPERIMENT/training\n    $ cp $TFOD/research/object_detection/samples/configs/ssd_mobilenet_v2_quantized_300x300_coco.config $EXPERIMENT/training\n    $ export CONFIG=$EXPERIMENT/training/ssd_mobilenet_v2_quantized_300x300_coco.config\n    ```\n   Update the file as follows:\n   \n   Line 9: set the number of classes to the number of labeled classes in our dataset\n   ```\n   num_classes: 5\n   ```\n\n   Line 156: set the path to the checkpoint file we'll use as the starting point for the training\n   ```\n   fine_tune_checkpoint: “/home/james/tfod_ssd/ssd_mobilenet_v2_quantized_300x300_coco_2019_01_03/model.ckpt\"\n   ```\n\n   Line 175: set the path to the training dataset TFRecord file(s)\n   ```\n   input_path: \"/home/james/tfod_ssd/tfrecord/train.record-?????-of-00100\n   ``` \n\n   Line 177 and 191: set the path to the dataset's label map protobuf text file\n   ```\n   label_map_path: \"/home/james/tfod_ssd/tfrecord/label_map.pbtxt\"\n   ```\n   \n   Line 189: set the path to the evaluation dataset TFRecord file(s)\n   ```\n   input_path: \"/home/james/tfod_ssd/tfrecord/eval.record-?????-of-00100\n   ``` \n   \n   Line 181: set the number of examples in the evaluation dataset\n   ```\n   num_examples: 8000\n   ```\n   \n   In the `eval_config` section we will comment out the `max_evals` entry and add \n   the following line to enable the COCO evaluation metrics:\n   ```\n   metrics_set: \"coco_detection_metrics\"\n   ```\n\n### Train the model\n```\n$ cd $TFOD/research/object_detection\n$ cp $TFOD/research/object_detection/legacy/train.py .\n$ python train.py --train_dir=$EXPERIMENT/training --pipeline_config_path=$CONFIG\n```\n### TensorBoard\nIn order to monitor the training progress we can run TensorBoard. The various events \nused by TensorBoard are logged to `$EXPERIMENT/training`, hence we'll launch the \nprogram like so:\n```bash\n$ tensorboard --logdir=$EXPERIMENT/training\n```\nBy default the port used is 6006, so we'll view the progress of the training on \nTensorBoard using http://localhost:6006 (replace `localhost` with the public IP \naddress if running the training on a remote/cloud instance).\n\n### Prepare for inference\nOnce we have a checkpoint file (after the model has completed training) then we \ncan export/freeze the inference graph and then optimize for inference. For example, \nif the final model checkpoint occurs at 9085 steps, resulting in a collection of \ncheckpoint files with prefix `$EXPERIMENT/training/model.ckpt-9085` (including \nfiles `model.ckpt-9085.data-00000-of-00001`, `model.ckpt-9085.index`, and \n`model.ckpt-9085.meta`) then the inference graph is frozen and optimized like so:\n```bash\n$ export EXPORT=$EXPERIMENT/training/export_9085\n$ export CHECKPOINT=$EXPERIMENT/training/model.ckpt-9085\n$ python $TFOD/research/object_detection/export_inference_graph.py --input_type image_tensor --pipeline_config_path $CONFIG --trained_checkpoint_prefix $CHECKPOINT --output_directory $EXPORT\n$ export GRAPH=$EXPORT/frozen_inference_graph.pb\n```\n\n### Perform inference\nUtilize the CLI for inference on a single image, collection of images in a directory:\n```bash\n$ object_detect_images --graph $GRAPH --labelmap /home/james/experiments/tfod_ssd/tfrecord_label_map.prototxt --images /home/james/data/test/imgs /home/james/data/test/rifle_00827.jpg\n```\nUtilize the CLI for inference on a video file, RTSP URL, or webcam:\n```bash\n$ object_detect_video --graph $GRAPH --labelmap /home/james/experiments/tfod_ssd/tfrecord_label_map.prototxt --videosrc example.mp4\n```\n\n### Optimization for NVIDIA Jetson (optional)\nIn order to deploy on an NIVDIA Jetson Nano or TX2 device we need to build and \noptimize a graph using the [TensorRT SDK](https://developer.nvidia.com/tensorrt).\n1. Install the [NVIDIA TensorFlow/TensorRT](https://github.com/NVIDIA-AI-IOT/tf_trt_models) \npackage:\n    ```bash\n   $ cd ${GIT_DIR}\n   $ git clone --recursive https://github.com/NVIDIA-Jetson/tf_trt_models.git\n   $ cd tf_trt_models\n   $ ./install.sh python3\n   ```\n2. Build an inference graph optimized for TensorRT from the model configuration \nand checkpoint files:\n   ```bash\n   $ export TRT_GRAPH=$EXPORT/ssd_mobilenet_v2_quantized_300x300_coco_trt.pb\n   $ build_trt_graph --config $CONFIG \\\n        --checkpoint $CHECKPOINT \\\n        --confidence 0.5 \\\n        --trt_graph $TRT_GRAPH \n   ```\n   *NOTE*: the above needs to be performed on the target platform where the object \n   detection application will be deployed so that the graph is optimized for the \n   appropriate NVIDIA GPU. \n   \nAt this point we can now run the object detection script(s) utilizing the TensorRT \noptimized graph:\n\nUtilize the CLI for inference on a single image, collection of images in a directory:\n```bash\n$ object_detect_images --graph $TRT_GRAPH --labelmap /home/james/experiments/tfod_ssd/tfrecord_label_map.prototxt --images /home/james/data/test/imgs /home/james/data/test/rifle_00827.jpg\n```\nUtilize the CLI for inference on a video file, RTSP URL, or webcam:\n```bash\n$ object_detect_video --graph $TRT_GRAPH --labelmap /home/james/experiments/tfod_ssd/tfrecord_label_map.prototxt --videosrc example.mp4\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmonocongo%2Ftfod_ssd","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmonocongo%2Ftfod_ssd","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmonocongo%2Ftfod_ssd/lists"}