freezing Keras model

為了將model提供其他應用程式做使用


  • 目標
    我會將keras 轉換成 tensorflow 的 pb檔是為了在tensorRT中建置model,在tensorRT中需要再將.pb檔轉成.uff,才能提供給TensorRT使用,因為tensorRT支援的關係,目前需要在Linux的作業系統之下才能將.pb檔轉成.uff檔。

概要:
Keras framework提供將model儲存成.h5檔的function,其中的內容包含自訂的 neural network 的graph,與訓練之後的參數(weights),也可以單獨儲存weights,如果只有保留weights則需要在Tensorflow中重新建立graph,並將weight帶入,但這方法非常沒有效率。
於是將訓練好的model freezing,不能再更改weights,也就是不能在訓練,並且將單獨儲存的weights(.ckpt檔.meta檔)重建出graph,最後輸出.pb。

需求:
1. 安裝keras、tensorflow
2. TensorBoard(要用來查詢output的名稱)


實作:
1.將import 的來源要改成 tensorflow.keras
ex. from keras import Conv2D ->tensorflow.keras.layers import Convolution2D



def freeze_graph(model_dir, output_node_names):
  """Extract the sub graph defined by the output nodes and convert
  all its variables into constant
  Args:
      model_dir: the root folder containing the checkpoint state file
      output_node_names: a string, containing all the output node's names,
                          comma separated
                        """
  if not tf.gfile.Exists(model_dir):
    raise AssertionError(
      "Export directory doesn't exists. Please specify an export "
      "directory: %s" % model_dir)

  if not output_node_names:
    print("You need to supply the name of a node to --output_node_names.")
    return -1

  # We retrieve our checkpoint fullpath
  checkpoint = tf.train.get_checkpoint_state(model_dir)
  input_checkpoint = checkpoint.model_checkpoint_path
 
  # We precise the file fullname of our freezed graph
  absolute_model_dir = "/".join(input_checkpoint.split('/')[:-1])
  output_graph = absolute_model_dir + "/frozen_model.pb"

  # We clear devices to allow TensorFlow to control on which device it will load operations
  clear_devices = True

  # We start a session using a temporary fresh Graph
  with tf.Session(graph=tf.Graph()) as sess:
    # We import the meta graph in the current default Graph
    saver = tf.train.import_meta_graph(input_checkpoint + '.meta', clear_devices=clear_devices)

    # We restore the weights
    saver.restore(sess, input_checkpoint)

    # We use a built-in TF helper to export variables to constants
    output_graph_def = tf.graph_util.convert_variables_to_constants(
      sess, # The session is used to retrieve the weights
      tf.get_default_graph().as_graph_def(), # The graph_def is used to retrieve the nodes
      output_node_names.split(",") # The output node names are used to select the usefull nodes
    )

    # Finally we serialize and dump the output graph to the filesystem
    with tf.gfile.GFile(output_graph, "wb") as f:
      f.write(output_graph_def.SerializeToString())
    print("%d ops in the final graph." % len(output_graph_def.node))

  return output_graph_def


freeze_graph('kerasfile path',"outputs name")

參考:https://towardsdatascience.com/freezing-a-keras-model-c2e26cb84a38

補充:
在讀入model後
可以get loss 比率和SSIM:
loss_function,SSIM=keras_model.evaluate(test_x,test_y)
將keras .h5 儲存成 tensorflow .pb

留言

這個網誌中的熱門文章

Optix Ray Tracing 基本概念流程

使用Visual Studio建置optix專案