作为一个http api服务器,tf模型简单易用。

tfserve的Python项目详细描述


发球

DownloadsPyPI version

tfserve是一个框架,设计用于以简单易用的方式作为http api服务器为tensorflow模型提供服务。它建在Werkzeug之上。

如何安装

$ pip install tfserve

安装tfserve后,安装tensorflowtensorflow-gpu(如果有可用的gpu,则安装后者)。

$ pip install tensorflow

$ pip install tensorflow-gpu

如何使用

python api

您需要5个部分:

  1. model:它可以是.pb文件或包含ckpt文件的模型目录。
  2. 输入张量名称:图的输入张量的名称。
  3. 输出张量名称:图的输出张量的名称。
  4. encode:python函数,接收请求正文数据并输出一个dict映射输入张量名称以输入numpy值。
  5. 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

如何使用服务器

img

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 the encode and decode 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!

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
Java程序运行时错误   JavaAndroidStudio:与往常一样,四舍五入到next.5或.0   apache使用Java以表单数据形式上载文件   带矢量的java Freeflight相机如何正确旋转?   java如何以编程方式检索有关当前项目的语言、操作系统、体系结构等信息   java Twitter4J tweet实体?   java PdfBox编码异常   java在拖动未装饰的舞台时,如何强制光标停留在窗口上   JavaSpring注释扫描优化   java无法通过IntelliJ Idea在tomcat上运行服务   java在生命周期中如何拦截请求?   java中的数组返回错误