Create a REST API using the model learned in Lobe and TensorFlow Serving.

In the article below, I explained how to learn with Lobe and how to export the learned model and use it from Python.

I tried "Lobe," which makes it easy to train machine learning models published by Microsoft. How to use the model learned in Lobe in Python

This time I would like to create a REST API in combination with TensorFlow Serving.

Preparation for operating TensorFlow Serving

See the article mentioned above for learning with Lobe and exporting models.

After trying various things, it seems that the name of the directory containing the model needs to be a number in order for TensorFlow Serving to work. (I haven't grasped this area yet, so I would appreciate it if anyone knows it.)

Therefore, create a directory named with numbers in the model directory exported from Lobe and copy or move the necessary files.

Launch TensorFlow Serving with Docker

The easiest way to use TensorFlow Serving is with Docker. The official Image is uploaded to docker hub, so use this. tensorflow/serving

The model directory used this time is as follows. TensorFlow Serving uses the model in the directory 1.

── sample_model
    ├── 1 #Create a new sample_Copy or move necessary files under model
    │   ├── saved_model.pb
    │   └── variables
    │       ├── variables.data-00000-of-00001
    │       └── variables.index
    ├── example
    │   ├── README.md
    │   ├── requirements.txt
    │   └── tf_example.py
    ├── saved_model.pb
    ├── signature.json
    └── variables
        ├── variables.data-00000-of-00001
        └── variables.index

Start docker as follows.

# docker run -t --rm -p 8501:8501 -v "`pwd`/sample_model:/models/sample_model" -e MODEL_NAME=sample_model tensorflow/serving

When the model is loaded and the operation starts normally, the following message will be displayed.

2020-11-03 01:04:51.142942: I tensorflow_serving/model_servers/server.cc:387] Exporting HTTP/REST API at:localhost:8501 ...
[evhttp_server.cc : 238] NET_LOG: Entering the event loop ...

If this message is output, TensorFlow Serving has started.

Try image classification with REST

With the steps up to this point, you are ready to try image classification with REST. Let's send a GET request to check the operation of TensorFlow Serving.

import requests
import json

url = 'http://localhost:8501/v1/models/sample_model'
res = requests.get(url)
print(json.loads(res.text))

If the following response is returned, it is working without any problem.

{'model_version_status': [{'version': '1', 'state': 'AVAILABLE', 'status': {'error_code': 'OK', 'error_message': ''}}]}

Forecast requests are made by POST.

import json

import cv2
import numpy as np
import requests

#Prediction request URL
url = 'http://localhost:8501/v1/models/sample_model:predict'

def predict(image_path):
    image = cv2.imread(image_path, cv2.IMREAD_COLOR)
    image = image.astype(np.float32) / 255
    image = image.tolist()

    headers = {"content-type": "application/json"}
    body = {"inputs": [image]}
    r = requests.post(url, data=json.dumps(body), headers=headers)
    r_json = json.loads(r.text)
    return r_json['outputs']['Prediction'][0]

You can get the prediction result by giving the image file as an argument to predict.

predict_label = predict('Image file')
print(predict_label)

Up to this point, you can create an environment that works at a minimum.

Recommended Posts

Create a REST API using the model learned in Lobe and TensorFlow Serving.
Let's create a REST API using SpringBoot + MongoDB
How to create a Rest Api in Django
Create a pseudo REST API server using GitHub Pages
How to use the model learned in Lobe in Python
Save the pystan model and results in a pickle file
Create a real-time auto-reply bot using the Twitter Streaming API
I tried hosting a TensorFlow deep learning model using TensorFlow Serving
I made a VGG16 model using TensorFlow (on the way)
Parse the Researchmap API in Python and automatically create a Word file for the achievement list
Create a CRUD API using FastAPI
Create an API that returns data from a model using turicreate
Create a TalkBot easily using Discord.py and A3RT's Talk API (pya3rt).
[Python] I wrote a REST API using AWS API Gateway and Lambda.
Create a REST API to operate dynamodb with the Django REST Framework
Create a model to store information from the Google Books API for intuitive handling and testing
Try using the Wunderlist API in Python
Try using the Kraken API in Python
Tweet using the Twitter API in Python
Create a graph using the Sympy module
Development and deployment of REST API in Python using Falcon Web Framework
Create an application using the Spotify API
Create a record with attachments in KINTONE using the Python requests module
Create a simple app that incorporates the Fetch API of Ajax requests in Flask and explain it quickly
Create a Python image in Django without a dummy image file and test the image upload
Create an easy-to-read pdf of laws and government ordinances using the law api
Create a Django project and application in a Python virtual environment and start the server
Visualization of the firing state of the hidden layer of the model learned in the TensorFlow MNIST tutorial
Create a filter to get an Access Token in the Graph API (Flask)
Create a clean DB for testing with FastAPI and unittest the API with pytest
Try using the BitFlyer Ligntning API in Python
Create a web map using Python and GDAL
Create a simple momentum investment model in Python
Create a Mac app using py2app and Python3! !!
Create a MIDI file in Python using pretty_midi
Try using ChatWork API and Qiita API in Python
Try using the DropBox Core API in Python
Create a GUI on the terminal using curses
I made a demo that lets the model learned in the Tensorflow mnist tutorial distinguish the handwritten numbers written on the canvas.
Automate background removal for the latest portraits in a directory with Python and API
I tried to notify the update of "Become a novelist" using "IFTTT" and "Become a novelist API"
Tutorial to infer the model learned in Tensorflow with C ++/OpenVINO at high speed
Create a data collection bot in Python using Selenium
Investigate the relationship between TensorFlow and Keras in transition
Create a color sensor using a Raspberry Pi and a camera
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 1 ~
[LINE Messaging API] Create a rich menu in Python
Building a seq2seq model using keras's Functional API Overview
Create a Todo app with the Django REST framework
Creating a graph using the plotly button and slider
Get LEAD data using Marketo's REST API in Python
Send and receive Gmail via the Gmail API using Python
Select the required variables in TensorFlow and save / restore
Create a tweet heatmap with the Google Maps API
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 2 ~
Create a 3D model viewer with PyQt5 and PyQtGraph
Building a seq2seq model using keras' Functional API Inference
Using the National Diet Library Search API in Python
Make a Sato Yohei discriminator using OpenCV and TensorFlow
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 3 ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 4 ~