The effect

The development of preparation

Tensorflow 2. Tensorflow/Hub 3.Ubuntu 16.04 LTS 4

First, train your own category model with the Hub

① Install Linux. Ubuntu is recommended

② Install Git and clone tensorFlow /hub project

③ Go to the hub project directory, hub/examples/image_retraining

Put your own pictures in your own folder (different categories of pictures in different subdirectories), like animal:



④ Open the command terminal in the image_retraining directory and enter the training command. For details, refer to the documentationThe Hub document

sudo python3 retrain.py --image_dir ./animal \
--tfhub_module https://tfhub.dev/google/imagenet/mobilenet_v1_100_224/feature_vector/1Copy the code

This command is used to produce light cell phone model data, picture size is 224×224, start training as shown below:



Note that before running retrain.py, use PIP to add dependencies.

⑤ Completion of training



The computer/TMP directory will generate the corresponding PB file and labels file, this is the data we need

Second, use AS to develop image recognition applications

① Copy the generated PB file and labels file to the project Assets directory, as follows:



② Add the tensorFlow dependency to the AS project

implementation 'org.tensorflow:tensorflow-android:+'Copy the code

(2) according totensorflowIn the official Android Demo, write the TF Classify module we need. Of course, I have written it, and you can download it directlyAndroid_TF_Classify



Note that the INPUT_NAME and OUTPUT_NAME have changed after tensorFlow is updated.

The folder name of picture classification is the name of the result recognized by this category. Each category of pictures should be more than 20, otherwise there may be errors, it is better to be more than 40;

In this example, the training pictures are generated in a lightweight way, and the normal production PB file is 8 times the size of the mobile APPLICATION PB file.

This example is a demo. If you need further study, please study the documentation carefully.

Photo attachment: animal.zip