惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
博客园 - 聂微东
Application and Cybersecurity Blog
Application and Cybersecurity Blog
MyScale Blog
MyScale Blog
罗磊的独立博客
H
Help Net Security
L
LangChain Blog
T
Threat Research - Cisco Blogs
量子位
S
Securelist
Last Week in AI
Last Week in AI
L
Lohrmann on Cybersecurity
T
The Exploit Database - CXSecurity.com
P
Privacy International News Feed
The Hacker News
The Hacker News
Vercel News
Vercel News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Blog of Author Tim Ferriss
T
Threatpost
Security Latest
Security Latest
P
Palo Alto Networks Blog
Microsoft Security Blog
Microsoft Security Blog
NISL@THU
NISL@THU
F
Full Disclosure
WordPress大学
WordPress大学
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Stack Overflow Blog
Stack Overflow Blog
C
Check Point Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Heimdal Security Blog
J
Java Code Geeks
Recorded Future
Recorded Future
Hugging Face - Blog
Hugging Face - Blog
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
阮一峰的网络日志
阮一峰的网络日志
U
Unit 42
B
Blog RSS Feed
月光博客
月光博客
C
Cisco Blogs
V
Visual Studio Blog
D
DataBreaches.Net
H
Hacker News: Front Page
博客园 - 叶小钗
N
News and Events Feed by Topic
爱范儿
爱范儿
A
Arctic Wolf

博客园 - vsignsoft

CentOS7 安装 mysql-8.0.25-el7-x86_64.tar.gz Apache Doris 编译 制作 macOS Mojave 映像文件 编程实现文件重定向 openssh 免用户名/密码/服务器地址,登录远程服务器 FreeSWITCH 增加模块 mod_ilbc 解析 iOS crash 文件 私有地址与公网地址的转换 Supervisor 自动管理进程 使用 uWSGI 部署 Flask web 应用 安装 Flask macOS 上创建 Windows 兼容的 iso镜像文件 公式 X/N = int(H/N) * 65536 + [rem(H/N) * 65536 + L]/N 的运用 在VirtualBox 里安装纯DOS,进行汇编编程实践 Xcode 6、7 打包 苹果笔记本电脑,开不了机经验记录 CST时间转换成 yyyy-MM-dd格式 git 常规使用小结 XCode6 开发本地化应用
Tensorflow 模型保存与调用
vsignsoft · 2020-11-18 · via 博客园 - vsignsoft

Tensorflow 两种保存模型的方式:pb 和  saved_model 都可以。

1、pb

1.1 模型保存成pb

freozen_pb.py

 1 import tensorflow as tf
 2 from tensorflow.python.framework import graph_util
 3 
 4 
 5 
 6 with tf.Session(graph=tf.Graph()) as sess:
 7     x = tf.placeholder(tf.int32, name='in_x')
 8     y = tf.placeholder(tf.int32, name='in_y')
 9     b = tf.Variable(1, name='b')
10     m = tf.multiply(x, y)
11     a = tf.add(m, b, name='out_add')
12 
13     sess.run(tf.global_variables_initializer())
14     constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph_def, ['out_add'])
15 
16     feed_dict = {x: 10, y: 3}
17     print(sess.run(a, feed_dict))
18 
19     with tf.gfile.FastGFile('./model.pb', mode='wb') as f:
20         f.write(constant_graph.SerializeToString())

1.2 调用pb模型

call_pb.py

 1 import tensorflow as tf
 2 from tensorflow.python.platform import gfile
 3 
 4 
 5 sess = tf.Session()
 6 with gfile.FastGFile('./model.pb', 'rb') as f:
 7     graph_def = tf.GraphDef()
 8     graph_def.ParseFromString(f.read())
 9     sess.graph.as_default()
10     tf.import_graph_def(graph_def, name='')
11 
12 sess.run(tf.global_variables_initializer())
13 #print(sess.run('b:0'))
14 
15 in_x = sess.graph.get_tensor_by_name('in_x:0')
16 in_y = sess.graph.get_tensor_by_name('in_y:0')
17 out_add = sess.graph.get_tensor_by_name('out_add:0')
18 
19 ret = sess.run(out_add, feed_dict={in_x: 8, in_y: 9})
20 print(ret)

2、 saved_model

2.1 模型保存成saved model

freozen_sm.py

 1 import os
 2 import tensorflow as tf
 3 
 4 saved_model_path = os.getcwd()
 5 
 6 with tf.Session(graph=tf.Graph()) as sess:
 7     x = tf.placeholder(tf.int32, name='in_x')
 8     y = tf.placeholder(tf.int32, name='in_y')
 9     b = tf.Variable(1, name='b')
10     m = tf.multiply(x, y)
11     a = tf.add(m, b, name='out_add')
12 
13     sess.run(tf.global_variables_initializer())
14 
15     tf.saved_model.simple_save(sess, './sm', {'in_x': x, 'in_y': y}, {'out_add': a}, )

2.2 调用saved model模型

call_sm.py

 1 import tensorflow as tf
 2 
 3 sess = tf.Session()
 4 tf.saved_model.load(sess, [tf.saved_model.tag_constants.SERVING], './sm')
 5 in_x = sess.graph.get_tensor_by_name('in_x:0')
 6 in_y = sess.graph.get_tensor_by_name('in_y:0')
 7 out_add = sess.graph.get_tensor_by_name('out_add:0')
 8 
 9 ret = sess.run(out_add, feed_dict={in_x: 8, in_y: 5})
10 print(ret)