페이지

2019년 7월 12일 금요일

Keras RNN 모델을 Heroku에 배포하기

Keras RNN 모델을 Heroku에 배포하기

실습 자료

실습 준비

개발 환경

  • Windows 10
  • Anaconda 2019.03
  • Python 3.7
  • Flask 1.1.1

배포 환경

  • Heroku
  • Python
  • Flask
  • Gunicorn

설치 프로그램

  1. Git
  2. Heroku CLI
  3. Anaconda

프로젝트 소스

다운로드 위치
프로젝트 폴더 구조
data\
deployment\
images\
models\
notebooks\
tables\
.dockerignore
.gitignore
Dockerfile
LICENSE
README.md
docker-compose.yml
requirements.txt

Heroku 계정 생성

  • Heroku 사이트에서 무료 계정을 생성합니다.

실습 진행

프로젝트 폴더 작업

새로운 프로젝트 폴더 구성
기존 프로젝트 폴더의 바깥에 새로운 프로젝트 폴더 run-keras를 만들고 기존 파일들 일부를 복사하여 아래와 같이 구성합니다.
rnn-keras\
    data\
    models\
    static\
    templates\
    ex.py
    run_keras_server.py
    utils.py
파일 참조 위치 수정
data 폴더와 models 폴더의 상대 경로가 바뀌었기 때문에 이를 참조하는 부분을 찾아서 수정합니다.
#word_idx = json.load(open('../data/word-index.json'))
#sequences = json.load(open('../data/sequences.json'))
#word_idx = json.load(open('../data/word-index.json')) 
word_idx = json.load(open('data/word-index.json'))
sequences = json.load(open('data/sequences.json'))
word_idx = json.load(open('data/word-index.json')) 
  • run_keras_server.py
#model = load_model('../models/train-embeddings-rnn.h5')
model = load_model('models/train-embeddings-rnn.h5')
모델 로딩 시점 수정
웹앱 환경에서 모델을 로드할 수 있도록 load_keras_model() 함수 호출 위치를 변경하여 모델 로딩 시점을 수정합니다.
  • run_keras_server.py
load_keras_model()

if __name__ == "__main__":
    print(("* Loading Keras model and Flask starting server..."
           "please wait until server has fully started"))
    #load_keras_model()
    # Run app
    app.run(host="0.0.0.0", port=80)

가상환경 구성하기

Anaconda Prompt 창에서 실습을 진행합니다.
가상환경 만들기
> conda create -n rnn_keras
가상환경 활성화
> conda activate rnn_keras
라이브러리 설치
> conda install pip
> pip install tensorflow
> pip install keras
> pip install flask
> pip install wtforms
> pip install gunicorn

웹앱을 로컬에서 실행하기

Anaconda Prompt 창에서 실습을 진행합니다.
> cd rnn-keras
웹앱 실행
> set FLASK_APP=run_keras_server.py
> flask run
브라우져로 확인
  1. 브라우져로 http://localhost:5000/ 주소의 페이지를 엽니다.
  2. HTML 폼에 값들을 입력하고 Enter 버튼을 클릭합니다.
  3. Input Seed Network Output 제목과 함께 본문이 표시되면 오류 없이 정상적으로 실행된 것입니다.
    • 오류가 발생하면 웹앱 실행 프롬프트 창에서 오류와 관련된 메시지가 있는지 확인하고 이를 해결합니다.

Heroku 웹앱으로 준비하기

Anaconda Prompt 창에서 실습을 진행합니다.
requirements.txt 파일 만들기
> cd run-keras
> pip freeze > requirements.txt
생성된 requirements.txt 파일 내용은 아래와 같습니다.
absl-py==0.7.1
astor==0.8.0
certifi==2019.6.16
Click==7.0
Flask==1.1.1
gast==0.2.2
google-pasta==0.1.7
grpcio==1.22.0
gunicorn==19.9.0
h5py==2.9.0
itsdangerous==1.1.0
Jinja2==2.10.1
Keras==2.2.4
Keras-Applications==1.0.8
Keras-Preprocessing==1.1.0
Markdown==3.1.1
MarkupSafe==1.1.1
numpy==1.16.4
protobuf==3.8.0
PyYAML==5.1.1
scipy==1.3.0
six==1.12.0
tensorboard==1.14.0
tensorflow==1.14.0
tensorflow-estimator==1.14.0
termcolor==1.1.0
Werkzeug==0.15.4
wincertstore==0.2
wrapt==1.11.2
WTForms==2.2.1
Procfile 파일 만들기
rnn-keras 폴더 아래에 Procfile 파일을 추가하고 텍스트 파일 편집기를 사용하여 아래 내용을 저장합니다.
web: gunicorn run_keras_server:app
.gitignore 파일 만들기
rnn-keras 폴더 아래에 .gitignore 파일을 추가하고 텍스트 파일 편집기를 사용하여 아래 내용을 저장합니다.
__pycache__/
.DS_Store
Git 저장소 만들기
> cd rnn-keras
> git init
> git add .
> git commit -m "Initial commit."

웹앱을 Heroku 클라우드로 배포하기

Command Prompt 창에서 실습을 진행합니다.
Heroku 클라우드에 로그인
> heroku login -i
Heroku 클라우드에 앱 생성
> heroku create
Creating app... done, ⬢ ancient-caverns-38516
https://ancient-caverns-38516.herokuapp.com/ | https://git.heroku.com/ancient-caverns-38516.git
웹앱 배포장소를 Heroku 클라우드로 지정하기
> cd rnn-keras
> heroku git:remote -a ancient-caverns-38516
set git remote heroku to https://git.heroku.com/ancient-caverns-38516.git
웹앱을 배포하기
> git push heroku master
...
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote:
remote: -----> Compressing...
remote:        Done: 438.2M
remote: -----> Launching...
remote:  !     Warning: Your slug size (438 MB) exceeds our soft limit (300 MB) which may affect boot time.
remote:        Released v3
remote:        https://ancient-caverns-38516.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/ancient-caverns-38516.git
 * [new branch]      master -> master
브라우져에서 웹앱 열기
브라우져에서 아래 주소의 페이지를 엽니다.
또는 명령 프롬프트에서 아래 명령을 사용하여 위 주소의 페이지를 브라우져로 열 수 있습니다.
> heroku open
Written with StackEdit.

댓글 없음:

댓글 쓰기

가설 검정 제대로 이해하기

가설 검정 제대로 이해하기 용어 정리 출처: 위키백과 , WIKIPEDIA 통계적 추론 추론 통계 또는 추론 통계학(inferential statistics)으로 불린다. 기술 통계학(descripti...