Difference between revisions of "Training Inception Model"

From Emgu CV: OpenCV in .NET (C#, VB, C++ and more)
Jump to: navigation, search
(Results)
Line 1: Line 1:
 
==Training your custom inception model==
 
==Training your custom inception model==
 +
 +
This tutorial is based on Tensorflow v1.12 and Emgu TF v1.12.
  
 
Follow [https://www.tensorflow.org/tutorials/image_retraining this tensorflow tutorial] to retrain a new inception model.
 
Follow [https://www.tensorflow.org/tutorials/image_retraining this tensorflow tutorial] to retrain a new inception model.
Line 7: Line 9:
  
 
==Optimize the graph for inference==
 
==Optimize the graph for inference==
We would like to optimized the inception graph for inference. To do that, we first build the optimize_for_inference module as follows:  
+
We would like to optimized the inception graph for inference.  
 +
 
 +
First we need to install bazel. For tensorflow 1.12.0, we can download the bazel 0.21.0 release here:
 +
 
 +
https://github.com/bazelbuild/bazel/releases/tag/0.21.0
 +
 
 +
Once that is installed, we can build the optimize_for_inference module as follows:  
  
 
<pre>
 
<pre>
bazel build tensorflow/python/tools:optimize_for_inference  
+
bazel build tensorflow/python/tools:optimize_for_inference --incompatible_package_name_is_a_function=false
 
</pre>
 
</pre>
  
Line 26: Line 34:
  
 
== Deploying the model ==
 
== Deploying the model ==
Emgu.TF v1.3 includes an InceptionObjectRecognition demo project. We can modify the project to use our custom trained model.
+
Emgu.TF v1.12 includes an InceptionObjectRecognition demo project. We can modify the project to use our custom trained model.
  
 
We can either include the trained model with our application, or, in our case, upload the trained model to internet for the app to download it, to reduce the application size. We have uploaded our two trained model files to github, under the url: <pre>
 
We can either include the trained model with our application, or, in our case, upload the trained model to internet for the app to download it, to reduce the application size. We have uploaded our two trained model files to github, under the url: <pre>
Line 49: Line 57:
  
 
...
 
...
 +
 +
        public MainForm()
 +
        {
 +
...
 +
 +
            inceptionGraph = new Inception();
 +
           
 +
            inceptionGraph.OnDownloadProgressChanged += OnDownloadProgressChangedEventHandler;
 +
            inceptionGraph.OnDownloadCompleted += onDownloadCompleted;
 +
            inceptionGraph.Init(new string[] {"optimized_graph.pb", "output_labels.txt"}, "https://github.com/emgucv/models/raw/master/inception_flower_retrain/", "Mul", "final_result");
 +
...
 +
        }
 +
  
 
         public void Recognize(String fileName)
 
         public void Recognize(String fileName)
Line 60: Line 81:
  
 
             //Uncomment the following code to use a retrained model to recognize followers, downloaded from the internet
 
             //Uncomment the following code to use a retrained model to recognize followers, downloaded from the internet
            Inception inceptionGraph = new Inception(null, new string[] {"optimized_graph.pb", "output_labels.txt"}, "https://github.com/emgucv/models/raw/master/inception_flower_retrain/", "Mul", "final_result");
+
 
             Tensor imageTensor = ImageIO.ReadTensorFromImageFile(fileName, 299, 299, 128.0f, 1.0f / 128.0f);
+
             Tensor imageTensor = ImageIO.ReadTensorFromImageFile<float>(fileName, 299, 299, 128.0f, 1.0f / 128.0f);
 +
 
  
 
             //Uncomment the following code to use a retrained model to recognize followers, if you deployed the models with the application
 
             //Uncomment the following code to use a retrained model to recognize followers, if you deployed the models with the application

Revision as of 22:08, 19 February 2019

Training your custom inception model

This tutorial is based on Tensorflow v1.12 and Emgu TF v1.12.

Follow this tensorflow tutorial to retrain a new inception model.

You can use the flower data from the tutorial, or you can create your own training data by replacing the data folder structures with your own. If you follows the tutorial for retraining, you should now have two files: /tmp/output_graph.pb and /tmp/output_labels.txt

Optimize the graph for inference

We would like to optimized the inception graph for inference.

First we need to install bazel. For tensorflow 1.12.0, we can download the bazel 0.21.0 release here:

https://github.com/bazelbuild/bazel/releases/tag/0.21.0

Once that is installed, we can build the optimize_for_inference module as follows:

bazel build tensorflow/python/tools:optimize_for_inference --incompatible_package_name_is_a_function=false

Now we optimized our graph

bazel-bin/tensorflow/python/tools/optimize_for_inference \
--input=/tmp/output_graph.pb \
--output=/tmp/optimized_graph.pb \
--input_names=Mul \
--output_names=final_result

An inference optimized graph optimized_graph.pb will be generated. We can use it along with the output_lablels.txt file to recognize flowers.

Deploying the model

Emgu.TF v1.12 includes an InceptionObjectRecognition demo project. We can modify the project to use our custom trained model.

We can either include the trained model with our application, or, in our case, upload the trained model to internet for the app to download it, to reduce the application size. We have uploaded our two trained model files to github, under the url:
https://github.com/emgucv/models/raw/master/inception_flower_retrain/ 
.

Source Code

We comment out the code that download the standard inception v3 model, and uncomment the code that use our custom trained model to recognize flowers.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.TF;
using Emgu.TF.Models;

...

        public MainForm()
        {
...

            inceptionGraph = new Inception();
            
            inceptionGraph.OnDownloadProgressChanged += OnDownloadProgressChangedEventHandler;
            inceptionGraph.OnDownloadCompleted += onDownloadCompleted;
            inceptionGraph.Init(new string[] {"optimized_graph.pb", "output_labels.txt"}, "https://github.com/emgucv/models/raw/master/inception_flower_retrain/", "Mul", "final_result");
...
        }


        public void Recognize(String fileName)
        {
            fileNameTextBox.Text = fileName;
            pictureBox.ImageLocation = fileName;

            //Use the following code for the full inception model
            //Inception inceptionGraph = new Inception();
            //Tensor imageTensor = ImageIO.ReadTensorFromImageFile(fileName, 224, 224, 128.0f, 1.0f / 128.0f);

            //Uncomment the following code to use a retrained model to recognize followers, downloaded from the internet

            Tensor imageTensor = ImageIO.ReadTensorFromImageFile<float>(fileName, 299, 299, 128.0f, 1.0f / 128.0f);


            //Uncomment the following code to use a retrained model to recognize followers, if you deployed the models with the application
            //For ".pb" and ".txt" bundled with the application, set the url to null
            //Inception inceptionGraph = new Inception(null, new string[] {"optimized_graph.pb", "output_labels.txt"}, null, "Mul", "final_result");
            //Tensor imageTensor = ImageIO.ReadTensorFromImageFile(fileName, 299, 299, 128.0f, 1.0f / 128.0f);

            float[] probability = inceptionGraph.Recognize(imageTensor);
            String resStr = String.Empty;
            if (probability != null)
            {
                String[] labels = inceptionGraph.Labels;
                float maxVal = 0;
                int maxIdx = 0;
                for (int i = 0; i < probability.Length; i++)
                {
                    if (probability[i] > maxVal)
                    {
                        maxVal = probability[i];
                        maxIdx = i;
                    }
                }
                resStr = String.Format("Object is {0} with {1}% probability.", labels[maxIdx], maxVal * 100);
            }
            messageLabel.Text = resStr;

        }

...

Results

We run our inception demo program, and voila Flower recognition daisy 1.png

Flower recognition rose 1.png