https://www.w3xue.com/exp/article/201812/10995.html

=====1====实践模型存入
import tensorflow as tf
from tensorflow import saved_model as sm
# 首先定义一个极其简单的计算图
X = tf.placeholder(tf.float32, shape=(3, ))
scale = tf.Variable([10, 11, 12], dtype=tf.float32)
y = tf.multiply(X, scale)
# 在会话中运行
with tf.Session() as sess:
sess.run(tf.initializers.global_variables())
value = sess.run(y, feed_dict={X: [1., 2., 3.]})
print(value) # 准备存储模型
path = '/home/×××/tf_model/model_1'
builder = sm.builder.SavedModelBuilder(path) # 构建需要在新会话中恢复的变量的 TensorInfo protobuf
X_TensorInfo = sm.utils.build_tensor_info(X)
scale_TensorInfo = sm.utils.build_tensor_info(scale)
y_TensorInfo = sm.utils.build_tensor_info(y)
# 构建 SignatureDef protobuf
SignatureDef = sm.signature_def_utils.build_signature_def(
inputs={'input_1': X_TensorInfo, 'input_2': scale_TensorInfo},
outputs={'output': y_TensorInfo},
method_name='what'
)
# 将 graph 和变量等信息写入 MetaGraphDef protobuf
# 这里的 tags 里面的参数和 signature_def_map 字典里面的键都可以是自定义字符串,TensorFlow 为了方便使用,不在新地方将自定义的字符串忘记,可以使用预定义的这些值
builder.add_meta_graph_and_variables(sess, tags=[sm.tag_constants.TRAINING],
signature_def_map={sm.signature_constants.CLASSIFY_INPUTS: SignatureDef}
)
 # 将 MetaGraphDef 写入磁盘
builder.save() =====222===模型导入 import tensorflow as tf
from tensorflow import saved_model as sm
# 需要建立一个会话对象,将模型恢复到其中
with tf.Session() as sess:
path = '/home/×××/tf_model/model_1'
MetaGraphDef = sm.loader.load(sess, tags=[sm.tag_constants.TRAINING], export_dir=path)
# 解析得到 SignatureDef protobuf
SignatureDef_d = MetaGraphDef.signature_def
SignatureDef = SignatureDef_d[sm.signature_constants.CLASSIFY_INPUTS]
# 解析得到 3 个变量对应的 TensorInfo protobuf
X_TensorInfo = SignatureDef.inputs['input_1']
scale_TensorInfo = SignatureDef.inputs['input_2']
y_TensorInfo = SignatureDef.outputs['output']
# 解析得到具体 Tensor
# .get_tensor_from_tensor_info() 函数中可以不传入 graph 参数,TensorFlow 自动使用默认图
X = sm.utils.get_tensor_from_tensor_info(X_TensorInfo, sess.graph)
scale = sm.utils.get_tensor_from_tensor_info(scale_TensorInfo, sess.graph)
y = sm.utils.get_tensor_from_tensor_info(y_TensorInfo, sess.graph)
print(sess.run(scale))
print(sess.run(y, feed_dict={X: [3., 2., 1.]}))
# 输出
[10. 11. 12.]
[30. 22. 12.]

  

========11111======实践模型存入(无格式,代码没对齐)

import tensorflow as tf
from tensorflow import saved_model as sm

# 首先定义一个极其简单的计算图
X = tf.placeholder(tf.float32, shape=(3, ))
scale = tf.Variable([10, 11, 12], dtype=tf.float32)
y = tf.multiply(X, scale)

# 在会话中运行
with tf.Session() as sess:
sess.run(tf.initializers.global_variables())
value = sess.run(y, feed_dict={X: [1., 2., 3.]})
print(value)

# 准备存储模型
path = '/home/×××/tf_model/model_1'
builder = sm.builder.SavedModelBuilder(path)

# 构建需要在新会话中恢复的变量的 TensorInfo protobuf
X_TensorInfo = sm.utils.build_tensor_info(X)
scale_TensorInfo = sm.utils.build_tensor_info(scale)

y_TensorInfo = sm.utils.build_tensor_info(y)

# 构建 SignatureDef protobuf
SignatureDef = sm.signature_def_utils.build_signature_def(
inputs={'input_1': X_TensorInfo, 'input_2': scale_TensorInfo},
outputs={'output': y_TensorInfo},
method_name='what'
)

# 将 graph 和变量等信息写入 MetaGraphDef protobuf
# 这里的 tags 里面的参数和 signature_def_map 字典里面的键都可以是自定义字符串,TensorFlow 为了方便使用,不在新地方将自定义的字符串忘记,可以使用预定义的这些值
builder.add_meta_graph_and_variables(sess, tags=[sm.tag_constants.TRAINING],
signature_def_map={sm.signature_constants.CLASSIFY_INPUTS: SignatureDef}
)

 # 将 MetaGraphDef 写入磁盘
builder.save()

==================222222===========================================

=========模型导入

这样我们就把模型整体存储到了磁盘中,而且我们将三个变量 X, scale, y 全部序列化后存储到了其中,所以恢复模型时便可以将他们完全解析出来:

import tensorflow as tf
from tensorflow import saved_model as sm

# 需要建立一个会话对象,将模型恢复到其中
with tf.Session() as sess:
path = '/home/×××/tf_model/model_1'
MetaGraphDef = sm.loader.load(sess, tags=[sm.tag_constants.TRAINING], export_dir=path)

# 解析得到 SignatureDef protobuf
SignatureDef_d = MetaGraphDef.signature_def
SignatureDef = SignatureDef_d[sm.signature_constants.CLASSIFY_INPUTS]

# 解析得到 3 个变量对应的 TensorInfo protobuf
X_TensorInfo = SignatureDef.inputs['input_1']
scale_TensorInfo = SignatureDef.inputs['input_2']
y_TensorInfo = SignatureDef.outputs['output']

# 解析得到具体 Tensor
# .get_tensor_from_tensor_info() 函数中可以不传入 graph 参数,TensorFlow 自动使用默认图
X = sm.utils.get_tensor_from_tensor_info(X_TensorInfo, sess.graph)
scale = sm.utils.get_tensor_from_tensor_info(scale_TensorInfo, sess.graph)
y = sm.utils.get_tensor_from_tensor_info(y_TensorInfo, sess.graph)

print(sess.run(scale))
print(sess.run(y, feed_dict={X: [3., 2., 1.]}))

# 输出
[10. 11. 12.]
[30. 22. 12.]

============333333讲解=

https://github.com/Jerryzhangzhao/DL_tensorflow/blob/master/save%20and%20restore%20model/use%20saved%20model/save_and_restore_by_savedmodelbuilder.py

save——model模块保存和载入使用简单例子的更多相关文章

  1. 【NLP学习其五】模型保存与载入的注意事项(记问题No module named 'model')

    这是一次由于路径问题(找不到模型)引出模型保存问题的记录 最近,我试着把使用GPU训练完成的模型部署至预发布环境时出现了一个错误,以下是log节选 unpickler.load() ModuleNot ...

  2. Django之model模块创建表完整过程

    Django中,与数据库相关的模块是model模块,它提供了一种简单易操作的API方式与数据库交互,它是通过ORM映射的方式来操作数据库,一个类对应数据库一张表,一个类属性,对应该表的一个字段,一个实 ...

  3. (原+译)pytorch中保存和载入模型

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/8108466.html 参考网址: http://pytorch.org/docs/master/not ...

  4. Tensorflow 保存和载入训练过程

    本节涉及点: 保存训练过程 载入保存的训练过程并继续训练 通过命令行参数控制是否强制重新开始训练 训练过程中的手动保存 保存训练过程前,程序征得同意 一.保存训练过程 以下方代码为例: import ...

  5. TensorFlow 模型的保存与载入

    参考学习博客: # https://www.cnblogs.com/felixwang2/p/9190692.html 一.模型保存 # https://www.cnblogs.com/felixwa ...

  6. 7.keras-模型保存和载入

    keras-模型保存和载入 1.数据的载入与预处理 import numpy as np from keras.datasets import mnist from keras.utils impor ...

  7. Django学习之四:Django Model模块

    目录 Django Model 模型 MODEL需要在脑子里记住的基础概念 区分清楚,必须不能混淆的 class Meta 内嵌元数据定义类 简单model创建实例 数据源配置 接着通过models在 ...

  8. docker保存、载入、导出、导入

    保存和载入 拿到CONTAINER ID docker ps -a 通过容器id生成镜像dockerlinuxdemoweb:update docker commit b33633d12871 doc ...

  9. Docker 本地导入镜像/保存镜像/载入镜像/删除镜像

    1.Docker导入本地镜像 有时候我们自己在本地或者其它小伙伴电脑上拷贝了一份镜像,有了这个镜像之后,我们可以把本地的镜像导入,使用docker import 命令. 例如这里下载了一个 aliba ...

随机推荐

  1. 【有钱的大佬看过来】Java开发学习大纲

    Java开发学习大纲文档V7.0 有钱的大佬可以买下这个版权,全网最完整最详细了,没钱的大佬可以按照自己的方式去整理.有需要的私聊作者QQ:253173641 来源于-幸福的沉淀:https://ww ...

  2. auth

    谨记:使用的任何框架在网上都会有对应的auth代码,多百度,直接引用插件就好了 tp5 auth 示例:https://blog.csdn.net/strugglm/article/details/7 ...

  3. Linux下Discuz!7.2 LAMP环境搭建

    linux下Discuz LAMP环境搭建 1.需要的源代码 httpd-2.2.15.tar.gz          mysql-5.1.44.tar.gz     php-5.3.2.tar.gz ...

  4. 前端杂谈: Attribute VS Property

    前端杂谈: Attribute VS Property 第一个问题: 什么是 attribute & 什么是 property ? attribute 是我们在 html 代码中经常看到的键值 ...

  5. LightOJ-1027-A Dangerous Maze(概率)

    链接: https://vjudge.net/problem/LightOJ-1027#author=634579757 题意: You are in a maze; seeing n doors i ...

  6. 树莓派GPIO接口

    一.GPIO模式 GPIO分为板上模式和BCM模式 板上模式即为平时百度谷歌搜到的图,按顺序1-40排列(1B是26引脚) BCM模式为CPU分的,在图上一般会单独标记 二.引脚分类 1.电源:3.3 ...

  7. json的概念,语法规则,数组,物件结构

    主要说一些关于json的简单应用 ㈠概念 JSON(JavaScript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式. 它基于 ECMAScript (欧洲计算机协 ...

  8. ASP.NET大文件上传断点续传解决方案

    HTML部分 <%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="index.aspx. ...

  9. input选择框默认样式修改

    input选择框是无法直接修改样式,我们只能间接来改变它样式. 原理:用图片来代替原来的input选择框,原来的input选择框定位到图片上方并让它opacity为0,鼠标点击时用js来改变图片,这样 ...

  10. LibreOJ NOI Round #2 Day 1

    LibreOJ NOI Round #2 Day 1 T1: 别被定义弄晕了 反着做,A->1/A+B 取倒数没法做,所以变成a/b,维护2*2的矩阵 区间?不用线段树,不用倍增 存在逆矩阵,直 ...