- Home
- >
- Software Development
- >
- Machine Learning using TensorFlow in 10 Lines of Code – InApps 2022
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.
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.
sudo service docker start |
Run the TensorFlow Docker Image
Get the TensorFlow Docker image by typing the following command on the host terminal
docker run –it gcr.io/tensorflow/tensorflow:latest–devel |
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.
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.
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.
curl –O http://download.tensorflow.org/example_images/flower_photos.tgz |
Untar it by typing
tar xzf flower_photos.tgz |
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]
.
docker run –it –v $HOME/tf_files:/tf_files gcr.io/tensorflow/tensorflow:latest–devel |
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:
# python tensorflow/examples/image_retraining/retrain.py </code> <code>> —bottleneck_dir=/tf_files/bottlenecks </code> <code>> —how_many_training_steps 500 </code> <code>> —model_dir=/tf_files/inception </code> <code>> —output_graph=/tf_files/retrained_graph.pb </code> <code>> —output_labels=/tf_files/retrained_labels.txt </code> <code>> —image_dir /tf_files/flower_photos |
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.
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.
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.
import tensorflow as tf import systems as sys</code> <code>image_path = sys.argv[1]</code> # Read in the image_data <code>image_data = tf.gfile.FastGFile(image_path, ‘rb’).read()</code> # Loads label file, strips off carriage return <code>label_lines = [line.rstrip() for line</code> <code>in tf.gfile.GFile(“/tf_files/retrained_labels.txt”)]</code> # Unpersists graph from file <code>with tf.gfile.FastGFile(“/tf_files/retrained_graph.pb”, ‘rb’) as f: graph_def = tf.GraphDef()</code> <code>graph_def.ParseFromString(f.read())_ = tf.import_graph_def(graph_def, name=”)</code></code> # Feed the image_data as input to the graph and get first prediction <code>with tf.Session() as sess:</code> <code>softmax_tensor = sess.graph.get_tensor_by_name(‘final_result:0’)</code> <code>predictions = sess.run(softmax_tensor, </code> <code>‘DecodeJpeg/contents:0’: image_data}) </code> # Sort to show labels of first prediction in order of confidence <code>top_k = predictions[0].argsort()[-len(predictions[0]):][::-1] </code> <code>for node_id in top_k: </code> <code>human_string = label_lines[node_id] </code> <code>score = predictions[0][node_id]</code> <code>print(‘%s (score = %.5f)’ % (human_string, score))
|
If you exited Docker, type the following command to restart Docker image:
docker run –it –v $HOME/tf_files:/tf_files gcr.io/tensorflow/tensorflow:latest–devel |
Run the following command in your Docker terminal to test, for example, an image containing daisy flower:
# python /tf_files/label_image.py /tf_files/flower_photos/daisy/21652746_cc379e0eea_m.jpg |
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.
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
Let’s create the next big thing together!
Coming together is a beginning. Keeping together is progress. Working together is success.