Code Monkey home page Code Monkey logo

planet-amazon's Introduction

Deep Learning Workflow to productionize a model

  1. Initial project setup
  2. Train your model and save the checkpoints
  3. Create predict.py file
  4. Expose your model using Flask or (Gramex: our preferred data-server)
  5. Create a Docker and Docker Compose file
  6. Create setup.sh script, to create the required environment to host your model
  7. Push your code into github or bitbucket
  8. Host your application in the cloud
  9. Cookie Cutter Usage

Initial project setup

1.1 Create a conda environment and install the required packages, eg: pytorch

conda create -y -n <project-name> python=3
conda activate <project-name>
conda install pytorch-cpu torchvision-cpu -c pytorch

1.2 Recommended directory structure to start your project

<project-name>\
├── checkpoint\
├── data\
├── docker\
├── nbs
│   └── experiment with your code here
├── python-scripts

Train your model and save the checkpoints

Use your favourite framework to train your model. Store the model checkpoints inside the checkpoint directory.

Create predict.py file

Define 3 functions in here:

def load_model(*args):
    '''Load and return the model from the checkpoint/ dir.'''
    pass

def load_data(input):
    '''Perform the required pre-processing on the given input required for the model and return.'''
    pass

def predict(model, input):
    '''Pass the input to the model and return the result.'''
    pass

Expose your model using Flask

eg: Flask server

4.1 Create server.py inside the dir and add two folders to hold your html templates and static files

<project-name>\
├── static
│   └── css
└── templates
│   └── home.html
│   └── predict.html
│   └── error.html etc
│   server.py

4.2 Create your @routes

Import the load_model, load_data & predict fn inside 'server.py'.
Create your REST API.

from predict import load_model, load_data, predict

@app.route('/', methods=['GET', 'POST'])
def home():
    '''Create your API'''
    pass

Create a Docker and Docker Compose file

5.1 Directory structure

docker/
├── docker-compose.yml
└── Dockerfile

5.2 Dockerfile

ARG BASE_CONTAINER=ubuntu:bionic-20180526@sha256:c8c275751219dadad8fa56b3ac41ca6cb22219ff117ca98fe82b42f24e1ba64e
FROM $BASE_CONTAINER
SHELL ["/bin/bash", "-c"]

ENV LANG:C.UTF-8 LC_ALL=C.UTF-8
<>Soumya Ranjan <se.Make>@srmsoumya"

RUN apt-get update --fix-missing && apt-get install -y \
    wget bzip2 ca-certificates \
    htop tmux unzip tree \
    libglib2.0-0 libxext6 libsm6 libxrender1 libgl1-mesa-glx \
    git && \
    apt-get clean

RUN mkdir -p /home/ubuntu
ENV HOME=/home/ubuntu
VOLUME $HOME
WORKDIR $HOME

EXPOSE 5000

ENTRYPOINT [ "/bin/bash" ]

5.3 docker-compose.yml

  • Give a suitable name to your container .
  • Make sure you expose the port where you wish to deploy your model, in our case port: 5000.
  • Give suitable names to your data volumes , which we will create in our next steps.
version: '3'
services: 
    planet:
        build: .
        container_name: <container-name>
        ports:
            - 5000:5000
        tty: true
        volumes: 
            - <project-name>-code:/home/ubuntu
            - <project-name>-opt:/opt
            - <project-name>-profile:/etc/profile.d/
volumes:
    <project-name>-code:
    <project-name>-opt:
    <project-name>-profile:

Create setup.sh script, to create the required environment to host your model.

6.1 Host your model on a storage account to which you have required access.
eg: Steps to upload your model in Amazon S3 Storage.

6.2 Export your conda environment into environment.yml file.

  • Make sure you are inside the project conda environment.
conda env export > environment.yml

6.3 setup.sh

#!/bin/bash

# Download the model
MODEL=./checkpoint/cp_best.pt.tar
if [ -f "$MODEL" ]; then
    echo "$MODEL exist, skipping download."
else
    wget <path-to-model-cloud-storage> -O checkpoint/<model-name>
fi

# Download the installer, install and then remove the installer
wget --quiet https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda.sh
/bin/bash ~/miniconda.sh -b -p /opt/conda
rm ~/miniconda.sh
ln -s /opt/conda/etc/profile.d/conda.sh /etc/profile.d/conda.sh

# Set conda path
echo ". /opt/conda/etc/profile.d/conda.sh" >> ~/.bashrc

PS1='$ '
source ~/.bashrc

# Update conda
conda update -y conda 

# Create an environment
conda env create -f environment.yml
echo "conda activate <project-name>" >> ~/.bashrc
source ~/.bashrc

Push your code into github or bitbucket

Host your application in the cloud.

8.1 We will be using Digital Ocean. Steps to create an ubuntu instance in Digital-Ocean.

8.2 SSH into your server

ssh -L5000:localhost:5000 root@IP

8.3 Install Docker and Docker Compose.

8.4. Setting up the docker environment

# Create docker-volumes
docker volume create <project-name>-code
docker volume create <project-name>-opt
docker volume create <project-name>-profile

# Pull the code to get the Dockerfile & docker-compose.yml
git clone <path-to-project>

# Run docker-compose in detached mode and enter inside the container
cd <project-name>/docker
docker-compose up -d
docker exec -it <container-name> /bin/bash

8.5. Inside the docker container, run the following commands to host the application

git clone <path-to-project>
cd <project-name>
source setup.sh

# Once the setup is done, run server in detached mode, use tmux or nohup
python server.py

8.6. Browse to localhost:5000 in your local browser, to upload and test the application

Cookie Cutter Usage

# Installation
pip install cookiecutter

cookiecutter CookieCutter_ProDL/

# Same can work for cloning templates on github*

cookiecutter https://github.com/user/CookieCutter_ProDL

planet-amazon's People

Contributors

srmsoumya avatar gitlost-murali avatar

Stargazers

Philipp Gärtner avatar Robin Cole avatar Jaidev Deshpande avatar

Watchers

Jaidev Deshpande avatar  avatar Ranjan Balappa avatar

planet-amazon's Issues

Resolve path issues

Add "python-scripts" to system path in "predict.py"
import sys; sys.path.append('./python_scripts/')

Add CI/CD

Implement CI/CD to host/update the application every time it is pushed.

Simplify the process

  1. Reduce the number of steps required by the user to productionize the model.
  2. Include defaults where possible.

First trial feedback

Given the following issues, continuing with the whole README is somewhat time consuming, so I'm leaving it here until these are fixed.

Issues

  • It's not clear whether github.com/srm-soumya/planet-amazon is to be used as a template,
    or if it is a fully-functioning project by itself.
  • In the README, it's not clear where the predict.py file should be located.
  • This file should raise NotImplementedErrors initially.
  • It's not clear that a Torch model has to be loaded - if this works with Keras or native TF models too,
    please mention that in the README.
  • Add link to a sample model in the README
  • No clue as to what the three functions in predict.py should be.
    • What are the input arguments? Serialized model paths / model instances / tensors / dataset instances, what?
    • What are the functions supposed to return?
  • The functions in predict.py need parsed command line arguments to work - this is not mentioned in the README.

Good to have

  • Maybe add an option for GPU in conda installations
  • Use cookiecutter for creating project structure, for both torch and Flask related code.

Base docker file

Create a base docker file with all the main dependencies so it can just be included

Project Template

Use cookie cutter or any other tool to create templates for the project.

Adding Gramex support

The platform should be usable with Gramex:

  • there need to be FunctionHandler examples
  • cookiecutter needs to initiate some Gramex code automatically and add prediction functions as gramex endpoints.

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.