Deep Learning With React Native

GeekyAnts
8 min readFeb 22, 2018

--

There has always been a huge amount of excitement around topics like Artificial Intelligence(AI). When someone mentions AI, you either think about AIs like Siri and how our lives have changed over the years because of it, or your thoughts might take a dark turn and wonder when Skynet is going to take over the world!

Rest assured, that is not going to happen anytime soon! 😛

We hear about AI every day. We hear about AI in our science fiction movies and we also talk to an AI on our phones.

But then recently, the terms Machine Learning and Deep Learning have started to surface in talks associated with AI. And here is where things started to get hazy.

Some might think that Machine Learning and Deep Learning are just new, fancier names for Artificial Intelligence. But this is not the case.

For example, in 2016 Google DeepMind’s AlphaGo program beat the South Korean Lee-Sedol in the board game Go. This is when the media started to mention AI, machine learning, and deep learning to describe how AlphaGo won. All three are part of the reason why AlphaGo won over Lee-Se-Dol. But they are not the same thing.

Imagine AI, Machine Learning, and Deep Learning as three concentric circles. The AI circle is the largest as it came first. The Machine Learning circle is a slightly smaller circle, and finally, deep learning is the smallest circle.

Earlier AIs were more task specific. For example, Deep Blue was programmed to play chess at championship level! But that was all it could do. The need for an AI that could do more than just one thing is what led to the birth of Machine Learning!

Enter Machine Learning

Machine Learning (ML) is a sub-field of AI. The reason for it being called Machine Learning is that it deals with —

Construction and study of a system that can learn from the data.

In short, Machine Learning is a system that learns from examples and experiences.

In ML, we provide a system with data. The system then analyses the data and improves itself to better serve its purpose.

ML is often compared with the human brain since we also learn from our surroundings and experiences and adapt to live more comfortably.

Now, you might come across a few questions in your mind like:

How can a computer learn better than I can?

Well, the answer to this can be broken down into the following points

  • A computer has access to unlimited data through the internet.
  • Though the human brain may be more complex than your computer, that in no way means that you can learn better than your computer. After all, there are creatures with far less complex brains that are capable of learning.
  • Another common misconception is that our brain is more flexible than a computer — that it can identify a number of objects and patterns than a computer can. But in reality, our brain can only identify certain types of patterns, based on what it is used to or what is it expecting. The human brain will easily identify the chair in the below picture. But it will have a hard time determining what data in the second picture below is all about.

So, it is not the flexibility of brain that helps us learn but the inflexibility. This is one of the basic principles that one needs to follow when building a Machine Learning system. Your system needs to be hardcoded with data that will tell it what kind of patterns it can expect.

Apples and Oranges! — The Machine Learning Problem

Suppose that you have to write a machine learning algorithm that can differentiate between an apple and an orange. The simplest system that can do this would be one that :

  1. Takes an image file as input
  2. Performs some analysis on the image
  3. Output the type of fruit

This is just an over-simplified description of the system. But in reality, you will start off by writing some rules to differentiate between the fruits. One such rule could be where you tell the system to count the number of orange pixels in the image and compare it with the other dominant pixel.

But what about grayscale images? Or images with no apples or oranges at all? The fact is that for every set of rules that you write, there will be at least one image where they won’t work!

So we clearly need a better algorithm that can figure out those rules for us. What we need is Classifier. A classifier is a function that takes data an input and assigns a label to it at the output.

There are 2 kinds of data that you’ll have to deal with.

  • Training Data: Data to train the classifier.
  • Testing Data: Data to test the model.

The output of the classifier will look something like this:

Here the first 2 columns are the measurements that you will use to define. These are called Features. The third column is the Label. Here the label tells us which kind of fruit the does the features represent. Each row is called an Example.

The act of extracting useful features from a dataset is called Feature Engineering. This kind of dataset may be useful for stock market analysis but it doesn’t help us differentiate between various fruits.

We needed something better. We needed Deep Learning!

Deep Learning

Deep Learning is a part of Machine Learning itself. It deals with algorithms that are inspired by the structure and function of the brain.

Deep Learning has led to great results in the field of Machine Learning and Computer Vision.

Deep Learning: Advantage

The major advantage of Deep Learning is that it does not need feature engineering. We can use the raw pixels from the image as the features. This is mainly possible because the classifier used in Deep Learning is a neural network.

Neural networks can learn complex functions. Under the hood, Deep Learning uses multiple layers of neurons.

Creating a neural network from scratch is not that easy. TensorFlow has proved to be extremely helpful in doing this for us.

TensorFlow

TensorFlow is an open source machine learning library by Google. I recommend using TensorFlow for deep learning as it can handle all the tasks required to create and train a neural network.

One of the most important features of TensorFlow is that we can save the trained model as a binary file.

The reason why TensorFlow is good at creating neural networks is that it doesn’t actually create a neural network from scratch. Instead, it retrains an existing classifier called Inception.

Inception

Inception is one of Google’s best image classifiers. In fact, it is one of the most powerful image classifiers out in the world.

An average image classifier is trained on around 1000–2000 images. Inception was trained over 1.2 million that range over 1000 categories. This was done over the duration of 2 weeks on a desktop with 8 GPUs.

Retraining Inception

Retraining inception (transfer learning) has several advantages:

  • It saves a lot of our time because are not creating a classifier all over again.
  • It also gives the system the reusability and accuracy that inception has.
  • While you’ll need 1000 images at least to train a new classifier. Retraining inceptions require around 100 images per category.

React-Native TensorFlow

React-Native TensorFlow is an open source library for React Native.

It provides us with 2 APIs

  • API for Direct interaction with TensorFlow.
  • A simple image recognition API.

Image Recognition API

Using the image recognition API is super easy. All you need to do is these 5 steps:

  1. Create a rn-cli.config.js file in the root of the project. Add TensorFlow model and Label file to it.
module.exports = {
getAssetExts() {
return ['pb', 'txt']
}
}

Here, pb is the extension of the output model file and txt is the extension for the label file.

2. Add TensorFlow model file to assets:

tensorflow_inception_graph.pb

3. Add the label file as to the assets:

tensorflow_labels.txt

4. Initialize tfImageRecognition API class using the model and the label:

const tfImageRecognition = new tfImageRecognition({
model: require('./assets/tensorflow_inception_graph.pb'),
labels: require('./assets/tensorflow_labels.txt'),
});

5. Call recognize function with the image to recognize:

const results = await ifImageRecognition.recognize({
image: require('./assets/panda.jpg'),
});

Finally, here’s what we get:

react-native-caffe2

react-native-caffe2 is another great open-source library that brings deep learning to the mobile platform. The main vision of this library is to implement deep learning solutions like image classification, style transfer, and text generation with high-level methods React Native to make it possible for anyone to create fully functional deep learning apps.

Note

  • The current version is only experimental.
  • Does not support Android for technical reasons. The NDK required for React Native is version 10, for Caffe2 is superior to version 13.
  • Support for iOS simulator only. Device support coming soon.

Click here to know more about this library:

Hotdog or Not Hotdog

The famous HBO show Silicon Valley actually released an app that identifies whether an image is a hotdog or not!

This was done by creating an artificial neural network that runs directly on your phone. The neural network was then trained using TensorFlow, Keras and Nvidia GPUs.

While this may be a superficial app with no real use-cases, it is a great example of Deep Learning React Native app. The AI is 100% powered by the user’s device, and images are processed without every leaving the phone. This way, the app manages to provide the users with fast performance, offline availability, and better privacy.

More?

Check out this codelab to learn how to train your own classifier using TensorFlow

Also check out fast.ai — A portal dedicated to make the power of deep learning accessible to all!

You can also go through this Deep Learning course by Andrew Ng

I am Rajat S, a Technical Content Writer at GeekyAnts. An aspiring coder who has a long way to go. A Die-Hard DC Comics Fan who loves Marvel Movies. 😛 Follow me on Twitter to know more about all the amazing things that are happening at GeekyAnts.

Thanks to Akarsh Srivastava, who is a Software Engineer at GeekyAnts. This post is inspired from his talk about Deep Learning using React Native.

And thank you for reading! Please do 👏 if you liked it.

--

--