TensorFlow Lite 如何使用 signature API 指定模型输入输出张量?

发布时间:2026/10/1 1:39:39

TensorFlow Lite 如何使用 signature API 指定模型输入输出张量? TensorFlow Lite 如何使用 signature API 指定模型输入输出张量【免费下载链接】tensorflowAn Open Source Machine Learning Framework for Everyone项目地址: https://gitcode.com/GitHub_Trending/te/tensorflow当你转换出的 TensorFlow Lite 模型有多个入口例如一个模型同时承担编码和解码两种任务或者输入输出张量有明确命名时用整数索引去指定输入/输出张量既脆弱又难读。TensorFlow Lite 的 signature API 就是解决这个问题通过签名中定义的张量名称喂入输入、按名称取回输出并用签名键signature key选择要执行哪个入口。本文给出从准备带签名的模型、转换、验证签名存在到用 Python / Java / C 按名执行推理的完整路径。适用前提均来自官方文档模型本身必须带有签名。签名可以在构建 SavedModel 或创建 concrete function 时指定通过from_saved_model转换器支持签名自 TensorFlow 2.5 起自 TensorFlow 2.7.0 起所有转换器 API包括from_keras_model、from_concrete_functions转换出的模型都带有签名当前提供 Java、C 和 Python 三种语言绑定iOS/Swift 尚不支持。Signature 由什么组成根据 Signatures in TensorFlow Lite 的定义一个签名由三部分构成Inputs签名内输入名到输入张量的映射Outputs签名内输出名到输出张量的映射Signature Key标识图中一个入口点entry point的名称。由此带来两个能力转换出的 TensorFlow Lite 模型遵循原 TensorFlow 模型签名来指定输入输出单个 TensorFlow Lite 模型可以支持多个入口点。文档中的示例模型就是一个双入口模型class Model(tf.Module): tf.function(input_signature[tf.TensorSpec(shape[None], dtypetf.float32)]) def encode(self, x): result tf.strings.as_string(x) return { encoded_result: result } tf.function(input_signature[tf.TensorSpec(shape[None], dtypetf.string)]) def decode(self, x): result tf.strings.to_number(x) return { decoded_result: result }从签名角度看这个模型有两个签名签名键输入输出encode{x}{encoded_result}decode{x}{decoded_result}也就是说后面所有按名访问张量的代码里x、encoded_result等名称都来自这里而不是自己起的名字。转换出带签名的模型从 SavedModel 转换主路径先把签名显式写入 SavedModel再交给TFLiteConverter。tf.saved_model.save的signatures参数就是指定签名键与对应 concrete function 的地方model Model() # Save the model SAVED_MODEL_PATH content/saved_models/coding tf.saved_model.save( model, SAVED_MODEL_PATH, signatures{ encode: model.encode.get_concrete_function(), decode: model.decode.get_concrete_function() }) # Convert the saved model using TFLiteConverter converter tf.lite.TFLiteConverter.from_saved_model(SAVED_MODEL_PATH) converter.target_spec.supported_ops [ tf.lite.OpsSet.TFLITE_BUILTINS, # enable TensorFlow Lite ops. tf.lite.OpsSet.SELECT_TF_OPS # enable TensorFlow ops. ] tflite_model converter.convert() # Print the signatures from the converted model interpreter tf.lite.Interpreter(model_contenttflite_model) signatures interpreter.get_signature_list() print(signatures)示例中模型包含tf.strings这类 TensorFlow 算子因此文档设置了converter.target_spec.supported_ops同时启用TFLITE_BUILTINS与SELECT_TF_OPS。你的模型若只含纯 TFLite 内置算子可以按自身情况调整。最后一步interpreter.get_signature_list()就是文档给出的验证方式在转换器把签名信息带入.tflite模型后用它打印出模型中实际存在的签名列表确认转换结果与预期一致。替代入口Keras 模型与 concrete functions如果输入不是 SavedModel文档提供了两个等价路径Keras 模型走from_keras_model转换器会自动使用默认签名keras_model tf.keras.Sequential( [ tf.keras.layers.Dense(2, input_dim4, activationrelu, namex), tf.keras.layers.Dense(1, activationrelu, nameoutput), ] ) converter tf.lite.TFLiteConverter.from_keras_model(keras_model) tflite_model converter.convert() interpreter tf.lite.Interpreter(model_contenttflite_model) signatures interpreter.get_signature_list() print(signatures)多个 concrete function 走from_concrete_functions把encode和decode两个入口一次性传入model Model() converter tf.lite.TFLiteConverter.from_concrete_functions( [model.encode.get_concrete_function(), model.decode.get_concrete_function()], model) converter.target_spec.supported_ops [ tf.lite.OpsSet.TFLITE_BUILTINS, # enable TensorFlow Lite ops. tf.lite.OpsSet.SELECT_TF_OPS # enable TensorFlow ops. ] tflite_model converter.convert() interpreter tf.lite.Interpreter(model_contenttflite_model) signatures interpreter.get_signature_list() print(signatures)用 signature API 按名指定输入输出张量转换完成后tf.lite.Interpreter提供两个与签名相关的方法get_signature_list()列出模型内的签名get_signature_runner(key)拿到某个入口的执行器。拿到 runner 后按参数名传输入、按字典名取输出全程不接触张量索引# Load the TFLite model in TFLite Interpreter interpreter tf.lite.Interpreter(model_contenttflite_model) # Print the signatures from the converted model signatures interpreter.get_signature_list() print(Signature:, signatures) # encode and decode are callable with input as arguments. encode interpreter.get_signature_runner(encode) decode interpreter.get_signature_runner(decode) # encoded and decoded are dictionaries with all outputs from the inference. input tf.constant([1, 2, 3], dtypetf.float32) print(Input:, input) encoded encode(xinput) print(Encoded result:, encoded) decoded decode(xencoded[encoded_result]) print(Decoded result:, decoded)注意两处按名访问的细节调用encode(xinput)时参数名x必须与签名中的输入名一致返回值是包含全部输出的字典所以decode的输入直接写作encoded[encoded_result]——这正是签名把上一个入口的输出名和下一个入口的输入名接起来的用法。如果模型里只定义了一个签名可以省略签名键get_signature_runner()会默认返回它有多个签名时必须传名称。文档在 inference.md 中给出了自定义签名名的示例保存时用signatures{my_signature: module.add.get_concrete_function()}运行端再按get_signature_runner(my_signature)取用。JavaAndroidJava 端通过Interpreter.runSignature(inputs, outputs, signatureName)执行三个参数分别是输入名到输入对象的 Map、输出名到输出数据的 Map、签名键模型只有一个签名时可省略。同一个Interpreter实例上分别跑两个入口try (Interpreter interpreter new Interpreter(file_of_tensorflowlite_model)) { // Run encoding signature. MapString, Object inputs new HashMap(); inputs.put(x, input); MapString, Object outputs new HashMap(); outputs.put(encoded_result, encoded_result); interpreter.runSignature(inputs, outputs, encode); // Run decoding signature. MapString, Object inputs new HashMap(); inputs.put(x, encoded_result); MapString, Object outputs new HashMap(); outputs.put(decoded_result, decoded_result); interpreter.runSignature(inputs, outputs, decode); }runSignature适用于自 TensorFlow 2.5 起转换的模型。inputs/outputsMap 中的键x、encoded_result等必须与签名中定义的输入输出名一致。CAndroid、iOS、LinuxC 端通过GetSignatureRunner(key)拿到每个入口独立的SignatureRunner之后ResizeInputTensor、input_tensor、output_tensor全部接受张量名称而非索引SignatureRunner* encode_runner interpreter-GetSignatureRunner(encode); encode_runner-ResizeInputTensor(x, {100}); encode_runner-AllocateTensors(); TfLiteTensor* input_tensor encode_runner-input_tensor(x); float* input GetTensorDatafloat(input_tensor); // Fill input. encode_runner-Invoke(); const TfLiteTensor* output_tensor encode_runner-output_tensor( encoded_result); float* output GetTensorDatafloat(output_tensor); // Access output.模型没有签名时怎么办signature API 的前提是模型带有签名。若你的模型没有定义 SignatureDefinference.md 给出的是索引式 API 这一条替代路径Python 中用allocate_tensors()get_input_details()/get_output_details()set_tensor(index, ...)invoke()get_tensor(index)Java 中用Interpreter.run()单输入单输出或runForMultipleInputsOutputs(inputs, map_of_indices_to_outputs)张量索引必须与转换时给 Converter 的顺序一致且getInputIndex(opName)/getOutputIndex(opName)在名称不是模型中的合法操作时会抛出IllegalArgumentException。这条路径不能指定多个入口这正是多入口模型应使用 signature API 的原因。已知限制与版本边界线程安全由于 TFLite 解释器不保证线程安全同一解释器上的多个 signature runner 不会并发执行iOS/Swift签名支持尚不可用版本2.5 起from_saved_model支持签名2.7 起实现多签名功能且所有转换器 API 都生成带签名的 TensorFlow Lite 模型。如果你的模型只有一个固定入口索引式 API 已经够用只有在多入口、或需要按名称对接上游输出与下游输入时才值得按上面的路径改用 signature API。完整代码可直接对照仓库中的 signatures.ipynb 与 inference.md。【免费下载链接】tensorflowAn Open Source Machine Learning Framework for Everyone项目地址: https://gitcode.com/GitHub_Trending/te/tensorflow创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
延伸阅读

更多相关文章

2026/9/28 5:06:37

Auto-PU论文复现:自动正例无标签学习的PyTorch实现

复现一篇没有官方源码的论文,九成时间不是耗在模型结构上,而是耗在一堆论文里根本不会写的细节上。这次我选的题目是Applied Intelligence 2025上关于Auto-PU的工作,核心是给正例无标签学习(PU learning,Positive-Unla…

2026/9/26 11:02:43

SEO关键词排名优化最佳实践:从策略到落地全解析

最近刚帮一个老客户把他们网站的SEO关键词排名重新拉回首页,顺手整理了一批文档。正好看到这个项目标题——"什么是SEO关键词排名优化的最佳实践",我觉得可以好好聊一聊这个问题。SEO这行做了十多年,看过太多人把关键词排名优化理解…

2026/10/1 1:36:20

文件系统跨平台适配原理:从VFS到格式化选型全解析

几次出差都栽在同一类事情上:客户给的U盘插到Windows笔记本上正常,拷到我的Linux工作站上就开始提示“文件系统不支持”,换个macOS环境又变成只读。后来自己折腾PE启动盘、数据恢复、给服务器换磁盘,才慢慢把文件系统这块的底账翻…

2026/10/1 1:36:20

S32DS与J-Link调试故障排查实战指南

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/1 1:36:20

ZBar Win64 集成实战:从 DLL 配置到条码识别率优化

简介:面向64位Windows平台的ZBar条形码与二维码识别库,包含源代码、编译配置、动态链接库与示例程序。适合需要将扫码能力集成到C、C、Python、Java等项目的开发者使用,可应用于商品管理、物流追踪、移动支付等场景。资源包内共有409个文件&a…

2026/10/1 1:36:20

RAG知识库问答系统从原理到实战的完整指南

简介:面向需要为企业搭建智能问答系统的开发者,这份资源包提供了一套基于大语言模型与RAG检索增强生成的完整知识库问答方案,适用于知识管理、智能客服、内部文档检索等场景。核心优势在于开箱即用:支持直接上传文档、自动爬取在线…

2026/10/1 1:36:20

Attention与AdamW的同构性:动态权重分配的范式革命

1. 为什么说“Attention → KDA”和“SGD → AdamW”是同一场范式革命的两翼?很多人一看到这个标题,第一反应是:“注意力机制和优化器?八竿子打不着啊。”——这恰恰是问题所在。过去五年里,我带过二十多个模型训练项目…

2026/10/1 1:31:20

Modbus Poll工控调试实战:从协议解析到故障排查

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/9/29 11:07:23

东莞市品牌网站建设报价常见报错与解决

东莞品牌网站建设报价单背后:一份保姆级建站教程避坑实录 网站做好了没人访问,这大概是很多老板最头疼的事。花了大几万做的品牌站,上线后流量惨淡,比路边摊还冷清。别急着骂外包公司,很多“东莞品牌网站建设报价”里藏着不少猫腻,比如用模板站冒充定制…

2026/9/29 21:48:03

如何划分训练/验证集:Spirula Studio五种eval_mode策略详解

如何划分训练/验证集:Spirula Studio五种eval_mode策略详解 【免费下载链接】spirula-studio Cross-vendor 3D Gaussian Splatting trainer - video to splat to mesh, Vulkan or CUDA. 项目地址: https://gitcode.com/GitHub_Trending/sp/spirula-studio Sp…

2026/9/29 7:00:49

SEO怎么推广速查手册新手避坑实战指南

SEO怎么推广速查手册新手避坑实战指南 模板网站太丑不够用?别急着加滤镜,那是治标不治本。很多老板盯着后台流量掉得眼红,却还在纠结首页Banner的圆角是不是3像素。这就像穿着西装去挖土,姿势不对,努力白费。我整理这份 速查手册…

还想了解更多?直接咨询顾问

免费诊断 + 免费方案 + 透明报价。

全国咨询热线400-8866-253
免费获取方案
☎咨询二维码 ☎ ↑