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)):
        print("h5_model.output[i]", h5_model.output[i].name)
        out_nodes.append(out_prefix + str(i + 1))
        tf.identity(h5_model.output[i], out_prefix + str(i + 1))
    K.set_learning_phase(0)
    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)
Ejemplo n.º 2
0
def keras_to_tensorflow(keras_model,
                        output_dir,
                        model_name,
                        out_prefix="output_",
                        log_tensorboard=True):

    if os.path.exists(output_dir) == False:
        os.mkdir(output_dir)

    out_nodes = []

    for i in range(len(keras_model.outputs)):
        out_nodes.append(out_prefix + str(i + 1))
        tf.identity(keras_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)

        if log_tensorboard:
            import_pb_to_tensorboard.import_to_tensorboard(
                os.path.join(output_dir, model_name), output_dir)
def h5_to_pb(h5_model,
             output_dir,
             model_name,
             out_prefix="output_",
             log_tensorboard=True):
    """
    Convert keras .h5 file to tensorflow .pb file
    :param h5_model: keras model after training
    :param output_dir: url to save the .pb file
    :param model_name: basename of the file
    :param out_prefix: output prefix string
    :param log_tensorboard: Whether to save an event file for tensorborad
    :return:
    """
    if not os.path.exists(output_dir):
        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(
            os.path.join(output_dir, model_name), output_dir)
Ejemplo n.º 4
0
def h5_to_pb(h5_model, output_dir, model_name, out_prefix="output_", log_tensorboard=False):
    """.h5模型文件转换成pb模型文件
    Argument:
        h5_model: str
            .h5模型文件
        output_dir: str
            pb模型文件保存路径
        model_name: str
            pb模型文件名称
        out_prefix: str
            根据训练,需要修改
        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)
def h5_to_pb(h5_model,
             output_dir,
             model_name,
             out_prefix="output_",
             log_tensorboard=True):
    '''
    将h5格式的计算图转化为pb格式的计算图
    :param h5_model: h5格式的计算图
    :param output_dir: pb格式保存路径
    :param model_name: 生成的pb文件模型路径
    :param out_prefix: 生成的pb文件中输出节点前缀
    :param log_tensorboard: 是否对计算图进行可视化的标记
    :return:
    '''
    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()
    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:
        import_pb_to_tensorboard.import_to_tensorboard(
            osp.join(output_dir, model_name), output_dir)
Ejemplo n.º 6
0
def h5_to_pb(input_path,
             output_dir,
             out_prefix="output_",
             log_tensorboard=True):
    h5_model = load_model(input_path)
    model_name = os.path.basename(input_path)
    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 = 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(
            os.path.join(output_dir, model_name), output_dir)
Ejemplo n.º 7
0
def h5_to_pb(h5_model,
             output_dir,
             model_name,
             out_prefix="output_",
             log_tensorboard=True):
    """.h5模型文件转换成pb模型文件
    Argument:
        h5_model: str
            .h5模型文件
        output_dir: str
            pb模型文件保存路径
        model_name: str
            pb模型文件名称
        out_prefix: str
            根据训练,需要修改
        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模型文件路径参数
        input_path = 'C:/Users/a2823/Documents/GitHub/PythonStudy/mechine_learning'
        weight_file = 'cat_dog.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)
Ejemplo n.º 8
0
def run_main():
    parser = argparse.ArgumentParser(
        description='Script to import frozen graph into TensorBoard')
    parser.add_argument(
        '-g',
        '--graph',
        type=str,
        default='',
        help="Protobuf graph file (.pb) to import into TensorBoard.",
        required='True')

    flags = parser.parse_args()

    if (os.path.exists('./dummy')):
        shutil.rmtree('./dummy')

    import_pb_to_tensorboard.import_to_tensorboard(model_dir=flags.graph,
                                                   log_dir='./dummy')
    print(
        '..or try `tensorboard --logdir=./dummy --port 6006 --host localhost`')
Ejemplo n.º 9
0
def h5_to_pb(h5_model,
             output_dir,
             model_name,
             out_prefix="output_",
             log_tensorboard=True):
    """[Convert h5 format to pb format.]

    Args:
        h5_model ([type]): [Model.]
        output_dir ([str]): [Output directory.]
        model_name ([str]): [Output pb file name.]
        out_prefix (str, optional): [prefix of output tensor name.]. Defaults to "output_".
        log_tensorboard (bool, optional): [Wheather to record log.]. Defaults to True.
    """
    if osp.exists(output_dir) == False:
        os.mkdir(output_dir)

    out_nodes = list()

    ## get all tensor node.
    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()

    ## Conver to pb file
    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:
        import_pb_to_tensorboard.import_to_tensorboard(
            osp.join(output_dir, model_name), output_dir)
"""
https://heartbeat.fritz.ai/deploying-pytorch-and-keras-models-to-android-with-tensorflow-mobile-a16a1fb83f2
讓pb檔案恢復成可以用tensorboard查看的狀態

已成功,這支程式是可以work的
"""

import tensorflow as tf

import os

model = 'my_frozen_graph.pb'  #请将这里的pb文件路径改为自己的

from tensorflow.python.tools import import_pb_to_tensorboard
import_pb_to_tensorboard.import_to_tensorboard(os.path.join('', model), '')

# # graph = tf.get_default_graph()
# graph_def = tf.GraphDef()#graph.as_graph_def()
# graph_def.ParseFromString(tf.gfile.GFile(model, 'rb').read())
# tf.import_graph_def(graph_def, name='graph')
# summaryWriter = tf.summary.FileWriter('log/', graph_def)
Ejemplo n.º 11
0
def ConvertToPB(yamlFile, WeightFile, ExportName="converted"):
    with open(yamlFile, 'r') as f:
        modelyaml = f.read()

    model = model_from_yaml(modelyaml,
                            custom_objects={
                                'box_iou': yolo3.model.box_iou,
                                'yolo_head': yolo3.model.yolo_head
                            })
    model.load_weights(WeightFile)

    # All new operations will be in test mode from now on.
    K.set_learning_phase(0)

    # Serialize the model and get its weights, for quick re-building.
    config = model.get_config()
    weights = model.get_weights()

    # Re-build a model where the learning phase is now hard-coded to 0.
    #new_model = Sequential.from_config(config)
    #new_model.set_weights(weights)

    temp_dir = "graph/" + ExportName
    if not os.path.exists(temp_dir):
        os.makedirs(temp_dir)

    checkpoint_prefix = os.path.join(temp_dir, "saved_checkpoint")
    checkpoint_state_name = "checkpoint_state"
    input_graph_name = ExportName + "_input_graph.pb"
    output_graph_name = ExportName + "_output_graph.pb"
    output_optimized_graph_name = ExportName + "_opt_output_graph.pb"

    # Temporary save graph to disk without weights included.
    saver = tf.train.Saver()
    checkpoint_path = saver.save(K.get_session(),
                                 checkpoint_prefix,
                                 global_step=0,
                                 latest_filename=checkpoint_state_name)
    tf.train.write_graph(K.get_session().graph, temp_dir, input_graph_name)

    input_graph_path = os.path.join(temp_dir, input_graph_name)
    input_saver_def_path = ""
    input_binary = False
    #[node.op.name for node in model.outputs]
    output_node_names = [node.op.name
                         for node in model.outputs][0]  # model dependent
    restore_op_name = "save/restore_all"
    filename_tensor_name = "save/Const:0"
    output_graph_path = os.path.join(temp_dir, output_graph_name)
    clear_devices = False

    # Embed weights inside the graph and save to disk.
    freeze_graph.freeze_graph(input_graph_path, input_saver_def_path,
                              input_binary, checkpoint_path, output_node_names,
                              restore_op_name, filename_tensor_name,
                              output_graph_path, clear_devices, "")

    input_graph_def = tf.GraphDef()
    with tf.gfile.Open(output_graph_path, "rb") as f:
        data = f.read()
        input_graph_def.ParseFromString(data)

    #input_graph_def = tf.graph_util.remove_training_nodes(input_graph_def)

    output_graph_def = optimize_for_inference_lib.optimize_for_inference(
        input_graph_def,
        [node.op.name
         for node in model.inputs],  # an array of the input node(s)
        [output_node_names],  # an array of output nodes
        tf.float32.as_datatype_enum)

    # Save the optimized graph

    f = tf.gfile.FastGFile(output_optimized_graph_name, "w")
    f.write(output_graph_def.SerializeToString())

    from tensorflow.python.tools import import_pb_to_tensorboard
    import_pb_to_tensorboard.import_to_tensorboard(output_graph_path,
                                                   "logs/%s/" % ExportName)
Ejemplo n.º 12
0
def construct_graph(
        network_class: Type[InferenceNetwork], config: Path,
        checkpoint_dir: str, batch_size: int, batches_per_step: int,
        image_filenames: Tuple[str], loop: bool, preprocess_fn: Callable,
        num_ipus: int, mode: str, save_graph_pb: bool
) -> Tuple[tf.Operation, tf.Operation, tf.Operation]:
    """Create inference graph on the device, set up in-feeds and out-feeds, connect dataset iterator to the graph.

    This function also exports the frozen graph into an event file, to be viewed in Tensorboard in `network_name_graph`
    directory.

    Args:
        network_class: Class corresponding to chosen model.
        config: Path to config file.
        checkpoint_dir: Checkpoint location.
        batch_size: Batch size per forward pass.
        batches_per_step: Number of forward passes per step.
        image_filenames: Collection of path to images.
        loop: Run inference in a loop.
        preprocess_fn: Pre-process function to apply to the image before feeding into the graph.
        num_ipus: Number of ipus.
        mode: Inference mode.
        save_graph_pb: If true, export frozen graph to event file to view in Tensorboard

    Returns: Compiled loop operator to run repeated inference over the dataset, infeed_queue intitializer, outfeed op.

    """
    # Model specific config
    with open(config.as_posix()) as file_stream:
        try:
            config_dict = yaml.safe_load(file_stream)
        except yaml.YAMLError as exc:
            tf.logging.error(exc)

    config_dict['network_name'] = config.stem
    if 'dtype' not in config_dict:
        config_dict["dtype"] = 'float16'

    # Create inference optimized frozen graph definition
    network = network_class(input_shape=config_dict["input_shape"],
                            num_outputs=1000,
                            batch_size=batch_size,
                            data_type=config_dict['dtype'],
                            config=config_dict,
                            checkpoint_dir=checkpoint_dir)

    # Export frozen graph to event file to view in Tensorboard"
    if save_graph_pb:
        log_dir = Path(f"{config_dict['network_name']}_graph")
        graph_filename = f"{log_dir}/{config_dict['network_name']}_graph.pb"
        if not log_dir.exists():
            log_dir.mkdir()
        with tf.io.gfile.GFile(graph_filename, "wb") as f:
            f.write(network.optimized_graph.SerializeToString())
        logging.info("%d ops in the final graph." %
                     len(network.optimized_graph.node))
        import_to_tensorboard(graph_filename, log_dir=log_dir.as_posix())

    # Reset graph before creating one on the IPU
    tf.reset_default_graph()

    # Create dataset
    dataset = get_dataset(image_filenames,
                          batch_size,
                          loop=loop,
                          preprocess_fn=preprocess_fn,
                          img_width=config_dict["input_shape"][1],
                          img_height=config_dict["input_shape"][0],
                          dtype=config_dict['dtype'])

    # Set up graph on device, connect infeed and outfeed to the graph.
    num_replicas = num_ipus if mode == 'replicated' else 1
    infeed_queue = ipu_infeed_queue.IPUInfeedQueue(
        dataset,
        device_ordinal=0,
        feed_name="infeed",
        replication_factor=num_replicas)
    outfeed_queue = ipu_outfeed_queue.IPUOutfeedQueue(
        device_ordinal=0,
        feed_name="outfeed",
        outfeed_all=True,
        replication_factor=num_replicas)

    def comp_fn():
        def body(img):
            with scopes.ipu_scope('/device:IPU:0'):
                if mode == 'sharded':
                    with autoshard.ipu_autoshard():
                        probs = tf.import_graph_def(
                            network.optimized_graph,
                            input_map={network.graph_input: img},
                            name="optimized",
                            return_elements=[network.graph_output])[0]
                    autoshard.automatic_sharding(num_shards=num_ipus,
                                                 input_ts=img,
                                                 loss_ts=probs,
                                                 frozen_inference=True)
                    outfeed_op = outfeed_queue.enqueue(probs)
                    outfeed_op._set_attr(
                        sharding._XLA_SHARDING,
                        attr_value_pb2.AttrValue(
                            s=probs.op.get_attr('_XlaSharding')))
                else:
                    probs = tf.import_graph_def(
                        network.optimized_graph,
                        input_map={network.graph_input: img},
                        name="optimized",
                        return_elements=[network.graph_output])[0]
                    outfeed_op = outfeed_queue.enqueue(probs)
                # Note that enqueue happens on the IPU.
                return outfeed_op

        return loops.repeat(batches_per_step, body, [], infeed_queue)

    loop_op = ipu_compiler.compile(comp_fn, [])

    # The dequeue of the outfeed needs to happen on the CPU.
    with tf.device('cpu'):
        outfeed_dequeue = outfeed_queue.dequeue()

    ipu_utils.move_variable_initialization_to_cpu()
    return loop_op, infeed_queue.initializer, outfeed_dequeue
Ejemplo n.º 13
0
def inspect2(pb_model, log_path):
    import_to_tensorboard(model_dir=pb_model, log_dir=log_path)