Bento ML Quick Example

Tutorial

Model Training

Model 생성

  • Iris  Dataset 과  SVM을 이용해서 모델을 만들어주고 저장한다. 이 때 저장 파일은 환경변수 BENTOML_HOME에 지정된 곳을 루트 디렉토리 아래 모델이 생성된다.
import bentoml
from sklearn import svm, datasets

# Load training data
iris = datasets.load_iris()
X, y = iris.data, iris.target

# Model Training
clf = svm.SVC()
clf.fit(X, y)

# Save model to BentoML local model store
bentoml.sklearn.save_model("iris_clf", clf)

생성된 모델에 대해서는 Shell 상에서도 아래 명령어를 통해서 확인할 수 있고, Model이 새롭게 저장될 때마다  Version을 통해서 형상관리가 된다. 그래서 List로 모델을 직접 선택해서 볼 수 있다.

bentoml models get iris_clf:latest

bentoml models list

bentoml models --help

Model Inference를 위해서 BentoML 은 Runner를 사용할 것을 권한다.  그냥 모델을 아래 명령어로 Load한 다음에 Inference 하는 것도 가능하나,

 bentoml.sklearn.load_model("iris_clf:latest")

Scheduling이나 보다 유연하게 리소스 할당을 하고 싶다면 제공되는 Runner를 이용하는 것을 권장한다.

import bentoml

# Create a Runner instance:
iris_clf_runner = bentoml.sklearn.get("iris_clf:latest").to_runner()

# Runner#init_local initializes the model in current process, this is meant for development and testing only:
iris_clf_runner.init_local()

# This should yield the same result as the loaded model:
iris_clf_runner.predict.run([[5.9, 3., 5.1, 1.8]])

Model Serving

아래내용을 service.py로 저장한다. 자세히 보면 Decorator가 API 입출력에 관한 부분을 잡아주고, 비동기로 Predict를 하는 구조이다.

import numpy as np
import bentoml
from bentoml.io import NumpyNdarray

iris_clf_runner = bentoml.sklearn.get("iris_clf:latest").to_runner()

svc = bentoml.Service("iris_classifier", runners=[iris_clf_runner])

@svc.api(input=NumpyNdarray(), output=NumpyNdarray())
async def classify(input_series: np.ndarray) -> np.ndarray:
    return await iris_clf_runner.predict.async_run(input_series)

위에서 만든 service.py를 실행하는 스크립트이다. 실행 이후에 http://127.0.0.1:3000을 열어보면 테스트 결과를 볼 수 있고 curl 로 결과를 볼 수도 있다.

bentoml serve service.py:svc --reload
curl -X POST -H "content-type: application/json" --data "[[5.9, 3, 5.1, 1.8]]" http://127.0.0.1:3000/classify

Deployment & Containerize

  • 이 일련의 과정을 "bentofile.yaml" 로 묶어서 한 번에 처리할 수도 있다.
service: "service.py:svc"
labels:
  owner: bentoml-team
  project: gallery
include:
- "*.py"
python:
  packages:
    - scikit-learn
    - pandas
bentoml build
  • Docker로 Containerize해서 K8s에 태워서 리소스를 유연하게 조절하면서 서빙할 수도 있다.
bentoml containerize iris_classifier:latest

References

Read more

내가 놓치고 있던 미래, 먼저 온 미래를 읽고

내가 놓치고 있던 미래, 먼저 온 미래를 읽고

장강명 작가의 책은, 유학시절 읽고 처음이었다. 유학시절 "한국이 싫어서"라는 책은 동기부여가 상당히 되는 책이었다. 한국을 떠나 새로운 정채성을 학생으로서 Build up 해나가고 있던 상황에서 이 책은 제목부터 꽤 솔깃하였다. 물론 결말이 기억날 정도로 인상깊은 책은 아니었지만 말이다. 그렇게 시간이 흘러 장강명 작가의 책은 더 이상 읽지 않던

By Bongho, Lee
고객 경험이란 무엇일까?

고객 경험이란 무엇일까?

고객경험이란 무엇일까? 1. 과거 어느 대형 프로젝트에서 있던 일이다. 신사업을 위해서 예측 모델 값을 제공해야 하는 상황이었다. 데이터도 없고,어느정도의 정확도를 제공해야 하는지 답이 없었다. 점추정을 할 것인가? 구간 추정을 할 것인가를 가지고 논의중이었다. Product Manager 줄기차게 고객경험을 내세우며 점추정으로 해야 한다고 주장하였다. 근거는 오롯이 "고객 경험"이었다.

By Bongho, Lee
수요예측, 수정구슬이 아닌 목표를 향한 냉정한 나침반

수요예측, 수정구슬이 아닌 목표를 향한 냉정한 나침반

수요예측의 정의와 비즈니스에서의 중요성 기업의 성장과 운영 효율화를 위해 **수요예측(Demand Forecasting)**은 선택이 아닌 필수 요소로 자리 잡았다. 많은 경영진들이 수요예측을 미래 판매량을 정확히 맞히는 '예언'으로 기대하지만, 이는 수요예측의 본질을 오해하는 것이다. 수요예측의 진짜 의미: 미래를 점치는 수정구슬이 아니라, 우리가 도달해야 할 '목표'를

By Bongho, Lee