编译 TFLite 模型(编译器的分析模型和综合模型)
看看资讯 / 100+人浏览
注意:免费节点订阅链接已更新至 2026-11-23,点击查看详情
本篇文章译自英文文档 Compile TFLite Models tvm 0.13.dev0 documentation
作者是 FrozenGene (Zhao Wu) · GitHub
更多 TVM 中文文档可访问 →Apache TVM 是一个端到端的深度学习编译框架,适用于 CPU、GPU 和各种机器学习加速芯片。 | Apache TVM 中文站
本文介绍如何用 Relay 部署 TFLite 模型。
首先安装 TFLite 包。
# 安装 tflite pip install tflite==2.1.0 --user 或者自行生成 TFLite 包,步骤如下:
# 获取 flatc 编译器。 # 详细可参考 https://github.com/google/flatbuffers,确保正确安装 flatc --version # 获取 TFLite 架构 wget https://raw.githubusercontent.com/tensorflow/tensorflow/r1.13/tensorflow/lite/schema/schema.fbs # 生成 TFLite 包 flatc --python schema.fbs # 将当前文件夹路径(包含生成的 TFLite 模块)添加到 PYTHONPATH。 export PYTHONPATH=${PYTHONPATH:+$PYTHONPATH:}$(pwd) 用 python -c "import tflite" 命令,检查 TFLite 包是否安装成功。
有关如何用 TVM 编译 TFLite 模型的示例如下:
用于下载和提取 zip 文件的程序
import os def extract(path): import tarfile if path.endswith("tgz") or path.endswith("gz"): dir_path = os.path.dirname(path) tar = tarfile.open(path) tar.extractall(path=dir_path) tar.close() else: raise RuntimeError("Could not decompress the file: " + path) 加载预训练的 TFLite 模型?
加载 Google 提供的 mobilenet V1 TFLite 模型:
from tvm.contrib.download import download_testdata model_url = "http://download.tensorflow.org/models/mobilenet_v1_2018_08_02/mobilenet_v1_1.0_224.tgz" # 下载模型 tar 文件,解压得到 mobilenet_v1_1.0_224.tflite model_path = download_testdata(model_url, "mobilenet_v1_1.0_224.tgz", module=["tf", "official"]) model_dir = os.path.dirname(model_path) extract(model_path) # 打开 mobilenet_v1_1.0_224.tflite tflite_model_file = os.path.join(model_dir, "mobilenet_v1_1.0_224.tflite") tflite_model_buf = open(tflite_model_file, "rb").read() # 从缓冲区获取 TFLite 模型 try: import tflite tflite_model = tflite.Model.GetRootAsModel(tflite_model_buf, 0) except AttributeError: import tflite.Model tflite_model = tflite.Model.Model.GetRootAsModel(tflite_model_buf, 0) 加载测试图像?
还是用猫的图像:
from PIL import Image from matplotlib import pyplot as plt import numpy as np image_url = "https://github.com/dmlc/mxnet.js/blob/main/data/cat.png?raw=true" image_path = download_testdata(image_url, "cat.png", module="data") resized_image = Image.open(image_path).resize((224, 224)) plt.imshow(resized_image) plt.show() image_data = np.asarray(resized_image).astype("float32") # 给图像添加一个维度,形成 NHWC 格式布局 image_data = np.expand_dims(image_data, axis=0) # 预处理图像: # https://github.com/tensorflow/models/blob/edb6ed22a801665946c63d650ab9a0b23d98e1b1/research/slim/preprocessing/inception_preprocessing.py#L243 image_data[:, :, :, 0] = 2.0 / 255.0 * image_data[:, :, :, 0] - 1 image_data[:, :, :, 1] = 2.0 / 255.0 * image_data[:, :, :, 1] - 1 image_data[:, :, :, 2] = 2.0 / 255.0 * image_data[:, :, :, 2] - 1 print("input", image_data.shape) 输出结果:
input (1, 224, 224, 3) 使用 Relay 编译模型?
# TFLite 输入张量名称、shape 和类型 input_tensor = "input" input_shape = (1, 224, 224, 3) input_dtype = "float32" # 解析 TFLite 模型,并将其转换为 Relay 模块 from tvm import relay, transform mod, params = relay.frontend.from_tflite( tflite_model, shape_dict={input_tensor: input_shape}, dtype_dict={input_tensor: input_dtype} ) # 针对 x86 CPU 构建模块 target = "llvm" with transform.PassContext(opt_level=3): lib = relay.build(mod, target, params=params) 输出结果:
/workspace/python/tvm/driver/build_module.py:268: UserWarning: target_host parameter is going to be deprecated. Please pass in tvm.target.Target(target, host=target_host) instead. "target_host parameter is going to be deprecated. " 在 TVM 上执行?
import tvm from tvm import te from tvm.contrib import graph_executor as runtime # 创建 runtime 执行器模块 module = runtime.GraphModule(lib["default"](tvm.cpu())) # 输入数据 module.set_input(input_tensor, tvm.nd.array(image_data)) # 运行 module.run() # 得到输出 tvm_output = module.get_output(0).numpy() 显示结果?
# 加载标签文件 label_file_url = "".join( [ "https://raw.githubusercontent.com/", "tensorflow/tensorflow/master/tensorflow/lite/java/demo/", "app/src/main/assets/", "labels_mobilenet_quant_v1_224.txt", ] ) label_file = "labels_mobilenet_quant_v1_224.txt" label_path = download_testdata(label_file_url, label_file, module="data") # 1001 个类的列表 with open(label_path) as f: labels = f.readlines() # 将结果转换为一维数据 predictions = np.squeeze(tvm_output) # 获得分数最高的第一个预测值 prediction = np.argmax(predictions) # 将 id 转换为类名,并显示结果 print("The image prediction result is: id " + str(prediction) + " name: " + labels[prediction]) 输出结果:
The image prediction result is: id 283 name: tiger cat 下载 Python 源代码:「链接」
下载 Jupyter Notebook:「链接」
版权声明:
作者: FreeClashNode
链接: https://www.freeclashnode.com/news/article-4075.htm
来源: www.freeclashnode.com
文章版权归作者所有,未经允许请勿转载。
免费节点实时更新
热门文章
- 11月2日免费节点分享|22.5M/S,V2ray节点/Clash节点/SSR节点/Singbox节点|免费上网梯子每天更新
- 10月25日免费节点分享|18.1M/S,SSR节点/V2ray节点/Singbox节点/Clash节点|免费上网梯子每天更新
- 10月31日免费节点分享|18.7M/S,Shadowrocket节点/Clash节点/V2ray节点/Singbox节点|免费上网梯子每天更新
- 10月27日免费节点分享|20M/S,Shadowrocket节点/Singbox节点/V2ray节点/Clash节点|免费上网梯子每天更新
- 10月26日免费节点分享|21.5M/S,V2ray节点/SSR节点/Singbox节点/Clash节点|免费上网梯子每天更新
- 10月28日免费节点分享|22.8M/S,Singbox节点/SSR节点/V2ray节点/Clash节点|免费上网梯子每天更新
- 10月30日免费节点分享|20.4M/S,V2ray节点/SSR节点/Singbox节点/Clash节点|免费上网梯子每天更新
- 11月1日免费节点分享|20.3M/S,V2ray节点/Shadowrocket节点/Clash节点/Singbox节点|免费上网梯子每天更新
- 10月29日免费节点分享|21.6M/S,SSR节点/Clash节点/V2ray节点/Singbox节点|免费上网梯子每天更新
- 11月7日免费节点分享|22.9M/S,Shadowrocket节点/V2ray节点/Singbox节点/Clash节点|免费上网梯子每天更新
最新文章
- 11月23日免费节点分享|21.4M/S,Shadowrocket节点/Clash节点/V2ray节点/Singbox节点|免费上网梯子每天更新
- 解锁全球视野:谷歌地图科学上网全攻略,让你畅行无阻
- 11月22日免费节点分享|22.8M/S,Clash节点/SSR节点/Singbox节点/V2ray节点|免费上网梯子每天更新
- 星卡V2rayNG免流终极指南:解锁零流量上网新姿势
- 11月21日免费节点分享|21.4M/S,V2ray节点/Singbox节点/Shadowrocket节点/Clash节点|免费上网梯子每天更新
- 深度解析Clash后台退出问题:操作系统、内存与配置的全面应对策略
- 11月20日免费节点分享|19.1M/S,V2ray节点/Clash节点/Shadowrocket节点/Singbox节点|免费上网梯子每天更新
- 御坂网络科学上网全攻略:解锁全球互联网的自由之门
- 11月19日免费节点分享|18.1M/S,Clash节点/V2ray节点/SSR节点/Singbox节点|免费上网梯子每天更新
- 掌上护盾的艺术:全面拆解clash手机壳的硬核实力与选购哲学
- 11月18日免费节点分享|21M/S,Clash节点/Singbox节点/V2ray节点/SSR节点|免费上网梯子每天更新
- 苹果设备解锁网络自由:科学上网全攻略与深度解析
- 11月17日免费节点分享|20.3M/S,Singbox节点/V2ray节点/Clash节点/Shadowrocket节点|免费上网梯子每天更新
- 突破网络瓶颈:全方位优化v2ray速度的终极指南
- 11月16日免费节点分享|18.2M/S,Singbox节点/Shadowrocket节点/V2ray节点/Clash节点|免费上网梯子每天更新
- 彻底解决vmess协议上传故障:从原理到实践的完整指南
- 11月15日免费节点分享|18M/S,Singbox节点/Clash节点/SSR节点/V2ray节点|免费上网梯子每天更新
- 付费V2Ray节点全指南:如何选择、配置与优化使用体验
- 11月14日免费节点分享|22.5M/S,SSR节点/Singbox节点/V2ray节点/Clash节点|免费上网梯子每天更新
- 从 Clash 到 V2Ray:订阅地址转换的全面指南
- 11月13日免费节点分享|19.3M/S,Singbox节点/Shadowrocket节点/Clash节点/V2ray节点|免费上网梯子每天更新
- V2RayNG连接故障全面解析:从原因排查到高效解决的终极指南
- 11月12日免费节点分享|22.4M/S,V2ray节点/Clash节点/Singbox节点/Shadowrocket节点|免费上网梯子每天更新
- 蓝灯破壁:一部跨越网络疆域的完整科学上网指南
- 11月11日免费节点分享|19.6M/S,Clash节点/V2ray节点/Singbox节点/Shadowrocket节点|免费上网梯子每天更新
- R8000科学上网终极指南:全面掌握Shadowsocks配置与优化技巧
- 11月10日免费节点分享|19M/S,Clash节点/SSR节点/V2ray节点/Singbox节点|免费上网梯子每天更新
- 解锁全球视野:星愿浏览器科学上网终极指南
- 11月9日免费节点分享|22.3M/S,Shadowrocket节点/Clash节点/V2ray节点/Singbox节点|免费上网梯子每天更新
- 轻松翻越网络高墙:Shadowrocket一键导入配置全攻略
- 11月8日免费节点分享|21.3M/S,Clash节点/V2ray节点/Singbox节点/SSR节点|免费上网梯子每天更新
- 当Vmess突然失效:一场技术与封锁的无声博弈
- 11月7日免费节点分享|22.9M/S,Shadowrocket节点/V2ray节点/Singbox节点/Clash节点|免费上网梯子每天更新
- 梅林固件科学上网完全指南:从入门到精通,零成本解锁网络自由
- 11月6日免费节点分享|20.9M/S,Shadowrocket节点/V2ray节点/Singbox节点/Clash节点|免费上网梯子每天更新
- 幸运破解器科学上网全解析:解锁数字世界的终极指南
- 11月5日免费节点分享|21.9M/S,Shadowrocket节点/Singbox节点/V2ray节点/Clash节点|免费上网梯子每天更新
- 11月4日免费节点分享|18M/S,Clash节点/Singbox节点/SSR节点/V2ray节点|免费上网梯子每天更新
- 穿越数字迷雾:深度解析V2Ray中转迟滞之谜与极速优化指南
- 跨越数字边界:科学上网的终极指南与深度思考
- 从连接到掌控:深入理解 Quantumult 的工作流程与网络管理哲学
- 11月2日免费节点分享|22.5M/S,V2ray节点/Clash节点/SSR节点/Singbox节点|免费上网梯子每天更新
- 深度解析《荒野冲突》:征服与策略的终极对决
- 11月1日免费节点分享|20.3M/S,V2ray节点/Shadowrocket节点/Clash节点/Singbox节点|免费上网梯子每天更新
- 斐讯路由器刷机配置v2ray全攻略:打造安全高效的翻墙网络环境
- 10月31日免费节点分享|18.7M/S,Shadowrocket节点/Clash节点/V2ray节点/Singbox节点|免费上网梯子每天更新
- 谷歌浏览器科学上网扩展全攻略:从入门到精通
- 10月30日免费节点分享|20.4M/S,V2ray节点/SSR节点/Singbox节点/Clash节点|免费上网梯子每天更新
- 苹果手机V2Ray完全指南:从零开始打造安全上网环境
- 10月29日免费节点分享|21.6M/S,SSR节点/Clash节点/V2ray节点/Singbox节点|免费上网梯子每天更新
归档
- 2026-11 44
- 2026-10 61
- 2026-09 58
- 2026-08 63
- 2026-07 61
- 2026-06 55
- 2026-05 56
- 2026-04 51
- 2026-03 60
- 2026-02 52
- 2026-01 56
- 2025-12 59
- 2025-11 55
- 2025-10 56
- 2025-09 55
- 2025-08 49
- 2025-07 31
- 2025-06 30
- 2025-05 31
- 2025-04 30
- 2025-03 388
- 2025-02 360
- 2025-01 403
- 2024-12 403
- 2024-11 390
- 2024-10 403
- 2024-09 388
- 2024-08 402
- 2024-07 427
- 2024-06 442
- 2024-05 181
- 2024-04 33
- 2024-03 31
- 2024-02 29
- 2024-01 51
- 2023-12 52
- 2023-11 32
- 2023-10 32
- 2023-09 3