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

推荐订阅源

V
Vulnerabilities – Threatpost
F
Fortinet All Blogs
Vercel News
Vercel News
C
Check Point Blog
P
Privacy International News Feed
Know Your Adversary
Know Your Adversary
Google DeepMind News
Google DeepMind News
T
Troy Hunt's Blog
TaoSecurity Blog
TaoSecurity Blog
I
Intezer
T
The Exploit Database - CXSecurity.com
Security Archives - TechRepublic
Security Archives - TechRepublic
H
Hacker News: Front Page
P
Proofpoint News Feed
GbyAI
GbyAI
Engineering at Meta
Engineering at Meta
Attack and Defense Labs
Attack and Defense Labs
S
Security @ Cisco Blogs
IT之家
IT之家
D
DataBreaches.Net
Hacker News: Ask HN
Hacker News: Ask HN
SecWiki News
SecWiki News
Y
Y Combinator Blog
Project Zero
Project Zero
H
Hackread – Cybersecurity News, Data Breaches, AI and More
L
Lohrmann on Cybersecurity
T
Tenable Blog
大猫的无限游戏
大猫的无限游戏
L
LINUX DO - 最新话题
G
Google Developers Blog
The GitHub Blog
The GitHub Blog
Recorded Future
Recorded Future
有赞技术团队
有赞技术团队
Martin Fowler
Martin Fowler
K
Kaspersky official blog
PCI Perspectives
PCI Perspectives
A
Arctic Wolf
Latest news
Latest news
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
N
Netflix TechBlog - Medium
雷峰网
雷峰网
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Google Online Security Blog
Google Online Security Blog
P
Palo Alto Networks Blog
The Hacker News
The Hacker News
WordPress大学
WordPress大学
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
月光博客
月光博客
Schneier on Security
Schneier on Security
M
MIT News - Artificial intelligence

博客园 - 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)