Deep learning to practice, an indispensable path is to the intelligent terminal, embedded equipment and other directions. But the terminal device does not have the powerful performance of GPU server, how to make the end device application deep learning?
Fortunately, Google has launched the tfmobile, last year further, the introduction of Tflite, its application of the idea of using migration learning to train their own model on the GPU server, and then porting the customized model to Tflite,
The terminal equipment only uses the model to make forward inference and predicts the result. This article is based on the following three articles:
You can also easily customize your image recognition application after you have mastered it.The first step. Preparing data
Data are: http://download.tensorflow.org/example_images/flower_photos.tgz
This is a collection of pictures about the flower classification, after downloading the decompression, you can see that there are 5 varieties of categories: Daisy (Daisy), Dandelion (dandelion), Rose (Rose), sunflower (sunflower), Tulip (tulip).
Our goal is to get a model of the pattern recognition by re-training the precompiled model.The second step. Re-training 1. Pick Precompiled model
From the "pre-compiled model provided by Google" list, we can generally see that there are two types of models, one is the float Models (floating point model), one is the quantized Models (quantitative model), what is the difference?
In fact, the float models is represented as a high-precision model, which means that the model size is larger, the recognition accuracy is higher, the recognition time is longer, is suitable for high-performance terminal equipment, and the quantized models is the low-precision model, whose precision takes a fixed 8-bit size, Therefore, the model size is small, the recognition accuracy is low, the recognition time is short, suitable for low performance terminal equipment, more detailed description can be see www.tensorflow.org/performance/quantization.
Our mobile devices are rapidly upgrading and can generally be used with the float Models. In this model, there are a lot of pre-compiled models to choose, for this article, the main focus is inception and mobilenet two architectures.
Note Mobilenet is actually divided into many kinds, such as mobilenet_v1_0.50_224, where the third parameter is the model size scale value (only approximate, not accurate), divided into 0.25/0.50/0.75/1.0 four scale values, the fourth parameter is the picture size, It has a value of 128/160/192/224 four values.
Interested in looking at each model hierarchy can be viewed in the following code:
ImportTensorFlow as TFImportTensorflow.gfile as Gfilemodel_path='/HOME/YOURNAME/DOCUMENTS/MOBILENET_V1_1.0_224/FROZEN_GRAPH.PB'defMain (UNUSEDARGV): with TF. Graph (). As_default () as Graph:with gfile. Fastgfile (Model_path,'RB') as F:graph_def=TF. Graphdef () graph_def. Parsefromstring (F.read ()) tf.import_graph_def (graph_def, name='') forOpinchgraph.get_operations (): forTensorinchop.values ():Print(tensor)if __name__=='__main__': Tf.app.run ()
Considering the performance of the test phone is not bad, we chose mobilenet_v1_1.0_224 this version as our pre-compiled model.2. Download the Training Code
You need to download the training model code and the Android-related code as follows:
git clone https://github.com/googlecodelabs/tensorflow-for-poets-2cd tensorflow- for-poets-2
Among them, the scripts directory of retrain.py is our concern, this code currently only supports INCEPTION_V3 and mobilenet two pre-compilation models, the default training model is INCEPTION_V3.3. Retrain the Model
The training commands of the two models are different, if you go to the default Inception_v3 model, you can use the following command:
Python-m scripts.retrain \
--learning_rate=0.01 \ --bottleneck_dir=tf_files/bottlenecks \ --how_many_training_steps=4000 \ -- model_dir=tf_files/models/ \ --output_graph=tf_files/retrained_graph.pb \ --output_ labels=tf_files/retrained_labels.txt \ --image_dir=tf_files/flower_photos \
If you go to the Mobilenet model, you can use the following command:
Python-m scripts.retrain \
--learning_rate=0.01 \ --bottleneck_dir=tf_files/bottlenecks \ --how_many_training_steps= 4000 \ --model_dir=tf_files/models/ \ --output_graph=tf_files/retrained_graph.pb \ --output_ labels=tf_files/retrained_labels.txt \ --image_dir=tf_files/flower_photos \ -- architecture=mobilenet_1.0_224
The model commands are interpreted as follows:
--architecture is a schema type that supports Mobilenet and inception_v3 two types of
--image_dir is the data address, assuming you set up the Tflite directory under the Tensorflow-for-poets-2 directory, put the flower picture set into it
--output_labels the last training to generate the label of the model, because the flower picture collection has been categorized according to subdirectories, so Retrained_labels.txt finally contains the above five categories of flowers category name
--output_graph the last training-generated model
After the--model_dir command is started, the pre-compilation model's
--how_many_training_steps the number of training steps, default is 4000 if not specified
--bottleneck_dir is used to cache the top layer's training data as a file.
--learning_rate Learning Rate
In addition, there are some parameters that can be adjusted as needed:
--testing_percentage divide the picture by how much as the test data, the default is 10
--validation_percentage divide the picture by how much as the validation data, the default is 10, when the two values are set, the ratio of training data to 80%
--eval_step_interval How many steps to perform an evaluation, the default is 10
--train_batch_size number of pictures in a single session, default is 100
--validation_batch_size number of images validated at once, default is 100
--random_scale given a scale value, then randomly expands the size of the training picture by default to 0
--random_brightness given a scale value, then randomly increases or decreases the brightness of the training picture by default to 0
--random_crop given a scale value, then randomly cropping the edge value of the training picture, which defaults to 0
We use mobilenet_1.0_224 to train, and then find a picture to see if we can recognize it correctly:
Python-m scripts.label_image \ --graph=tf_files/retrained_graph.pb \ --image=tf_ Files/flower_photos/daisy/3475870145_685a19116d.jpg
The result is:
Time (11. 010sdaisy (Score=0.62305) Tulips (Score=0.22490) Dandelion (Score=0.14169) Roses (Score=0.00966) Sunflowers (Score= 0.00071)
or accurately identify the daisy out of the way.5. Convert model format
PB format is not able to run on the Tflite, Tflite absorbed Google's Protobuffer advantages, created the Flatbuffer format, the specific performance is the suffix. tflite file.
The above Toco's official website has described how to convert the PB format to a tflite file via the command line, or the format can be converted in code. Not only supports the PB format, but also supports the conversion of the HDF5 file format into Tflite, which enables the sharing of models with other frameworks.
How does that turn out? This example is converted by command line mode. If the training model is INCEPTION_V3, the command line is as follows:
toco \ --graph_def_file=tf_files/retrained_graph.pb \ --output_file=tf_files/optimized_graph.lite \ --input_format=tensorflow_graphdef \ --output_format=tflite \ --input_shape= 1,299,299,3 \ --input_array=Mul \ -- output_array=final_result \ --inference_type=FLOAT \ --input_data_type=float
If the training model is mobilenet, the command line mode is as follows:
toco \ --graph_def_file=tf_files/retrained_graph.pb \ --output_file=tf_files/optimized_graph.lite \ --input_format=tensorflow_graphdef \ --output_format=tflite \ --input_shape= 1,224,224,3 \ --input_array=input \ --output_array=final_result \ --inference_type=FLOAT \ --input_data_type=float
Some points need to be explained:
The--input_array parameter represents the portal tensor op name for the model diagram structure, and the entry name for the mobilenet is input,inception_v3, the entry name is Mul, why? You can view the contents of the scripts/retrain.py code:
ifArchitecture = ='Inception_v3': #Pylint:disable=line-too-longData_url ='http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz' #Pylint:enable=line-too-longBottleneck_tensor_name ='pool_3/_reshape:0'bottleneck_tensor_size= 2048Input_width= 299Input_height= 299input_depth= 3Resized_input_tensor_name='mul:0'Model_file_name='CLASSIFY_IMAGE_GRAPH_DEF.PB'Input_mean= 128INPUT_STD= 128elifArchitecture.startswith ('mobilenet_'):
. .. data_url='http://download.tensorflow.org/models/mobilenet_v1_'Data_url+ = version_string +'_'+ size_string +'_frozen.tgz'Bottleneck_tensor_name='mobilenetv1/predictions/reshape:0'bottleneck_tensor_size= 1001Input_width=Int (size_string) input_height=Int (size_string) input_depth= 3Resized_input_tensor_name='input:0'
The Resized_input_tensor_name is the entry name for the newly generated model, and you can view the newly generated model hierarchy by using the code visualization "1. Pick precompiled model" above. So the name must be correctly written, otherwise running the command throws the exception "Valueerror:invalid tensors ' input ' were found".
The--output_array is the export name of the model. Why is Final_result this name, because in scripts/retrain.py there are:
parser.add_argument ( '--final_tensor_name', type=str, default='final_result', help ="" "\ The name of the output classification layer in the retrained graph.\ """ )
That is, the export name defaults to Final_result.
--input_shape Note that the Mobilenet training picture size is 224, while the training image size of Inception_v3 is 299.
Finally Optimized_graph.lite is the model file that we will be porting to Android.The third step. Android Tflite 1. Download Android Studio
This step is not the focus of this article, please download and install the latest SDK and NDK on your own developer.android.com/studio/.2. Introduction of Engineering
Introducing Tensorflow-for-poets-2/android/tflite code from Android Studio, there are four classes, three classes dealing with layouts, and we just need to focus on the Imageclassifier.java class.3. Importing models
The generated model can be imported into the resource directory of the above project through the command line method:
4. Modify the Imageclassifier.java classCP tf_files/optimized_graph.lite android/tflite/app/src/main/assets/CP tf_files/ Retrained_labels.txt Android/tflite/app/src/main/assets/mobilenet.txt
Note that you can modify four places:
/**Name of the model file stored in Assets.*/ Private Static FinalString Model_path = "Mobilenet.lite"; /**Name of the label file stored in Assets.*/ Private Static FinalString Label_path = "Mobilenet.txt"; Static Final intdim_img_size_x = 224;//if inception, change into 299. Static Final intdim_img_size_y = 224;//if inception, change into 299.5. Run the viewing effect
Once you've connected your phone, clicking "Run" and "run" will deploy the app to your phone, and any images captured by the camera will be ranked according to the 5 categories in the label.
We can use Baidu search some of these five categories of flowers to identify, to see the correct rate of recognition.
PostScript: According to my test results, in the picture collection of flowers, the new model recognition rate of mobilenet_1.0_244 model is higher, and the recognition rate of the new model generated by the INCEPTION_V3 model is low or not allowed.
It is recommended that the new data set be compared between the two models to find the model that best suits you.