{"id":13642895,"url":"https://github.com/theAIGuysCode/yolov4-custom-functions","last_synced_at":"2025-04-20T21:32:04.205Z","repository":{"id":37695507,"uuid":"284466437","full_name":"theAIGuysCode/yolov4-custom-functions","owner":"theAIGuysCode","description":"A Wide Range of Custom Functions for YOLOv4, YOLOv4-tiny, YOLOv3, and YOLOv3-tiny Implemented in TensorFlow, TFLite, and TensorRT.","archived":false,"fork":false,"pushed_at":"2023-01-30T00:33:28.000Z","size":63503,"stargazers_count":600,"open_issues_count":82,"forks_count":372,"subscribers_count":21,"default_branch":"master","last_synced_at":"2024-08-02T01:17:32.975Z","etag":null,"topics":["custom-yolov4","object-detection","tensorflow","tensorrt","tf2","tflite","yolov3","yolov4","yolov4-tiny"],"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/theAIGuysCode.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-08-02T13:25:04.000Z","updated_at":"2024-06-20T03:56:48.000Z","dependencies_parsed_at":"2022-07-09T04:17:05.468Z","dependency_job_id":"0899a6e4-cb44-4681-8429-b2f8e280cad2","html_url":"https://github.com/theAIGuysCode/yolov4-custom-functions","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/theAIGuysCode%2Fyolov4-custom-functions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theAIGuysCode%2Fyolov4-custom-functions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theAIGuysCode%2Fyolov4-custom-functions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theAIGuysCode%2Fyolov4-custom-functions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/theAIGuysCode","download_url":"https://codeload.github.com/theAIGuysCode/yolov4-custom-functions/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223839131,"owners_count":17211878,"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":["custom-yolov4","object-detection","tensorflow","tensorrt","tf2","tflite","yolov3","yolov4","yolov4-tiny"],"created_at":"2024-08-02T01:01:37.757Z","updated_at":"2025-04-20T21:32:04.185Z","avatar_url":"https://github.com/theAIGuysCode.png","language":"Python","funding_links":[],"categories":["Other Versions of YOLO"],"sub_categories":[],"readme":"# yolov4-custom-functions\r\n[![license](https://img.shields.io/github/license/mashape/apistatus.svg)](LICENSE)\r\n\r\nA wide range of custom functions for YOLOv4, YOLOv4-tiny, YOLOv3, and YOLOv3-tiny implemented in TensorFlow, TFLite and TensorRT.\r\n\r\nDISCLAIMER: This repository is very similar to my repository: [tensorflow-yolov4-tflite](https://github.com/theAIGuysCode/tensorflow-yolov4-tflite). I created this repository to explore coding custom functions to be implemented with YOLOv4, and they may worsen the overal speed of the application and make it not optimized in respect to time complexity. So if you want to run the most optimal YOLOv4 code with TensorFlow than head over to my other repository. This one is to explore cool customizations and applications that can be created using YOLOv4!\r\n\r\n### Demo of Object Counter Custom Function in Action!\r\n\u003cp align=\"center\"\u003e\u003cimg src=\"data/helpers/object_counter.gif\"\\\u003e\u003c/p\u003e\r\n\r\n## Currently Supported Custom Functions and Flags\r\n* [x] [Counting Objects (total objects and per class)](#counting)\r\n* [x] [Print Info About Each Detection (class, confidence, bounding box coordinates)](#info)\r\n* [x] [Crop Detections and Save as New Image](#crop)\r\n* [x] [License Plate Recognition Using Tesseract OCR](#license)\r\n* [x] [Apply Tesseract OCR to Detections to Extract Text](#ocr)\r\n\r\nIf there is a custom function you want to see created then create an issue in the issues tab and suggest it! If enough people suggest the same custom function I will add it quickly!\r\n\r\n## Getting Started\r\n### Conda (Recommended)\r\n\r\n```bash\r\n# Tensorflow CPU\r\nconda env create -f conda-cpu.yml\r\nconda activate yolov4-cpu\r\n\r\n# Tensorflow GPU\r\nconda env create -f conda-gpu.yml\r\nconda activate yolov4-gpu\r\n```\r\n\r\n### Pip\r\n```bash\r\n# TensorFlow CPU\r\npip install -r requirements.txt\r\n\r\n# TensorFlow GPU\r\npip install -r requirements-gpu.txt\r\n```\r\n### Nvidia Driver (For GPU, if you are not using Conda Environment and haven't set up CUDA yet)\r\nMake sure to use CUDA Toolkit version 10.1 as it is the proper version for the TensorFlow version used in this repository.\r\nhttps://developer.nvidia.com/cuda-10.1-download-archive-update2\r\n\r\n## Downloading Official Pre-trained Weights\r\nYOLOv4 comes pre-trained and able to detect 80 classes. For easy demo purposes we will use the pre-trained weights.\r\nDownload pre-trained yolov4.weights file: https://drive.google.com/open?id=1cewMfusmPjYWbrnuJRuKhPMwRe_b9PaT\r\n\r\nCopy and paste yolov4.weights from your downloads folder into the 'data' folder of this repository.\r\n\r\nIf you want to use yolov4-tiny.weights, a smaller model that is faster at running detections but less accurate, download file here: https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v4_pre/yolov4-tiny.weights\r\n\r\n## Using Custom Trained YOLOv4 Weights\r\n\u003cstrong\u003eLearn How To Train Custom YOLOv4 Weights here: https://www.youtube.com/watch?v=mmj3nxGT2YQ \u003c/strong\u003e\r\n\r\n\u003cstrong\u003eWatch me Walk-Through using Custom Model in TensorFlow :https://www.youtube.com/watch?v=nOIVxi5yurE \u003c/strong\u003e\r\n\r\nUSE MY LICENSE PLATE TRAINED CUSTOM WEIGHTS: https://drive.google.com/file/d/1EUPtbtdF0bjRtNjGv436vDY28EN5DXDH/view?usp=sharing\r\n\r\nCopy and paste your custom .weights file into the 'data' folder and copy and paste your custom .names into the 'data/classes/' folder.\r\n\r\nThe only change within the code you need to make in order for your custom model to work is on line 14 of 'core/config.py' file.\r\nUpdate the code to point at your custom .names file as seen below. (my custom .names file is called custom.names but yours might be named differently)\r\n\u003cp align=\"center\"\u003e\u003cimg src=\"data/helpers/custom_config.png\" width=\"640\"\\\u003e\u003c/p\u003e\r\n\r\n\u003cstrong\u003eNote:\u003c/strong\u003e If you are using the pre-trained yolov4 then make sure that line 14 remains \u003cstrong\u003ecoco.names\u003c/strong\u003e.\r\n\r\n## YOLOv4 Using Tensorflow (tf, .pb model)\r\nTo implement YOLOv4 using TensorFlow, first we convert the .weights into the corresponding TensorFlow model files and then run the model.\r\n```bash\r\n# Convert darknet weights to tensorflow\r\n## yolov4\r\npython save_model.py --weights ./data/yolov4.weights --output ./checkpoints/yolov4-416 --input_size 416 --model yolov4 \r\n\r\n# Run yolov4 tensorflow model\r\npython detect.py --weights ./checkpoints/yolov4-416 --size 416 --model yolov4 --images ./data/images/kite.jpg\r\n\r\n# Run yolov4 on video\r\npython detect_video.py --weights ./checkpoints/yolov4-416 --size 416 --model yolov4 --video ./data/video/video.mp4 --output ./detections/results.avi\r\n\r\n# Run yolov4 on webcam\r\npython detect_video.py --weights ./checkpoints/yolov4-416 --size 416 --model yolov4 --video 0 --output ./detections/results.avi\r\n```\r\nIf you want to run yolov3 or yolov3-tiny change ``--model yolov3`` and .weights file in above commands.\r\n\r\n\u003cstrong\u003eNote:\u003c/strong\u003e You can also run the detector on multiple images at once by changing the --images flag like such ``--images \"./data/images/kite.jpg, ./data/images/dog.jpg\"``\r\n\r\n### Result Image(s) (Regular TensorFlow)\r\nYou can find the outputted image(s) showing the detections saved within the 'detections' folder.\r\n#### Pre-trained YOLOv4 Model Example\r\n\u003cp align=\"center\"\u003e\u003cimg src=\"data/helpers/result.png\" width=\"640\"\\\u003e\u003c/p\u003e\r\n\r\n### Result Video\r\nVideo saves wherever you point --output flag to. If you don't set the flag then your video will not be saved with detections on it.\r\n\u003cp align=\"center\"\u003e\u003cimg src=\"data/helpers/demo.gif\"\\\u003e\u003c/p\u003e\r\n\r\n## YOLOv4-Tiny using TensorFlow\r\nThe following commands will allow you to run yolov4-tiny model.\r\n```\r\n# yolov4-tiny\r\npython save_model.py --weights ./data/yolov4-tiny.weights --output ./checkpoints/yolov4-tiny-416 --input_size 416 --model yolov4 --tiny\r\n\r\n# Run yolov4-tiny tensorflow model\r\npython detect.py --weights ./checkpoints/yolov4-tiny-416 --size 416 --model yolov4 --images ./data/images/kite.jpg --tiny\r\n```\r\n\u003ca name=\"custom\"/\u003e\r\n\r\n## Custom YOLOv4 Using TensorFlow\r\nThe following commands will allow you to run your custom yolov4 model. (video and webcam commands work as well)\r\n```\r\n# custom yolov4\r\npython save_model.py --weights ./data/custom.weights --output ./checkpoints/custom-416 --input_size 416 --model yolov4 \r\n\r\n# Run custom yolov4 tensorflow model\r\npython detect.py --weights ./checkpoints/custom-416 --size 416 --model yolov4 --images ./data/images/car.jpg\r\n```\r\n\r\n#### Custom YOLOv4 Model Example (see video link above to train this model)\r\n\u003cp align=\"center\"\u003e\u003cimg src=\"data/helpers/custom_result.png\" width=\"640\"\\\u003e\u003c/p\u003e\r\n\r\n## Custom Functions and Flags\r\nHere is how to use all the currently supported custom functions and flags that I have created.\r\n\r\n\u003ca name=\"counting\"/\u003e\r\n\r\n### Counting Objects (total objects or per class)\r\nI have created a custom function within the file [core/functions.py](https://github.com/theAIGuysCode/yolov4-custom-functions/blob/master/core/functions.py) that can be used to count and keep track of the number of objects detected at a given moment within each image or video. It can be used to count total objects found or can count number of objects detected per class.\r\n\r\n#### Count Total Objects\r\nTo count total objects all that is needed is to add the custom flag \"--count\" to your detect.py or detect_video.py command.\r\n```\r\n# Run yolov4 model while counting total objects detected\r\npython detect.py --weights ./checkpoints/yolov4-416 --size 416 --model yolov4 --images ./data/images/dog.jpg --count\r\n```\r\nRunning the above command will count the total number of objects detected and output it to your command prompt or shell as well as on the saved detection as so:\r\n\u003cp align=\"center\"\u003e\u003cimg src=\"data/helpers/total_count.png\" width=\"640\"\\\u003e\u003c/p\u003e\r\n\r\n#### Count Objects Per Class\r\nTo count the number of objects for each individual class of your object detector you need to add the custom flag \"--count\" as well as change one line in the detect.py or detect_video.py script. By default the count_objects function has a parameter called \u003cstrong\u003eby_class\u003c/strong\u003e that is set to False. If you change this parameter to \u003cstrong\u003eTrue\u003c/strong\u003e it will count per class instead.\r\n\r\nTo count per class make detect.py or detect_video.py look like this:\r\n\u003cp align=\"center\"\u003e\u003cimg src=\"data/helpers/by_class_config.PNG\" width=\"640\"\\\u003e\u003c/p\u003e\r\n\r\nThen run the same command as above:\r\n```\r\n# Run yolov4 model while counting objects per class\r\npython detect.py --weights ./checkpoints/yolov4-416 --size 416 --model yolov4 --images ./data/images/dog.jpg --count\r\n```\r\nRunning the above command will count the number of objects detected per class and output it to your command prompt or shell as well as on the saved detection as so:\r\n\u003cp align=\"center\"\u003e\u003cimg src=\"data/helpers/perclass_count.png\" width=\"640\"\\\u003e\u003c/p\u003e\r\n\r\n\u003cstrong\u003eNote:\u003c/strong\u003e You can add the --count flag to detect_video.py commands as well!\r\n\r\n\u003ca name=\"info\"/\u003e\r\n\r\n### Print Detailed Info About Each Detection (class, confidence, bounding box coordinates)\r\nI have created a custom flag called \u003cstrong\u003eINFO\u003c/strong\u003e that can be added to any detect.py or detect_video.py commands in order to print detailed information about each detection made by the object detector. To print the detailed information to your command prompt just add the flag `--info` to any of your commands. The information on each detection includes the class, confidence in the detection and the bounding box coordinates of the detection in xmin, ymin, xmax, ymax format.\r\n\r\nIf you want to edit what information gets printed you can edit the \u003cstrong\u003edraw_bbox\u003c/strong\u003e function found within the [core/utils.py](https://github.com/theAIGuysCode/yolov4-custom-functions/blob/master/core/utils.py) file. The line that prints the information looks as follows:\r\n\u003cp align=\"center\"\u003e\u003cimg src=\"data/helpers/info_details.PNG\" height=\"50\"\\\u003e\u003c/p\u003e\r\n\r\nExample of info flag added to command:\r\n```\r\npython detect.py --weights ./checkpoints/yolov4-416 --size 416 --model yolov4 --images ./data/images/dog.jpg --info\r\n```\r\nResulting output within your shell or terminal:\r\n\u003cp align=\"center\"\u003e\u003cimg src=\"data/helpers/info_output.PNG\" height=\"100\"\\\u003e\u003c/p\u003e\r\n\r\n\u003cstrong\u003eNote:\u003c/strong\u003e You can add the --info flag to detect_video.py commands as well!\r\n\r\n\u003ca name=\"crop\"/\u003e\r\n\r\n### Crop Detections and Save Them as New Images\r\nI have created a custom function within the file [core/functions.py](https://github.com/theAIGuysCode/yolov4-custom-functions/blob/master/core/functions.py) that can be applied to any detect.py or detect_video.py commands in order to crop the YOLOv4 detections and save them each as their own new image. To crop detections all you need to do is add the `--crop` flag to any command. The resulting cropped images will be saved within the \u003cstrong\u003edetections/crop/\u003c/strong\u003e folder.\r\n  \r\n Example of crop flag added to command:\r\n```\r\npython detect.py --weights ./checkpoints/yolov4-416 --size 416 --model yolov4 --images ./data/images/dog.jpg --crop\r\n```\r\n Here is an example of one of the resulting cropped detections from the above command.\r\n \u003cp align=\"center\"\u003e\u003cimg src=\"data/helpers/crop_example.png\" height=\"250\"\\\u003e\u003c/p\u003e\r\n \r\n\u003ca name=\"license\"/\u003e\r\n\r\n## License Plate Recognition Using Tesseract OCR\r\nI have created a custom function to feed Tesseract OCR the bounding box regions of license plates found by my custom YOLOv4 model in order to read and extract the license plate numbers. Thorough preprocessing is done on the license plate in order to correctly extract the license plate number from the image. The function that is in charge of doing the preprocessing and text extraction is called \u003cstrong\u003erecognize_plate\u003c/strong\u003e and can be found in the file [core/utils.py](https://github.com/theAIGuysCode/yolov4-custom-functions/blob/master/core/utils.py).\r\n\r\n\u003cstrong\u003eDisclaimer: In order to run tesseract OCR you must first download the binary files and set them up on your local machine. Please do so before proceeding or commands will not run as expected!\u003c/strong\u003e\r\n\r\nOfficial Tesseract OCR Github Repo: [tesseract-ocr/tessdoc](https://github.com/tesseract-ocr/tessdoc)\r\n\r\nGreat Article for How To Install Tesseract on Mac or Linux Machines: [PyImageSearch Article](https://www.pyimagesearch.com/2017/07/03/installing-tesseract-for-ocr/)\r\n\r\nFor Windows I recommend: [Windows Install](https://github.com/UB-Mannheim/tesseract/wiki)\r\n\r\nOnce you have Tesseract properly installed you can move onwards. If you don't have a trained YOLOv4 model to detect license plates feel free to use one that I have trained. It is not perfect but it works well. [Download license plate detector model and learn how to save and run it with TensorFlow here](#custom)\r\n\r\n### Running License Plate Recognition on Images (video example below)\r\nThe license plate recognition works wonders on images. All you need to do is add the `--plate` flag on top of the command to run the custom YOLOv4 model.\r\n\r\nTry it out on this image in the repository!\r\n```\r\n# Run License Plate Recognition\r\npython detect.py --weights ./checkpoints/custom-416 --size 416 --model yolov4 --images ./data/images/car2.jpg --plate\r\n```\r\n\r\n### Resulting Image Example\r\nThe output from the above command should print any license plate numbers found to your command terminal as well as output and save the following image to the `detections` folder.\r\n\u003cp align=\"center\"\u003e\u003cimg src=\"data/helpers/lpr_demo.png\" width=\"640\"\\\u003e\u003c/p\u003e\r\n\r\nYou should be able to see the license plate number printed on the screen above the bounding box found by YOLOv4.\r\n\r\n### Behind the Scenes\r\nThis section will highlight the steps I took in order to implement the License Plate Recognition with YOLOv4 and potential areas to be worked on further.\r\n\r\nThis demo will be showing the step-by-step workflow on the following original image.\r\n\u003cp align=\"center\"\u003e\u003cimg src=\"data/images/car2.jpg\" width=\"640\"\\\u003e\u003c/p\u003e\r\n\r\nFirst step of the process is taking the bounding box coordinates from YOLOv4 and simply taking the subimage region within the bounds of the box. Since this image is super small the majority of the time we use cv2.resize() to blow the image up 3x its original size. \r\n\u003cp align=\"center\"\u003e\u003cimg src=\"data/helpers/subimage.png\" width=\"400\"\\\u003e\u003c/p\u003e\r\n\r\nThen we convert the image to grayscale and apply a small Gaussian blur to smooth it out.\r\n\u003cp align=\"center\"\u003e\u003cimg src=\"data/helpers/gray.png\" width=\"400\"\\\u003e\u003c/p\u003e\r\n\r\nFollowing this, the image is thresholded to white text with black background and has Otsu's method also applied. This white text on black background helps to find contours of image.\r\n\u003cp align=\"center\"\u003e\u003cimg src=\"data/helpers/threshold.png\" width=\"400\"\\\u003e\u003c/p\u003e\r\n\r\nThe image is then dilated using opencv in order to make contours more visible and be picked up in future step.\r\n\u003cp align=\"center\"\u003e\u003cimg src=\"data/helpers/dilation.png\" width=\"400\"\\\u003e\u003c/p\u003e\r\n\r\nNext we use opencv to find all the rectangular shaped contours on the image and sort them left to right.\r\n\u003cp align=\"center\"\u003e\u003cimg src=\"data/helpers/contours.png\" width=\"400\"\\\u003e\u003c/p\u003e\r\n\r\nAs you can see this causes many contours to be found other than just the contours of each character within the license plate number. In order to filter out the unwanted regions we apply a couple parameters to be met in order to accept a contour. These parameters are just height and width ratios (i.e. the height of region must be at least 1/6th of the total height of the image). A couple other parameters on area of region etc are also placed. Check out code to see exact details. This filtering leaves us with.\r\n\u003cp align=\"center\"\u003e\u003cimg src=\"data/helpers/final.png\" width=\"400\"\\\u003e\u003c/p\u003e\r\n\r\nThe individual characters of the license plate number are now the only regions of interest left. We segment each subimage and apply a bitwise_not mask to flip the image to black text on white background which Tesseract is more accurate with. The final step is applying a small median blur on the image and then it is passed to Tesseract to get the letter or number from it. Example of how letters look like when going to tesseract.\r\n\u003cp align=\"center\"\u003e\u003cimg src=\"data/helpers/string.png\" width=\"650\"\\\u003e\u003c/p\u003e\r\n\r\nEach letter or number is then just appended together into a string and at the end you get the full license plate that is recognized! BOOM!\r\n\r\n### Running License Plate Recognition on Video\r\nRunning the license plate recognition straight on video at the same time that YOLOv4 object detections causes a few issues. Tesseract OCR is fairly expensive in terms of time complexity and slows down the processing of the video to a snail's pace. It can still be accomplished by adding the `--plate` command line flag to any detect_video.py commands.\r\n\r\nHowever, I believe the best route to go is to run video detections without the plate flag and instead run them with `--crop` flag which crops the objects found on screen and saves them as new images. [See how it works here](#crop) Once the video is done processing at a higher FPS all the license plate images will be cropped and saved within [detections/crop](https://github.com/theAIGuysCode/yolov4-custom-functions/blob/master/detections/crop/) folder. I have added an easy script within the repository called [license_plate_recognizer.py](https://github.com/theAIGuysCode/yolov4-custom-functions/blob/master/license_plate_recognizer.py) that you can run in order to recognize license plates. Plus this allows you to easily customize the script to further enhance any recognitions. I will be working on linking this functionality automatically in future commits to the repository.\r\n\r\nRunning License Plate Recognition with detect_video.py is done with the following command.\r\n```\r\npython detect_video.py --weights ./checkpoints/custom-416 --size 416 --model yolov4 --video ./data/video/license_plate.mp4 --output ./detections/recognition.avi --plate\r\n```\r\n\r\nThe recommended route I think is more efficient is using this command. Customize the rate at which detections are cropped within the code itself.\r\n```\r\npython detect_video.py --weights ./checkpoints/custom-416 --size 416 --model yolov4 --video ./data/video/license_plate.mp4 --output ./detections/recognition.avi --crop\r\n```\r\n\r\nNow play around with [license_plate_recognizer.py](https://github.com/theAIGuysCode/yolov4-custom-functions/blob/master/license_plate_recognizer.py) and have some fun!\r\n\r\n\u003ca name=\"ocr\"/\u003e\r\n\r\n## Running Tesseract OCR on any Detections\r\nI have also implemented a generic use of Tesseract OCR with YOLOv4. By enabling the flag `--ocr` with any detect.py image command you can search detections for text and extract what is found. Generic preprocessing is applied on the subimage that makes up the inside of the detection bounding box. However, so many lighting or color issues require advanced preprocessing so this function is by no means perfect. You will also need to install tesseract on your local machine prior to running this flag (see links and suggestions in above section)\r\n\r\nExample command (note this image doesn't have text so will not output anything, just meant to show how command is structured):\r\n```\r\npython detect.py --weights ./checkpoints/yolov4-416 --size 416 --model yolov4 --images ./data/images/dog.jpg --ocr\r\n```\r\n\r\n## YOLOv4 Using TensorFlow Lite (.tflite model)\r\nCan also implement YOLOv4 using TensorFlow Lite. TensorFlow Lite is a much smaller model and perfect for mobile or edge devices (raspberry pi, etc).\r\n```bash\r\n# Save tf model for tflite converting\r\npython save_model.py --weights ./data/yolov4.weights --output ./checkpoints/yolov4-416 --input_size 416 --model yolov4 --framework tflite\r\n\r\n# yolov4\r\npython convert_tflite.py --weights ./checkpoints/yolov4-416 --output ./checkpoints/yolov4-416.tflite\r\n\r\n# yolov4 quantize float16\r\npython convert_tflite.py --weights ./checkpoints/yolov4-416 --output ./checkpoints/yolov4-416-fp16.tflite --quantize_mode float16\r\n\r\n# yolov4 quantize int8\r\npython convert_tflite.py --weights ./checkpoints/yolov4-416 --output ./checkpoints/yolov4-416-int8.tflite --quantize_mode int8 --dataset ./coco_dataset/coco/val207.txt\r\n\r\n# Run tflite model\r\npython detect.py --weights ./checkpoints/yolov4-416.tflite --size 416 --model yolov4 --images ./data/images/kite.jpg --framework tflite\r\n```\r\n### Result Image (TensorFlow Lite)\r\nYou can find the outputted image(s) showing the detections saved within the 'detections' folder.\r\n#### TensorFlow Lite int8 Example\r\n\u003cp align=\"center\"\u003e\u003cimg src=\"data/helpers/result-int8.png\" width=\"640\"\\\u003e\u003c/p\u003e\r\n\r\nYolov4 and Yolov4-tiny int8 quantization have some issues. I will try to fix that. You can try Yolov3 and Yolov3-tiny int8 quantization \r\n\r\n## YOLOv4 Using TensorRT\r\nCan also implement YOLOv4 using TensorFlow's TensorRT. TensorRT is a high-performance inference optimizer and runtime that can be used to perform inference in lower precision (FP16 and INT8) on GPUs. TensorRT can allow up to 8x higher performance than regular TensorFlow.\r\n```bash# yolov3\r\npython save_model.py --weights ./data/yolov3.weights --output ./checkpoints/yolov3.tf --input_size 416 --model yolov3\r\npython convert_trt.py --weights ./checkpoints/yolov3.tf --quantize_mode float16 --output ./checkpoints/yolov3-trt-fp16-416\r\n\r\n# yolov3-tiny\r\npython save_model.py --weights ./data/yolov3-tiny.weights --output ./checkpoints/yolov3-tiny.tf --input_size 416 --tiny\r\npython convert_trt.py --weights ./checkpoints/yolov3-tiny.tf --quantize_mode float16 --output ./checkpoints/yolov3-tiny-trt-fp16-416\r\n\r\n# yolov4\r\npython save_model.py --weights ./data/yolov4.weights --output ./checkpoints/yolov4.tf --input_size 416 --model yolov4\r\npython convert_trt.py --weights ./checkpoints/yolov4.tf --quantize_mode float16 --output ./checkpoints/yolov4-trt-fp16-416\r\npython detect.py --weights ./checkpoints/yolov4-trt-fp16-416 --model yolov4 --images ./data/images/kite.jpg --framework trt\r\n```\r\n\r\n## Command Line Args Reference\r\n\r\n```bash\r\nsave_model.py:\r\n  --weights: path to weights file\r\n    (default: './data/yolov4.weights')\r\n  --output: path to output\r\n    (default: './checkpoints/yolov4-416')\r\n  --[no]tiny: yolov4 or yolov4-tiny\r\n    (default: 'False')\r\n  --input_size: define input size of export model\r\n    (default: 416)\r\n  --framework: what framework to use (tf, trt, tflite)\r\n    (default: tf)\r\n  --model: yolov3 or yolov4\r\n    (default: yolov4)\r\n\r\ndetect.py:\r\n  --images: path to input images as a string with images separated by \",\"\r\n    (default: './data/images/kite.jpg')\r\n  --output: path to output folder\r\n    (default: './detections/')\r\n  --[no]tiny: yolov4 or yolov4-tiny\r\n    (default: 'False')\r\n  --weights: path to weights file\r\n    (default: './checkpoints/yolov4-416')\r\n  --framework: what framework to use (tf, trt, tflite)\r\n    (default: tf)\r\n  --model: yolov3 or yolov4\r\n    (default: yolov4)\r\n  --size: resize images to\r\n    (default: 416)\r\n  --iou: iou threshold\r\n    (default: 0.45)\r\n  --score: confidence threshold\r\n    (default: 0.25)\r\n  --count: count objects within images\r\n    (default: False)\r\n  --dont_show: dont show image output\r\n    (default: False)\r\n  --info: print info on detections\r\n    (default: False)\r\n  --crop: crop detections and save as new images\r\n    (default: False)\r\n    \r\ndetect_video.py:\r\n  --video: path to input video (use 0 for webcam)\r\n    (default: './data/video/video.mp4')\r\n  --output: path to output video (remember to set right codec for given format. e.g. XVID for .avi)\r\n    (default: None)\r\n  --output_format: codec used in VideoWriter when saving video to file\r\n    (default: 'XVID)\r\n  --[no]tiny: yolov4 or yolov4-tiny\r\n    (default: 'false')\r\n  --weights: path to weights file\r\n    (default: './checkpoints/yolov4-416')\r\n  --framework: what framework to use (tf, trt, tflite)\r\n    (default: tf)\r\n  --model: yolov3 or yolov4\r\n    (default: yolov4)\r\n  --size: resize images to\r\n    (default: 416)\r\n  --iou: iou threshold\r\n    (default: 0.45)\r\n  --score: confidence threshold\r\n    (default: 0.25)\r\n  --count: count objects within video\r\n    (default: False)\r\n  --dont_show: dont show video output\r\n    (default: False)\r\n  --info: print info on detections\r\n    (default: False)\r\n  --crop: crop detections and save as new images\r\n    (default: False)\r\n```\r\n\r\n### References  \r\n\r\n   Huge shoutout goes to hunglc007 for creating the backbone of this repository:\r\n  * [tensorflow-yolov4-tflite](https://github.com/hunglc007/tensorflow-yolov4-tflite)\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FtheAIGuysCode%2Fyolov4-custom-functions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FtheAIGuysCode%2Fyolov4-custom-functions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FtheAIGuysCode%2Fyolov4-custom-functions/lists"}