生成与解析tensoflow2 tf_serving_warmup_requests
tensorflow模型在更新的一瞬间容易产生超时, tf-serving 中的warmup主要是通过模型启动时加载model/assets.extra/tf_serving_warmup_requests达到热启动的目的,使得模型更新时不易产生超时的问题
本文主要是讲解 tf_serving_warmup_requests的生成和反解析的过程,同时由于有些模型不需要训练,在已有模型的基础上,获取旧模型的warmup的特征,并手动给部分新特征进行预热,能够减少手动构造全部特征进行预热的繁琐工作
生成tensoflow2 tf_serving_warmup_requests 文件
def gen_warmup_file():
extra_path = 'tt/assets.extra/tf_serving_warmup_requests'
feature={'a':tf.constant([[20,20],[89,90]],dtype=tf.int32),
'b':tf.constant([[3.4,3,4],[7.01,8.26]],dtype=tf.float32)}
with tf.io.TFRecordWriter(extra_path) as wrriter:
request = predict_pb2.PredictRequest(
model_spec=model_pb2.ModelSpec(name='模型版本',signature_name='serving_default'),
inputs={k:tf.make_tensor_proto(v) for k,v in feature.items()}
)
log = prediction_log_pb2.PredictionLog(predict_log=prediction_log_pb2.PredictionLog(request=request))
wrriter.write(log.serializeToString())
反解析tensoflow2 tf_serving_warmup_requests 文件
import tensorflow as tf
from tensorflow_serving.apis import model_pb2
from tensorflow_serving.apis import predict_pb2
from tensorflow_serving.apis import prediction_log_pb2
def parse_warmup(warmup_file):
for serized_exmple in tf.compat.v1.python_io.tf_record_iterator(warmup_file):
log = prediction_log_pb2.PredictionLog()
log.ParseFromString(serized_exmple)
request = log.predict_log.request
# 遍历request
for name, tensor_ in request.inputs.items():
print(name, tensor_.tensor_content, tensor_.tensor_shape.dim)
# 添加新特征到warmup文件中进行预热
request.inputs['a'].CopyFrom(tf.make_tensor_proto(tf.constant([[2345], [4567]], dtype=tf.int32)))
# 保存request到新的路径下
extra_path = 'tt/assets.extra/tf_serving_warmup_requests'
with tf.io.TFRecordWriter(extra_path) as wrriter:
log = prediction_log_pb2.PredictionLog(predict_log=prediction_log_pb2.PredictionLog(request=request))
wrriter.write(log.serializeToString())
edited_by:Francis,SZ
date:2022/07/09