TF Keras Applications EfficientNetB0
개요
이 튜토리얼에서는 TF Keras Applications의 EfficientNetB0 모델을 RBLN Python API를 사용하여 컴파일하고 추론하는 방법을 보여줍니다.
환경 설정 및 설치 확인
시작하기 전에 시스템 환경이 올바르게 구성되어 있으며, 필요한 모든 필수 패키지가 설치되어 있는지 확인하십시오. 다음 항목이 포함됩니다:
- 시스템 요구 사항:
- 필수 패키지:
- 설치 명령어:
| pip install tensorflow numpy
pip install \
--extra-index-url https://pypi.rbln.ai/simple \
rebel-compiler==0.10.2
|
Note
rebel-compiler를 사용하려면 RBLN Portal 계정이 필요합니다.
- 위 명령은 Debian 계열 Linux(예: Ubuntu)에서 pip로 패키지를 설치하는 일반적인 절차를 전제로 합니다. OS나 환경이 다른 경우에는 설치 가이드에서 지원되는 설치 조합과 적용 가능한 명령을 확인하세요.
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)]
|
참고