<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Train a Simple Audio Recognition Model
This notebook demonstrates how to train a 20 kB Simple Audio Recognition model to recognize keywords in speech.
The model created in this notebook is used in the micro_speech example for TensorFlow Lite for MicroControllers.
<table class="tfo-notebook-buttons" align="left">
  <td>
    <a target="_blank" href="https
Step1:  DO NOT MODIFY the following constants as they include filepaths used in this notebook and data that is shared during training and inference.
Step2:  Setup Environment
Install Dependencies
Step3:  DELETE any old data from previous runs
Step4:  Clone the TensorFlow Github Repository, which contains the relevant code required to run this tutorial.
Step5:  Load TensorBoard to visualize the accuracy and loss as training proceeds.
Step6:  Training
The following script downloads the dataset and begin training.
Step7:  Generate a TensorFlow Model for Inference
Combine relevant training results (graph, weights, etc) into a single file for inference. This process is known as freezing a model and the resulting model is known as a frozen model/graph, as it cannot be further re-trained after this process.
Step8:  Generate a TensorFlow Lite Model
Convert the frozen graph into a TensorFlow Lite model, which is fully quantized for use with embedded devices.
The following cell will also print the model size, which will be under 20 kilobytes.
Step9:  Generate a TensorFlow Lite for MicroControllers Model
Convert the TensorFlow Lite model into a C source file that can be loaded by TensorFlow Lite for Microcontrollers.
Step10:  Deploy to a Microcontroller
Follow the instructions in the micro_speech README.md for TensorFlow Lite for MicroControllers to deploy this model on a specific microcontroller.
Reference Model
<ASSISTANT_TASK:>
Python Code:
# A comma-delimited list of the words you want to train for.
# The options are: yes,no,up,down,left,right,on,off,stop,go
# All the other words will be used to train an "unknown" label and silent
# audio data with no spoken words will be used to train a "silence" label.
WANTED_WORDS = "yes,no"
# The number of steps and learning rates can be specified as comma-separated
# lists to define the rate at each stage. For example,
# TRAINING_STEPS=12000,3000 and LEARNING_RATE=0.001,0.0001
# will run 12,000 training loops in total, with a rate of 0.001 for the first
# 8,000, and 0.0001 for the final 3,000.
TRAINING_STEPS = "12000,3000"
LEARNING_RATE = "0.001,0.0001"
# Calculate the total number of steps, which is used to identify the checkpoint
# file name.
TOTAL_STEPS = str(sum(map(lambda string: int(string), TRAINING_STEPS.split(","))))
# Print the configuration to confirm it
!echo "Training these words:" $WANTED_WORDS
!echo "Training steps in each stage:" $TRAINING_STEPS
!echo "Learning rate in each stage:" $LEARNING_RATE
!echo "Total number of training steps:" $TOTAL_STEPS
Explanation: Train a Simple Audio Recognition Model
This notebook demonstrates how to train a 20 kB Simple Audio Recognition model to recognize keywords in speech.
The model created in this notebook is used in the micro_speech example for TensorFlow Lite for MicroControllers.
<table class="tfo-notebook-buttons" align="left">
  <td>
    <a target="_blank" href="https://colab.research.google.com/github/tensorflow/tensorflow/blob/master/tensorflow/lite/micro/examples/micro_speech/train/train_micro_speech_model.ipynb"><img src="https://www.tensorflow.org/images/colab_logo_32px.png" />Run in Google Colab</a>
  </td>
  <td>
    <a target="_blank" href="https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/micro/examples/micro_speech/train/train_micro_speech_model.ipynb"><img src="https://www.tensorflow.org/images/GitHub-Mark-32px.png" />View source on GitHub</a>
  </td>
</table>
Training is much faster using GPU acceleration. Before you proceed, ensure you are using a GPU runtime by going to Runtime -> Change runtime type and set Hardware accelerator: GPU. Training 15,000 iterations will take 1.5 - 2 hours on a GPU runtime.
Configure Defaults
MODIFY the following constants for your specific use case.
End of explanation
# Calculate the percentage of 'silence' and 'unknown' training samples required
# to ensure that we have equal number of samples for each label.
number_of_labels = WANTED_WORDS.count(',') + 1
number_of_total_labels = number_of_labels + 2 # for 'silence' and 'unknown' label
equal_percentage_of_training_samples = int(100.0/(number_of_total_labels))
SILENT_PERCENTAGE = equal_percentage_of_training_samples
UNKNOWN_PERCENTAGE = equal_percentage_of_training_samples
# Constants which are shared during training and inference
PREPROCESS = 'micro'
WINDOW_STRIDE ='20'
MODEL_ARCHITECTURE = 'tiny_conv' # Other options include: single_fc, conv,
                      # low_latency_conv, low_latency_svdf, tiny_embedding_conv
QUANTIZE = '1' # For booleans, we provide 1 or 0 (instead of True or False)
# Constants used during training only
VERBOSITY = 'WARN'
EVAL_STEP_INTERVAL = '1000'
SAVE_STEP_INTERVAL = '5000'
# Constants for training directories and filepaths
DATASET_DIR =  'dataset/'
LOGS_DIR = 'logs/'
TRAIN_DIR = 'train/' # for training checkpoints and other files.
# Constants for inference directories and filepaths
import os
MODELS_DIR = 'models/'
os.mkdir(MODELS_DIR)
MODEL_TF = MODELS_DIR + 'model.pb'
MODEL_TFLITE = MODELS_DIR + 'model.tflite'
MODEL_TFLITE_MICRO = MODELS_DIR + 'model.cc'
Explanation: DO NOT MODIFY the following constants as they include filepaths used in this notebook and data that is shared during training and inference.
End of explanation
%tensorflow_version 1.x
import tensorflow as tf
Explanation: Setup Environment
Install Dependencies
End of explanation
!rm -rf {DATASET_DIR} {LOGS_DIR} {TRAIN_DIR} {MODELS_DIR}
Explanation: DELETE any old data from previous runs
End of explanation
!git clone -q https://github.com/tensorflow/tensorflow
Explanation: Clone the TensorFlow Github Repository, which contains the relevant code required to run this tutorial.
End of explanation
%load_ext tensorboard
%tensorboard --logdir {LOGS_DIR}
Explanation: Load TensorBoard to visualize the accuracy and loss as training proceeds.
End of explanation
!python tensorflow/tensorflow/examples/speech_commands/train.py \
--data_dir={DATASET_DIR} \
--wanted_words={WANTED_WORDS} \
--silence_percentage={SILENT_PERCENTAGE} \
--unknown_percentage={UNKNOWN_PERCENTAGE} \
--preprocess={PREPROCESS} \
--window_stride={WINDOW_STRIDE} \
--model_architecture={MODEL_ARCHITECTURE} \
--quantize={QUANTIZE} \
--how_many_training_steps={TRAINING_STEPS} \
--learning_rate={LEARNING_RATE} \
--train_dir={TRAIN_DIR} \
--summaries_dir={LOGS_DIR} \
--verbosity={VERBOSITY} \
--eval_step_interval={EVAL_STEP_INTERVAL} \
--save_step_interval={SAVE_STEP_INTERVAL} \
Explanation: Training
The following script downloads the dataset and begin training.
End of explanation
!python tensorflow/tensorflow/examples/speech_commands/freeze.py \
--wanted_words=$WANTED_WORDS \
--window_stride_ms=$WINDOW_STRIDE \
--preprocess=$PREPROCESS \
--model_architecture=$MODEL_ARCHITECTURE \
--quantize=$QUANTIZE \
--start_checkpoint=$TRAIN_DIR$MODEL_ARCHITECTURE'.ckpt-'$TOTAL_STEPS \
--output_file=$MODEL_TF \
Explanation: Generate a TensorFlow Model for Inference
Combine relevant training results (graph, weights, etc) into a single file for inference. This process is known as freezing a model and the resulting model is known as a frozen model/graph, as it cannot be further re-trained after this process.
End of explanation
input_tensor = 'Reshape_2'
output_tensor = 'labels_softmax'
converter = tf.lite.TFLiteConverter.from_frozen_graph(
    MODEL_TF, [input_tensor], [output_tensor])
converter.inference_type = tf.uint8
converter.quantized_input_stats = {input_tensor: (0.0, 9.8077)} # (mean, standard deviation)
tflite_model = converter.convert()
tflite_model_size = open(MODEL_TFLITE, "wb").write(tflite_model)
print("Model is %d bytes" % tflite_model_size)
Explanation: Generate a TensorFlow Lite Model
Convert the frozen graph into a TensorFlow Lite model, which is fully quantized for use with embedded devices.
The following cell will also print the model size, which will be under 20 kilobytes.
End of explanation
# Install xxd if it is not available
!apt-get update && apt-get -qq install xxd
# Convert to a C source file
!xxd -i {MODEL_TFLITE} > {MODEL_TFLITE_MICRO}
# Update variable names
REPLACE_TEXT = MODEL_TFLITE.replace('/', '_').replace('.', '_')
!sed -i 's/'{REPLACE_TEXT}'/g_model/g' {MODEL_TFLITE_MICRO}
Explanation: Generate a TensorFlow Lite for MicroControllers Model
Convert the TensorFlow Lite model into a C source file that can be loaded by TensorFlow Lite for Microcontrollers.
End of explanation
# Print the C source file
!cat {MODEL_TFLITE_MICRO}
Explanation: Deploy to a Microcontroller
Follow the instructions in the micro_speech README.md for TensorFlow Lite for MicroControllers to deploy this model on a specific microcontroller.
Reference Model: If you have not modified this notebook, you can follow the instructions as is, to deploy the model. Refer to the micro_speech/train/models directory to access the models generated in this notebook. 
New Model: If you have generated a new model to identify different words: (i) Update kCategoryCount and kCategoryLabels in micro_speech/micro_features/micro_model_settings.h and (ii) Update the values assigned to the variables defined in micro_speech/micro_features/model.cc with values displayed after running the following cell.
End of explanation
<END_TASK>