TF Keras Applications EfficientNetB0
¶
TensorFlow is one of the most popular open-source deep learning frameworks. TF Keras Applications, an extension library of TensorFlow, provides a rich set of pre-trained backbone classification models.
This tutorial will provide a comprehensive guide on how to compile and deploy the TensorFlow model with a TF Keras Applications example EfficientNetB0
. At the end of this tutorial, you will have a clear understanding of how to use RBLN SDK for TensorFlow models.
This tutorial includes following steps:
- How to compile the TensorFlow
EfficientNetB0
model and save the compiled one - How to deploy the compiled model in the runtime-based inference environment
Prerequisite¶
Before we start, please make sure you have installed the following pip packages in your system:
Note
If you want to skip the details and quickly compile and deploy the models on RBLN NPU, you can directly jump to the summary section in this tutorial. The code summarized in this section includes all the necessary steps required to compile and deploy the model so it can be used as a quick starting point for your own project.
Step 1. How to compile¶
Prepare the model¶
First, we will import the EfficientNetB0
model from the TF Keras Applications library and convert it to the tf.function form.
Compile the model¶
Once the tf.function is created, we can simply compile it with rebel.compile_from_tf_function()
method.
If the NPU is installed on your host machine, you can omit the npu
argument in the rebel.compile_from_tf_function()
function. In this case, the function will automatically detect and use the installed NPU. However, if the NPU is not installed on your host machine, you need to specify the target NPU using the npu
argument to avoid any errors.
Currently, there are two supported NPU names: RBLN-CA02
, RBLN-CA12
. If you are unsure about the name of your target NPU, you can check it by running the rbln-stat
command in the shell on the host machine where the NPU is installed.
Save the compiled model¶
To save the compiled model into the disk, we can use the compiled_model.save()
method as below.
Step 2. How to deploy¶
Now, we can deploy the model by loading the compiled model, running the inference, and checking the output results.
Prepare the input¶
We need to prepare the preprocessed image as an input data required for the pre-trained EfficientNetB0
model. We used preprocess_input()
of the tf.keras.applications.efficientnet
module for preprocessing of the input image.
Run inference¶
The RBLN Runtime module rebel.Runtime()
is used to load the compiled model. We can use the run()
method of the instantiated runtime module for running inference on the given sentnce. Additionally, the __call__
magic method can also be used to run inference.
You can see fundamental information of the runtime module, such as input/output shapes and the compiled model size, by using the print(module)
function.
Check results¶
The decode_predictions()
method of the tf.keras.applications.efficientnet
module is used to decode the output rebel_result
.
The results will look like:
Summary¶
Here is the complete code for compilation of the TF Keras Applications EfficientNetB0
:
The complete code for deployment of the compiled EfficientNetB0
is as follows: