定义 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 模式转换的更多相关文章

  1. Linux中的模式转换

    模式转换: 编辑-->输入: i: 在当前光标所在字符的前面,转为输入模式: a: 在当前光标所在字符的后面,转为输入模式: o: 在当前光标所在行的下方,新建一行,并转为输入模式: I:在当前 ...

  2. 如何使用tensorboard查看tensorflow  graph****.pb文件的模型结构

    参考网上的:https://github.com/tensorflow/tensorflow/issues/8854 import tensorflow as tf from tensorflow.p ...

  3. imx lcd HV和DE模式转换

    有些时候拿到的lcd手册中关于芯片的时序使用的DE模式的,而imx6内核中使用的参数设置趋势HV模式,应此就需要将DE模式的参数转化为HV模式. 参考链接: https://community.nxp ...

  4. Linux大小端模式转换函数

    转自 http://www.cnblogs.com/kungfupanda/archive/2013/04/24/3040785.html 不同机器内部对变量的字节存储顺序不同,有的采用大端模式(bi ...

  5. htonl(),htons(),ntohl(),ntons()--大小端模式转换函数

    不同机器内部对变量的字节存储顺序不同,有的采用大端模式(big-endian),有的采用小端模式(little-endian). 大端模式是指高字节数据存放在低地址处,低字节数据放在高地址处. 小端模 ...

  6. 命令和python模式转换

    安装完paython成功之后,就必须了解一下:命令模式和python交互模式 1.我们输入  cmd  之后进入的运行环境就是命令模式 2.在命令模式下输入  python,看到>>> ...

  7. adg的数据传输应用三大模式转换

    1.最大可用性模式(Maximum Availability) 1)该模式提供了仅次于"最大保护模式"的数据保护能力: 2)要求至少一个物理备库收到重做日志后,主库的事务才能够提交 ...

  8. HTML连载30-CSS显示模式&模式转换

    一.CSS显示模式​ 1.在HTML中HTML将所有的标签分为两类,分别是容器类和文本级.在CSS中CSS也将所有的标签分为两类,分别是块级元素和行内元素 2.什么是块级元素呢?什么是行内元素​? ( ...

  9. TensorFlow的图切割模块——Graph Partitioner

    背景 [作者:DeepLearningStack,阿里巴巴算法工程师,开源TensorFlow Contributor] 在经过TensorFlow的Placer策略模块调整之后,下一步就是根据Pla ...

  10. TensorFlow中的Session、Graph、operation、tensor

    TensorFlow中的Session.Graph.operation.tensor

随机推荐

  1. Android 内存优化浅析

    一:内存占用几大要点 1,Object Cache:Image cache,single instance obj(重量级别,例如数据库连接obj,bitmap ref),Thread过多, 2,Vi ...

  2. 删除File Explorer、This PC、Folders

    win+R,输入regedit,打开Registry Editor 定位:Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentV ...

  3. golang基本数据类型

    1.整形 默认整数类型为int 变量在选择时应遵循最小化原则 1.1 有符号(int) 类型 取值范围 占用空间(字节) int8 -2^7到 2^7-1 (-128到127) 1 int16 -2^ ...

  4. MYSQL实现排名函数RANK,DENSE_RANK和ROW_NUMBER

    1. 排名分类 1.1 区别RANK,DENSE_RANK和ROW_NUMBER RANK并列跳跃排名,并列即相同的值,相同的值保留重复名次,遇到下一个不同值时,跳跃到总共的排名. DENSE_RAN ...

  5. CSS3-transform缩放

    缩放:transform:scale(倍数); 实现hover的时候加载播放图标,具体效果如下: 首先需要创建一个大盒子,盒子上面部分用一个div来呈放图片,下面部分直接书写文字.观察发现播放图标是存 ...

  6. UR #3 核聚变反应强度( gcd )

    tags: -分解质因数 , gcd 题目大意 给定\(n\)个数,求\(a_1\)与\(a_i\)次小公约数 分析 易知次小公约数是\(\gcd\)的因数,于是用\(\gcd\)除去它的最小质因子即 ...

  7. openlayers-1 下载及安装使用

    javascript - Import from in Openlayers - Geographic Information Systems Stack Exchange 在浏览器中运行开放层示例 ...

  8. HDFS相关问题处理

    机房搬迁后datanode启动失败,报错如下: 2022-10-21 10:28:40,551 INFO org.apache.hadoop.hdfs.server.common.Storage: L ...

  9. k8s利用endpoints和service访问外部服务

    一.原理解析 在k8s集群中我们通过创建service去访问对应pod内的服务,而在创建service的时候会同时创建一个与service同名的endpoints对象,endpoints与pod实际建 ...

  10. centos7 安装 elasticsearch 7.15

    下载 elasticsearch wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.15.2-linu ...