Let us learn what is tensorflow and what actually tensors are:

Traditionally there are two ways to represent data by:

Scalar: Contains a singular value. Vector: can be imagined as an array of values.

Tensors are special types of directional vectors that possess the properties of both scalars and vectors.

Tensorflow is a library by google that is developed by google for various learning applications.

Tensorflow operations produce the result only when running in the session.

Steps in building a Neural Network:

Input with assigned weights. Hidden Layer 1 Hidden Layer 2 Output Compare actual output with intended output This gives cost function. We use optimizer to minimize cost function Back

If we build our own Model for image classification it will take hours and hours to train, Instead, we use Google’s Inception-v3. Inception-v3 is trained for the ImageNet Large Visual Recognition Challenge using the data from 2012.

Steps:

Install Docker: Docker is a software that allows us to perform operations without worrying about dependencies (Docker Install) Now Open docker quick start terminal that gets created when you install Docker Type in this command, to use tensorflow docker image:

$ docker run -it gcr.io/tensorflow/tensorflow

4. After Installation is done, create a directory to store our training class images.Get a couple hundred images train the neural network.

5. Through your bash terminal, do this “cd directoryPath”.

6. Once you are done with this, make sure you are in the docker container that created the tensorflow image. Do this,

cd /tensorflow git pull

This will allow us to set up an environment so that we can train our own data on the inception net model.

7. After the pull is completed,

python tensorflow/examples/image_retraining/retrain.py\ > --botteleneck_dir=/your_dir/bottlenecks \ > --how_many_training_steps=500 \ > --model_directory=/your_dir/inception \ > --output_graph=/your_dir/inception \ > --output_labels=/your_dir/retrained_graph.pb \ > --image_dir=/your_dir/folder_with image\ \

Here,your_dir= Directory that you created initially.

folder_with_image = Folder that contains all your training class images.

8.If everything is done correctly training should start at this point, it would take about thirty minutes or so to complete training, but then it depends on the number of images.

9.Now after training is done, it’s time to test our model, the python code required is as follows:

import tensorflow as tf import sys # change this as you see fit #image_path = sys.argv[1] # Read in the image_data #image_data = tf.gfile.FastGFile(image_path, 'rb').read() import os import shutil from os import listdir from os import mkdir from shutil import copyfile from os.path import isfile, join varPath = '/toScan' destDir = "/scanned" imgFiles = [f for f in listdir(varPath) if isfile(join(varPath, f))] # Loads label file, strips off carriage return label_lines = [line.rstrip() for line in tf.gfile.GFile("/tf_files/retrained_labels.txt")] # Unpersists graph from file with tf.gfile.FastGFile("/tf_files/retrained_graph.pb", 'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) _ = tf.import_graph_def(graph_def, name='') with tf.Session() as sess: # Feed the image_data as input to the graph and get first prediction softmax_tensor = sess.graph.get_tensor_by_name('final_result:0') #try: # shutil.rmtree(destDir) #except: # None #mkdir ('scanned') for imageFile in imgFiles: image_data = tf.gfile.FastGFile(varPath+"/"+imageFile, 'rb').read() print (varPath+"/"+imageFile) predictions = sess.run(softmax_tensor, \ {'DecodeJpeg/contents:0': image_data}) # Sort to show labels of first prediction in order of confidence top_k = predictions[0].argsort()[-len(predictions[0]):][::-1] firstElt = top_k[0]; newFileName = label_lines[firstElt] +"--"+ str(predictions[0][firstElt])[2:7]+".jpg" print(newFileName) copyfile(varPath+"/"+imageFile, destDir+"/"+newFileName) for node_id in top_k: human_string = label_lines[node_id] score = predictions[0][node_id] #print (node_id) print('%s (score = %.5f)' % (human_string, score))