GithubHelp home page GithubHelp logo

maajidkhan / deploying3deeplearningmodels-flask- Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 0.0 24 KB

This course project describes the use of this learning methods like VGG16, ResNet50, Xception, etc.; to perform Multi-Class image classification and implement these deep learning algorithms on a large-scale Multi-Class Image Classification dataset from ImageNet yearly challenge task. The same will be deployed as an application using python's FLASK framework.

License: MIT License

Python 45.81% CSS 5.16% JavaScript 27.63% HTML 21.40%

deploying3deeplearningmodels-flask-'s Introduction

Deep Learning Keras and Flask as Web App In this tutorial, we will present a simple method to take a Keras model and create Python Flask Web App.

Specifically, we will learn:

How to load a Keras model into memory so it can be efficiently used for inference How to use the Flask web framework to create an endpoint for our API How to use the web api for our UI screen How to make predictions using our model, and return the results to the UI Configuring your development environment We’ll be making the assumption that Keras is already configured and installed on your machine. If not, please ensure you install Keras using the official install instructions.

We’ll need to install Flask, it is a Python web framework, so we can build our API endpoint. We’ll also need requests so we can consume our API as well.

$ pip install flask gevent requests pillow Also we will use the requirements file. We use it to simple load dependencies.

Below is our requirements.txt file

Werkzeug Flask numpy Keras gevent pillow h5py tensorflow We must to use the below command to load dependencies

  $ pip install -r requirements.txt

Make sure you have the following installed:

tensorflow keras flask pillow h5py gevent Create your Keras Rest Api We create a app.py class and we use three image model (ResNet50, VGG16, Xception)

We include this libraries below.

  # ResNet50
  from keras.applications.resnet50 import ResNet50
  from keras.applications.imagenet_utils import preprocess_input as preprocess_input_resNet50, decode_predictions as decode_predictions_resNet50
  # VGG16
  from keras.applications.vgg16 import VGG16
  from keras.applications.vgg16 import preprocess_input as preprocess_input_vgg16, decode_predictions as decode_predictions_vgg16
  # Xception
  from keras.applications.xception import Xception
  from keras.applications.xception import preprocess_input as preprocess_input_xception, decode_predictions as decode_predictions_xception
  You can also use pretrained model from Keras Check https://keras.io/applications/

We will need to upload models that were created before

  modelResNet50 = ResNet50(weights='imagenet')
  print('ResNet50 Model loaded.')

  modelVGG16 = VGG16(weights='imagenet', include_top=True)
  print('VGG16 Model loaded.')

  modelXception = Xception(weights='imagenet', include_top=True)
  print('Xception Model loaded.')

We will need to create API endpoint services

  @app.route('/', methods=['GET'])
  def index():
      # Main page
      return render_template('index.html')
  In the code above, it is redirected to the index.html page

        @app.route('/predictResNet50', methods=['GET', 'POST'])
        def predictResNet50():
            if request.method == 'POST':
                file_path = get_file_path_and_save(request)

                img = image.load_img(file_path, target_size=(224, 224))

                # Preprocessing the image
                x = image.img_to_array(img)
                # x = np.true_divide(x, 255)
                x = np.expand_dims(x, axis=0)

                # Be careful how your trained model deals with the input
                # otherwise, it won't make correct prediction!
                x = preprocess_input_resNet50(x, mode='caffe')
                # Make prediction
                preds = modelResNet50.predict(x)

                # Process your result for human
                # pred_class = preds.argmax(axis=-1)            # Simple argmax
                pred_class = decode_predictions_resNet50(preds, top=1)   # ImageNet Decode
                result = str(pred_class[0][0][1])               # Convert to string
                return result
            return None



        @app.route('/predictVGG16', methods=['GET', 'POST'])
        def predictVGG16():
            if request.method == 'POST':
                file_path = get_file_path_and_save(request)

                img = image.load_img(file_path, target_size=(224, 224))
                img_data = image.img_to_array(img)
                img_data = np.expand_dims(img_data, axis=0)
                img_data = preprocess_input_vgg16(img_data)

                preds = modelVGG16.predict(img_data)

                # decode the results into a list of tuples (class, description, probability)
                pred_class = decode_predictions_vgg16(preds, top=1)
                result = str(pred_class[0][0][1])  # Convert to string
                return result
            return None


        @app.route('/predictXception', methods=['GET', 'POST'])
        def predictXception():
            if request.method == 'POST':
                file_path = get_file_path_and_save(request)

                img = image.load_img(file_path, target_size=(224, 224))
                img_data = image.img_to_array(img)
                img_data = np.expand_dims(img_data, axis=0)
                img_data = preprocess_input_xception(img_data)

                preds = modelXception.predict(img_data)

                # decode the results into a list of tuples (class, description, probability)
                pred_class = decode_predictions_xception(preds, top=1)
                result = str(pred_class[0][0][1])  # Convert to string
                return result
            return None

In the code above, we created three endpoint service . Their names are ResNet50, VGG16, Xception.

Firstly, We are recording the pictures from come to client. We are converting it to a matrix of 224 x 224. Then we are preprocess it.

Finally, we use a model to prediction and convert it string result. Than we send it back to the client.

Starting the Keras Server The Flask + Keras server can be started by running:

  $ python app.py
  ResNet50 Model loaded.
  VGG16 Model loaded.
  Xception Model loaded.
  Running on http://localhost:5000
  You can now access the Prediction Flask WebApp via http://127.0.0.1:5000

alt text

We have successfully called the Keras REST API and obtained the model’s predictions via Python and We have achieved successful estimates :)

deploying3deeplearningmodels-flask-'s People

Contributors

maajidkhan avatar

Stargazers

 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.