Skip to content

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:

  1. How to compile the TensorFlow EfficientNetB0 model and save the compiled one
  2. 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.

1
2
3
4
5
6
7
import tensorflow as tf
from tf_keras.applications.efficientnet import EfficientNetB0
import rebel  # RBLN Compiler

# Instantiate TF Keras Applications EfficientNetB0 model
model = EfficientNetB0(weights='imagenet')
func = tf.function(lambda input_img : model(input_img))

Compile the model

Once the tf.function is created, we can simply compile it with rebel.compile_from_tf_function() method.

1
2
3
4
5
6
7
8
9
# Compile the model
input_info = [("input_img", [1, 224, 224, 3], tf.float32)]
compiled_model = rebel.compile_from_tf_function(
    func,
    input_info,
    # If the NPU is installed on your host machine, you can omit the `npu` argument.
    # The function will automatically detect and use the installed NPU.
    npu="RBLN-CA12",
)

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.

# Save the compiled model to disk
compiled_model.save("efficientnetb0.rbln")

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.

import urllib.request
from tf_keras.preprocessing import image
from tf_keras.applications.efficientnet import preprocess_input, decode_predictions
import numpy as np
import rebel  # RBLN Runtime

# Prepare the input
img_url = "https://rbln-public.s3.ap-northeast-2.amazonaws.com/images/tabby.jpg"
img_path = "./tabby.jpg"
with urllib.request.urlopen(img_url) as response, open(img_path, "wb") as f:
    f.write(response.read())
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
x = np.ascontiguousarray(x)

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.

1
2
3
4
5
# Load the compiled model
module = rebel.Runtime("efficientnetb0.rbln")

# Run inference
rebel_result = module.run(x)

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.

# Check results
print('Top1 category:', decode_predictions(rebel_result, top=1)[0])

The results will look like:

Top1 category: [('n02123045', 'tabby', 0.3798828)]

Summary

Here is the complete code for compilation of the TF Keras Applications EfficientNetB0:

import tensorflow as tf
from tf_keras.applications.efficientnet import EfficientNetB0
import rebel  # RBLN Compiler

# Instantiate TF Keras Applications EfficientNetB0 model
model = EfficientNetB0(weights='imagenet')
func = tf.function(lambda input_img : model(input_img))

# Compile the model
input_info = [("input_img", [1, 224, 224, 3], tf.float32)]
compiled_model = rebel.compile_from_tf_function(
    func,
    input_info,
    # If the NPU is installed on your host machine, you can omit the `npu` argument.
    # The function will automatically detect and use the installed NPU.
    npu="RBLN-CA12",
)

# Save the compiled model to disk
compiled_model.save("efficientnetb0.rbln")

The complete code for deployment of the compiled EfficientNetB0 is as follows:

import urllib.request
from tf_keras.preprocessing import image
from tf_keras.applications.efficientnet import preprocess_input, decode_predictions
import numpy as np
import rebel  # RBLN Runtime

# Prepare the input
img_url = "https://rbln-public.s3.ap-northeast-2.amazonaws.com/images/tabby.jpg"
img_path = "./tabby.jpg"
with urllib.request.urlopen(img_url) as response, open(img_path, "wb") as f:
    f.write(response.read())
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
x = np.ascontiguousarray(x)

# Load the compiled model
module = rebel.Runtime("efficientnetb0.rbln")

# Run inference
rebel_result = module.run(x)

# Check results
print('Top1 category:', decode_predictions(rebel_result, top=1)[0])