How to use the model learned in Lobe in Python

Lobe is a tool that makes it easy to build machine learning models published by Microsoft. The article below explains how to use it. I tried "Lobe," which makes it easy to train machine learning models published by Microsoft.

In this article, I would like to write the procedure from exporting the model learned in Lobe to using it from Python.

Export the learned model

Select File-> Export. スクリーンショット 2020-10-31 20.30.04.png

Since TensorFlow will be used this time, select it and specify the save destination. スクリーンショット 2020-10-31 20.35.42.png

If you select Optimize & Optimizing, you can optimize the model and save it. スクリーンショット 2020-10-31 20.39.02.png

You can export the model learned by the procedure so far.

Use the trained model in Python

There is a folder called example in the save destination of the exported model. It contains sample code (tf_example.py) for use from TensorFlow.

When using the command line:

# python example/tf_example.py 'Image path'

Below, only the minimum required parts from the sample code are described.

Import required libraries

predict.py


import json

import numpy as np
import tensorflow as tf
from PIL import Image

Read input / output information from Signature

predict.py


with open("Path where you saved the model/signature.json", "r") as f:
    signature = json.load(f)

inputs = signature.get('inputs')
outputs = signature.get('outputs')

load model

predict.py


#For TensorFlow1 series
session = tf.compat.v1.Session(graph=tf.Graph())
tf.compat.v1.saved_model.loader.load(sess=session, tags=signature.get("tags"), export_dir='Path where you saved the model')

#For TensorFlow 2 series
model = tf.saved_model.load('Path where you saved the model')
infer = model.signatures["serving_default"]

Prepare an image to predict

predict.py


#Get the size of input
input_width, input_height, input_channel = inputs["Image"]["shape"][1:]

#For TensorFlow1 series
image = Image.open('Image path')
image = image.resize((input_width, input_height))
image = np.asarray(image) / 255.0
feed_dict = {inputs["Image"]["name"]: [image]}
fetches = [(key, output["name"]) for key, output in outputs.items()]

#For TensorFlow 2 series
image = Image.open('Image path')
image = image.resize((input_width, input_height))
image = np.array(image, dtype=np.float32) / 255.0
image = image.reshape([1, input_width, input_height, input_channel])

Make predictions

predict.py


#For TensorFlow1 series
output = session.run(fetches=[name for _, name in fetches], feed_dict=feed_dict)
print(output[0][0].decode())

#For TensorFlow 2 series
predict = infer(tf.constant(image))['Prediction'][0]
print(predict.numpy().decode())

As mentioned above, the model learned in Lobe can be used from Python very easily.

I uploaded the sample code to git. lobe_py

I made a REST API in combination with TensorFlow Serving. Create a REST API using the model learned in Lobe and TensorFlow Serving.

Lobe is a Beta version as of October 31st. The model that can be created is only image classification, but it seems that object detection and data classification will be added in the future.

Recommended Posts

How to use the model learned in Lobe in Python
How to use SQLite in Python
How to use Mysql in python
How to use ChemSpider in Python
How to use the __call__ method in a Python class
[Introduction to Python] How to use class in Python?
How to use regular expressions in Python
How to use is and == in Python
How to use the asterisk (*) in Python. Maybe this is all? ..
[Introduction to Python] How to use the in operator in a for statement?
How to use Python Image Library in python3 series
Summary of how to use MNIST in Python
[Algorithm x Python] How to use the list
How to use tkinter with python in pyenv
python3: How to use bottle (2)
How to use the generator
[Python] How to use list 1
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
How to develop in Python
[Python] How to use input ()
How to use the decorator
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
How to retrieve the nth largest value in Python
[For beginners] How to use say command in python!
How to get the variable name itself in python
How to get the number of digits in Python
How to know the current directory in Python in Blender
How to use the Raspberry Pi relay module Python
[Introduction to Udemy Python3 + Application] 27. How to use the dictionary
I tried to summarize how to use pandas in python
[Introduction to Udemy Python3 + Application] 30. How to use the set
[Python] How to output the list values in order
How to use Spacy Japanese model in Google Colaboratory
I want to use the R dataset in python
[Python] How to do PCA in Python
Python: How to use async with
How to use the zip function
How to use the optparse module
How to use classes in Theano
[Python] How to use Pandas Series
How to collect images in Python
How to use Requests (Python Library)
In the python command python points to python3.8
How to get the Python version
[Python] How to import the library
[Python] How to use list 3 Added
How to use OpenPose's Python API
How to use FTP with Python
Python: How to use pydub (playback)
How to use python zip function
How to use the ConfigParser module
How to handle Japanese in Python
[Python] How to use Typetalk API
[python] How to check if the Key exists in the dictionary
[python] How to use the library Matplotlib for drawing graphs