Sweet 16 lines to make your own image classifier – TensorFlow

Introduction :
Image classification is getting more popular nowadays, thanks to good quality mobile cameras, high speed processors. There could be possibly many things can be done using image classification. Identifying faulty part in the shop floor, identifying the different fruit categories in the vegetable market etc..,
TensorFlow™ is an open source software library for numerical computation using data flow graphs. Nodes in the graph represent mathematical operations, while the graph edges represent the multidimensional data arrays (tensors) communicated between them

Objective : In this blog, I shall demonstrate the steps required to classify the cool drink bottle image as Coke ot Pepsi using Tesnorflow, inception architecture, retraining,

Approach:
Inception-v3 is trained for the ImageNet Large Visual Recognition Challenge using the data from 2012. This is a standard task in computer vision, where models try to classify entire images into 1000 classes, like “Zebra”, “Dalmatian”, and “Dishwasher”. convolutional neural network (CNN) on this academic data set

Using tensor flow, retrain the inception classifier on our training set images, once the classifier object is built, call the classifier with the test image.
Setup /Required items:

Step 1: Install anaconda (Anaconda Distribution is a free, easy-to-install package manager, environment manager and Python distribution with a collection of over 720 open source packages with free community support. Anaconda is platform-agnostic, supports Windows, macOS or Linux. )

Step 2: Install Tensorflow
Below are commands to be used from anaconda prompt
conda create -n tensorflow python=3.6.2
activate tensorflow
conda install -c condaforge tensorflow

Step 3: Verify Tensorflow installation
From python prompt, run the below comamnds
>>>import tensorflow as tf
>>> hello = tf.constant(‘Hello, TensorFlow!’)
>>> sess = tf.Session()
>>>print(sess.run(hello))

Step 4: Download images of set which are to be classfied from internet to train the system (use fatkun batch download extension to download all google image results)

Implementation/Code details

Download the retrain.py from the tensorflow git

Run the retrain script
python retrain.py –bottleneck_dir=F:\tf_files\bottlenecks –model_dir=f:\tf_files\models\inception –summaries_dir=f:\tf_files\training_summaries\inception –how_many-training-steps 500 –output_graph=f:\tf_files\retrained_graph.pb –output_labels=f:\tf_files\retrained_labels.txt –image_dir=F:\tf_files\photos

output labels and output graph are important objects which will be used in classifier
This task takes long time depending on your CPU/GPU of system

Once these objects are ready, build our classifier, just 16 lines of code

Test results:
Now run the classifier code from python prompt with tensorflow environment

(tfenv) F:\Anaconda3\TFexamples\image_retraining>python Mylabel_image.py F:\Anaconda3\TFexamples\PepsiTest.jpg

Output will be like this
pepsi (score = 0.997804)

Result from classifier
Result from classifier

coke (score = 0.002196)

Result screen shot:

References:

https://codelabs.developers.google.com/codelabs/tensorflow-for-poets

https://research.googleblog.com/2016/03/

 

Add a Comment

Your email address will not be published. Required fields are marked *