最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

將keras的h5模型轉(zhuǎn)換為tensorflow的pb模型操作

 更新時(shí)間:2020年05月25日 10:28:51   作者:mishidemudong  
這篇文章主要介紹了將keras的h5模型轉(zhuǎn)換為tensorflow的pb模型操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧

背景:目前keras框架使用簡單,很容易上手,深得廣大算法工程師的喜愛,但是當(dāng)部署到客戶端時(shí),可能會出現(xiàn)各種各樣的bug,甚至不支持使用keras,本文來解決的是將keras的h5模型轉(zhuǎn)換為客戶端常用的tensorflow的pb模型并使用tensorflow加載pb模型。

h5_to_pb.py
 
from keras.models import load_model
import tensorflow as tf
import os 
import os.path as osp
from keras import backend as K
#路徑參數(shù)
input_path = 'input path'
weight_file = 'weight.h5'
weight_file_path = osp.join(input_path,weight_file)
output_graph_name = weight_file[:-3] + '.pb'
#轉(zhuǎn)換函數(shù)
def h5_to_pb(h5_model,output_dir,model_name,out_prefix = "output_",log_tensorboard = True):
  if osp.exists(output_dir) == False:
    os.mkdir(output_dir)
  out_nodes = []
  for i in range(len(h5_model.outputs)):
    out_nodes.append(out_prefix + str(i + 1))
    tf.identity(h5_model.output[i],out_prefix + str(i + 1))
  sess = K.get_session()
  from tensorflow.python.framework import graph_util,graph_io
  init_graph = sess.graph.as_graph_def()
  main_graph = graph_util.convert_variables_to_constants(sess,init_graph,out_nodes)
  graph_io.write_graph(main_graph,output_dir,name = model_name,as_text = False)
  if log_tensorboard:
    from tensorflow.python.tools import import_pb_to_tensorboard
    import_pb_to_tensorboard.import_to_tensorboard(osp.join(output_dir,model_name),output_dir)
#輸出路徑
output_dir = osp.join(os.getcwd(),"trans_model")
#加載模型
h5_model = load_model(weight_file_path)
h5_to_pb(h5_model,output_dir = output_dir,model_name = output_graph_name)
print('model saved')

將轉(zhuǎn)換成的pb模型進(jìn)行加載

load_pb.py
 
import tensorflow as tf
from tensorflow.python.platform import gfile
 
def load_pb(pb_file_path):
  sess = tf.Session()
  with gfile.FastGFile(pb_file_path, 'rb') as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())
    sess.graph.as_default()
    tf.import_graph_def(graph_def, name='')
 
  print(sess.run('b:0'))
  #輸入
  input_x = sess.graph.get_tensor_by_name('x:0')
  input_y = sess.graph.get_tensor_by_name('y:0')
  #輸出
  op = sess.graph.get_tensor_by_name('op_to_store:0')
  #預(yù)測結(jié)果
  ret = sess.run(op, {input_x: 3, input_y: 4})
  print(ret)
 

補(bǔ)充知識:h5模型轉(zhuǎn)化為pb模型,代碼及排坑

我是在實(shí)際工程中要用到tensorflow訓(xùn)練的pb模型,但是訓(xùn)練的代碼是用keras寫的,所以生成keras特定的h5模型,所以用到了h5_to_pb.py函數(shù)。

附上h5_to_pb.py(python3)

#*-coding:utf-8-*

"""
將keras的.h5的模型文件,轉(zhuǎn)換成TensorFlow的pb文件
"""
# ==========================================================

from keras.models import load_model
import tensorflow as tf
import os.path as osp
import os
from keras import backend
#from keras.models import Sequential

def h5_to_pb(h5_model, output_dir, model_name, out_prefix="output_", log_tensorboard=True):
  """.h5模型文件轉(zhuǎn)換成pb模型文件
  Argument:
    h5_model: str
      .h5模型文件
    output_dir: str
      pb模型文件保存路徑
    model_name: str
      pb模型文件名稱
    out_prefix: str
      根據(jù)訓(xùn)練,需要修改
    log_tensorboard: bool
      是否生成日志文件
  Return:
    pb模型文件
  """
  if os.path.exists(output_dir) == False:
    os.mkdir(output_dir)
  out_nodes = []
  for i in range(len(h5_model.outputs)):
    out_nodes.append(out_prefix + str(i + 1))
    tf.identity(h5_model.output[i], out_prefix + str(i + 1))
  sess = backend.get_session()

  from tensorflow.python.framework import graph_util, graph_io
  # 寫入pb模型文件
  init_graph = sess.graph.as_graph_def()
  main_graph = graph_util.convert_variables_to_constants(sess, init_graph, out_nodes)
  graph_io.write_graph(main_graph, output_dir, name=model_name, as_text=False)
  # 輸出日志文件
  if log_tensorboard:
    from tensorflow.python.tools import import_pb_to_tensorboard
    import_pb_to_tensorboard.import_to_tensorboard(os.path.join(output_dir, model_name), output_dir)

if __name__ == '__main__':
  # .h模型文件路徑參數(shù)
  input_path = 'D:/CSP'
  weight_file = 'xingren.h5'
  weight_file_path = os.path.join(input_path, weight_file)
  output_graph_name = weight_file[:-3] + '.pb'

  # pb模型文件輸出輸出路徑
  output_dir = osp.join(os.getcwd(),"trans_model")
  #model.save(xingren.h5)
  # 加載模型
  #h5_model = Sequential()
  h5_model = load_model(weight_file_path)
  #h5_model.save(weight_file_path)
  #h5_model.save('xingren.h5')
  h5_to_pb(h5_model, output_dir=output_dir, model_name=output_graph_name)
  print ('Finished')

在運(yùn)行的時(shí)候遇到了下面問題:

原因:我們訓(xùn)練模型的時(shí)候用save_weights函數(shù)保存模型,但是這個函數(shù)只保存了權(quán)重文件,并沒有又保存模型的參數(shù)。要把save_weights改為save。

下邊是兩個函數(shù)介紹:

save()保存的模型結(jié)果,它既保持了模型的圖結(jié)構(gòu),又保存了模型的參數(shù)。

save_weights()保存的模型結(jié)果,它只保存了模型的參數(shù),但并沒有保存模型的圖結(jié)構(gòu)

以上這篇將keras的h5模型轉(zhuǎn)換為tensorflow的pb模型操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • django settings.py配置文件的詳細(xì)介紹

    django settings.py配置文件的詳細(xì)介紹

    本文主要介紹了django settings.py配置文件的詳細(xì)介紹,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • jupyter notebook運(yùn)行代碼沒反應(yīng)且in[ ]沒有*

    jupyter notebook運(yùn)行代碼沒反應(yīng)且in[ ]沒有*

    本文主要介紹了jupyter notebook運(yùn)行代碼沒反應(yīng)且in[ ]沒有*,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Python算法練習(xí)之二分查找算法的實(shí)現(xiàn)

    Python算法練習(xí)之二分查找算法的實(shí)現(xiàn)

    二分查找也稱折半查找(Binary Search),它是一種效率較高的查找方法。本文將介紹python如何實(shí)現(xiàn)二分查找算法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2022-06-06
  • 詳解python datetime模塊

    詳解python datetime模塊

    這篇文章主要介紹了python datetime模塊的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下
    2020-08-08
  • Python實(shí)現(xiàn)打磚塊小游戲代碼實(shí)例

    Python實(shí)現(xiàn)打磚塊小游戲代碼實(shí)例

    這篇文章主要介紹了Python打磚塊小游戲,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • Python中hash()函數(shù)之哈希值的奧秘詳解

    Python中hash()函數(shù)之哈希值的奧秘詳解

    hash()是Python 中的一個內(nèi)置函數(shù),用于計(jì)算對象的哈希值,哈希值是一個整數(shù),用于唯一標(biāo)識對象,下面這篇文章主要給大家介紹了關(guān)于Python中hash()函數(shù)之哈希值奧秘的相關(guān)資料,需要的朋友可以參考下
    2024-06-06
  • Python爬蟲實(shí)戰(zhàn)之12306搶票開源

    Python爬蟲實(shí)戰(zhàn)之12306搶票開源

    今天小編就為大家分享一篇關(guān)于Python爬蟲實(shí)戰(zhàn)之12306搶票開源,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • Python?sklearn庫中的隨機(jī)森林模型詳解

    Python?sklearn庫中的隨機(jī)森林模型詳解

    本文主要說明?Python?的?sklearn?庫中的隨機(jī)森林模型的常用接口、屬性以及參數(shù)調(diào)優(yōu)說明,需要讀者或多或少了解過sklearn庫和一些基本的機(jī)器學(xué)習(xí)知識
    2023-08-08
  • Python NumPy中的隨機(jī)數(shù)及ufuncs函數(shù)使用示例詳解

    Python NumPy中的隨機(jī)數(shù)及ufuncs函數(shù)使用示例詳解

    這篇文章主要介紹了Python NumPy中的隨機(jī)數(shù)及ufuncs函數(shù)使用,ufunc函數(shù)是NumPy中的一種通用函數(shù),它可以對數(shù)組中的每個元素進(jìn)行操作,而不需要使用循環(huán)語句,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2023-05-05
  • python 消除 futureWarning問題的解決

    python 消除 futureWarning問題的解決

    今天小編就為大家分享一篇python 消除 futureWarning問題的解決,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12

最新評論

静宁县| 合江县| 祁连县| 绥棱县| 小金县| 华容县| 常山县| 奉节县| 松滋市| 新疆| 光泽县| 抚远县| 余姚市| 太仆寺旗| 四会市| 邵武市| 阳城县| 永昌县| 峨山| 宜城市| 噶尔县| 定结县| 应用必备| 吉隆县| 金山区| 盐城市| 大港区| 奉节县| 灵丘县| 定西市| 澄城县| 贵德县| 昌江| 盐边县| 三穗县| 梅州市| 绥滨县| 武穴市| 新昌县| 海伦市| 柯坪县|