# Edge Optimization (Your First Edge AI Project)
Note: This page is a part of the tutorial Your First Edge AI Project
In the earlier parts of the guide, we built a model and evaluated its performance.
Now it's time to turn this model (which is now in an HDF5 format '.h5' used by deep learning frameworks such as Keras and Tensorflow) into optimized C-code that can be deployed on an Edge device.
Check here to see the supported layers for C code conversion.
# New Edge Project
Open the downloaded .h5 file (Model0.h5 in our example) and go to the Edge tab.
Note: Your model file might have a different name.
In the Architecture field, we can choose to optimize our Edge model (C-code) for different target architectures. As of the time of writing this tutorial, there's only ANSI C option but more options will be added in the future.
The two fields Output Directory and C Prefix control the output directory and the function names inside the generated C API for the Edge model. Let's leave both these fields as in the picture.
# Timestamps API
This API allows us to track the time when the input data for a prediction/classification was input to the model.
# Generate Tests (Optional)
In the next section of the Edge tab, we can select to generate tests together with our Edge model. The purpose of these tests is to verify that the Edge model gives the same output as the HDF5 (.h5) model when feeding them both the same data as input.
Let's enable the test by checking the box. This "Data Compare" test verifies that the output from the Edge model matches the output from the HDF5 (.h5) model for the same input. In order to set it up, we need to provide the input and output data to test with. For that, there are two possibilities. We can use:
- the two datasets ("model0_preprocessor_and_network_test_input.data" and "model0_preprocessor_and_network_test_output.data" in this example) that can be downloaded when clicking the Model files button beside the model that completed the training in the Cloud
- any of the datasets in your project "Data" folder (input) and the corresponding data predictions in the "Predictions" folder (output)
We will focus below on the second option.
As input, we will use an "accel.csv" file from one of the sessions in our "sessions" or "Data" folder.
Select "CSV file" as "File Type" and click the "..." button next to the "File path" field. In the file browser dialog, select one of the "accel.csv" data files. In our example, we choose the file in the folder "962640cf9404f600_09_17_2019_16_42_32", see picture below.
As output, we will use a "Model0.data" file containing the output from the "Model0.h5" model that we downloaded from the AI Training Service in the "Predictions" folder.
Select "Imagimob Data file" as "File Type" and click the "..." button next to the "File path" field. In the file browser dialog, select the "Model0.data" (this filename is the same as the model name) file which you find in the folder "962640cf9404f600_09_17_2019_16_42_32" in the "Prediction/sessions" folder, see picture below.
Note: it is really important that you use an "accel.csv" and "Model0.data" that belong to the same session. Otherwise, the Data Compare test will fail regardless of whether the Edge model is correct or not.
Now the contents of your Input/Expected Output fields should look as below. Here it is important to double-check that input and output are from the same session, session "962640cf9404f600_09_17_2019_16_42_32" in our case, as it can be seen in both paths.
# Build Edge
Now we are ready to build the Edge model and run the tests.
Click on "Build Edge" at the bottom of the Edge tab.
In a matter of seconds, the Edge model has been generated, as well as a report. Here we can see how much memory this model will use when deployed on a device. The optimized C-code is automatically saved in the "Edge" folder. If you chose to generate a Data Compare Test as described in the previous section, the "Edge" folder will also contain the files "Makefile" and "runner.c" used to run the test.
Let's click "OK". At this point, a dialog will pop up if you chose to generate a Data Compare Test as described in the previous section. Click on Build Now if you want to run the compare test right away.
# Test run the model (Optional)
Now it's time to test run the model and the previously generated tests.
Note: For this to work you need to have Cygwin installed and configured. See the Installation guide.
Right-click the "Makefile" in the "Edge" folder that was generated as we pushed the Build Edge button.
Select "Make All". This will invoke Cygwin to test run the model and all the tests.
This is what it will look like:
From the output above, we can now confirm that the generated Edge model is equal to the HDF5 (.h5) model to within the sixth decimal. We can safely deploy this model and be confident that it will perform just as well as the .h5 model. The only difference is that our Edge model consumes a fraction of the memory/processing power during runtime and can run on any embedded platform!
Well done! You have completed your first Edge AI project.