본문 바로가기
machine learning

kubeflow pvc에 있는 모델 inference하기

by 유주원 2022. 12. 30.

이번에는 pvc에 있는 model을 kubeflow inference service를 이용하여 serving을 진행해보도록 하자.

kubeflow ui에서 왼쪽 models 메뉴를 클릭한 후 NEW MODEL SERVER를 클릭하자.

클릭하면 빈 화면이 나타나는데 해당 화면에 inference service에서 사용할 yaml 값을 기입하자.

 

inference.yaml

 

apiVersion: "serving.kubeflow.org/v1beta1"
kind: "InferenceService"
metadata:
  name: “iris"
spec:
  predictor:
    sklearn:
      Image: sklearn-server:v0.6.1
      storageUri: "pvc://test/model”

 

위의 yaml 값을 입력해 준 후 create를 하면 model service가 목록에 나타나는 것을 확인 할 수가 있다.

 

호출하기 위해서는 host와 port 값을 알아야 한다.

https://kserve.github.io/website/master/get_started/first_isvc/#4-determine-the-ingress-ip-and-ports

 

First InferenceService - KServe Documentation Website

First InferenceService Run your first InferenceService In this tutorial, you will deploy an InferenceService with a predictor that will load a scikit-learn model trained with the iris dataset. This dataset has three output class: Iris Setosa, Iris Versicol

kserve.github.io

위 사이트를 통해 host와 port 값을 확인하자. 

export INGRESS_HOST=$(kubectl get po -l istio=ingressgateway -n istio-system -o jsonpath='{.items[0].status.hostIP}')
export INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="http2")].nodePort}')

확인된 값을 통해 아래와 같이 호출하면 된다.

 

input.json

{
"instances": [
[6.8, 2.8, 4.8, 1.4],
[6.0, 3.4, 4.5, 1.6]
]
}
 
호출 쿼리
$> curl -H "Host: iris.kubeflow-user-example-com.example.com" -v http://INGRESS_HOST:INGRESS_PORT/v1/models/sklearn-iris2:predict -d @./input.json

결과

{"predictions": [1, 1]}

 

issue 1)

pipeline에서 sklearn 모델을 만들 경우 sklearnserver v0.6.1(sklearn 서빙하기 위해 kserve에서 제공하는 이미지)의 python 패키징 버전을 맞추어야 동작이 된다. 버전이 맞지 않는 경우 inference 실행 시 모델을 찾을 수 없다는 에러가 발생한다.

sklearnserver v0.6.1의 경우 sklearn == 0.20.3, joblib==1.0.1

 

만약 sklearn을 더 상위 버전을 쓰고 싶은 경우 sklearnserver 이미지를 받은 후 해당 이미지 내의 sklearn 버전을 update 한 후 다시 이미지를 빌드 한 후 inference service에 다시 빌드한 이미지를 사용하면 된다.

joblib의 경우 버전을 정확히 맞추어야 할 것 같다. 나 같은 경우 동일한 메이저 버전이더라도 마이너 버전이 다른 경우 serving이 안되는 문제가 있었다.

아래는 해당 이슈와 관련된 논의

https://stackoverflow.com/questions/48948209/keyerror-when-loading-pickled-scikit-learn-model-using-joblib

 

KeyError when loading pickled scikit-learn model using joblib

I have an object that contains within it two scikit-learn models, an IsolationForest and a RandomForestClassifier, that I would like to pickle and later unpickle and use to produce predictions. Apart

stackoverflow.com

joblib의 경우 상위 버전인 경우 model dump 시 pickling을 하지 않도록 수정되었다고 명시됨. 그래서 sklearnserver에서 모델을 읽을 때 오류를 밷어냄.

 

issue 2)

gs에서 제공하는 scikit learn model을 inference할 경우 모델을 못 띄운다. 원인은 못찾음. gs에서 제공하는 tf model의 경우는 정상적으로 동작 된다.

 

scikit learn yaml

 

apiVersion: "serving.kubeflow.org/v1beta1"
kind: "InferenceService"
metadata:
  name: "sklearntest"
spec:
  predictor:
    sklearn:
      image: sklearn-server:v0.6.1       
      storageUri: "gs://kfserving-samples/models/sklearn/iris”

 

tensorflow yaml

 

apiVersion: "serving.kubeflow.org/v1beta1"
kind: "InferenceService"
metadata:
  name: "tftest"
spec:
  predictor:
    tensorflow:
      image: tensorflow_serving_1.14.0        
      storageUri: "gs://kfserving-examples/models/tensorflow/flowers"

 

sklearnserver docker 주소

https://hub.docker.com/r/kfserving/sklearnserver/tags

 

Docker

 

hub.docker.com

 

sklearnserver github

https://github.com/kserve/kserve/blob/master/python/sklearnserver/README.md