【问题标题】:Use .h5 model with nvidia tensorrt on cpu instead of gpu在 cpu 而不是 gpu 上使用带有 nvidia tensorrt 的 .h5 模型
【发布时间】:2020-04-24 09:46:49
【问题描述】:

我有一个 .h5 模型(用于 GPU?),我想在我的 CPU 上运行它。我使用 python 转换了模型,看起来它确实被转换了,但是在 docker tensorrt 中运行它时,我得到了错误:

[[TRTEngineOp_8]] E0106 21:02:54.141211 1 model_repository_manager.cc:810] 未能加载“retinanet_TRT”版本 1:内部:没有注册 OpKernel 以支持 {{node TRTEngineOp_16}} 使用的操作“TRTEngineOp”,这些属性:[use_calibration=false , fixed_input_size=true, input_shapes=[[?,?,?,3]], OutT=[DT_FLOAT], precision_mode="FP16", static_engine=false, serialized_segment="\ne\n\021T...2\005VALID ",cached_engine_batches=[],InT=[DT_FLOAT],calibration_data="",output_shapes=[[?,?,?,64]],workspace_size_bytes=2127659,max_cached_engines_count=1,segment_funcdef_name="TRTEngineOp_16_native_segment"] 注册设备:[CPU, XLA_CPU] 注册内核: 设备='GPU'

我可以做些什么来转换模型,以便我只能在 CPU 上使用它?

是这样转换的:

with tf.Graph().as_default():
    with tf.Session() as sess:
        graph = sess.graph
        K.set_session(sess)
        K.set_learning_phase(0)
        inference_model = create_model(num_classes=num_classes)
        load_model()

        # Find output nodes
        outputs, output_node_list = get_nodes_from_model(inference_model.outputs)
        # find input nodes
        inputs, input_node_list = get_nodes_from_model(inference_model.inputs)

        generate_config()

        with sess.as_default():
            freeze_var_names = list(set(v.op.name for v in tf.global_variables()).difference(None or []))
            output_names = output_node_list or []
            output_names += [v.op.name for v in tf.global_variables()]
            input_graph_def = graph.as_graph_def()
            for node in input_graph_def.node:
                # print(node.name)
                node.device = ""
            frozen_graph = tf.compat.v1.graph_util.convert_variables_to_constants(
                sess, input_graph_def, output_names, freeze_var_names)
            trt_graph = trt.create_inference_graph(
                # frozen model
                input_graph_def=frozen_graph,
                outputs=output_node_list,
                # specify the max workspace
                max_workspace_size_bytes=500000000,
                # precision, can be "FP32" (32 floating point precision) or "FP16"
                precision_mode=precision,
                is_dynamic_op=True)
            # Finally we serialize and dump the output graph to the filesystem
            with tf.gfile.GFile(model_save_path, 'wb') as f:
                f.write(trt_graph.SerializeToString())

            print("TensorRT model is successfully stored! \n")

is_dynamic_op=True 已经帮助转换了模型(它现在说它已成功存储),但我仍然无法将它加载到 docker TensorRT 服务器中。

我正在使用 nvcr.io/nvidia/tensorflow:19.10-py3 容器来转换模型和用于 TensorRT 服务器的 nvcr.io/nvidia/tensorrtserver:19.10-py3 容器。

【问题讨论】:

    标签: tensorflow cpu tensorrt


    【解决方案1】:

    只是不要将您的模型转换为 TensorRT。

    with tf.Graph().as_default():
        with tf.Session() as sess:
            graph = sess.graph
            K.set_session(sess)
            K.set_learning_phase(0)
            inference_model = create_model(num_classes=num_classes)
            load_model()
    
            # Find output nodes
            outputs, output_node_list = get_nodes_from_model(inference_model.outputs)
            # find input nodes
            inputs, input_node_list = get_nodes_from_model(inference_model.inputs)
    
            generate_config()
    
            with sess.as_default():
                freeze_var_names = list(set(v.op.name for v in tf.global_variables()).difference(None or []))
                output_names = output_node_list or []
                output_names += [v.op.name for v in tf.global_variables()]
                input_graph_def = graph.as_graph_def()
                for node in input_graph_def.node:
                    # print(node.name)
                    node.device = ""
                frozen_graph = tf.compat.v1.graph_util.convert_variables_to_constants(
                    sess, input_graph_def, output_names, freeze_var_names)
    
                # Finally we serialize and dump the output graph to the filesystem
                with tf.gfile.GFile(model_save_path, 'wb') as f:
                    f.write(frozen_graph.SerializeToString())
    
    

    【讨论】:

    • 这很容易。我在任何地方都找不到。非常感谢您的回答!这节省了我很多时间,因为我现在可以在没有良好 gpu 的机器上进行开发,尤其是调试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-15
    • 1970-01-01
    • 1970-01-01
    • 2019-10-23
    相关资源
    最近更新 更多