CFU Playground (2) - CNN - tensorflow lite model
It is not very difficult to change the code from Pytorch to Tensorflow one-to-one, but there are restrictions to use Tensorflow lite micro. Tensorflow lite micro provides various libraries, utilities, and examples in C++ so that it can be run on a microcontroller using ARM core or Tensillica. However, not all neural networks are possible. What is definitely available is a CNN, and a basic fully connected network library is possible. LSTM is on the Kernel, but when converting to Tensorflow lite converter, it was confirmed that errors occurred. Separately, I have seen Github of a person who achieved a degree by implementing LSTM in C. (https://github.com/lephong/lstm-rnn) If you look closely, you can refer to it, but in order to minimize uncertainty, I choose to convert the RNN model into a CNN model.
Source code:
After changing the times series sequence to a 6x6 array, it look like an image pattern and run it using CNN. Input 0~35, 1~36, 2~37,... , make the input 6x6 and match the y value with the values of 36, 37, 38,... respectively. You may use Conv1D, but Conv2D is advantageous to use Tensorflow lite micro smoothly. The following models can achieve sufficiently similar performance.
Model: "sequential_2"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_2 (Conv2D) (None, 6, 6, 4) 8
flatten_2 (Flatten) (None, 144) 0
dense_4 (Dense) (None, 4) 580
dense_5 (Dense) (None, 1) 5
=================================================================
Total params: 593
Trainable params: 593
Non-trainable params: 0
_________________________________________________________________
There is no big problem in making the model, and here you have to first convert it to tensorflow lite without quantization.
# Convert the model to the TensorFlow Lite format without quantization
converter = tf.lite.TFLiteConverter.from_saved_model(MODEL_TF)
model_no_quant_tflite = converter.convert()
Next, basic optimization is applied to tensorflow lite, and if you want to quantize more, you can create a model by adding a few more options. (Performance is not shown here, and since the board to be applied basically supports floating point, so additional quantize was not applied here. You can try uncomment the some lows in the script)
# Convert the model to the TensorFlow Lite format with quantization
converter = tf.lite.TFLiteConverter.from_saved_model(MODEL_TF)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
#def representative_dataset():
# for value in X_train:
# Each scalar value must be inside of a 2D array that is wrapped in a list
# yield [np.array(value, dtype = np.float32, ndmin = 4)]
# Set the optimization flag.
# Enforce integer only quantization
# converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
# converter.inference_input_type = tf.int8
# converter.inference_output_type = tf.int8
# Provide a representative dataset to ensure we quantize correctly.
#converter.representative_dataset = representative_dataset
model_tflite = converter.convert()
Afterwards, load the saved models to compare the performance, and if you are satisfied with the performance to a certain extent, change it to C++ code with the command below. If you link the generated code with the tensorflow lite micro library, you can run it on the Microcontroller board.
# Convert to a C source file, i.e, a TensorFlow Lite for Microcontrollers model
!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}
Comments
Post a Comment