[LEADERG AI ZOO] Jupyter-Image-Object-Detection-YOLOv4-Tiny-CPP









The YOLOv4 Tiny algorithm can be applied to factory defect detection, medical image analysis, biological image analysis, industrial safety image analysis, mask image analysis, etc. 









Open the marking software. Prepare a png or jpg image for annotation. It is recommended that the image has the same aspect ratio. 






Setting parameters. classes= 4 : The number of detection classes.


Open data/yolov4-tiny.cfg, search for "classes=", under anchors =, there are  places where you need to set the number of categories.


Then search for #filters =(classes + 5)x3 #255, modify the filters= below this string, for example : 4 categories, then filters=27, there are places to be set. 


To set the category name, please set the category name in data/label.names. The category name must be exactly the same as the category name when labeling. 






The annotated voc xml convert yolo format. 






Prepare a list of training images.


  • image_path = "data/train/images": Training image path.
  • txt = "data/train.txt": The output training image list.






To prepare the verification image list.


  • image_path = "data /val/images": verification image path.
  • txt = "data/val.txt": The output verification image list. 






Please remember to execute this ipynb before training. -width 320 -height 320 is the size of the image to be trained (can be modified).


If the image is not in size, it will be scaled. It is recommended that the aspect ratio of the image is the same. 


After execution, please copy the 

anchors =  32, 57,  40,180, 149, 69, 124,146,  74,294, 148,284


Paste this group of numbers on all anchors in data/yolov4-tiny.cfg, there are 2 places that need to be modified. Next, modify width=320 and height=320 in cfg, which need to be the same as the value set by ipynb. 







For training, if the device supports GPU accelerated computing, please select 6_train_GPU, otherwise, please select 6_train_CPU.


model/yolov4-tiny_best.weights :Continue training with this model. If you do not continue, please delete this parameter. 


os.system("start src/darknet/build/darknet/x64/darknet_no_gpu.exe detector train data/voc.data data/yolov4-tiny.cfg model/yolov4-tiny_best.weights -map -clear")







Inferring a single image. 

  • model/yolov4-tiny_best.weights : Inferred model. 
  • data/test/images/inclusion-2.jpg : Inferred image.
  • -thresh 0.75 : The threshold of inference. 







Use webcam inference. -c 0 is the webcam device id.







Infer all images in the folder. 


  • image_path = "data/test/images" : Inference folder (jpg, png). 
  • model_path = "model/yolov4-tiny_best.weights" : Inferred model. 








Inference API, run 10_inference_api_CPU.ipynb to open the server, and then run 11_inference_api_browser.ipynb, jump out of the browser, you can select a picture for inference.  


  • --port 8801 : The opened port. 
  • --model_file model/yolov4-tiny_best.weights : Inferred model. 
  • --threshold 0.7 : Inference threshold, the higher the value, the stricter the similarity. 







Cycle inference to the picture of data/plate/test. 






Recommended Article

LEADERG AOI VISION - AI Image Analysis Software (No Code, Windows User Interface, Deep Learning, Machine Learning)

LEADERG AI ZOO - AI Sample Code Software (Save 90% of Development Time, Low Code, Jupyter Lab User Interface, Deep Learning, Machine Learning, Big Data)

LEADERG AI Computer (Deep Learning, PC, workstation, server, NVIDIA GPU, TESLA A100, RTX-3090-24G, Intel VPU, GPU Server)

LEADERG AI Education

Thanks for our customers

Taiwan University, Tsing Hua University, Yang Ming Chiao Tung University, Cheng Kung University, Taipei Medical University, Taipei University of Nursing and Health Sciences, National Chung Hsing University, Chi Nan University, Ilan University, United University, Defence University, Military Academy, Naval Academy, Feng Chia University, Chang Gung University, I-Shou University, Shih Chien University, Taiwan University of Science and Technology, Taichung University of Science and Technology, Yunlin University of Science and Technology, Chin-Yi University of Science, Formosa University, Pintung University of Science and Technology, Kaohsiung University of Science and Technology, Chaoyang University of Technology, Ming Chi University of Technology, Southern Taiwan University of Science and Technology, China University of Technology, Gushan Senior High School, Taipei Veterans General Hospital, Chang Gung Medical Foundation, Tzu Chi Medical Foundation, E-Da Hospital, Industry Technology Research Institute, Institute for Information Industry, Chung-Shan Institute of Science and Technology, Armaments Bureau, Ministry of Justice Investigation Bureau, Institute of Nuclear Energy Research, Endemic Species Research Institute, Institute of Labor, Occupational Safety And Health, Metal Industries Research & Development Centre, Taiwan Instrument Research Institute, Automotive Research & Testing Center, Taiwan Water Corporation, Taiwan Semiconductor Manufacturing Co., Ltd., United Microelectronics Corp., Nanya Technology, Winbond Electronics Corp., Xintec Inc., Arima Lasers Corporation, AU Optronics Corporation, Innolux Corporation, HannStar Display Corporation, Formosa Plastics Group., Formosa Technologies Corporation, Nan Ya Plastics Corp., Formosa Chemicals & Fibre Corporation, Chinese Petroleum Corporation, Logitech, ELAN Microelectronics Corp., Lextar Electronics Corporation, Darfon Electronics Corp., WPG Holdings, Mirle Automation Corporation, Symtek Automation Asia Co., Ltd, ChipMOS Technologies Inc., Dynapack International Technology Corporation, Primax Electronics Ltd., Feng Hsin Steel, China Ecotek, Grade Upon Technology Corp., AAEON Technology Inc., Stark Technology, Inc., Horng Terng Automation Co., Ltd., Zhen Ding Technology Holding Ltd, Boardtek Electronics Corporation, MiTAC International Corporation, Allion Labs, Inc., Sound Land Corp., Hong Hu Tech, etc.