YOLO-V4: Easy installation and inferencing on an image or video

Rafi
4 min readMay 13, 2020

--

Introduction

YOLO-V4 is an object detection algorithm which is an evolution of the YOLO-V3 model. YOLO object detector is famous for its’s balanced accuracy and inference time among all the other object detectors. The YOLOv4 method is created by Alexey Bochkovskiy, Chien-Yao Wang, and Hong-Yuan Mark Liao. YOLO v4 consumes and combines new features that improved the accuracy and FPS of this architecture. The features are as follows:

  • Weighted-Residual-Connections (WRC)
  • Cross-Stage-Partial-connections (CSP)
  • Cross mini-Batch Normalization (CmBN)
  • Self-adversarial-training (SAT)
  • Mish activation
  • Mosaic data augmentation

Accuracy -YOLO-V4 — — 43.5% AP (65.7% AP50) for the MS COCO dataset.

Speed-YOLOv4 — — twice faster than EfficientDet with comparable performance. Improvement in YOLOv3’s AP and FPS by 10% and 12%, respectively.

Comparison for Object Detector

What is new in YOLO-V4?

  • Efficient and powerful object detection model:Creating a CNN that operates in real-time on a conventional GPU.
  • Easy to train and deploy an object detection system: can uses a conventional GPU to train-test and achieve real-time, high quality, and convincing object detection results.It makes everyone can use a 1080 Ti or 2080 Ti GPU to train a super fast and accurate object detector.
  • Verified the influence of state-of-the-art Bag-of freebies and Bag-of-Specials methods of object detection during the detector training.
  • Modified state-of-the-art methods and make them more efficient and suitable for single GPU training, including CBN , PAN , SAM , etc

Installation

For the installation and inference, we will be using Google Colab virtual instance notebook for testing and demonstration purpose. As Google Colab is already shipped with GPU, OPENCV, CUDNN Cuda libraries its easy to prototype and run the benchmark system.

  1. Clone the AlexyAB GitHub project.
  2. Change the Makefile and update the parameters to 1 to activate the GPU.
import cv2, os
import matplotlib.pyplot as plt
%matplotlib inline
!rm -fr darknet
!git clone https://github.com/AlexeyAB/darknet/
% cd darknet
!sed -i 's/OPENCV=0/OPENCV=1/g' Makefile
!sed -i 's/GPU=0/GPU=1/g' Makefile
!sed -i 's/CUDNN=0/CUDNN=1/g' Makefile
!sed -i 's/CUDNN_HALF=0/CUDNN_HALF=1/g' Makefile
!apt update
!apt-get install libopencv-dev

Compile and Configure

Compile the project after editing the Makefile and download the pre-trained weights of the YOLO-V4.

!make &> compile.log

Download pre-trained weights:

!wget https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.weights

Inferencing-Testing on an image

Inferencing or testing the results of prediction by YOLO-v4 can be done by the following commands and code blocks.

def predictImage(imageDir):
os.system("cd /content/darknet && ./darknet detect cfg/yolov4.cfg yolov4.weights {}".format(imageDir))
image = cv2.imread("/content/darknet/predictions.jpg")
height, width = image.shape[:2]
resized_image = cv2.resize(image,(3*width, 3*height), interpolation = cv2.INTER_CUBIC)
fig = plt.gcf()
fig.set_size_inches(15, 8)
plt.axis("off")
plt.imshow(cv2.cvtColor(resized_image, cv2.COLOR_BGR2RGB))
plt.show()
predictImage("data/person.jpg")
Inference on Image

Inferencing-Testing on a video

For testing the prediction results on a video we need to either download video or upload it. The following command can be used for running inferencing on the video.

Downloading testing video:

!wget https://vod-progressive.akamaized.net/exp=1589120892~acl=%2A%2F1775639804.mp4%2A~hmac=d01a28ca249124a97243c592811bf2e486d23a0bc5a5dbaf3894924739c9ca1d/vimeo-prod-skyfire-std-us/01/2608/16/413041027/1775639804.mp4?download=1&filename=production+ID%3A4267745.mp4

For prediction on video use this code block and rename the test.mp4 with your video name.

def predictVideo(videoDir):
os.system(""" cd /content/darknet && ./darknet detector demo cfg/coco.data cfg/yolov4.cfg yolov4.weights \
-dont_show {} -i 0 -out_filename output.avi
""".format(videoDir))
predciVideo('test.mp4')
Inference on video-yolov4

Colab link: click

References:

--

--

Rafi

Deep Learning, Computer-Vision, Object Detection, CNN architecture, Jetson Nano, Android-AI