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.
Select File-> Export.
Since TensorFlow will be used this time, select it and specify the save destination.
If you select Optimize & Optimizing, you can optimize the model and save it.
You can export the model learned by the procedure so far.
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.
predict.py
import json
import numpy as np
import tensorflow as tf
from PIL import Image
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')
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"]
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])
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