栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

生成与解析tensoflow2 tf

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

生成与解析tensoflow2 tf

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1008615.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号