Click here to Skip to main content
15,946,342 members
Articles / Artificial Intelligence / Tensorflow
Article

Training Your Own TensorFlow Neural Network For Android

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
23 Sep 2020CPOL4 min read 5.5K   7  
In this article we will generate output from a program will provide a TensorFlow freeze graph ready to be used or converted to TensorFlow Lite.
Here we look at how Darknet can be downloaded and built in three commands. We also discuss how to gather the images of objects in the various classes for training and testing, and how to label and divide the images into two lists.

This is the last part of a series of articles on using TensorFlow on Android. The previous parts of the series covered setting up a development environment, finding a pre-trained model, adapting it for use in an Android application, and creating an application that uses it. The previous parts used a YOLO implementation to create an application that recognizes 80 classes of objects.

You can train your own YOLO network to recognize the item classifications of your own choosing. YOLO was originally implemented by Joseph Redmond and Farhadi Ali using Darknet. Darknet is an open-source C-based framework for making neural networks. If your machine already has a C compiler and make installed, Darknet can be downloaded and built in three commands:

git clone https://github.com/pjreddie/darknet
cd darknet
make

Decide on the image classes that you want your network to recognize. Make a list of the classes in a text file so that they are in a defined order. Each class should be on its own line. This file will be used for training. Also having an ordered list is important because the image classes are going to be notated elsewhere by a zero-based index of the classes within this list. Name this file classes.txt and place it in the Darknet data folder.

Next, gather the images of objects in the various classes for training and testing. This can be one of the most challenging steps, since hundreds of images for each class are necessary. For the items that you want to recognize, you will want pictures of them in a variety of situations from different perspectives. Each image file must also be labeled. An image is labeled by creating a text file with the same name as the image (but with a txt extension) containing a line for each sample object in the training images. Each line has five elements separated by a space.

<object-class> <x> <y> <w> <h>

An example of such a line follows. For this line an object that is categorized as being in class 5 is located at the x,y position of 10,12 within an image and is 45 pixels wide and 22 pixels tall. Since zero based indices are used the digit 5 here represents the item on the sixth line in the file classes.txt that you created earlier.

5 10 12 45 22

With the labeled images, you will want to divide the images into two lists. Images for these two lists should be divided with an 80%/20% to a 90%/10% split. The larger of the lists will be used for training the network, and the smaller of the two lists will be used for testing the network. Each list should contain the absolute path to the image files on lines by themselves. Name the files test.txt and train.txt.

These files will now be made part of a configuration set. In the data folder create a file named training_config.data. The file will contain information on the number of classes being the network will detect, the path to the file containing the training data file list, the path to the file containing the test file list, and the name of the folder in which to place intermittent results during the training.

classes= 20
train  = /dev/darknet/data/train.txt
valid  = /dev/darknet/data/test.txt
names = data/classes.txt
backup = backup

With that file in place, you are now ready to begin the training:

./darknet detector train cfg/training_config.data  cfg/yolov3-voc.cfg

Training is a time-consuming process. Once training has started, it may be a good time to step away from the computer and do something else. If you are running the training on a laptop, ensure it is connected to a power supply. Training is computationally intensive, and the computer may remain in one of its higher power states for a while.

Once training completes, you will have a DarkNet.weights file. There are several open source utilities for converting this file to a TensorFlow freeze graph for either TensorFlow 1 or TensorFlow 2. The TensorFlow-compatible utility that I would suggest is named Yolov4-tflite, a Python script utility that you can clone from the following URL:

https://github.com/hunglc007/tensorflow-yolov4-tflite

To convert a weights file, the utility accepts a path to the file:

Python
python save_model.py --weights ../darknet/yolov3.weights  -output my-yolov --input_size 416 --model yolov3

The output from the program provides a TensorFlow freeze graph ready to be used or converted to TensorFlow Lite.

Conclusion

This is the last of a six-part series on using TensorFlite Lite on Android. I focused on using existing models and turned attention to visual processing. This is not the only domain in which TensorFlow can be used. Provided you have information on how to form the input or interpret the output, the information presented in this series could also be used for importing and using TensorFlow models for non-visual algorithms.

If you want to dive deeper into TensorFlow and start experimenting with training other models, there are a number of different frameworks that you can use to get started. Keras is one of the most popular for TensorFlow. You can find other articles on Keras here on CodeProject.

This article is part of the series 'Mobile Neural Networks On Android with TensorFlow Lite View All

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
I attended Southern Polytechnic State University and earned a Bachelors of Science in Computer Science and later returned to earn a Masters of Science in Software Engineering. I've largely developed solutions that are based on a mix of Microsoft technologies with open source technologies mixed in. I've got an interest in astronomy and you'll see that interest overflow into some of my code project articles from time to time.



Twitter:@j2inet

Instagram: j2inet


Comments and Discussions

 
-- There are no messages in this forum --