TensorFlow 的 Graph 模式转换
定义 TensorFlow 图形并将其保存到磁盘上。
使用 TensorFlow 的 tf.Graph()
和 tf.Session()
函数来定义和运行 TensorFlow 图形,并使用 tf.train.write_graph()
函数将其保存到磁盘上。
import tensorflow as tf # Define a TensorFlow graph
graph = tf.Graph()
with graph.as_default():
x = tf.placeholder(tf.float32, shape=[None, 200])
W = tf.Variable(tf.zeros([200, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x, W) + b) # Save the graph to disk
with tf.Session(graph=graph) as sess:
tf.train.write_graph(sess.graph_def, './', 'graph.pb', as_text=False)
使用 TensorFlow 的 tf.lite.TFLiteConverter
类加载图形,并设置转换器的选项。
使用 tf.lite.TFLiteConverter.from_frozen_graph()
函数加载保存的 TensorFlow 图形,并设置转换器的选项。
# Load the graph and create a converter
converter = tf.lite.TFLiteConverter.from_frozen_graph(
graph_def_file='./graph.pb',
output_arrays=['Softmax'],
output_dtype=tf.float32.as_datatype_enum,
inference_type=tf.lite.constants.QUANTIZED_UINT8,
mean=[0.],
std=[255.],
optimizations=[tf.lite.Optimize.DEFAULT]
)
可以调用转换器的 convert()
方法将 TensorFlow 图形转换为 TensorFlow Lite 模型。
# Convert the graph to a TensorFlow Lite model
tflite_model = converter.convert() # Save the TensorFlow Lite model to disk
with open('./model.tflite', 'wb') as f:
f.write(tflite_model)
加载模型执行推理
import tflite_runtime.interpreter as tflite # Load the TensorFlow Lite model and create an interpreter
interpreter = tflite.Interpreter(model_path='./model.tflite', num_threads=4)
interpreter.allocate_tensors() # Perform inference on a sample input
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
input_data = np.zeros(input_details[0]['shape'], dtype=np.float32)
interpreter
TensorFlow 的 Graph 模式转换的更多相关文章
- Linux中的模式转换
模式转换: 编辑-->输入: i: 在当前光标所在字符的前面,转为输入模式: a: 在当前光标所在字符的后面,转为输入模式: o: 在当前光标所在行的下方,新建一行,并转为输入模式: I:在当前 ...
- 如何使用tensorboard查看tensorflow graph****.pb文件的模型结构
参考网上的:https://github.com/tensorflow/tensorflow/issues/8854 import tensorflow as tf from tensorflow.p ...
- imx lcd HV和DE模式转换
有些时候拿到的lcd手册中关于芯片的时序使用的DE模式的,而imx6内核中使用的参数设置趋势HV模式,应此就需要将DE模式的参数转化为HV模式. 参考链接: https://community.nxp ...
- Linux大小端模式转换函数
转自 http://www.cnblogs.com/kungfupanda/archive/2013/04/24/3040785.html 不同机器内部对变量的字节存储顺序不同,有的采用大端模式(bi ...
- htonl(),htons(),ntohl(),ntons()--大小端模式转换函数
不同机器内部对变量的字节存储顺序不同,有的采用大端模式(big-endian),有的采用小端模式(little-endian). 大端模式是指高字节数据存放在低地址处,低字节数据放在高地址处. 小端模 ...
- 命令和python模式转换
安装完paython成功之后,就必须了解一下:命令模式和python交互模式 1.我们输入 cmd 之后进入的运行环境就是命令模式 2.在命令模式下输入 python,看到>>> ...
- adg的数据传输应用三大模式转换
1.最大可用性模式(Maximum Availability) 1)该模式提供了仅次于"最大保护模式"的数据保护能力: 2)要求至少一个物理备库收到重做日志后,主库的事务才能够提交 ...
- HTML连载30-CSS显示模式&模式转换
一.CSS显示模式 1.在HTML中HTML将所有的标签分为两类,分别是容器类和文本级.在CSS中CSS也将所有的标签分为两类,分别是块级元素和行内元素 2.什么是块级元素呢?什么是行内元素? ( ...
- TensorFlow的图切割模块——Graph Partitioner
背景 [作者:DeepLearningStack,阿里巴巴算法工程师,开源TensorFlow Contributor] 在经过TensorFlow的Placer策略模块调整之后,下一步就是根据Pla ...
- TensorFlow中的Session、Graph、operation、tensor
TensorFlow中的Session.Graph.operation.tensor
随机推荐
- 项目启动报错:关于modals以及node版本相关
programme1: 1.代码用master分支的. 2. 删除node_module , yarn lock 文件,package-lock文件. 3. 用 npm install 或者 yar ...
- SqlServer中 Partition By 的使用( 对多行数据分组后排序取每个产品的第一行数据)
1:数据库表结构: (产品图片表) select * from product_imgs 2:应用场景: 可见表中每一个产品(pro_id)可能对应多个图片(img),我想要按照添加时间的先后顺序,获 ...
- Python Telnetlib模块连接网络设备
# -*- coding: UTF-8 -*- import telnetlib import time import datetime import os import json Username= ...
- 新版 Mediasoup Windows 安装 编译
https://vc.feiyefeihua.top/ ps:视频测试demo,服务器配置很低,加载有点慢:需要有音视频设备,不然会报错. 关于官网文档 官网文档地址 只测试了 Windows .讲的 ...
- 完整的javaweb文档
1.index.jsp <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8&qu ...
- SSM框架 拦截器 出现“发现了以元素 'mvc:exclude-mapping' 开头的无效内容。应以 '{"http://www.springframework.org/schema/mvc":mapping}' 之一开头”错误
导致错误位置与代码: spring-mvc.xml文件中的拦截器配置代码,代码如下: <mvc:interceptors> <mvc:interceptor> <mvc: ...
- React整洁的代码的一些原则
1. Model is everything models are the heart of your app. If you have models separated from th rest ...
- JavaScript基础知识整理(引用类型-Object)
Object类型 其他的引用类型都是Object类型的实例,创建Object实例有两种方式 (1)使用构造函数 var obj = new Object(); obj.name = "xia ...
- Git系列 -> 如何获取远程库某个文件
有时候远程库存放很多文件夹,我们可能只需要下载或获取某个文件中的内容,可以采用以下: 首先点击clone 按钮,然后复制clone URL , 如果想要下载其中的某个文件夹的内容,需要在后面加入指定文 ...
- APP性能测试——首次启动耗时测试
首次启动耗时: 即第一次安装(清除数据也可以),打开软件,直到进入到首页activity页面,并计算耗时. 示例代码: import os import time # 测试首次启动时间 # 创建App ...