tinyms.serving

tinyms.serving.list_servables()[源代码]

List the model that is currently served by the backend server.

A GET request will be sent to the server(127.0.0.1:5000) which will then be routed to 127.0.0.1:5000/servables, and the backend servalbe information will be returned to the client.

返回

res_body[‘servables’] (str) will be returned, the backend servable information. Error message will be returned and printed if requests.status_code is not ok. ‘Server not started’ will be returned if server is not started

实际案例

>>> # Running the quickstart tutorial, after server started and servable json defined
>>> list_servables()
[{'description': 'This servable hosts a lenet5 model predicting numbers', 'model': {'class_num': 10, 'format': 'ckpt', 'name': 'lenet5'}, 'name': 'lenet5'}]
tinyms.serving.predict(img_path, servable_name, dataset_name='mnist', strategy='TOP1_CLASS')[源代码]

Send the predict request to the backend server, get the return value and do the post process

Predict the input image, and get the result. User must specify the image_path, servable_name, dataset_name and output_strategy to get the predict result.

参数
  • img_path (str) – path to the image

  • servable_name (str) – the name in servable_json, now supports 6 servables: lenet5, resnet50_imagenet2012, resnet50_cifar10, mobilenetv2, ssd300 and cyclegan_cityscape.

  • dataset_name (str) – the name of the dataset that is used to train the model, now supports 5 datasets: mnist, imagenet2012, cifar10, voc, cityscape

  • strategy (str) – the output strategy, for lenet5, resnet50 and mobilenetv2, select between ‘TOP1_CLASS’ and ‘TOP5_CLASS’, for ssd300, only TOP1_CLASS, for cyclegan_cityscape, select between gray2color and color2gray

返回

For lenet5, resnet50, mobilenetv2, the output is a string of predict result. For ssd300, the output is a string of bounding boxes coordinates and labels, which can be further processed using ImageViewer function For cyclegan, the output is a numpy of image, which can be transformed to image using Image.fromarray

实际案例

>>> # Running the quickstart tutorial, after server started and servable json defined
>>> print(predict('/root/7.png', 'lenet5', 'mnist', 'TOP1_CLASS'))
TOP1: 7, score: 0.99943381547927856445
tinyms.serving.server_started(host='127.0.0.1', port=5000)[源代码]

Detect whether the serving server is started or not.

A bool value of True will be returned if the server is started, else False.

参数
  • host (str) – the ip address of the server, default is 127.0.0.1

  • port (int) – the port address of the server, default is 5000

返回

A bool value of True(if server started) or False(if server not started).

实际案例

>>> # Running the quickstart tutorial, after starting the server
>>> if server_started() is True:
>>>     print(predict(image_path, 'lenet5', 'mnist', strategy))
tinyms.serving.start_server(host='127.0.0.1', port=5000)[源代码]

Start the flask server in a subprocess.

Catch the signal of CTRL + D to shutdown, otherwise call shutdown() function to shutdown the server, if the ip and port already in use, server won’t start for a second time.

参数
  • host (str) – the ip address of the flask server

  • port (int) – the port of the server

返回

Start the server in a sub process.

实际案例

>>> # In the client part
>>> start_server()
Server starts at host 127.0.0.1, port 5000
tinyms.serving.shutdown()[源代码]

Shutdown the flask server.

Search fot the pid of the process running on port 5000, and kill it. This function will be automatically called when SIGINT, SIGHUP and SIGTERM signals catched.

返回

A string message of server shutting down or not.

实际案例

>>> # In the client part, after predict()
>>> shutdown()
'Server shutting down...'
tinyms.serving.run_flask(host='127.0.0.1', port=5000)[源代码]

Start the flask server, only be used to trigger starting the flask server in subprocess.

Directly calling this function is not recommended, please use start_server(). Only Error message will be displayed.

参数
  • host (str) – the ip address of the flask server

  • port (int) – the port of the server

返回

Server Started

实际案例

>>> # In the start_server function
>>> cmd = ['python -c "from tinyms.serving import run_flask; run_flask()"']
>>> server_process = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)