TensorFlow 결정 포레스트 및 TensorFlow Serving

TensorFlow Serving (TF Serving)은 RPC 또는 REST API를 사용하여 대규모 프로덕션 환경에서 온라인으로 TensorFlow 모델을 실행하는 도구입니다. TensorFlow Decision Forests(TF-DF)는 TF Serving >=2.11에서 기본적으로 지원됩니다.

TF-DF 모델은 TF Serving과 직접 호환됩니다. 이그드라실 모델은 먼저 변환 후 TF Serving과 함께 사용할 수 있습니다.

제한사항

TensorFlow는 상당한 양의 계산 오버헤드를 추가합니다. 작고 대기 시간에 민감한 모델(예: 모델 추론 시간 ~1μs)의 경우 이 오버헤드는 모델 자체에 필요한 시간보다 훨씬 더 클 수 있습니다. 이 경우 Yggdrasil Decision Forests를 사용하여 TF-DF 모델을 실행하는 것이 좋습니다.

사용예

다음 예에서는 TF Serving에서 TF-DF 모델을 실행하는 방법을 보여줍니다.

먼저 TF Serving을 설치합니다 . 이 예에서는 사전 컴파일된 버전의 TF-Serving + TF-DF를 사용합니다.

# Download TF Serving
wget https://github.com/tensorflow/decision-forests/releases/download/serving-1.0.1/tensorflow_model_server_linux.zip
unzip tensorflow_model_server_linux.zip

# Check that TF Serving works.
./tensorflow_model_server --version

이 예에서는 이미 훈련된 TF-DF 모델을 사용합니다.

# Get a TF-DF model
git clone https://github.com/tensorflow/decision-forests.git
MODEL_PATH=$(pwd)/decision-forests/tensorflow_decision_forests/test_data/model/saved_model_adult_rf

echo "The TF-DF model is available at: ${MODEL_PATH}"

참고: TF-Serving에는 모델의 전체 경로가 필요합니다. 이것이 우리가 $(pwd) 사용하는 이유입니다.

TF-Serving은 모델 버전 관리를 지원합니다. 모델은 이름이 모델 버전인 디렉터리에 포함되어야 합니다. 모델 버전은 "1"과 같은 정수입니다. 다음은 TF-Serving의 일반적인 디렉터리입니다.

  • /path/to/model
    • 1 : 모델의 버전 1
    • 5 : 모델 버전 5
    • 6 : 모델 버전 6

이 예에서는 "1"이라는 디렉터리에 모델을 넣기만 하면 됩니다.

mkdir -p /tmp/tf_serving_model
cp -R "${MODEL_PATH}" /tmp/tf_serving_model/1

이제 모델에서 TF-Sering을 시작할 수 있습니다.

./tensorflow_model_server \
    --rest_api_port=8502 \
    --model_name=my_model \
    --model_base_path=/tmp/tf_serving_model

마지막으로 Rest API를 사용하여 TF Serving에 요청을 보낼 수 있습니다. 예측+인스턴스 API와 예측+입력 API의 두 가지 형식을 사용할 수 있습니다. 다음은 각각의 예입니다.

# Predictions with the predict+instances API.
curl http://localhost:8502/v1/models/my_model:predict -X POST \
    -d '{"instances": [{"age":39,"workclass":"State-gov","fnlwgt":77516,"education":"Bachelors","education_num":13,"marital_status":"Never-married","occupation":"Adm-clerical","relationship":"Not-in-family","race":"White","sex":"Male","capital_gain":2174,"capital_loss":0,"hours_per_week":40,"native_country":"United-States"}]}'
# Predictions with the predict+inputs API
curl http://localhost:8502/v1/models/my_model:predict -X POST \
    -d '{"inputs": {"age":[39],"workclass":["State-gov"],"fnlwgt":[77516],"education":["Bachelors"],"education_num":[13],"marital_status":["Never-married"],"occupation":["Adm-clerical"],"relationship":["Not-in-family"],"race":["White"],"sex":["Male"],"capital_gain":[2174],"capital_loss":[0],"hours_per_week":[40],"native_country":["United-States"]} }'