TNS
VOXPOP
Will JavaScript type annotations kill TypeScript?
The creators of Svelte and Turbo 8 both dropped TS recently saying that "it's not worth it".
Yes: If JavaScript gets type annotations then there's no reason for TypeScript to exist.
0%
No: TypeScript remains the best language for structuring large enterprise applications.
0%
TBD: The existing user base and its corpensource owner means that TypeScript isn’t likely to reach EOL without a putting up a fight.
0%
I hope they both die. I mean, if you really need strong types in the browser then you could leverage WASM and use a real programming language.
0%
I don’t know and I don’t care.
0%
Software Development

Machine Learning using TensorFlow in 10 Lines of Code

Dec 2nd, 2016 5:00am by
Featued image for: Machine Learning using TensorFlow in 10 Lines of Code
Feature image via Pixabay.
This post is a one in a series of tutorials and analysis exploring the fields of machine learning and artificial intelligence. Check back on Fridays for future installments.

In an earlier post, we saw the different components in machine learning and how a machine learning algorithm learns a function to arrive at a decision. In this tutorial, we will see this learning in action and will witness how the different machine learning components interact together to perform a given task. We will explore all this by building an image classifier in only 10 lines of code, using machine learning library, created by Google, called TensorFlow.

So, let us get a head start by defining the problem that we want to solve using machine learning.

Defining the Problem Statement

Here is what we want machine learning algorithm to do: Given an input image, it should find out whether there is a flower present in the image or not. And, if a flower is present in the image, then it should determine which set of categories the flower belongs to and by how much percentage it is confident in its choice. The categories of flowers which are present in the input dataset are daisy, dandelion, roses, sunflowers and tulips.

This process can be applied to any of image subject as well, depending on the type of input data set on which the machine learning algorithm is trained. For example, if you want to build an image classifier to classify cat images from other animals, then one can train using an input data set consisting of several images of cat and test it on the images on which the machine learning was not trained before.

Prerequisites

*A fast machine with 64-bit Linux operating system.
*Basic knowledge of the Unix commands and usage of CLI (command line interface).
*Basic understanding of containers and Virtual Machines.
*Tensorflow library.
*Programming knowledge using Python.
*Basic knowledge of git

In this exploration, we will not build an image classifier from scratch. Instead, we will use a pre-build machine learning model to train on our own input flower dataset. The pre-build trained model is called Inception v3 network, which is used in the ImageNet Large Scale Visual Recognition Competition (ILSVRC) and has been trained before on different categories of objects for image classification purposes.

For this tutorial, I am using a Ubuntu 14.04 virtual machine for building the image classifier using TensorFlow. All the steps are similar for the OS X operating system. For Windows, there are resources available online as well.

Setting up the Tools

First, we will use a Docker container package provided by TensorFlow. The best part about using a container is that all the dependencies required for running TensorFlow are already present in the container, without the need of setting up several software, libraries, etc., ourselves (although TensorFlow library can be installed natively on the computer). To install Docker toolbox on the computer, please follow the instructions as provided in the following link.

The container acts like a small computer which is separate from your host computer. It has its own file system, and this small machine is assigned an IP address after it gets successfully installed on the host computer. To check whether Docker is installed correctly, start the Docker daemon by typing the following command on the host terminal.

Run the TensorFlow Docker Image

Get the TensorFlow Docker image by typing the following command on the host terminal


After typing this, a new with root user@ some long number will appear as shown in the screenshot below. Note, the Docker file system will contain a folder called as tensorflow as shown in the image below.

Docker File System

To verify TensorFlow is working correctly, you can do verify that by typing python with three lines of code to print hello TensorFlow on the Docker terminal.

TensorFlow Docker Image

Preparing Input Data for Training

To enable the Inception V3 model to identify flowers, the input data has to be prepared on which the machine learning model will get trained. For doing this, first, create a directory in your host machine and name it as tf_files.

In my case, I have chosen this as ~/Documents/tutorial_ML/tf_files. In case you are still in the Docker terminal, you can press Ctrl+D to exit Docker and return to your host machine terminal or open a new terminal. To download the flower data set, first go to your tf_files directory and then type the following command on the host terminal to download the input dataset.


Untar it by typing


You will see the following sub-directories named daisy, dandelion, roses, sunflowers and tulips in the flower_photos directory.

Link the Dataset to the TensorFlow Docker Image

Since the TensorFlow Docker image does not contain the image data set, we need to link those files virtually by typing the following command on the host terminal. It will take you to the Docker terminal with the root@xxxx.


In my case $HOME is equal to ~/Documents/tutorial_ML/. To verify the images are linked correctly, you can type ls /tf_files this on the terminal and will see the folder called flower_photos

Getting the Sample Code

Go to the tensorflow directory present in the Docker file system and type git pull to get the latest sample code from git TensorFlow repo. The sample code will be in /tensorflow/tensorflow/examples/image_retraining/.

For training, type the following command by invoking python:


While the Inception model is being retrained you will see initially some bottlenecks being created similar to the screenshot below. The bottlenecks are the last layer before the final layer of the Inception model which will classify flower images.

Bottlenecks being created

After the bottlenecks training is over, the final layer gets trained and you will see some terms like train accuracy, validation accuracy on the Docker terminal, while the model is being trained, similar to the screenshot below. At the end, you will see an accuracy value between 85 percent to 99 percent.

Retraining Inception

Testing the Image Classifier

Go to your home directory in Docker /tf_files/ and create a file called label_image.py. Type the following lines of code and save it.


If you exited Docker, type the following command to restart Docker image:


Run the following command in your Docker terminal to test, for example, an image containing daisy flower:


If you see in the screenshot below, I get a score of 0.98929 for the test image. This indicates that the classifier is 98 percent sure that the image contains a daisy flower. Similarly, I tested on dog and cat image and I got a very low score for all the flower categories. This shows that there is very less chance of a flower being present in the test image.
Image Classification result for daisy, dog and cat test images

You can experiment by training a different data set to build your very own image classifier! Happy Learning and stay tuned for the next exploration!

Group Created with Sketch.
TNS owner Insight Partners is an investor in: Docker.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.