Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
461 views
in Technique[技术] by (71.8m points)

python - Converting TF2 Object detection API model to frozen graph

I trained model ssd_resnet50_v1_fpn_640x640_coco17_tpu-8 using Tensorflow object detection API https://github.com/tensorflow/models/blob/master/research/object_detection/model_main_tf2.py

After I exported it to Save model: .exporter_main_v2.py --input_type image_tensor --pipeline_config_path .modelsmy_ssd_resnet50_v1_fpnpipeline.config --trained_checkpoint_dir .modelsmy_ssd_resnet50_v1_fpn --output_directory .exported-modelsmodelsBel_model using https://github.com/tensorflow/models/blob/master/research/object_detection/exporter_main_v2.py

On this step inference work fine using Tensorflow. Both from the saved model and from checkpoint. This code was used for test inference: https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/_downloads/07fcc19ba03226cd3d83d4e40ec44385/auto_examples_python.zip

After I try to convert saved model to frozen graph to use it in OpenCV using this approach https://github.com/opencv/opencv/issues/16879#issuecomment-603815872

import tensorflow as tf
print(tf.__version__)

from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2

loaded = tf.saved_model.load('models/mnist_test')
infer = loaded.signatures['serving_default']

f = tf.function(infer).get_concrete_function(flatten_input=tf.TensorSpec(shape=[None, 28, 28, 1], dtype=tf.float32))
f2 = convert_variables_to_constants_v2(f)
graph_def = f2.graph.as_graph_def()

# Export frozen graph
with tf.io.gfile.GFile('frozen_graph.pb', 'wb') as f:
   f.write(graph_def.SerializeToString())

Unfortunately on this step I receive error:

Traceback (most recent call last):
  File ".frozen_graph.py", line 8, in <module>
    f = tf.function(infer).get_concrete_function(input_1=tf.TensorSpec(shape=[None, 640, 640, 3], dtype=tf.float32))
  File "C:UsersBleachminiconda3envsTFstdlibsite-packagesensorflowpythoneagerdef_function.py", line 1299, in get_concrete_function
    concrete = self._get_concrete_function_garbage_collected(*args, **kwargs)
  File "C:UsersBleachminiconda3envsTFstdlibsite-packagesensorflowpythoneagerdef_function.py", line 1205, in _get_concrete_function_garbage_collected
    self._initialize(args, kwargs, add_initializers_to=initializers)
  File "C:UsersBleachminiconda3envsTFstdlibsite-packagesensorflowpythoneagerdef_function.py", line 725, in _initialize
    self._stateful_fn._get_concrete_function_internal_garbage_collected(  # pylint: disable=protected-access
  File "C:UsersBleachminiconda3envsTFstdlibsite-packagesensorflowpythoneagerfunction.py", line 2969, in _get_concrete_function_internal_garbage_collected
    graph_function, _ = self._maybe_define_function(args, kwargs)
  File "C:UsersBleachminiconda3envsTFstdlibsite-packagesensorflowpythoneagerfunction.py", line 3361, in _maybe_define_function
    graph_function = self._create_graph_function(args, kwargs)
  File "C:UsersBleachminiconda3envsTFstdlibsite-packagesensorflowpythoneagerfunction.py", line 3196, in _create_graph_function
    func_graph_module.func_graph_from_py_func(
  File "C:UsersBleachminiconda3envsTFstdlibsite-packagesensorflowpythonframeworkfunc_graph.py", line 990, in func_graph_from_py_func
    func_outputs = python_func(*func_args, **func_kwargs)
  File "C:UsersBleachminiconda3envsTFstdlibsite-packagesensorflowpythoneagerdef_function.py", line 634, in wrapped_fn
    out = weak_wrapped_fn().__wrapped__(*args, **kwds)
  File "C:UsersBleachminiconda3envsTFstdlibsite-packagesensorflowpythonframeworkfunc_graph.py", line 977, in wrapper
    raise e.ag_error_metadata.to_exception(e)
TypeError: in user code:

    C:UsersBleachminiconda3envsTFstdlibsite-packagesensorflowpythoneagerfunction.py:1669 __call__  *
        return self._call_impl(args, kwargs)
    C:UsersBleachminiconda3envsTFstdlibsite-packagesensorflowpythoneagerfunction.py:1685 _call_impl  **
        raise structured_err
    C:UsersBleachminiconda3envsTFstdlibsite-packagesensorflowpythoneagerfunction.py:1678 _call_impl
        return self._call_with_structured_signature(args, kwargs,
    C:UsersBleachminiconda3envsTFstdlibsite-packagesensorflowpythoneagerfunction.py:1756 _call_with_structured_signature
        self._structured_signature_check_missing_args(args, kwargs)
    C:UsersBleachminiconda3envsTFstdlibsite-packagesensorflowpythoneagerfunction.py:1775 _structured_signature_check_missing_args
        raise TypeError("{} missing required arguments: {}".format(

    TypeError: signature_wrapper(*, input_tensor) missing required arguments: input_tensor

Please help me resolve this. Maybe you can advise me another approach to creating a frozen graph. Is it possible easier solution to train a model using Keras?

question from:https://stackoverflow.com/questions/65921237/converting-tf2-object-detection-api-model-to-frozen-graph

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...