text: # Copyright (C) 2020. Huawei Technologies Co., Ltd. All rights reserved.
  #
  # Permission is hereby granted, free of charge, to any person obtaining a copy
  # of this software and associated documentation files (the "Software"), to deal
  # in the Software without restriction, including without limitation the rights
  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  # copies of the Software, and to permit persons to whom the Software is
  # furnished to do so, subject to the following conditions:
  #
  # The above copyright notice and this permission notice shall be included in
  # all copies or substantial portions of the Software.
  #
  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  # THE SOFTWARE.
  """Save model in pb file format."""
  
  import os
  from xt.model.tf_compat import tf
  from tensorflow.python.framework import graph_util, graph_io
  from xt.model.tf_compat import K
  
  
  def pb_model(h5_model, file_name, out_prefix="output_"):
      """
      Output model in pb file.
  
      :param h5_model:
      :param file_name:
      :param out_prefix:
      """
      output_dir = os.path.dirname(file_name) + "/" + "pb_model"
      model_name = os.path.basename(file_name) + ".pb"
      if os.path.exists(output_dir) is 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()
      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)
  