在Tensorflow对象检测API中顺序导出两个不同的推理图时出错

Shubham

我正在使用Tensorflow对象检测API,并且已经训练了两个单独的模型(FRCNN Inception V2和SSD Mobilenet V2)。在我的代码流中,当两个模型都经过训练后,我需要导出推理图。以下是相同的代码:

# Dependencies
import tensorflow as tf
import glob
import os
import re

from google.protobuf import text_format
from object_detection import exporter
from object_detection.protos import pipeline_pb2

class ExportInferenceGraph():

    def __init__(self):

        # input parameters for exporter
        self.pipeline_config_path = None
        self.trained_checkpoint_prefix = None
        self.output_directory = None


        #############################################################################
        '''
        code used form export_inference_graph.py file from Tensorflow github
        '''
        #############################################################################

        flags = tf.app.flags

        flags.DEFINE_string('input_type', 'image_tensor', 'Type of input node. Can be '
                            'one of [`image_tensor`, `encoded_image_string_tensor`, '
                            '`tf_example`]')
        flags.DEFINE_string('input_shape', None,
                            'If input_type is `image_tensor`, this can explicitly set '
                            'the shape of this input tensor to a fixed size. The '
                            'dimensions are to be provided as a comma-separated list '
                            'of integers. A value of -1 can be used for unknown '
                            'dimensions. If not specified, for an `image_tensor, the '
                            'default shape will be partially specified as '
                            '`[None, None, None, 3]`.')
        flags.DEFINE_string('config_override', '',
                            'pipeline_pb2.TrainEvalPipelineConfig '
                            'text proto to override pipeline_config_path.')
        flags.DEFINE_boolean('write_inference_graph', False,
                            'If true, writes inference graph to disk.')
        self.FLAGS = flags.FLAGS

        #############################################################################



    # method to get latest checkpoint files
    def get_latest_checkpoints(self, trainingDir):

        # getting list of all meta files
        metaFiles = glob.glob(trainingDir + '/*.meta')

        tempList = []
        # sorting based on num_steps
        for _file in metaFiles:
            tempList.append(int(re.findall(r'[0-9]+', os.path.basename(_file))[0]))

        tempList.sort(reverse = True)

        # returning path of latest checkpoint file
        return trainingDir + '/model.ckpt-' + str(tempList[0])


    # parsing flags and exporting graphs
    def export(self, pipeline_config_path, trained_checkpoint_dir, output_directory):

        # path to store exported inference graphs
        if not os.path.exists(output_directory):
            os.makedirs(output_directory)

        # getting latest checkpoint file
        self.trained_checkpoint_prefix = self.get_latest_checkpoints(trained_checkpoint_dir)

        print(self.trained_checkpoint_prefix)

        self.pipeline_config_path = pipeline_config_path
        self.output_directory = output_directory


        #############################################################################
        '''
        code used form export_inference_graph.py file from Tensorflow
        '''
        #############################################################################

        pipeline_config = pipeline_pb2.TrainEvalPipelineConfig()

        with tf.gfile.GFile(self.pipeline_config_path, 'r') as f:
            text_format.Merge(f.read(), pipeline_config)
        text_format.Merge(self.FLAGS.config_override, pipeline_config)
        if self.FLAGS.input_shape:
            input_shape = [
                int(dim) if dim != '-1' else None
                for dim in self.FLAGS.input_shape.split(',')
            ]
        else:
            input_shape = None

        exporter.export_inference_graph(
            self.FLAGS.input_type, pipeline_config, self.trained_checkpoint_prefix,
            self.output_directory, input_shape = input_shape,
            write_inference_graph = self.FLAGS.write_inference_graph)

        #############################################################################

这是代码片段,显示了如何调用两个不同模型的方法:

export = ExportInferenceGraph()

export.export(pipeline_config_path = TRAIN_CONFIG_FILES[0], 
                trained_checkpoint_dir = MODEL_TRAIN_DIRS[0], 
                output_directory = INFERENCE_SUB_DIRS[0])

export.export(pipeline_config_path = TRAIN_CONFIG_FILES[1], 
                trained_checkpoint_dir = MODEL_TRAIN_DIRS[1], 
                output_directory = INFERENCE_SUB_DIRS[1])

但是我收到以下错误:

2020-01-22 17:42:47.520891: W tensorflow/core/framework/op_kernel.cc:1502] OP_REQUIRES failed at save_restore_v2_ops.cc:184 : Not 
found: Key BoxPredictor_0/BoxEncodingPredictor/biases not found in checkpoint
Traceback (most recent call last):
  File "C:\Users\Lenovo\Anaconda3\envs\wsp\lib\site-packages\tensorflow\python\client\session.py", line 1356, in _do_call
    return fn(*args)
  File "C:\Users\Lenovo\Anaconda3\envs\wsp\lib\site-packages\tensorflow\python\client\session.py", line 1341, in _run_fn
    options, feed_dict, fetch_list, target_list, run_metadata)
  File "C:\Users\Lenovo\Anaconda3\envs\wsp\lib\site-packages\tensorflow\python\client\session.py", line 1429, in _call_tf_sessionrun
    run_metadata)
tensorflow.python.framework.errors_impl.NotFoundError: Key BoxPredictor_0/BoxEncodingPredictor/biases not found in checkpoint     
         [[{{node save_1/RestoreV2}}]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Lenovo\Anaconda3\envs\wsp\lib\site-packages\tensorflow\python\training\saver.py", line 1286, in restore
    {self.saver_def.filename_tensor_name: save_path})
  File "C:\Users\Lenovo\Anaconda3\envs\wsp\lib\site-packages\tensorflow\python\client\session.py", line 950, in run
    run_metadata_ptr)
  File "C:\Users\Lenovo\Anaconda3\envs\wsp\lib\site-packages\tensorflow\python\client\session.py", line 1173, in _run
    feed_dict_tensor, options, run_metadata)
  File "C:\Users\Lenovo\Anaconda3\envs\wsp\lib\site-packages\tensorflow\python\client\session.py", line 1350, in _do_run
    run_metadata)
  File "C:\Users\Lenovo\Anaconda3\envs\wsp\lib\site-packages\tensorflow\python\client\session.py", line 1370, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.NotFoundError: Key BoxPredictor_0/BoxEncodingPredictor/biases not found in checkpoint     
         [[node save_1/RestoreV2 (defined at ../TensorFlow/models/research\object_detection\exporter.py:323) ]]

Original stack trace for 'save_1/RestoreV2':
  File "ocr_train.py", line 99, in <module>
    output_directory = INFERENCE_SUB_DIRS[1])
  File "D:\shubham\wsp\OCR\export_inference_graph.py", line 109, in export
    write_inference_graph = self.FLAGS.write_inference_graph)
  File "../TensorFlow/models/research\object_detection\exporter.py", line 489, in export_inference_graph
    write_inference_graph=write_inference_graph)
  File "../TensorFlow/models/research\object_detection\exporter.py", line 418, in _export_inference_graph
    trained_checkpoint_prefix=checkpoint_to_use)
  File "../TensorFlow/models/research\object_detection\exporter.py", line 323, in write_graph_and_checkpoint
    tf.import_graph_def(inference_graph_def, name='')
  File "C:\Users\Lenovo\Anaconda3\envs\wsp\lib\site-packages\tensorflow\python\util\deprecation.py", line 507, in new_func        
    return func(*args, **kwargs)
  File "C:\Users\Lenovo\Anaconda3\envs\wsp\lib\site-packages\tensorflow\python\framework\importer.py", line 443, in import_graph_def
    _ProcessNewOps(graph)
  File "C:\Users\Lenovo\Anaconda3\envs\wsp\lib\site-packages\tensorflow\python\framework\importer.py", line 236, in _ProcessNewOps
    for new_op in graph._add_new_tf_operations(compute_devices=False):  # pylint: disable=protected-access
  File "C:\Users\Lenovo\Anaconda3\envs\wsp\lib\site-packages\tensorflow\python\framework\ops.py", line 3751, in _add_new_tf_operations
    for c_op in c_api_util.new_tf_operations(self)
  File "C:\Users\Lenovo\Anaconda3\envs\wsp\lib\site-packages\tensorflow\python\framework\ops.py", line 3751, in <listcomp>        
    for c_op in c_api_util.new_tf_operations(self)
  File "C:\Users\Lenovo\Anaconda3\envs\wsp\lib\site-packages\tensorflow\python\framework\ops.py", line 3641, in _create_op_from_tf_operation
    ret = Operation(c_op, self)
  File "C:\Users\Lenovo\Anaconda3\envs\wsp\lib\site-packages\tensorflow\python\framework\ops.py", line 2005, in __init__
    self._traceback = tf_stack.extract_stack()


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Lenovo\Anaconda3\envs\wsp\lib\site-packages\tensorflow\python\training\saver.py", line 1296, in restore
    names_to_keys = object_graph_key_mapping(save_path)
  File "C:\Users\Lenovo\Anaconda3\envs\wsp\lib\site-packages\tensorflow\python\training\saver.py", line 1614, in object_graph_key_mapping
    object_graph_string = reader.get_tensor(trackable.OBJECT_GRAPH_PROTO_KEY)
  File "C:\Users\Lenovo\Anaconda3\envs\wsp\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 678, in get_tensor
    return CheckpointReader_GetTensor(self, compat.as_bytes(tensor_str))
tensorflow.python.framework.errors_impl.NotFoundError: Key _CHECKPOINTABLE_OBJECT_GRAPH not found in checkpoint

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "ocr_train.py", line 99, in <module>
    output_directory = INFERENCE_SUB_DIRS[1])
  File "D:\shubham\wsp\OCR\export_inference_graph.py", line 109, in export
    write_inference_graph = self.FLAGS.write_inference_graph)
  File "../TensorFlow/models/research\object_detection\exporter.py", line 489, in export_inference_graph
    write_inference_graph=write_inference_graph)
  File "../TensorFlow/models/research\object_detection\exporter.py", line 418, in _export_inference_graph
    trained_checkpoint_prefix=checkpoint_to_use)
  File "../TensorFlow/models/research\object_detection\exporter.py", line 327, in write_graph_and_checkpoint
    saver.restore(sess, trained_checkpoint_prefix)
  File "C:\Users\Lenovo\Anaconda3\envs\wsp\lib\site-packages\tensorflow\python\training\saver.py", line 1302, in restore
    err, "a Variable name or other graph key that is missing")
tensorflow.python.framework.errors_impl.NotFoundError: Restoring from checkpoint failed. This is most likely due to a Variable name or other graph key that is missing from the checkpoint. Please ensure that you have not altered the graph expected based on the 
checkpoint. Original error:

Key BoxPredictor_0/BoxEncodingPredictor/biases not found in checkpoint
         [[node save_1/RestoreV2 (defined at ../TensorFlow/models/research\object_detection\exporter.py:323) ]]

Original stack trace for 'save_1/RestoreV2':
  File "ocr_train.py", line 99, in <module>
    output_directory = INFERENCE_SUB_DIRS[1])
  File "D:\shubham\wsp\OCR\export_inference_graph.py", line 109, in export
    write_inference_graph = self.FLAGS.write_inference_graph)
  File "../TensorFlow/models/research\object_detection\exporter.py", line 489, in export_inference_graph
    write_inference_graph=write_inference_graph)
  File "../TensorFlow/models/research\object_detection\exporter.py", line 418, in _export_inference_graph
    trained_checkpoint_prefix=checkpoint_to_use)
  File "../TensorFlow/models/research\object_detection\exporter.py", line 323, in write_graph_and_checkpoint
    tf.import_graph_def(inference_graph_def, name='')
  File "C:\Users\Lenovo\Anaconda3\envs\wsp\lib\site-packages\tensorflow\python\util\deprecation.py", line 507, in new_func        
    return func(*args, **kwargs)
  File "C:\Users\Lenovo\Anaconda3\envs\wsp\lib\site-packages\tensorflow\python\framework\importer.py", line 443, in import_graph_def
    _ProcessNewOps(graph)
  File "C:\Users\Lenovo\Anaconda3\envs\wsp\lib\site-packages\tensorflow\python\framework\importer.py", line 236, in _ProcessNewOps
    for new_op in graph._add_new_tf_operations(compute_devices=False):  # pylint: disable=protected-access
  File "C:\Users\Lenovo\Anaconda3\envs\wsp\lib\site-packages\tensorflow\python\framework\ops.py", line 3751, in _add_new_tf_operations
    for c_op in c_api_util.new_tf_operations(self)
  File "C:\Users\Lenovo\Anaconda3\envs\wsp\lib\site-packages\tensorflow\python\framework\ops.py", line 3751, in <listcomp>        
    for c_op in c_api_util.new_tf_operations(self)
  File "C:\Users\Lenovo\Anaconda3\envs\wsp\lib\site-packages\tensorflow\python\framework\ops.py", line 3641, in _create_op_from_tf_operation
    ret = Operation(c_op, self)
  File "C:\Users\Lenovo\Anaconda3\envs\wsp\lib\site-packages\tensorflow\python\framework\ops.py", line 2005, in __init__
    self._traceback = tf_stack.extract_stack()

注意:我可以看到第一个图形成功导出,但是它引发了第二个图形的错误。我调换了导出方法的调用,但在第二次导出时仍然失败。

我还是Tensorflow的新手,这里需要一些帮助。我猜它使用的是它为第一个模型创建的图。

Shubham

我深入研究Tensorflow目录,并到达_export_inference_graph方法路径是TensorFlow / models / research / object_detection / exporter.py在函数末尾添加此行解决了我的问题。

tf.reset_default_graph()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

修改并组合使用tensorflow对象检测API生成的两个不同的冻结图以进行推理

在Tensorlfow对象检测API中导出推理图时出错

对象检测:导出/导入推理时出错

导出推理图时出错(ValueError)

比较两个对象中的vlaues时出错

如何在Tensorflow对象检测API中使用两个模型

使用JavaScript中的循环从一个对象创建两个对象时出错

在 R 中绘制两个函数的两条不同曲线时出错

使用两个Tensorflow模型进行推理

Tensorflow对象检测API中的图验证损失

难道==比较两个基本VS Java中两个对象时实际工作相同或不同?

合并顺序不同的两个文件中的数据。

在R中合并两个不同的图

实现 tensorflow 对象检测模型时出错

在Swift中检测两个对象之间的碰撞

在 Tensorflow 中导出推理图时出现“解析输入...形状不完整”错误

从两个不同数组中的对象创建对象

使用AngularFirestore中来自两个不同集合的数据构建对象时遇到麻烦

将Tensorflow冻结的推理图加载到OpenCV DNN时出错

比较两个Tensorflow图

在tensorflow中添加两个不同维度的张量

为什么回读时写入两个不同流的单个对象代表两个不同的对象?

导出推理图后,ssd_mobilenet_v2_coco无法检测到自定义训练对象

R中两个不同对象签名的setMethod

枚举 Dart 中两个对象的哪些字段不同

合并不同数组中的两个对象

合并两个训练有素的网络以进行顺序推理

使用fast_rcnn_inception_v2_pets模型运行tensorflow对象检测api时出错

如何以交替顺序排列两个不同对象列表的 XMLArray 项目?