作为一个http api服务器,tf模型简单易用。
tfserve的Python项目详细描述
发球
tfserve是一个框架,设计用于以简单易用的方式作为http api服务器为tensorflow模型提供服务。它建在Werkzeug之上。
如何安装
$ pip install tfserve
安装tfserve
后,安装tensorflow
的tensorflow-gpu
(如果有可用的gpu,则安装后者)。
$ pip install tensorflow
或
$ pip install tensorflow-gpu
如何使用
python api
您需要5个部分:
- model:它可以是
.pb
文件或包含ckpt文件的模型目录。 - 输入张量名称:图的输入张量的名称。
- 输出张量名称:图的输出张量的名称。
encode
:python函数,接收请求正文数据并输出一个dict
映射输入张量名称以输入numpy值。decode
:python函数,接收一个dict
映射输出张量名称到输出numpy值并返回http响应。
按照示例学习如何组合这些部分…
示例
部署图像分类服务,该服务接收二进制jpg图像,并返回在图像中找到的对象的类及其概率。
# 1. Model: trained mobilenet on ImageNet that can be downloaded from# https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_1.4_224.tgzMODEL_PATH="mobilenet_v2_1.4_224/mobilenet_v2_1.4_224_frozen.pb"# 2. Input tensor names:INPUT_TENSORS=["import/input:0"]# 3. Output tensor names:OUTPUT_TENSORS=["import/MobilenetV2/Predictions/Softmax:0"]# 4. encode function: Receives raw jpg image as request data. Returns dict# mappint import/input:0 to numpy value.defencode(request_data):withtempfile.NamedTemporaryFile(mode="wb",suffix=".jpg")asf:f.write(request_data)# Model receives 224x224 normalized RGB image.img=Image.open(f.name).resize((224,224))img=np.asarray(img)/255.return{INPUT_TENSORS[0]:img}# 5. decode function: Receives `dict` mapping import/MobilenetV2/Predictions/Softmax:0 to# numpy value and builds dict with for json response.defdecode(outputs):p=outputs[OUTPUT_TENSORS[0]]# 1001 vector with probabilities for each class.index=np.argmax(p)return{"class":index_to_class_map[index-1],"prob":float(p[index])}
就这样!现在创建tfserveapp对象并运行它!
app=TFServeApp(MODEL_PATH,INPUT_TENSORS,OUTPUT_TENSORS,encode,decode)app.run('127.0.0.1',5000)# Host and port where the server will be running
有关完整示例,请参见client.py
。
如何使用服务器
The server supports only
POST
method to/
with the input information as part of the request body.
输入将在encode函数中处理,以生成将传递给图形的feed_dict对象。图形输出将在decode函数中处理,服务器将返回decode函数返回的任何内容。
cli
tfserve
还提供带有内置编码/解码处理程序的cli程序:
tfserve -m PATH [-i INPUTS][-o OUTPUTS][-h HANDLER][-b][-H HOST][-p PORT] -m PATH, --model PATH path to pb file or directory containing checkpoint -i INPUTS, --inputs INPUTS a comma separated list of input tensors -o OUTPUTS, --outputs OUTPUTS a comma separated list of output tensors -h HANDLER, --handler HANDLER encode/decode handler (deault is 'json') -b, --batch process multiple inputs (default is to process one input per request) -H HOST, --host HOST host interface to bind to (0.0.0.0) -p PORT, --port PORT port to listen on (5000)
示例
$ tfserve -m models/graph.pb -i x:0 -o out:0 -h json -H localhost
用models/graph.pb
模型运行tfserve
,该模型接受名为x:0
(dimension:[?,5]),并输出名为out:0
的张量。服务器将在http://localhost:5000/上运行,并接收POST
对/
的请求。
通过使用json处理程序,您可以在请求正文中以json
对象的形式提供输入数据:
{"x:0":[1,1,3,4,5]}
您将收到一个json
输出对象:
{"out:0":0.48}
有关cli的更多信息
运行:
$ tfserve --help
帮助
- 如果我不知道张量的名字怎么办?
You can use
tfserve.helper.estimate_io_tensors(model_path)
function to get a list of possible input/output tensor names. Also, you can use the CLI by running:tfserve -m [model_path] --help-model
- 如果我想同时运行多个推论怎么办?
You can use
batch=True
when building tfserve.TFServeApp. You will then need to handle the batch dimension yourself in theencode
anddecode
function. Also, if using the CLI, just add the--batch
flag.
限制
It only works with one-to-one models. That is, models that need to run the graph only once to get the inference. Other architectures of inference will be supported soon. Help is appreciated!