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 conda–forge 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)
coke (score = 0.002196)
Result screen shot:
References:
https://codelabs.developers.google.com/codelabs/tensorflow-for-poets
https://research.googleblog.com/2016/03/
With inception V2 there is a .config file that you can point to the tensor records which were generated from the annotations. Every Inception V3 tutorial I find relies on crapping the pictures and putting them into a director for the category. How do you used labeled annotations with inception V3?
Yes, I used two folders containing pepsi and coke separately. I had not done any annotations, essentially I downloaded google search images for pepsi and coke using Fatkun plugin (https://chrome.google.com/webstore/detail/fatkun-batch-download-ima/nnjjahlikiabnchcpehcpkdeckfgnohf)
and saved them individually
I am getting error like :
TypeError: Cannot interpret feed_dict key as Tensor: The name ‘DecodeJpeg/contents:0’ refers to a Tensor which does not exist. The operation, ‘DecodeJpeg/contents’, does not exist in the graph.
when I give : pthon build_classifier.py image.png