TF Keras Applications EfficientNetB0
Overview
이 튜토리얼에서는 TF Keras Applications의 EfficientNetB0 모델을 RBLN Python API
를 사용하여 컴파일하고 추론하는 방법을 보여줍니다.
Setup & Installation
시작하기 전에 시스템 환경이 올바르게 구성되어 있으며, 필요한 모든 필수 패키지가 설치되어 있는지 확인하십시오. 다음 항목이 포함됩니다:
- System Requirements:
- Packages Requirements:
- Installation Command:
| pip install tensorflow numpy
pip install --extra-index-url https://pypi.rbln.ai/simple/ rebel-compiler>=0.7.4
|
Note
RBLN SDK는 .whl
패키지로 배포됩니다. RBLN 컴파일러와 런타임을 사용하려면
RBLN Portal 계정이 필요합니다.
RBLN Python API
사용
모델 컴파일
TF Keras Applications 모듈에서 EfficientNetB0
를 임포트하고, 사전 학습된 가중치로 모델을 인스턴스화한 후 tf.function
으로 변환합니다.
그 다음 RBLN 컴파일러로 모델을 컴파일하고 디스크에 저장합니다.
| 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')
|
모델 추론
EfficientNetB0에 필요한 입력 이미지를 다운로드하고 전처리합니다.
preprocess_input()
을 사용해 적절히 전처리한 후, RBLN Runtime으로 컴파일된 모델을 로드하고 추론을 실행합니다.
decode_predictions()
를 사용해 예측 결과를 디코딩하고 최상위 예측을 출력합니다.
| 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])
|
결과는 다음과 같이 표시됩니다:
| Top1 category: [('n02123045', 'tabby', 0.3798828)]
|
Summary and References
이 튜토리얼에서는 RBLN Python API
를 사용하여 TF Keras Applications EfficientNetB0 모델을 컴파일하고 추론하는 방법을 보여주었습니다.
컴파일된 모델은 이미지 분류용으로 RBLN NPU에서 효율적으로 사용될 수 있습니다.
References: