Build A TensorFlow Image Classifier In 5 Minutes
TensorFlow is basically an open source software library for Machine Intelligence that was developed by the researchers and engineers within Google's Machine Intelligence Research organization for the purposes of conducting deep neural networks and machine learning research.
Here, we'll Build one with just a few lines of Python and use a process called Transfer Learning- That means applying the learning's from a previous training session to a new training session, if the inception model is given a look at, It can be seen that when an image is fed as an input in each layer, it will perform a series of operations on that data until it outputs a label and classification percentage of each layer is a different set of abstractions.
Looking at the image above, In the first layers, it's basically taught Edge Detection and then Shape Detection in the middle layers and they get increasingly more abstract up until the end. If you look at the last few layers, these layers are the highest level detectors for whole objects. And for Transfer Learning, we'll Retrain that last layers on the features of let's say for example Darth Vader so that it can add a representation of him to its repository of knowledge.
All of this will take place in Seven Steps in order:
1. Install Docker: The first step requires the installation of Docker, which is a tool used for creating a virtual container on your machine for running applications. The benefit of Docker is that you don't have to install any dependencies on your machine. So, download a Docker Image that has all necessary dependencies for TensorFlow built in. Download a Docker Toolbox, go through the installation process, and then you can launch your Docker container anytime by Double-Clicking on the Docker Quick Start Terminal.
2. Installation of TF Image: The second Step involves installing TensorFlow Docker Image by pasting in the line which will eventually take a few minutes. And once it is installed, you can move on to Step 3.
3. Downloading the Dataset: Now, we download our image dataset to our local machine that will stop Docker with Control D and create a Directory called tf_files/wars in our Home Directory. We locally would want to put a folder labeled Darth Vader that consists of say 100 pictures within it. Once you get all the images, you now Drag them into the tf_files/wars folder that brings you to step 4.
4. Linking TF Image To DataSet: Now that we have our Images in the tf_files directory, we will now link them to the Docker Container with this command. Here you now have all linked up! You can now move on to step 5.
5. DL Training Script: This step basically requires the training via Git. You just have to cd into TensorFlow directory and then run "git pull". This code will allow in retaining the inception classifier with newly linked Darth Vader image dataset.
6. Retraining the Model: This step is considered as the actual retraining part. The Bottleneck Directory will be used to cache the output of the lower layers on disk so that they don't have to repeatedly be recalculated. You can then run this example for say 200 iterations, the next flag will ask where to store the trained model, the output graph which we can later view in tensor Board and the output labels, which will be the same as the training data folder name and the image directory where the Vader Images were stored.
Now, go ahead and run the script right from the terminal. It'll take about a few minutes or so to train the classifier. The Script should output a training accuracy somewhere between 85-99% when it's done. And this brings us to our final step.
7. Classify: This is the final step where we'd write a script that will use our new retrained classifier to detect if a novel image contains Darth Vader. You'll first import TensorFlow, then create a variable to store the user input image path and also create another variable to store the data from that image and one more final to load the label of that image from the label file.
Next, grab the model from the saved retrained graph file, store it in the graph_def variable and parse it. Now that you have the image ready, it's time to make a prediction by feeding the image data into the restrained model to get prediction output. In order to do this, we'll create a TensorFlow session that will give us an environment to perform operations on the tensor data. The first thing to be done here is to get the softmax function from that last layer of the model. This function uses the final layer to map input data into the probability of an expected output.
Now, execute the softmax tensor function on the input image data via a session run function. It will input the predictions as an array. Now, sort the predictions in order of confidence and lastly for every prediction, get the prediction label and the score and print it out to the terminal. Now, take the script and run it on any one of the Vader pictures. The result should turn out to be good enough. Tensorflow, therefore, makes it much easier to classify an image!
Build a TensorFlow Image Classifier in 5 minutes: Siraj Raval
Video Source: Siraj Raval