LoginSignup
18

More than 5 years have passed since last update.

AWSのEC2上で、FlaskとKerasを使ってAI ウェブアプリを動かす

Last updated at Posted at 2017-04-21

以下の構成で deep learning アプリを構築しました。

Keras <-> Flask <-> uWSGI <-> socket <-> Nginx <-> Client

AWSでは月750時間(=31日x24時間)無料のEC2インスタンス t2.microを launchします

1. EC2で Deep Learning環境の構築

ssh接続の準備

key-pairのダウンロード

$ mkdir config
$ mv Download/riki-ssh.pem.txt config/riki-ssh.pem
$ chmod 600 config/riki-ssh.pem
# $ ssh -i config/riki-ssh.pem ubuntu@ec2-52-10-25-30.us-west-2.compute.amazonaws.com
$ ssh -i config/riki-ssh.pem ubuntu@<FQDN>

ubuntuのアップグレード

$ sudo apt-get update
$ sudo apt-get upgrade -y

ロケールの設定

$ sudo apt-get install language-pack-ja
$ sudo update-locale LANG=ja_JP.UTF-8

Python環境構築

$ sudo apt-get install -y build-essential python-pip python-dev git python-numpy swig python-dev default-jdk zip zlib1g-dev ipython
$ sudo apt-get install -y git gcc make openssl libssl-dev libbz2-dev libreadline-dev libsqlite3-dev
$ git clone https://github.com/yyuu/pyenv.git ~/.pyenv
$ git clone https://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv
$ touch .bash_profile
$ nano .bash_profile
.bash_profile
# pyenv
export PYENV_ROOT=$HOME/.pyenv
export PATH=$PYENV_ROOT/bin:$PATH
eval "$(pyenv init -)"
# virtualenv
eval "$(pyenv virtualenv-init -)"
export PYENV_VIRTUALENV_DISABLE_PROMPT=1
$ source .bash_profile
$ pyenv install 3.6.1
$ pyenv virtualenv 3.6.1 ML-3.6.1
$ pyenv global ML-3.6.1

keras環境の構築

$ sudo apt-get install -y graphviz libgraphviz-dev pkg-config
$ pip install -U numpy matplotlib pandas seaborn ipython jupyter
$ pip install -U tensorflow keras h5py pygraphviz pydot pydot_ng

動作テスト

$ git clone https://github.com/furukawa-ai/deeplearning_papers.git
$ cd deeplearning_papers/implementation/basics
$ python mlp.py

2. webアプリ(手書き文字認識)を動作させる

Keras <-> Flask <-> uwsgi <-> socket <-> Nginx <-> Client

まずEC2の設定で80番ポートを開放。

nginxをインストール

# (1) nginxサイトが配布するPGPキーを追加
curl http://nginx.org/keys/nginx_signing.key | sudo apt-key add -

# (2) リポジトリを一覧に追加
sudo sh -c "echo 'deb http://nginx.org/packages/ubuntu/ trusty nginx' >> /etc/apt/sources.list"
sudo sh -c "echo 'deb-src http://nginx.org/packages/ubuntu/ trusty nginx' >> /etc/apt/sources.list"

# (3) アップデート後、nginxをインストール
sudo apt-get update
sudo apt-get install nginx

$ sudo mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/orig.default.conf.orig
$ sudo touch /etc/nginx/conf.d/uwsgi.conf
$ sudo nano /etc/nginx/conf.d/uwsgi.conf

server {
    listen       80;

    location / {
        include uwsgi_params;
        uwsgi_pass unix:///tmp/uwsgi.sock;
    }
}

$ sudo nginx -t
$ sudo service nginx start

flask, uwsgi をインストール、実行

$ pip install -U flask uwsgi
$ cd
$ git clone https://github.com/msrks/keras_num_pred.git
$ cd keras_num_pred
$ uwsgi --socket /tmp/uwsgi.sock --module app --callable app --chmod-socket=666 &

自動起動設定

$ sudo nano /etc/rc.local

sudo service nginx start
cd keras_num_pred; uwsgi --socket /tmp/uwsgi.sock --module app --callable app --chmod-socket=666 &

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18