Coder Social home page Coder Social logo

klonggan / deeplearning_flask Goto Github PK

View Code? Open in Web Editor NEW

This project forked from alvertogit/deeplearning_flask

0.0 1.0 0.0 5.72 MB

Data Science AI Artificial Intelligence Deep Learning Python Keras TensorFlow TensorFlow2 Flask Docker NGINX Gunicorn microservices REST API Jupyter Lab Notebook

Jupyter Notebook 87.34% Dockerfile 0.72% Python 9.21% HTML 2.52% Makefile 0.09% Shell 0.12%

deeplearning_flask's Introduction

DEEP LEARNING ON FLASK

This repository stores a test to demonstrate skills mainly with Python, Keras, Flask, Docker, Jupyter Notebook, microservices and REST API.

PURPOSE

The goal is to deploy on Flask a deep learning model as a microservice. The model is used to predict handwritten digits and it has been previously trained on a Jupyter Notebook. REST API are utilized to communicate with the deployed model. e.g. send image to be analized and return the generated predictions to the client.

DEPENDENCIES

The code has been tested using:

  • Python (3.8): an interpreted high-level programming language for general-purpose programming.
  • Jupyter Lab (3.1.6): a web-based interactive development environment for Jupyter Notebooks, code and data.
  • Flask (2.0.1): a microframework for Python based on Werkzeug, Jinja 2 and good intentions.
  • Gunicorn (20.1.0): a Python WSGI HTTP Server for UNIX.
  • NGINX (1.21.1): a free, open-source, high-performance HTTP server, reverse proxy, and IMAP/POP3 proxy server.
  • Docker (19.03.13-ce): an open platform for developers and sysadmins to build, ship, and run distributed applications, whether on laptops, data center VMs, or the cloud.
  • Docker-Compose (1.29.2): a tool for defining and running multi-container Docker applications.
  • Keras (TensorFlow built-in): a high-level neural networks [API], written in Python and capable of running on top of TensorFlow.
  • TensorFlow (2.6.0): an open source software Deep Learning library for high performance numerical computation using data flow graphs.
  • Matplotlib (3.4.3): a plotting library for Python and its numerical mathematics extension NumPy.
  • NumPy (1.19.5): a library for Python, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.
  • scikit-image (0.18.2): a collection of algorithms for image processing with Python.

PYTHON VIRTUAL ENVIRONMENT

Virtual environment (<env_name>=dlflask38) can be generated from requirements.txt file located in the repository.

Command to configure virtual environment with venv:

~/deeplearning_flask$ python3 -m venv dlflask38
~/deeplearning_flask$ source dlflask38/bin/activate
(dlflask38)~/deeplearning_flask$ python3 -m pip install pip==21.2.4
(dlflask38)~/deeplearning_flask$ python3 -m pip install setuptools==57.4.0
(dlflask38)~/deeplearning_flask$ python3 -m pip install -r requirements.txt

REPOSITORY CONTENT

The repository main folder contains:

deeplearning_flask
├── .env.example
├── app
│   ├── app
│   │   ├── __init__.py
│   │   ├── api.py
│   │   ├── model.py
│   │   ├── static
│   │   │   └── 4.jpg
│   │   └── templates
│   │       └── dlflask.html
│   ├── config.py
│   ├── Makefile
│   ├── mnist_model.h5
│   ├── server.py
│   └── tests
│       ├── __init__.py
│       ├── conftest.py
│       └── test_app.py
├── Deep Learning MNIST prediction model with Keras.ipynb
├── docker-compose.yml
├── Dockerfile
├── docs
├── nginx
│   └── conf.d
│       └── local.conf
├── README.md
└── requirements.txt

ARCHITECTURE

The architecture created with docker-compose uses two different Docker containers for:

The following diagram illustrates the architecture in blocks:

Architecture

DEEP LEARNING MODEL

The definition and training of the deep learning MNIST model was done through a notebook in Jupyter Lab. The employed notebook is stored in the main folder, to run it use the command shown below:

(dlflask38)~/deeplearning_flask$ jupyter lab Deep\ Learning\ MNIST\ prediction\ model\ with\ Keras.ipynb

HOW TO RUN DEEP LEARNING ON FLASK WITH DOCKER COMPOSE

The steps and commands to run the deep learning model on the Flask server with docker-compose are described below.

Before executing docker-compose is strongly recommended to close other applications to free up resources and ports to avoid potential issues. Then docker-compose can be executed to build services.

~/deeplearning_flask$ docker-compose build

Next step consists in executing docker-compose up command.

~/deeplearning_flask$ docker-compose up

If everything goes fine at the end it should appear something similar to:

...
...
web_1    | 2020-06-04 19:30:17.818273: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version

TEST SERVER & REST API

There are different ways to check that the server is running properly. One is opening a web browser such as Chrome or Firefox and paste the following URL:

http://127.0.0.1/

The web browser should show the text "Deep Learning on Flask".

REST API can be tested with pytest or curl.

It is possible to execute tests of Flask microservice created with pytest from inside the Flask Docker container using Makefile:

~/deeplearning_flask$ docker exec -it deeplearning_flask_web_1 /bin/bash
~/app# make test
...
test_app.py ..                                                         [100%]

A POST example using curl from outside Docker container is shown below:

~/deeplearning_flask$ curl -F file=@app/app/static/4.jpg -X POST 'http://127.0.0.1/api/predictlabel' | json_pp
{
  "most_probable_label": "4",
  "predictions": [
    {
      "label": "0",
      "probability": "8.025511e-09"
    },
    {
      "label": "1",
      "probability": "1.9455256e-05"
    },
    {
      "label": "2",
      "probability": "1.4459256e-05"
    },
    {
      "label": "3",
      "probability": "9.0475e-06"
    },
    {
      "label": "4",
      "probability": "0.9971827"
    },
    {
      "label": "5",
      "probability": "7.5980934e-06"
    },
    {
      "label": "6",
      "probability": "1.1683321e-06"
    },
    {
      "label": "7",
      "probability": "0.0018591178"
    },
    {
      "label": "8",
      "probability": "0.0005154088"
    },
    {
      "label": "9",
      "probability": "0.00039107347"
    }
  ],
  "success": true
}

CREDITS

author: alvertogit copyright: 2018-2021

deeplearning_flask's People

Contributors

alvertogit avatar

Watchers

James Cloos 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.