Object detection in just 3 lines of R code using Tiny YOLO

AbdulMajedRaja RS
Heartbeat
Published in
5 min readAug 22, 2018

--

The field of computer vision used to only exist as a discipline of academic research. However, it’s grown beyond its roots and is now an increasingly essential element of artificial intelligence and machine learning. A lot of real world products and solutions like Automated People Counting and Video Surveillance are built using computer vision tools, platforms, and technology.

DataCamp Discount 78%

What is Computer Vision?

From Wikipedia,

Computer vision is an interdisciplinary field that deals with how computers can be made for gaining high-level understanding from digital images or videos. From the perspective of engineering, it seeks to automate tasks that the human visual system can do

Along with improvements in computer vision research, the problems we aim to solve have also evolved. One particular problem that computer vision works to solve is object detectiondetecting objects in an image or a video — preferably in real time.

This problem has resulted in a lot of new neural network architectures like R-CNN, RetinaNet, and YOLO. In this post, we’re going to see how to use the R packageimage.darknet and atiny YOLO model for object detection in a given image, in just 3 lines of R code.

What is YOLO?

YOLO (You Only Look Once) is a state-of-the-art object detection architecture. YOLO was first introduced in 2015 by Joseph Redmon et al.

How does YOLO work?

Unlike previous object detection methods that repurpose classifiers to perform detection, YOLO uses a single neural network that predicts bounding boxes and class probabilities directly from full images in one evaluation. Since YOLO makes predictions with a single network evaluation (unlike systems like R-CNN which require thousands for a single image), YOLO is extremely fast—in fact, it’s more than 1000x faster than R-CNN and 100x faster than Fast R-CNN.

YOLO Model

To learn more about how the YOLO model works, check out their paper on arxiv.

Join more than 14,000 of your fellow machine learners and data scientists. Subscribe to the premier newsletter for all things deep learning.

YOLO in R:

The R packageimage.darknet, developed by BNOSAC, provides image classification and object detection functionality based on darknet.

Installation:

image.darknet is currently only available on GitHub; hence, it can be installed into your R terminal using devtools, as shown below:

#install.packages("devtools") If devtools is not available
devtools::install_github("bnosac/image", subdir = "image.darknet", build_vignettes = TRUE)

Input Image:

For this tutorial, we’ll use the following image with a Google Car and a cyclist for our object detection task.

Image Courtesy: Justin Sullivan / Getty Images

3 lines of magic:

Here, we’ll use Tiny YOLO, a smaller version of YOLO, that’s more suitable for mobile machine learning and IoT devices. Also, the R package image.darknet comes with a pre-trained tiny YOLO model and weights, thus reducing further dependencies.

Line #1:

Let’s begin the code by loading image.darknet package into our current R session:

library(image.darknet)

Line #2:

Once the package is loaded, the first step in the object detection process is to define the model we’ll use (in our case, it is tiny YOLO). The function image_darknet_model() is used to define:

  • type: detect or classify based on if it’s used for object detection or image classification ( detect in our case)
  • model, weights and labels: The model that we’ll use for object detection and its weights and labels
yolo_tiny_voc <- image_darknet_model(type = ‘detect’, 
model = “tiny-yolo-voc.cfg”,
weights = system.file(package=”image.darknet”, “models”, “tiny-yolo-voc.weights”),
labels = system.file(package=”image.darknet”, “include”, “darknet”, “data”, “voc.names”))

Line #3:

Once the object detection model is defined, we can use the function image_darknet_detect() to detect objects in the given image. This is passed as the value of the argument file along with the model object that we defined in the previous step. This function also takes an option parameter threshold to filter out detections with class probabilities below the defined threshold.

x <- image_darknet_detect(file = "/Documents/google-car.png", 
object = yolo_tiny_voc,
threshold = 0.19)

Once this step is executed, our output image with the predicted objects are ready in the current working directory (use getwd() to get the path of your working directory).

Output Predicted Image:

And voilà! There it is—just 3 lines of R code for object detection to help you in your AI endeavor. The complete code used here is also available on GitHub.

Discuss this post on Reddit and Hacker News.

Editor’s Note: Heartbeat is a contributor-driven online publication and community dedicated to providing premier educational resources for data science, machine learning, and deep learning practitioners. We’re committed to supporting and inspiring developers and engineers from all walks of life.

Editorially independent, Heartbeat is sponsored and published by Comet, an MLOps platform that enables data scientists & ML teams to track, compare, explain, & optimize their experiments. We pay our contributors, and we don’t sell ads.

If you’d like to contribute, head on over to our call for contributors. You can also sign up to receive our weekly newsletters (Deep Learning Weekly and the Comet Newsletter), join us on Slack, and follow Comet on Twitter and LinkedIn for resources, events, and much more that will help you build better ML models, faster.

--

--