Keras模型的保存方式

在运行并且训练出一个模型后获得了模型的结构与许多参数,为了防止再次训练以及需要更好地去使用,我们需要保存当前状态

基本保存方式 h5

# 此处假设model为一个已经训练好的模型类
model.save('my_model.h5')

转换为json格式存储基本参数

# 此处假设model为一个已经训练好的模型类
json_string = model.to_json()
open('my_model_architecture.json','w').write(json_string)

转换为二进制pb格式

以下代码为我从网络中寻找到的,可以将模型中的内容转换为pb格式,但需要更改其中的h5为你的模型的h5

import sys \\
from keras.models import load_model \\
import tensorflow as tf
import os
import os.path as osp
from keras import backend as K def freeze_session(session, keep_var_names=None, output_names=None, clear_devices=True):
from tensorflow.python.framework.graph_util import convert_variables_to_constants
graph = session.graph
with graph.as_default():
freeze_var_names = list(set(v.op.name for v in tf.global_variables()).difference(keep_var_names or []))
output_names = output_names or []
output_names += [v.op.name for v in tf.global_variables()]
input_graph_def = graph.as_graph_def()
if clear_devices:
for node in input_graph_def.node:
node.device = ""
frozen_graph = convert_variables_to_constants(session, input_graph_def,output_names,freeze_var_names)
return frozen_graph input_fld = sys.path[0]
weight_file = 'my_model.h5'
output_graph_name = 'tensor_model.pb' output_fld = input_fld + '/tensorflow_model/'
if not os.path.isdir(output_fld):
os.mkdir(output_fld)
weight_file_path = osp.join(input_fld, weight_file)
K.set_learning_phase(0)
net_model = load_model(weight_file_path)
print('input is :', net_model.input.name)
print ('output is:', net_model.output.name)
sess = K.get_session()
frozen_graph = freeze_session(K.get_session(), output_names=[net_model.output.op.name])
from tensorflow.python.framework import graph_io
graph_io.write_graph(frozen_graph, output_fld, output_graph_name, as_text=False)
print('saved the constant graph (ready for inference) at: ', osp.join(output_fld, output_graph_name))

最新文章

  1. easyui combobox 左匹配模糊查询
  2. HTML meta viewport属性说明(mark)
  3. mysql:sql行列转换
  4. VBA Excel 单元格操作
  5. C++设计模式-Visitor访问者模式
  6. Java IO详解(六)------随机访问文件流
  7. (转)Spring注解完成Bean的定义
  8. MySQL查看和修改表的存储引擎(转载+加点东西)
  9. [AI开发]Python+Tensorflow打造自己的计算机视觉API服务
  10. Oracle之数组
  11. 自定义Qt构建步骤,添加数据文件(txt,json等)到构建目录
  12. 前端工程化系列[02]-Grunt构建工具的基本使用
  13. Angular 学习笔记 (Material Select and AutoComplete)
  14. 剑指Offer 38. 二叉树的深度 (二叉树)
  15. cdnbest节点如何升级
  16. 每日英语:China Destroys Six Tons of Confiscated Ivory
  17. PKCS#7
  18. 第二百九十三,Memcached缓存
  19. ng-深度学习-课程笔记-8: 超参数调试,Batch正则(Week3)
  20. 【MYSQL安装】mysql 5.6在centos6.4上的安装

热门文章

  1. CSS中关于linebox的baseline位置移动的理解
  2. EBS登陆界面IE显示异常
  3. windows下使用xerces -c解析XML
  4. MyISAM引擎表出现“Error 'Incorrect key file for table”
  5. Laravel 教程 - 实战 iBrand 开源电商 API 系统
  6. SpringMvc-view
  7. Android(java)学习笔记50:通过反射获取成员变量和成员方法并且使用
  8. BZOJ3531:[SDOI2014]旅行(树链剖分)
  9. PIL 一秒切九图 朋友圈发图神器
  10. 动态规划(DP),递推,最大子段和,POJ(2479,2593)