# Clone the repository from GitHub
!git clone --depth 1 -q https://github.com/tensorflow/tensorflow
# Copy the training scripts into our workspace
!cp -r tensorflow/tensorflow/lite/micro/examples/magic_wand/train train

"""
Explanation: Train a gesture recognition model for microcontroller use
This notebook demonstrates how to train a 20kb gesture recognition model for TensorFlow Lite for Microcontrollers. It will produce the same model used in the magic_wand example application.
The model is designed to be used with Google Colaboratory.
<table class="tfo-notebook-buttons" align="left">
  <td>
    <a target="_blank" href="https://colab.research.google.com/github/tensorflow/tflite-micro/blob/main/tensorflow/lite/micro/examples/magic_wand/train/train_magic_wand_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/tflite-micro/blob/main/tensorflow/lite/micro/examples/magic_wand/train/train_magic_wand_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 selecting GPU. Training will take around 5 minutes on a GPU runtime.
Configure dependencies
Run the following cell to ensure the correct version of TensorFlow is used.
We'll also clone the TensorFlow repository, which contains the training scripts, and copy them into our workspace.
End of explanation
"""


# Download the data we will use to train the model
!wget http://download.tensorflow.org/models/tflite/magic_wand/data.tar.gz
# Extract the data into the train directory
!tar xvzf data.tar.gz -C train 1>/dev/null

"""
Explanation: Prepare the data
Next, we'll download the data and extract it into the expected location within the training scripts' directory.
End of explanation
"""


# The scripts must be run from within the train directory
%cd train
# Prepare the data
!python data_prepare.py
# Split the data by person
!python data_split_person.py

"""
Explanation: We'll then run the scripts that split the data into training, validation, and test sets.
End of explanation
"""


# Load TensorBoard
%load_ext tensorboard
%tensorboard --logdir logs/scalars

"""
Explanation: Load TensorBoard
Now, we set up TensorBoard so that we can graph our accuracy and loss as training proceeds.
End of explanation
"""


!python train.py --model CNN --person true

"""
Explanation: Begin training
The following cell will begin the training process. Training will take around 5 minutes on a GPU runtime. You'll see the metrics in TensorBoard after a few epochs.
End of explanation
"""


# Install xxd if it is not available
!apt-get -qq install xxd
# Save the file as a C source file
!xxd -i model.tflite > /content/model.cc
# Print the source file
!cat /content/model.cc

"""
Explanation: Create a C source file
The train.py script writes a model, model.tflite, to the training scripts' directory.
In the following cell, we convert this model into a C++ source file we can use with TensorFlow Lite for Microcontrollers.
End of explanation
"""


