## 3 TinyML Implemenatation
### Implementation Methodology
See Table 13, where the file compression steps and the resulting size in each step are summarized. The \(cc\) file is used in the methodology's next step. Obtaining the _tflite_ file is an intermediate and necessary step since it is not possible to directly convert the \(H5\) file to the \(cc\) file. However, although the computers and some embedded systems with more powerful hardware, such as Raspberry Pi 4 or FPGAs, are capable of deploying a DNN model trained with a _tflite_ file, the _tflite_ file is not yet suitable to run by our three embedded systems under test (ESP32, PI Pico, Arduino Nano BLE). For that reason, the \(H5\) to _tflite_ to \(cc\) files process conversion is necessary. The code fragment to convert the file with \(H5\) extension to _tflite_ is shown in Figure 8.3  
It should be noted that once the process of converting and compressing the trained model file has been carried out to produce a suitable file format and size to be supported by the embedded system, a computer evaluation and testing step must be carried out again to verify that the accuracy of the model has not changed or that if there was a change, it was not significant. In addition, the model's performance is evaluated with the _tflite_ file by introducing input features from the Iris database and verifying the flower type prediction. The portion code used to test, evaluate, and perform the prediction is shown in Figure 8.4  
As a result of the prediction, the following fragment is obtained in the Python IDLE output terminal: \([1.6642942\times 10^{-5}\) \(7.3261708\times 10^{-1}\) \(2.6736629\times 10^{-1}\) \(]Iris-virginica\). Observe that the highest value of the output vector (\(7.3261708e-01\)) corresponds to the Virginica class type; therefore, the prediction class is Virginica. The following step is converting the _tflite_ file to \(cc\) using the instruction \"!xxd -i exercise model.tflite - exercise model.cc\" in Python, specifically in the Colab environment. Afterward, \"!cat exercise model.cc\" allows us to obtain an array of data in hexadecimal memory address representation. The array is saved in a file with a \"cc\" extension, which is much more compact than the _tflite_ and can be recorded within the embedded system that \begin{table}
\begin{tabular}{c c c c} \hline \hline \multirow{2}{*}{**Step**} & **Code Snippet to** & \multirow{2}{*}{**File Extension**} & \multirow{2}{*}{**File Size**} \\  & **Implementation** & & \\ \hline (1) Obtain H5 File & \(model.save(H5 File)\) & \(H5\) & 5236 KB \\  & \(convert=\) & & \\ (2) Obtain tflite file & \(tflite.TFLiteConverter(H5 File)\) & \(tflite\) & 4156 KB \\ (3) Obtain cc file & \(!xxd-ifile.tflite>file.cc\) & \(cc\) & 5.2 KB \\ \hline \hline \end{tabular}
\end{table} 
Table 13: Steps for conversion and compression of the trained model for the DNN in Python. Figure 14: Code snippet with the statement to compress the H5 file and convert it into a flite file. Figure 13: Portion of code with the statement of accuracy and the loss function in Python. we consider (ESP32, Pi Pico, Nano 33 BLE) and supported in the RAM. All this procedure is shown in Figure 16.  
Table 13 summarizes the process of obtaining the files that contain the trained DNN models, as well as the conversion and compression steps to which the model file is subjected for its adaptation in size and compatible format for execution on ESP32, Raspberry Pi Pico, and Arduino Nano 33 BLE Sense systems. Figure 14 shows the use of the following statements through which the conversion of the file \(H5\) to \(tflite\) is carried out:  
\[ \begin{array}{c}\mathit{converter}=tf\_lite.TFLiteConverter.from\_keras\_model( model)\\ \mathit{converter.optimizations}=[tf\_lite.Optimize.DEFAULT\_TL]\\ \mathit{tflite\_model}=\mathit{converter.convert}()\end{array}