 Ok, w tym labie wejściemy do modelu neural-network, po prostu trajniemy modelu neural-network. Odpływamy od pytańskrypcji, które widzieliście.h5 filość. Wejściemy do modelu desikotu. Ok, więc zostajemy z Cubemix. Musimy zacząć nowy projekt. I po prostu pamiętajmy, 5.3 wersji. Uwielbiam się do bortselektoru, czy możesz tutaj skończyć nowy projekt. Uwielbiam się do ciebie. I potem musimy włączyć IoT. I zwłaszcza kliknąć na pory numeru. I inicializować wszystkie prawerialne. Więc mamy nasz mikrofon. I co myślisz? Co musimy mieć do migratowania modelu? Potrzebujemy modelu. Tutaj jest kliknął. Więc proszę opuścić kliknął dodatkowy. A potem proponuję wyłączyć selekcję, przez wyłączenie artificialne inteligencje. Ok, więc możemy zobaczyć X-Cube.io expansion pack. Poza tym, żeby być bardziej oczywistym, mamy stm32 Cubemix tool. To jest generacja kod. I mamy hardware abstraction library żeby konfigurarować i generować kod dla inicializacji mikrofonu. I potem możemy wyłączyć kubemix z eksternalnymi libranami. To tak zwane expansion package. I jednym z tych expansion package jest X-Cube.io. Mamy expansion package na mems, Bluetooth, etc. Teraz fokusujemy na expansion package. To jest library, czy nawet więcej niż library. To jest repositorie, np. dokumentowanie, etc. Teraz wyłączymy kubemix z expansion package X-Cube.io. Wersja 4.0.0. Teraz jest 4.1.0. Ale dzisiaj użyjemy 4.0.0. I stm32cubemix wyłączymy z X-Cube.io. To jest kub.io. Dobra, zbieramy wszystkie elementy. I mamy dwa elementy aby wyłączyć aplikację i kod. Co jest kod? Kod jest our library, our neural network library. Po prostu pamiętajmy, to jest obiekt format, to jest obiekt library bez obiektu kapabilii. Ok, więc kod, jeśli considerujemy, nie wiem, Bluetooth, kod to jest aktywalny protokolstak, ale protokolstak to jest tylko kod. Musimy wyłączyć aplikację na kod protokolstak. Musimy wyłączyć kod aby wyłączyć neural network library, implementowanie neural network algorithms i musimy wyłączyć na kod aplikację. To może być twoja aplikacja, użyjemy aplikacji radio. Możemy wyłączyć między 3 różnych aplikacji. System performance to jest bardzo proste aplikacja pokazany przez akurat pokazany przez speed of inference pokazany przez używanie hip i stack. To jest bardzo ważne, bo kod kapabilii jest złożona. Możemy używać system performance aplikacji, np. jeśli wypracujesz z Free Artos, bo Free Artos używanie kontrolu hipu to jest ważne, aby kontrolować hip używanie. Ale dzisiaj nie użyjemy system performance, użyjemy aplikacji validation, ponieważ chcemy wyłączyć neural networky na kod na realnym silicon z modelem na górę mojego silnika procesora. To znaczy, że dostajemy jakaś w porządku? W porządku w porządku, nie absolutnie w porządku. Ponieważ będziemy skomparować praktyczne modely całkowitych, z pełną precyzyją, z wielkich resursów na górę PC z górą modelą, na górę mikro-kontrolową. Chcemy zazwyczaj, zazwyczaj, co jest drogą akuracji modelu migratycznego do silnika. W porządku w porządku możemy skomparować porządku między migratycznym modelem i oryginalnym modelem. I będziemy poznać wrażenie validation. Trzeba aplikacji, aplikacji na górę. To jest twoje w porządku. Kiedy chcesz używać górę mikro-kontrolową i chcesz zazwyczaj aplikacji. Możesz zazwyczaj aplikacji. Więc zazwyczaj zazwyczaj w porządku z górą modelą. Oczywiście, zwłaszcza górę mikro-kontrolową. W porządku validation ma być zazwyczaj i w porządku 4.0.0. Więc dodatkowe itemy pojawiające się w porządku w porządku w porządku. I w porządku mamy xCube.AI 4.0.0 itemy. Więc teraz możemy konfigurować tę wizję do klikania. Ok, więc zaktywujemy górę i aplikacji. I teraz musimy dodawać model dodania górę. Więc proszę oprowadzić do migratu. Musimy dodawać model do migratu. więc please press add network. Then it would be nice to put our custom name to the network ASC. Audio Steam or acoustic Steam classification. And we have Keras model, because I used Keras library. And we have one file containing the model. So please select saved model. We can split the output from the learning process to topology of the neural network and the weights file. Quantized model, I will discuss it at the end of the workshop. So saved model ASC, Keras saved model. Then let's select the model. Bros. And please go to the CAI, so this is our repository. AI folder, sensing one folder, utilities, AI resources, models. And then please select the last one. Session Keras mod 93model.h5. Please do not select this. The first one, it is quantized model. Let's select the last one. So now we have a model to migrate. And the first step to perform is to analyze the model in front of our resources. In front of the resources of the selected MCU. Let's do this, analyze. Okay, the popup window just appears. Let's close this window to avoid confusing you with strange numbers. We will focus on the most important and most practical numbers. Those three values, complexity, flash, occupation and RAM occupation. Complexity, 517K of something, MACC. MACC means multiply and accumulate complexity. This is the universal metric to evaluate the complexity of the neural network model. And what is the relation to the clock cycles? There is no direct relation, but do not worry. We can say that one MACC is about nine clock cycles for the cortex M4 and seven clock cycles for the cortex M7. You can find those numbers in the documentation. So you can easily calculate, you can easily evaluate the number of clock cycles needed for one inference, for one prediction. The next value is well known to all of us, flash occupation. This is quite a small model, only 31 kilobytes. But on my PCI have model which occupies about three megabytes of flash. And what should we do in such a case? Let's imagine that we are standing in front of such a problem. Here we have three megabytes. What should we do? We should start with the original model without touching the model, with validation on the desktop to see the accuracy of the model. So please press the validate on the desktop button. And now we are running and we are comparing two models, two runtimes. First runtime, it is the original model taken from the Python and running on the top of the Python, Keras and TensorFlow with double float precision. And on my four course processor, this is our base to compare. And the second model, migrated to C, which is running on the top of the C simulator. Here. C compiler and simulator. That's why it is so called validate on desktop. And as a result, we will get the relative error. And the result is here. L2R, it is the relative error between the original model and migrated to C model running on the simulator. And this is, the number is quite nice because this is about eight in power of minus eight. When the border, the threshold is 0.01. So it is okay. But this number, this border, this threshold has been selected in, again, this is arbitrary selection. This is selection of our development team. You can select lower border to be acceptable. Depends on application. Okay, let's press okay. I will explain you on the slide. Again, we have two models. The original Python, Keras, TensorFlow model on PC. And the generated C model, the migrated C model. Then we can use two different data sets. We can use random data set if we don't have custom data set. Or we can use custom data set if we have. Alternative selection. Then we are feeding both models in the same time with the same data set. We are evaluating the relative error. Previously, we discussed the confusion matrix. So we had custom data set and we were able to evaluate the absolute error. Because when we have custom data set, we know what we can expect. And we can use the absolute error evaluation both for the original model and for the migrated model. But relative model shows us what is the drop of the accuracy. Of the prediction of the inference after migration to C. Okay, but let's consider the big models. Let's imagine that here we have 3 megabytes. We just validated our model, it is okay. So the migrated model behavior is okay. The accuracy drop is acceptable. But we need to decrease the resources usage. We can do this using compression here. Let's select the compression factor 4. And the compression means it is again quite complex algorithm. The compression, it is weight sharing inside the dense layer. So we are not touching the convolution. We are not touching the max pooling layer. So our filtering, our feature extractors, we are touching only our brain, our dense layers. Okay, so after setting compression 4, we can analyze again. So we can observe now the dramatic reduction of the flash occupation. Almost by 50%. The RAM occupation is almost the same because we need the same data structures. Okay, the model is compressed, but we need to again check the accuracy drop. Because we just check the accuracy drop between original model and not compressed model. Now we will check the accuracy drop between original model and compressed model migrated to C. So validate on desktop. Okay, and now the result is a little bit worse because we have 2 in power of minus 4. Przewiosly we had 8 in power of minus 8. So 4 orders of magnitude drop, but we are still far from the border, acceptable border of the relative error. So it is okay. So that's all regarding the network, the core, the library. Now we need to configure the application layer. So please go to the platform settings. And the application layer is very simple because we have only UR COMPORT to exchange data with the PC. So let's select USART as in her nose and USART1. We need COMPORT to exchange data with the PC to be able to validate the neural network accuracy on the real silicon. We need to send the data to the neural network. And send back the results of the inference. Project manager tab, project name, then project location, c slash ai slash hands on. And through studio as a ID. And we can generate code. Okay, so we can open project on your PC. I need to import the project. So again switch to through studio. Then file, open project from file system. Directory and validate NN unique. Okay. And finish. And then let's unroll the project. And unroll source SRC folder and double click on main dot C. Then double click on the ASC underscore data dot C. And double click on the ASC dot C. This is the array, which consists of the weights and biases. So in fact all the neural network functionality is hidden behind those numbers. This is the frozen array with the coefficients of the neural networks. So we can call it as a meta program. Because the functionality of the neural network is hidden behind those numbers. Okay, but let's go to the main dot C. The AI process call. It is only one line inside the main loop here. In main dot C. MX X QBI process. That's all in ASC dot underscore data dot C. We have weights and biases. And this is ASC dot C. It is our programming interface. But to see the header files. Please go to the, please open the ASC dot C. Then go to the line number 23. Put cursor on the ASC dot H. And then press F3 to open ASC dot H. And what we need to do to integrate the model, the simulated model with our application. We need input data structure. And here is the image resolution. The picture resolution. We need output data structure. And here is the number of the classes to predict. We need to allocate the resources for the neural network. So AS, AI, ASC create. Then this is the complementary function to the create. Then we need to init the data structures and run the inference. That's all. So input data structure, output data structure, allocation the resources, initialization of the data structures and running the inference. That's all. Let's build the project. So please highlight the project name. Then right click properties. CC++ build behavior enable parallel build. Just to speed up the building process. Okay. Then you can take a hammer or you can just press the back icon. And it is building. Please do not care about warnings because this is related to GCC compiler. And when board is flashed, so the play button or the resume button is active, we can terminate the session. And then we can open terminal. The parameters of the communication are 115 kilobits per second. Okay. And we need to press black button. And the action to perform is described here. At this point we should close the terminal to free. To free the virtual comport for the data exchange with the Cuban mix. You can skip this step. And then let's come back to the Cuban mix and our network or ASC or whatever name you put. And now we can select, we can validate on target. So we can, now we will send data to the neural network running on the real silicon. And we will compare the prediction to the prediction of the original model. We will again get a relative error. So let's select manual. Let's select the comport. The steling comport. The bold rate. It is okay. And then start the validation. So it is only 114 pictures to validate. And we can see the accuracy is almost the same. 1.9 in power of minus four. Like during the running the network on the top of the simulator. C simulator. So our neural network model running on the silicon is okay. This is the conclusion. You can ask, okay, it is nice to validate the network, but how to update my application with the new model. We just need to exchange for files. We need to exchange the file containing the weights and biases and companion.h file, companion header file. And we need extend the programming interface. So asc.c and companion.h file. So we need to be windows explorer experts. That's all.