{"id":13435606,"url":"https://github.com/zzh8829/yolov3-tf2","last_synced_at":"2025-05-14T19:08:02.922Z","repository":{"id":37444373,"uuid":"179342663","full_name":"zzh8829/yolov3-tf2","owner":"zzh8829","description":"YoloV3 Implemented in Tensorflow 2.0","archived":false,"fork":false,"pushed_at":"2024-08-30T23:51:23.000Z","size":4347,"stargazers_count":2511,"open_issues_count":171,"forks_count":899,"subscribers_count":75,"default_branch":"master","last_synced_at":"2025-05-14T19:07:57.292Z","etag":null,"topics":["deep-learning","machine-learning","neural-network","object-detection","tensorflow","tensorflow-examples","tensorflow-tutorials","tf2","yolo","yolov3"],"latest_commit_sha":null,"homepage":null,"language":"Jupyter Notebook","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/zzh8829.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,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-04-03T17:57:49.000Z","updated_at":"2025-05-02T12:15:37.000Z","dependencies_parsed_at":"2022-08-08T20:16:01.671Z","dependency_job_id":"d6ffb866-bf83-4641-bf88-d427142ac9a7","html_url":"https://github.com/zzh8829/yolov3-tf2","commit_stats":{"total_commits":86,"total_committers":23,"mean_commits":3.739130434782609,"dds":"0.36046511627906974","last_synced_commit":"2784050f2fc1ff060f4c8b3ac2db231370569aa8"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zzh8829%2Fyolov3-tf2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zzh8829%2Fyolov3-tf2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zzh8829%2Fyolov3-tf2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zzh8829%2Fyolov3-tf2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zzh8829","download_url":"https://codeload.github.com/zzh8829/yolov3-tf2/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254209859,"owners_count":22032897,"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":["deep-learning","machine-learning","neural-network","object-detection","tensorflow","tensorflow-examples","tensorflow-tutorials","tf2","yolo","yolov3"],"created_at":"2024-07-31T03:00:37.362Z","updated_at":"2025-05-14T19:08:01.861Z","avatar_url":"https://github.com/zzh8829.png","language":"Jupyter Notebook","funding_links":[],"categories":["Jupyter Notebook","Model Implementations","Unofficial","Table of Contents","Other Versions of YOLO","Sample Codes / Projects \u003ca name=\"sample\" /\u003e ⛏️📐📁","Model 💛💛💛💛💛\u003ca name=\"Model\" /\u003e"],"sub_categories":["Reinforcement Learning \u003ca name=\"RL\" /\u003e🔮","Object detection 目标检测"],"readme":"# YoloV3 Implemented in TensorFlow 2.0\n\n[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/zzh8829/yolov3-tf2/blob/master/colab_gpu.ipynb)\n\nThis repo provides a clean implementation of YoloV3 in TensorFlow 2.0 using all the best practices.\n\n## Key Features\n\n- [x] TensorFlow 2.0\n- [x] `yolov3` with pre-trained Weights\n- [x] `yolov3-tiny` with pre-trained Weights\n- [x] Inference example\n- [x] Transfer learning example\n- [x] Eager mode training with `tf.GradientTape`\n- [x] Graph mode training with `model.fit`\n- [x] Functional model with `tf.keras.layers`\n- [x] Input pipeline using `tf.data`\n- [x] Tensorflow Serving\n- [x] Vectorized transformations\n- [x] GPU accelerated\n- [x] Fully integrated with `absl-py` from [abseil.io](https://abseil.io)\n- [x] Clean implementation\n- [x] Following the best practices\n- [x] MIT License\n\n![demo](https://raw.githubusercontent.com/zzh8829/yolov3-tf2/master/data/meme_out.jpg)\n![demo](https://raw.githubusercontent.com/zzh8829/yolov3-tf2/master/data/street_out.jpg)\n\n## Usage\n\n### Installation\n\n#### Conda (Recommended)\n\n```bash\n# Tensorflow CPU\nconda env create -f conda-cpu.yml\nconda activate yolov3-tf2-cpu\n\n# Tensorflow GPU\nconda env create -f conda-gpu.yml\nconda activate yolov3-tf2-gpu\n```\n\n#### Pip\n\n```bash\npip install -r requirements.txt\n```\n\n### Nvidia Driver (For GPU)\n\n```bash\n# Ubuntu 18.04\nsudo apt-add-repository -r ppa:graphics-drivers/ppa\nsudo apt install nvidia-driver-430\n# Windows/Other\nhttps://www.nvidia.com/Download/index.aspx\n```\n\n### Convert pre-trained Darknet weights\n\n```bash\n# yolov3\nwget https://pjreddie.com/media/files/yolov3.weights -O data/yolov3.weights\npython convert.py --weights ./data/yolov3.weights --output ./checkpoints/yolov3.tf\n\n# yolov3-tiny\nwget https://pjreddie.com/media/files/yolov3-tiny.weights -O data/yolov3-tiny.weights\npython convert.py --weights ./data/yolov3-tiny.weights --output ./checkpoints/yolov3-tiny.tf --tiny\n```\n\n### Detection\n\n```bash\n# yolov3\npython detect.py --image ./data/meme.jpg\n\n# yolov3-tiny\npython detect.py --weights ./checkpoints/yolov3-tiny.tf --tiny --image ./data/street.jpg\n\n# webcam\npython detect_video.py --video 0\n\n# video file\npython detect_video.py --video path_to_file.mp4 --weights ./checkpoints/yolov3-tiny.tf --tiny\n\n# video file with output\npython detect_video.py --video path_to_file.mp4 --output ./output.avi\n```\n\n### Training\n\nI have created a complete tutorial on how to train from scratch using the VOC2012 Dataset.\nSee the documentation here https://github.com/zzh8829/yolov3-tf2/blob/master/docs/training_voc.md\n\nFor customzied training, you need to generate tfrecord following the TensorFlow Object Detection API.\nFor example you can use [Microsoft VOTT](https://github.com/Microsoft/VoTT) to generate such dataset.\nYou can also use this [script](https://github.com/tensorflow/models/blob/master/research/object_detection/dataset_tools/create_pascal_tf_record.py) to create the pascal voc dataset.\n\nExample commend line arguments for training\n``` bash\npython train.py --batch_size 8 --dataset ~/Data/voc2012.tfrecord --val_dataset ~/Data/voc2012_val.tfrecord --epochs 100 --mode eager_tf --transfer fine_tune\n\npython train.py --batch_size 8 --dataset ~/Data/voc2012.tfrecord --val_dataset ~/Data/voc2012_val.tfrecord --epochs 100 --mode fit --transfer none\n\npython train.py --batch_size 8 --dataset ~/Data/voc2012.tfrecord --val_dataset ~/Data/voc2012_val.tfrecord --epochs 100 --mode fit --transfer no_output\n\npython train.py --batch_size 8 --dataset ~/Data/voc2012.tfrecord --val_dataset ~/Data/voc2012_val.tfrecord --epochs 10 --mode eager_fit --transfer fine_tune --weights ./checkpoints/yolov3-tiny.tf --tiny\n```\n\n### Tensorflow Serving\nYou can export the model to tf serving\n```\npython export_tfserving.py --output serving/yolov3/1/\n# verify tfserving graph\nsaved_model_cli show --dir serving/yolov3/1/ --tag_set serve --signature_def serving_default\n```\n\nThe inputs are preprocessed images (see `dataset.transform_iamges`)\n\noutputs are\n```\nyolo_nms_0: bounding boxes\nyolo_nms_1: scores\nyolo_nms_2: classes\nyolo_nms_3: numbers of valid detections\n```\n\n## Benchmark (No Training Yet)\n\nNumbers are obtained with rough calculations from `detect_video.py`\n\n### Macbook Pro 13 (2.7GHz i5)\n\n| Detection   | 416x416 | 320x320 | 608x608 |\n|-------------|---------|---------|---------|\n| YoloV3      | 1000ms  | 500ms   | 1546ms  |\n| YoloV3-Tiny | 100ms   | 58ms    | 208ms   |\n\n### Desktop PC (GTX 970)\n\n| Detection   | 416x416 | 320x320 | 608x608 |\n|-------------|---------|---------|---------|\n| YoloV3      | 74ms    | 57ms    | 129ms   |\n| YoloV3-Tiny | 18ms    | 15ms    | 28ms    |\n\n### AWS g3.4xlarge (Tesla M60)\n\n| Detection   | 416x416 | 320x320 | 608x608 |\n|-------------|---------|---------|---------|\n| YoloV3      | 66ms    | 50ms    | 123ms   |\n| YoloV3-Tiny | 15ms    | 10ms    | 24ms    |\n\n### RTX 2070 (credit to @AnaRhisT94)\n\n| Detection   | 416x416 |\n|-------------|---------|\n| YoloV3 predict_on_batch     | 29-32ms    | \n| YoloV3 predict_on_batch + TensorRT     | 22-28ms    | \n\n\nDarknet version of YoloV3 at 416x416 takes 29ms on Titan X.\nConsidering Titan X has about double the benchmark of Tesla M60,\nPerformance-wise this implementation is pretty comparable.\n\n## Implementation Details\n\n### Eager execution\n\nGreat addition for existing TensorFlow experts.\nNot very easy to use without some intermediate understanding of TensorFlow graphs.\nIt is annoying when you accidentally use incompatible features like tensor.shape[0]\nor some sort of python control flow that works fine in eager mode, but\ntotally breaks down when you try to compile the model to graph.\n\n### model(x) vs. model.predict(x)\n\nWhen calling model(x) directly, we are executing the graph in eager mode. For\n`model.predict`, tf actually compiles the graph on the first run and then\nexecute in graph mode. So if you are only running the model once, `model(x)` is\nfaster since there is no compilation needed. Otherwise, `model.predict` or\nusing exported SavedModel graph is much faster (by 2x). For non real-time usage,\n`model.predict_on_batch` is even faster as tested by @AnaRhisT94)\n\n### GradientTape\n\nExtremely useful for debugging purpose, you can set breakpoints anywhere.\nYou can compile all the keras fitting functionalities with gradient tape using the\n`run_eagerly` argument in model.compile. From my limited testing, all training methods\nincluding GradientTape, keras.fit, eager or not yeilds similar performance. But graph\nmode is still preferred since it's a tiny bit more efficient.\n\n### @tf.function\n\n@tf.function is very cool. It's like an in-between version of eager and graph.\nYou can step through the function by disabling tf.function and then gain\nperformance when you enable it in production. Important note, you should not\npass any non-tensor parameter to @tf.function, it will cause re-compilation\non every call. I am not sure whats the best way other than using globals.\n\n### absl.py (abseil)\n\nAbsolutely amazing. If you don't know already, absl.py is officially used by\ninternal projects at Google. It standardizes application interface for Python\nand many other languages. After using it within Google, I was so excited\nto hear abseil going open source. It includes many decades of best practices\nlearned from creating large size scalable applications. I literally have\nnothing bad to say about it, strongly recommend absl.py to everybody.\n\n### Loading pre-trained Darknet weights\n\nvery hard with pure functional API because the layer ordering is different in\ntf.keras and darknet. The clean solution here is creating sub-models in keras.\nKeras is not able to save nested model in h5 format properly, TF Checkpoint is\nrecommended since its offically supported by TensorFlow.\n\n### tf.keras.layers.BatchNormalization\n\nIt doesn't work very well for transfer learning. There are many articles and\ngithub issues all over the internet. I used a simple hack to make it work nicer\non transfer learning with small batches.\n\n### What is the output of transform_targets ???\n\nI know it's very confusion but the output is tuple of shape\n```\n(\n  [N, 13, 13, 3, 6],\n  [N, 26, 26, 3, 6],\n  [N, 52, 52, 3, 6]\n)\n```\nwhere N is the number of labels in batch and the last dimension \"6\" represents\n`[x, y, w, h, obj, class]` of the bounding boxes.\n\n### IOU and Score Threshold\n\nthe default threshold is 0.5 for both IOU and score, you can adjust them\naccording to your need by setting `--yolo_iou_threshold` and\n`--yolo_score_threshold` flags\n\n### Maximum number of boxes\n\nBy default there can be maximum 100 bounding boxes per image, \nif for some reason you would like to have more boxes you can use the `--yolo_max_boxes` flag.\n\n### NAN Loss / Training Failed / Doesn't Converge \n\nMany people including me have succeeded in training, so the code definitely works\n@LongxingTan in https://github.com/zzh8829/yolov3-tf2/issues/128 provided some of his insights summarized here:\n  \n  1. For nan loss, try to make learning rate smaller\n  2. Double check the format of your input data. Data input labelled by vott and labelImg is different. so make sure the input box is the right, and check carefully the format is `x1/width,y1/height,x2/width,y2/height` and **NOT** x1,y1,x2,y2, or x,y,w,h\n\nMake sure to visualize your custom dataset using this tool\n```\npython tools/visualize_dataset.py --classes=./data/voc2012.names\n```\n\nIt will output one random image from your dataset with label to `output.jpg`\nTraining definitely won't work if the rendered label doesn't look correct\n\n## Command Line Args Reference\n\n```bash\nconvert.py:\n  --output: path to output\n    (default: './checkpoints/yolov3.tf')\n  --[no]tiny: yolov3 or yolov3-tiny\n    (default: 'false')\n  --weights: path to weights file\n    (default: './data/yolov3.weights')\n  --num_classes: number of classes in the model\n    (default: '80')\n    (an integer)\n\ndetect.py:\n  --classes: path to classes file\n    (default: './data/coco.names')\n  --image: path to input image\n    (default: './data/girl.png')\n  --output: path to output image\n    (default: './output.jpg')\n  --[no]tiny: yolov3 or yolov3-tiny\n    (default: 'false')\n  --weights: path to weights file\n    (default: './checkpoints/yolov3.tf')\n  --num_classes: number of classes in the model\n    (default: '80')\n    (an integer)\n\ndetect_video.py:\n  --classes: path to classes file\n    (default: './data/coco.names')\n  --video: path to input video (use 0 for cam)\n    (default: './data/video.mp4')\n  --output: path to output video (remember to set right codec for given format. e.g. XVID for .avi)\n    (default: None)\n  --output_format: codec used in VideoWriter when saving video to file\n    (default: 'XVID)\n  --[no]tiny: yolov3 or yolov3-tiny\n    (default: 'false')\n  --weights: path to weights file\n    (default: './checkpoints/yolov3.tf')\n  --num_classes: number of classes in the model\n    (default: '80')\n    (an integer)\n\ntrain.py:\n  --batch_size: batch size\n    (default: '8')\n    (an integer)\n  --classes: path to classes file\n    (default: './data/coco.names')\n  --dataset: path to dataset\n    (default: '')\n  --epochs: number of epochs\n    (default: '2')\n    (an integer)\n  --learning_rate: learning rate\n    (default: '0.001')\n    (a number)\n  --mode: \u003cfit|eager_fit|eager_tf\u003e: fit: model.fit, eager_fit: model.fit(run_eagerly=True), eager_tf: custom GradientTape\n    (default: 'fit')\n  --num_classes: number of classes in the model\n    (default: '80')\n    (an integer)\n  --size: image size\n    (default: '416')\n    (an integer)\n  --[no]tiny: yolov3 or yolov3-tiny\n    (default: 'false')\n  --transfer: \u003cnone|darknet|no_output|frozen|fine_tune\u003e: none: Training from scratch, darknet: Transfer darknet, no_output: Transfer all but output, frozen: Transfer and freeze all,\n    fine_tune: Transfer all and freeze darknet only\n    (default: 'none')\n  --val_dataset: path to validation dataset\n    (default: '')\n  --weights: path to weights file\n    (default: './checkpoints/yolov3.tf')\n```\n\n## Change Log\n\n#### October 1, 2019\n\n- Updated to Tensorflow to v2.0.0 Release\n\n\n## References\n\nIt is pretty much impossible to implement this from the yolov3 paper alone. I had to reference the official (very hard to understand) and many un-official (many minor errors) repos to piece together the complete picture.\n\n- https://github.com/pjreddie/darknet\n    - official yolov3 implementation\n- https://github.com/AlexeyAB\n    - explinations of parameters\n- https://github.com/qqwweee/keras-yolo3\n    - models\n    - loss functions\n- https://github.com/YunYang1994/tensorflow-yolov3\n    - data transformations\n    - loss functions\n- https://github.com/ayooshkathuria/pytorch-yolo-v3\n    - models\n- https://github.com/broadinstitute/keras-resnet\n    - batch normalization fix\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzzh8829%2Fyolov3-tf2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzzh8829%2Fyolov3-tf2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzzh8829%2Fyolov3-tf2/lists"}