Tensorflow Serving Tutorial 텐서플로우 서빙 설명

Tensorflow Serving Tutorial

 
 

딥러닝 모델을 만들면, 만들고 끝!이 아닌 Product에 모델을 배포해야 합니다. 이 모델을 추가하는 과정을 어떻게 할 수 있을까요? 그리고 직접 API를 만드는 것과 Tensorflow Serving API을 사용하는 것의 차이점은 무엇일까요? 이런 궁금증을 해결하기 위해 다양한 자료를 보고 정리한 글입니다. 시리즈물로 정리할 예정이며 이번 글에선 Tensorflow Serving Tutorial에 대해서만 작성했습니다. 잘못된 내용이 있으면 말씀해 주세요!!!

수정 사항

  • 2019.09.01
    • 글을 작성할 당시엔 estimator가 많이 사용되지 않아 그 예제를 작성하진 않았습니다. 요샌 estimator도 많이 사용되는데, 관련해서 flask-tensorflow estimator로 serving하는 Repo를 발견해 공유드립니다
    • 차금강님의 flask-tensorflow Github
    • 깔끔하게 코드를 작성해주셔서 매우 유용합니다 :)

관련 글

  • Tensorflow Serving Tutorial(현재 글)
  • Tensorflow Serving My Model
  • Serving with Google CloudML
  • Serving with Flask

Model Serving


  • Model Serving한다는 것은 inference를 의미. 대표적인 3가지 방법
  • Tensorflow Serving
    • Python : tensorflow-serving-api 사용
    • 다른 언어 : bazel 빌드
    • Serving시 Python 사용하면 퍼포먼스가 상대적으로 좋지 않음
    • 어려운 이유
      • C++ code. Tensorflow 사용하는 사람들은 대부분 Python만 익숙
      • Kubernetes, gRPC, bazel? 모르는 용어 투성이
      • Compile 필요하고 오래 걸림
  • Google Cloud CloudML
    • 장점 : 쉬움! CloudML이 다 해줌
    • 단점 : 비용 발생
  • Flask를 사용한 API
    • 장점 : 빠르게 Prototype 만들 때 사용 가능
    • 단점 : 초당 API Request가 많은 대용량 서비스라면 사용하기 힘듬(퍼포먼스 이슈)
  • 정리하면 Tensorflow Serving보다 다른 방법(CloudML, Flask)은 상대적으로 쉬운 편이고, CloudML은 노드 시간당 비용이 발생하고, Flask 사용한 방법은 대용량 Request에 버티기 힘듬
  • 결국 주어진 상황에 따라 선택하면 될 듯(유저에게 사용하는 API가 아니고, 사내 자동화를 위해서라면 초당 Request가 적을테니 Flask 사용해도 OK)

Architecture


경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

  • 파란색 : Client
  • 초록색 : Server
  • 연두색 : 여러 Model들(v1, v2 …)
  • gRPC
    • RPC의 일종
    • GET / POST만 존재

Component

  • Servables
    • 클라이언트가 계산을 수행하는데 사용하는 기본 object(perform computation)
    • model 저장
    • 4개의 component의 중심
  • Loaders
    • servable(model)의 life cycle을 관리
    • manager를 위한 임시 저장소(temporary storage for the manager)
  • Sources
    • contain servables
    • gateway
    • loader로 올림
    • 모델의 다른 버전을 track
  • Managers
    • full lifecycle 관리

Install


  • 1) Bazel
  • 2) Tensorflow-serving-api

(공통) Ubuntu(Docker)

  • docker ubuntu 16.04 이미지를 통해 컨테이너 실행
  • Dockerfile
FROM ubuntu:16.04
		
RUN apt-get update && apt-get install -y software-properties-common && add-apt-repository ppa:deadsnakes/ppa && \
    apt-get update && apt-get install -y python3.6 python3.6-dev python3-pip git 
	
RUN ln -sfn /usr/bin/python3.6 /usr/bin/python3 && ln -sfn /usr/bin/python3 /usr/bin/python && ln -sfn /usr/bin/pip3 /usr/bin/pip
  • build
docker build -t docker-ubuntu16-python3.6 .
  • docker run
docker run -it docker-ubuntu16-python3.6 bash

1) Bazel

  • 소스코드 직접 빌드시 사용
  • Bazel 0.5.4 이상 version
  • 필요한 패키지 설치
sudo apt-get install pkg-config zip g++ zlib1g-dev unzip
  • Installer 실행
chmod +x bazel-0.15.0-installer-linux-x86_64.sh
./bazel-0.15.0-installer-linux-x86_64.sh --user
  • Environment 설정
export PATH="$PATH:$HOME/bin"

gRPC

  • dependencies 설치
sudo apt-get update && sudo apt-get install -y \
        automake \
        build-essential \
        curl \
        libcurl3-dev \
        git \
        libtool \
        libfreetype6-dev \
        libpng12-dev \
        libzmq3-dev \
        pkg-config \
        python-dev \
        python-numpy \
        python-pip \
        software-properties-common \
        swig \
        zip \
        zlib1g-dev
  • gRPC 설치
pip3 install grpcio
  • tensorflow serving clone
  • bazel build(뒤에 … 꼭 포함해야 함)
cd serving
bazel build -c opt --local_resources 5000,1.0,1.0 tensorflow_serving/…

2) Tensorflow-serving-api

  • Bazel을 설치하지 않고(=빌드하지 않고) 그냥 Python에서 사용하고 싶은 경우
  • Tensorflow Serving 배포 URI를 패키지 소스로 추가
echo "deb [arch=amd64] http://storage.googleapis.com/tensorflow-serving-apt stable tensorflow-model-server tensorflow-model-server-universal" | sudo tee /etc/apt/sources.list.d/tensorflow-serving.list

curl https://storage.googleapis.com/tensorflow-serving-apt/tensorflow-serving.release.pub.gpg | sudo apt-key add -
  • TensorFlow ModelServer 설치
sudo apt-get update && sudo apt-get install tensorflow-model-server
  • 공식적으론 Python2만 지원, 비공식적으로 Python3 존재
  • 설치(Python2)
pip3 install tensorflow-serving-api
  • 설치(Python3)
pip3 install tensorflow-serving-api-python3

Test

  • serving에서 제공하는 sample 모델 사용
  • tensorflow_model_server를 띄운 상태에서 Client가 Request
  • 설치와 마찬가지로 1) Bazel 2) tensorflow-serving-api로 나눠서 설명

공통

  • Sample Model 다운(serving folder에서 실행)
python tensorflow_serving/example/mnist_saved_model.py /tmp/mnist_model
  • model_base_path에 model.pb 파일과 variables 폴더를 저장하면 됨. saved_model github 참고해서 구현

1) Bazel

  • Tensorflow server 실행
bazel-bin/tensorflow_serving/model_servers/tensorflow_model_server --port=9000 --model_name=mnist --model_base_path=/tmp/mnist_model/
  • Client Request
bazel-bin/tensorflow_serving/example/mnist_client --num_tests=1000 --server=localhost:9000

2) Tensorflow-serving-api

  • python을 사용했지만 3점대(python3)도 몇가지 문법만 수정하면 정상 작동함!
  • Tensorflow server 실행
tensorflow_model_server --port=9000 --model_name=mnist --model_base_path=/tmp/mnist_model/
  • Client Request
python tensorflow_serving/example/mnist_client.py --num_tests=1000 --server=localhost:9000
  • 동시에 여러 모델을 추가하고 싶을 경우 config file 생성하고 실행 시 model_config_file path 지정
model_config_list: {
  config: {
    name: "Model1",
    base_path: "/path/to/model1",
    model_platform: "tensorflow"
  },
  config: {
    name: "Model2",
    base_path: "/path/to/model1",
    model_platform: "tensorflow"
  },
}
  • 실행
bazel-bin/tensorflow_serving/model_servers/tensorflow_model_server --port=9000 --model_config_file=<path_to_your_config_on_disk>

Reference

[출처] https://zzsza.github.io/data/2018/07/12/tensorflow-serving-tutorial/

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
공지 오라클 기본 샘플 데이터베이스 졸리운_곰 2014.01.02 86999
공지 [SQL컨셉] 서적 "SQL컨셉"의 샘플 데이타 베이스 SAMPLE DATABASE of ORACLE 가을의 곰을... 2013.02.10 79256
공지 [G_SQL] Sample Database 가을의 곰을... 2012.05.20 95995
26 감석분석 작업 로그 : 감성분석(Sentiment Analysis) - 깔끔한 텍스트 방식(tidytext) : xwMOOC 자연어 처리 졸리운_곰 2019.12.24 1244
25 '애자일과 데이터 관리의 결합'··· '데이터옵스'의 정의와 주요 기술 file 졸리운_곰 2019.11.17 1684
24 데브옵스와 분석의 결합··· ‘데이터옵스’를 아시나요? file 졸리운_곰 2019.11.17 1244
23 데이터옵스(DATAOPS) 란 무엇일까? file 졸리운_곰 2019.11.17 1639
22 데이터옵스는 단순히 데이터에 대한 데브옵스가 아님니다. DataOps is NOT Just DevOps for Data file 졸리운_곰 2019.11.17 1639
21 R에서 파이썬까지…데이터과학 학습 사이트 8곳 file 졸리운_곰 2019.04.21 1759
20 [통계] prediction VS forecast file 졸리운_곰 2019.04.03 1584
19 forecast 와 prediction의 차이를 아시나요? 졸리운_곰 2019.04.03 899
18 10분만에 끝내는 데이터분석 file 졸리운_곰 2019.04.03 1869
17 처음으로 케글 데이터분석에 도전하기 : Competing on kaggle.com for the First Time file 졸리운_곰 2019.01.27 4412
16 데이터 과학자가 갖춰야 할 5가지 스킬셋 file 졸리운_곰 2018.11.11 1379
15 데이터 사이언스 괜찮은 강의들 리스트 1 file 졸리운_곰 2018.11.11 2471
14 데이터 사이언스 학습 안내 졸리운_곰 2018.11.11 1673
13 Prophet: Automatic Forecasting Procedure 자동 예측 프로시져 프로그램 /데이터분석 / 데이터 과학 file 졸리운_곰 2018.09.04 1318
12 데이터 분석 어디에 집중할 것인가? 가장 먼저 실험에 집중하라 file 졸리운_곰 2018.02.06 1408
11 빅데이터 융합기획전문가 1기 교육 표창장 file 졸리운_곰 2018.01.03 1317
10 빅 데이터 기획에 대한 이해 file 졸리운_곰 2017.12.09 2025
9 분야별 빅데이터 애널리틱스 적용 사례 및 성공의 비결 file 졸리운_곰 2017.12.08 2162
8 하둡 에코시스템을 활용한 Hybrid DW 구축 사례 file 졸리운_곰 2017.12.08 2007
7 gmail 수신 메일로 워드클라우드 생성 : Creating a gmail wordcloud 졸리운_곰 2017.11.20 1894
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED