GithubHelp home page GithubHelp logo

floleuerer / fastai-docker-deploy Goto Github PK

View Code? Open in Web Editor NEW
19.0 1.0 0.0 1.18 MB

Deploy fastai models with Docker

License: MIT License

Dockerfile 1.39% Python 41.89% HTML 27.82% JavaScript 20.44% CSS 8.46%
fastai docker fastai-model deployment

fastai-docker-deploy's Introduction

fastai-docker-deploy

Building DeepLearning models is really easy with fast.ai - deploying models unfortunatley is not! So i tried to find a cheap and easy way, to deploy models with Docker as a REST-API (folder fastai-rest). Besides that, i also to develop a "frontend" component using nginx to secure the API calls by enabling SSL with letsencrypt. I added a small Website so you can interact with the model :).

All of this is running on a 5 $ DigitalOcean Droplet. See my website Dog or HotDog?.

If you are just intrested in deploying a model as a REST-API see fastai-rest and the README there and my blogpost.

Architecutre

Here's an overview of the architecture.

                DigitalOcean Droplet
     +-------------------------------------+
     |               Docker                |
     |                                     |
     |                      /api calls     |
     |    +-----------+  interal redirect  |
     |    |           |     http 8080      |
     |    |  fastai   | <------------+     |
     |    |           |              |     |
     |    |           |              |     |
     |    +-----------+              |     |   dog-or-hotdog.meansqua.red/api   +------------+
     |      Container      +-----------+   |          http(s) 80/443            |            |
     |                     |           |   |        redirect to fastai          |            |
     |                     |           | <--------------------------------------+            |
     |                     |   nginx   |   |                                    |   client   |
     |                     |           | <--------------------------------------+            |
     |                     |           |   |    dog-or-hotdog.meansqua.red      |            |
     |                     +-----------+   |         http(s) 80/443             |            |
     |                       Container     |       Website nginx/html           +------------+
     |                                     |
     +-------------------------------------+

Prerequisites

Create Droplet + Domain

  • Create Docker Droplet on DigitalOcean (see my blogpost - make sure to follow the instructions and create a swap-file!)
  • Create (sub-)domain (e.g. dog-or-hotdog.meansqua.red) and assign Droplet IP

Setup

The nginx-frontend/config and nginx-frontend/html folders are mounted into the Docker container. So it's curcial to use the same the paths (so make sure to clone the repo in /docker)!

Hostname

The hostname dog-or-hotdog.meansqua.red has to be replaced with your hostname in the following files:

nginx-frontend/config/default.config
nginx-html/classify.js

Letsencrypt

Install certbot and create letsencrypt certificates

# add repositories
sudo apt-get update
sudo apt-get install software-properties-common
sudo add-apt-repository universe
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update

# install certbot
sudo apt-get install certbot

# open firewall port 80 temporarily
sudo ufw allow 80/tcp

# create letscrypt certificate
sudo certbot certonly --standalone

# close firewall again
sudo ufw delete allow 80/tcp

The certificates are stored in /etc/letsencrypt/live/<domainname>.

Cronjob for letsencrypt certificate renewal

To renew the letsencrypt certificates automatically add the following command to the root users crontab. This will renew your certificate at the first day of each month at 00:00.

crontab -e

# add the following line
0 0 1 * * /usr/bin/certbot renew --pre-hook "docker-compose -f /docker/fastai2-docker-deploy/docker-compose.yml down" --post-hook "docker-compose -f /docker/fastai2-docker-deploy/docker-compose.yml up -d"

Clone repository

Clone this repository on your Droplet

mkdir /docker && cd /docker
git clone https://github.com/floleuerer/fastai-docker-deploy.git

Download fastai-model

The fastai model is not part of the repository. The Dog or HotDog model can be downloaded here and has to be copied to fastai-rest/app/model.pkl.

cd /docker/fastai-docker-deploy/fastai-rest/app/
wget https://www.meansqua.red/files/model.pkl

Build docker images

cd /docker/fastai-docker-deploy
docker-compose build

Run the App

After successfully building the docker images you can start / stop the app:

cd /docker/fastai-docker-deploy
# start
docker-compose up -d

# stop
docker-compose down

Adopt project

To use this App with your own fastai image classification model, you have to make some adjustments.

Hostname

The hostname "dog-or-hotdog.meansqua.red" has to be replaced with your hostname in the following files:

nginx-frontend/config/default.config

server {
    listen       80;
    server_name dog-or-hotdog.meansqua.red;


server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name dog-or-hotdog.meansqua.red;

    ssl_certificate /etc/letsencrypt/live/dog-or-hotdog.meansqua.red/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/dog-or-hotdog.meansqua.red/privkey.pem;

nginx-html/classify.js

let url = "https://dog-or-hotdog.meansqua.red/api/analyze:predict";

fastai-Model

Copy your exported model (learn.export()) to fastai-rest/app/model.pkl.

The model labels are replaced with a human friendlier text (dog -> Dog and hot_dog -> Hot Dog). Please replace and add all your class labels in nginx-frontend/classes.json.

{
    "dog": "Dog", 
    "hot_dog": "Hot Dog"
}

Add example images for random image classification

Use the python scripts in nginx-frontend/html/examples/ to create an examples.json that is used for the random image classificaiton (click on drop area). Move examples.json to ngnix-frontend/html/examples.json.

Rebuild the docker containers

See "Build docker images" above.

fastai-docker-deploy's People

Contributors

floleuerer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.