XGBoost를 사용하는 InferenceService
학습이 완료된 XGBoost 모델을 저장 한 경우, KFServing에서 제공하는 XGBoost 서버를 사용하여 간단히 배포 할 수 있습니다.
전제 조건
- 모델 피클의 이름은
model.bst
여야 합니다. - xgboost v0.82 버전을 사용해야 합니다.
모델 생성
XGBoost 서버를 테스트하려면 먼저 파이썬을 사용하여 간단한 XGBoost 모델을 생성해야 합니다.
Scikit-learn의 기본적인 데이터셋 중의 하나인 아이리스 꽃 데이터를 사용하여, 아이리스 꽃을 분류하는 모델을 작성해 보겠습니다. 모델 피클의 이름 model.bst
이어야 합니다.
import xgboost as xgb from sklearn.datasets import load_iris iris = load_iris() X = iris['data'] y = iris['target'] dtrain = xgb.DMatrix(X, label=y) param = {'max_depth': 6, 'eta': 0.1, 'silent': 1, 'nthread': 4, 'num_class': 10, 'objective': 'multi:softmax' } xgb_model = xgb.train(params=param, dtrain=dtrain) xgb_model.save_model('model.bst')
생성 된 모델을 사용하여 XGBoost 서버를 실행하고 예측을 수행 할 수 있습니다. 모델은 PV, S3 호환 가능 개체 저장소, Azure Blob 저장소 또는 Google Cloud Storage에 있을 수 있습니다.
모델 저장하기
쿠버네티스의 퍼시스턴스 볼륨에 모델을 저장해 보겠습니다. PVC 는 앞서 생성한 kfserving-models-pvc
을 사용하겠습니다. 모델을 학습시키기 위해서 쿠버네티스 잡(Job)을 사용하겠습니다. Job을 생성할 때 모델을 저장하기 위한 PVC를 마운트 해줍니다.
모델 코드 작성하기
아이리스 꽃을 분류하는 간단한 모델입니다. 모델을 저장할 위치를 --model_path
파라미터로 입력받게 하였습니다.
import argparse import os from joblib import dump from sklearn import datasets from sklearn import svm def train(): parser = argparse.ArgumentParser() parser.add_argument('--model_path', default='/mnt/pv/models/sklearn/iris', type=str) args = parser.parse_args() if not (os.path.isdir(args.model_path)): os.makedirs(args.model_path) model_file = os.path.join(args.model_path, 'model.joblib') clf = svm.SVC(gamma='scale') iris = datasets.load_iris() X, y = iris.data, iris.target clf.fit(X, y) dump(clf, model_file) if __name__ == '__main__': train()
컨테이너 이미지를 만들기
컨테이너 이미지를 만들기 위한 Dockerfile 입니다. 파이썬을 기본 이미지로 사용하고, xgboost
와 scikit-learn
패키지를 추가로 설치합니다.
Dockerfile
FROM python:3.6-slim RUN pip install xgboost==0.82 scikit-learn RUN mkdir -p /app ADD xgboost_iris.py.py /app/
쿠버네티스 잡 실행하기
컨테이너 이미지를 빌드하고, 컨테이너 이미지 레지스트리에 푸시 한 다음, 쿠버네티스 잡(Job)을 생성하겠습니다.
Job을 생성할 때는 모델을 저장하기 위해서 PVC를 마운트 해줍니다. 이 일련의 작업들은 직접 실행 할 수 있습니다. 하지만 좀 더 편하게 하기 위해서 앞서 배운 Kubeflow Fairing을 사용하겠습니다.
다음은 로컬 개발 환경에서 Fairing을 사용하여 컨테이너 이미지를 만들고, 쿠버네티스 잡을 실행하는 예제입니다.
import uuid from kubeflow import fairing from kubeflow.fairing.kubernetes import utils as k8s_utils CONTAINER_REGISTRY = 'kangwoo' namespace = 'admin' job_name = f'xgboost-iris-job-{uuid.uuid4().hex[:4]}' command=["python", "iris.py", "--model_path", "/mnt/pv/models/xgboost/iris"] output_map = { "Dockerfile": "Dockerfile", "xgboost_iris.py": "xgboost_iris.py" } fairing.config.set_preprocessor('python', command=command, path_prefix="/app", output_map=output_map) fairing.config.set_builder('docker', registry=CONTAINER_REGISTRY, image_name="xgboost-iris", dockerfile_path="Dockerfile") fairing.config.set_deployer('job', namespace=namespace, job_name=job_name, pod_spec_mutators=[k8s_utils.mounting_pvc(pvc_name='seldon-models-pvc', pvc_mount_path='/mnt/pv')], cleanup=True, stream_log=True) fairing.config.run()
fairing을 실행하면 쿠버네티스 잡이 생성되고, 학습이 완료된 모델이 지정한 경로에 저장됩니다.
XGBoost을 사용하는 InferenceService 로 예측 하기
InferenceService 생성
InferenceService 매니페스트를 작성합니다. predictor로 xgboost 을 사용합니다. storageUri
필드로 모델 저장 위치를 지정해 줍니다. pvc 의 이름이 kfserving-models-pvc
이고 저장 위치가 models/xgboost/iris
이므로, pvc://kfserving-models-pvc/models/xgboost/iris
라고 지정해 줍니다.
xgboost.yaml
apiVersion: "serving.kubeflow.org/v1alpha2" kind: "InferenceService" metadata: name: "xgboost-iris" spec: default: predictor: xgboost: storageUri: "pvc://kfserving-models-pvc/models/xgboost/iris"
InferenceService 를 생성합니다.
다음은 admin 네임스페이스 InferenceService 를 생성하는 예제입니다.
kubectl -n admin apply -f xgboost.yaml
생성한 InferenceService를 조회해 보겠습니다.
kubectl -n admin get inferenceservice
InferenceService 가 정상적으로 생성되면 다음과 같은 응답 결과를 확인할 수 있습니다.
NAME URL READY DEFAULT TRAFFIC CANARY TRAFFIC AGE xgboost-iris <http://xgboost-iris.admin.example.com/v1/models/xgboost-iris> True 100 58s
예측 실행하기
예측을 요청하기 위해서는 모델 서버에 접근해야 합니다. 모델 서버는 ingressgateway 를 통해서 접근할 수 있습니다. ingressgateway 는 모델 서버들을 구분하기 위해서 호스트 이름을 사용합니다. ingressgateway에 접근하 기 위한 주소는 앞서 정의한 CLUSTER_IP 를 사용하겠습니다.
예측을 요청할 데이터를 json 파일로 작성합니다.
iris-input.json
{ "instances": [ [6.8, 2.8, 4.8, 1.4], [6.0, 3.4, 4.5, 1.6] ] }
다음은 admin 네임스페이스의 xgboost-iris InferenceService 에 예측을 요청하는 예제입니다.
MODEL_NAME=xgboost-iris SERVICE_HOSTNAME=$(kubectl -n admin get inferenceservice xgboost-iris -o jsonpath='{.status.url}' | cut -d "/" -f 3) INPUT_PATH=@./iris-input.json curl -v -H "Host: ${SERVICE_HOSTNAME}" http://$CLUSTER_IP/v1/models/$MODEL_NAME:predict -d $INPUT_PATH
정상적으로 실행되면 다음과 같은 응답 결과를 확인 할 수 있습니다.
* Trying ::1... * TCP_NODELAY set * Connected to localhost (::1) port 8080 (#0) > POST /v1/models/xgboost-iris:predict HTTP/1.1 > Host: xgboost-iris.admin.example.com > User-Agent: curl/7.64.1 > Accept: */* > Content-Length: 76 > Content-Type: application/x-www-form-urlencoded > * upload completely sent off: 76 out of 76 bytes < HTTP/1.1 200 OK < content-length: 27 < content-type: text/html; charset=UTF-8 < date: Sat, 28 Mar 2020 18:54:46 GMT < server: istio-envoy < x-envoy-upstream-service-time: 10165 < * Connection #0 to host localhost left intact {"predictions": [1.0, 1.0]}