Machine Learning using TensorFlow in 10 Lines of Code – InApps is an article under the topic Software Development Many of you are most interested in today !! Today, let’s InApps.net learn Machine Learning using TensorFlow in 10 Lines of Code – InApps in today’s post !

Read more about Machine Learning using TensorFlow in 10 Lines of Code – InApps at Wikipedia



You can find content about Machine Learning using TensorFlow in 10 Lines of Code – InApps from the Wikipedia website

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.

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 [email protected] 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 [email protected].

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.

Read More:   PubNub Offers Serverless Programming for Real-time Streaming Services – InApps 2022

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!

Docker is a sponsor of InApps.

Feature image via Pixabay.

InApps is a wholly owned subsidiary of Insight Partners, an investor in the following companies mentioned in this article: Docker.



Source: InApps.net

Rate this post
As a Senior Tech Enthusiast, I bring a decade of experience to the realm of tech writing, blending deep industry knowledge with a passion for storytelling. With expertise in software development to emerging tech trends like AI and IoT—my articles not only inform but also inspire. My journey in tech writing has been marked by a commitment to accuracy, clarity, and engaging storytelling, making me a trusted voice in the tech community.

Let’s create the next big thing together!

Coming together is a beginning. Keeping together is progress. Working together is success.

Let’s talk

Get a custom Proposal

Please fill in your information and your need to get a suitable solution.

    You need to enter your email to download

      Success. Downloading...