Training and freezing models

Start training a new micro speech model with

python tensorflow/examples/speech_commands/train.py -- --model_architecture=tiny_conv --window_stride=20 --preprocess=micro --wanted_words="yes,no" --silence_percentage=25 --unknown_percentage=25 --quantize=1

or, if using bazel

bazel run -c opt --copt=-mavx2 --copt=-mfma tensorflow/examples/speech_commands:train -- --model_architecture=tiny_conv --window_stride=20 --preprocess=micro --wanted_words="yes,no" --silence_percentage=25 --unknown_percentage=25 --quantize=1

This will run for a few hours

At the end you'll get your final test accuracy and checkpoint file

Checkpoint files are stored in /tmp

In this case we want /tmp/speech_commands_train/conv.ckpt-18000.* (the last place the trainer saved to)

Freeze

Take the trained weights and turn them into a frozen model on disk.

python tensorflow/examples/speech_commands/freeze.py --model_architecture=tiny_conv --window_stride=20 --preprocess=micro --wanted_words="yes,no" --quantize=1 --output_file=/tmp/tiny_conv.pb --start_checkpoint=/tmp/speech_commands_train/conv.ckpt-100

or if using bazel something like:

bazel run tensorflow/examples/speech_commands:freeze -- --model_architecture=tiny_conv --window_stride=20 --preprocess=micro --wanted_words="yes,no" --quantize=1 --output_file=/tmp/tiny_conv.pb --start_checkpoint=/tmp/speech_commands_train/tiny_conv.ckpt-18000

Convert

Convert the TensorFlow model into a TF Lite file

bazel run tensorflow/lite/toco:toco -- --input_file=/tmp/tiny_conv.pb --output_file=/tmp/tiny_conv.tflite --input_shapes=1,49,40,1 --input_arrays=Reshape_1 --output_arrays='labels_softmax' --inference_type=QUANTIZED_UINT8 --mean_values=0 --std_values=9.8077

The file can now be found in /tmp/tiny_conf.tflite

Extract & Save

Finally, you can use docker cp to copy the file from your container to your desktop. From the host computer (not the docker contrainer) run docker cp CONTAINERID:/tmp/tiny_conf.tflite .

You should now have access to the file!

Here are some example files

This guide was first published on Jul 17, 2019. It was last updated on 2019-11-15 17:24:28 -0500. This page (Training and freezing models) was last updated on Nov 20, 2019.