由於重点在如何启动一个TensorFlow Severing 服务,资料采用 keras.datasets.cifar10
进行示范,CIFAR10 为小型的影像分类资料集,具有 50,000 笔训练资料集与 10,000 笔测试资料集,皆为 32X32 像素的彩色图片。更多资讯参阅官方介绍, 10 种分类与描述如下:
建立简易模型,程序码参阅 Colab 。
将模型保存为SavedModel格式,以便将模型加载到 TensorFlow Serving 中。
TensorFlow Serving允许我们在发出推理请求时选择要使用的模型版本或「可服务」版本。每个版本将导出到给定路径下的不同子目录。为此,需在目录创建 protobuf
文件,并将包含一个版本号,此版本号码也是後续 API 所使用的参数之一。
以下会在/tmp/
建立版次版次version = 1
之相关档案。
# Fetch the Keras session and save the model
# The signature definition is defined by the input and output tensors,
# and stored with the default serving key
import tempfile
MODEL_DIR = tempfile.gettempdir()
version = 1
export_path = os.path.join(MODEL_DIR, str(version))
print(f'export_path = {export_path}')
tf.keras.models.save_model(
model,
export_path,
overwrite=True,
include_optimizer=True,
save_format=None,
signatures=None,
options=None
)
print('Saved model:')
!ls -l {export_path}
saved_model_cli
可以检查前述SavedModel中相关资讯,这对理解模型相当有用,包含:
!saved_model_cli show --dir {export_path} --all
依官方范例此为Colab环境所需设定内容,如使用本机端的 Notebook ,请注意相关提醒。
使用 Aptitude 安装 TensorFlow Serving,因为 Colab 在 Debian 环境中运行,所以可以安装 Debian 相关套件。将 tensorflow-model-server
以 Root 身分添加到 Aptitude 知道的套件列表中。
另外最简单的方式是以 Docker 布署 TensorFlow Serving,您可自行参考Docker 范例。
!echo "deb http://storage.googleapis.com/tensorflow-serving-apt stable tensorflow-model-server tensorflow-model-server-universal" | {SUDO_IF_NEEDED} tee /etc/apt/sources.list.d/tensorflow-serving.list && \
curl https://storage.googleapis.com/tensorflow-serving-apt/tensorflow-serving.release.pub.gpg | {SUDO_IF_NEEDED} apt-key add -
!{SUDO_IF_NEEDED} apt update
!{SUDO_IF_NEEDED} apt-get install tensorflow-model-server
加载後,我们可以开始使用 REST 发出推理请求,相关参数:
rest_api_port
: REST 请求的 Port。model_name
:您将在 REST 请求的 URL 中使用它。model_base_path
:保存模型的目录的路径。%%bash --bg
nohup tensorflow_model_server \
--rest_api_port=8501 \
--model_name=fashion_model \
--model_base_path="${MODEL_DIR}" >server.log 2>&1
预设请求最新版本的 servable 。
import requests
headers = {"content-type": "application/json"}
json_response = requests.post(
'http://localhost:8501/v1/models/fashion_model:predict',
data=data,
headers=headers
)
predictions = json.loads(json_response.text)['predictions']
show(0, 'The model thought this was a {} (class {}), and it was actually a {} (class {})'.format(
class_names[np.argmax(predictions[0])], np.argmax(predictions[0]), class_names[int(test_labels[0])], test_labels[0]))
向服务器请求指定版本version = 1
。
# docs_infra: no_execute
version = 1
headers = {"content-type": "application/json"}
json_response = requests.post(
f'http://localhost:8501/v1/models/fashion_model/versions/{version}:predict',
data=data,
headers=headers
)
predictions = json.loads(json_response.text)['predictions']
for i in range(0,3):
show(i, 'The model thought this was a {} (class {}), and it was actually a {} (class {})'.format(
class_names[np.argmax(predictions[i])], np.argmax(predictions[i]), class_names[int(test_labels[i])], test_labels[i]))
>>: [Day - 12] - Spring 注入式效能提升运作与方法
暑假後期,指导教授给了第三届 Line Chatbot 设计大赛的资讯。 虽然比赛内容主要放在设计、...
fetch() 是 ES6 的新语法,主要是搭配 Promise来执行请求网站和请求後获取 Resp...
ChromeCacheView 又来到发废文时间 今天来认识 ChromeCacheView看名字就...
过去我面试了不少公司的软件工程师职位,涵盖前端、後端,所以今天以Java相关职缺中常考的面试考题作为...
汇入 require: 可汇入多次, 如果出现错误, 会出现提醒, 并停止代码运行 include:...