有什么方法可以将模型从张量流对象检测api转换为keras模型

时间:2020-09-21 10:22:10

标签: tensorflow2.0 object-detection tf.keras

文档指出keras.model.save("model_name")将以两个文件的形式保存模型(1)saved_model.pb-包含配置,以及(2)variables-包含模型权重。 / p>

从tensorflow的对象检测api下载的模型也具有此文件。因此,当我使用该目录加载模型时,

keras.models.load_model("/content/ssd_mobilenet_v1_fpn_640x640_coco17_tpu-8/saved_model")

我遇到这种错误

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/core.py:1045: UserWarning: google3.third_party.tensorflow.python.ops.nn_ops is not loaded, but a Lambda layer uses it. It may cause errors.
  , UserWarning)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/core.py:1045: UserWarning: google3.third_party.tensorflow_models.object_detection.models.feature_map_generators is not loaded, but a Lambda layer uses it. It may cause errors.
  , UserWarning)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-20-b099c035cea7> in <module>()
----> 1 loaded_model = keras.models.load_model("/content/ssd_mobilenet_v1_fpn_640x640_coco17_tpu-8/saved_model")
      2 # loaded_model = tf.saved_model.load("/content/ssd_mobilenet_v1_fpn_640x640_coco17_tpu-8/saved_model")

8 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/save.py in load_model(filepath, custom_objects, compile, options)
    185     if isinstance(filepath, six.string_types):
    186       loader_impl.parse_saved_model(filepath)
--> 187       return saved_model_load.load(filepath, compile, options)
    188 
    189   raise IOError(

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/saved_model/load.py in load(path, compile, options)
    119 
    120   model = tf_load.load_internal(
--> 121       path, options=options, loader_cls=KerasObjectLoader)
    122 
    123   # pylint: disable=protected-access

/usr/local/lib/python3.6/dist-packages/tensorflow/python/saved_model/load.py in load_internal(export_dir, tags, options, loader_cls)
    631       try:
    632         loader = loader_cls(object_graph_proto, saved_model_proto, export_dir,
--> 633                             ckpt_options)
    634       except errors.NotFoundError as err:
    635         raise FileNotFoundError(

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/saved_model/load.py in __init__(self, *args, **kwargs)
    192     self._models_to_reconstruct = []
    193 
--> 194     super(KerasObjectLoader, self).__init__(*args, **kwargs)
    195 
    196     # Now that the node object has been fully loaded, and the checkpoint has

/usr/local/lib/python3.6/dist-packages/tensorflow/python/saved_model/load.py in __init__(self, object_graph_proto, saved_model_proto, export_dir, ckpt_options)
    128       self._concrete_functions[name] = _WrapperFunction(concrete_function)
    129 
--> 130     self._load_all()
    131     self._restore_checkpoint()
    132 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/saved_model/load.py in _load_all(self)
    219 
    220     # Finish setting up layers and models. See function docstring for more info.
--> 221     self._finalize_objects()
    222 
    223   @property

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/saved_model/load.py in _finalize_objects(self)
    524         layers_revived_from_saved_model.append(node)
    525 
--> 526     _finalize_saved_model_layers(layers_revived_from_saved_model)
    527     _finalize_config_layers(layers_revived_from_config)
    528 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/saved_model/load.py in _finalize_saved_model_layers(layers)
    704         call_fn = _get_keras_attr(layer).call_and_return_conditional_losses
    705         if call_fn.input_signature is None:
--> 706           inputs = infer_inputs_from_restored_call_function(call_fn)
    707         else:
    708           inputs = call_fn.input_signature[0]

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/saved_model/load.py in infer_inputs_from_restored_call_function(fn)
    980     return tensor_spec.TensorSpec(defun.common_shape(x.shape, y.shape),
    981                                   x.dtype, x.name)
--> 982   spec = fn.concrete_functions[0].structured_input_signature[0][0]
    983   for concrete in fn.concrete_functions[1:]:
    984     spec2 = concrete.structured_input_signature[0][0]

IndexError: list index out of range

是否有其他方法可以执行此转换。

Tensorflow版本:2.3.0

model source下载模型

1 个答案:

答案 0 :(得分:1)

您可以尝试在以下这行API之后添加保存方法来还原模型并进行保存:

https://github.com/tensorflow/models/blob/master/research/object_detection/model_lib_v2.py#L583-L584

或者您可以创建一个图形并在会话中还原检查点,然后根据需要在另一个脚本中保存到model.h5