TF Keras Applications EfficientNetB0
Overview
In this tutorial, we demonstrate how to compile and run inference with the TF Keras Applications EfficientNetB0 model
for image classification using the RBLN Python API
.
Setup & Installation
Before you begin, ensure that your system environment is properly configured and that all required packages are installed. This includes:
- System Requirements:
- Packages Requirements:
- Installation Command:
| pip install tensorflow numpy
pip install --extra-index-url https://pypi.rbln.ai/simple/ rebel-compiler>=0.8.2
|
Using RBLN Python API
Model Compilation
Import EfficientNetB0 from the TF Keras Applications module, instantiate the model with pre-trained weights,
and convert it into a tf.function.
| 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,
)
# Save the compiled model to disk
compiled_model.save('efficientnetb0.rbln')
|
Model Inference
Download and preprocess the input image required for EfficientNetB0.
Use preprocess_input()
for proper preprocessing.
Load the compiled model using RBLN Runtime and run inference on the preprocessed input.
Decode the output predictions using decode_predictions()
and display the top prediction.
| 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])
|
Example Output:
| Top1 category: [('n02123045', 'tabby', 0.3798828)]
|
References