如何在不丢失 metagraphdef 的情况下将 .meta .index 和 .data 文件转换为 SavedModel (.pb) 格式?
How do I convert a .meta .index and .data file into SavedModel (.pb) format without losing metagraphdef?
我正在尝试转换预训练模型的这三个文件:
- semantic_model.data-00000-of-00001
- semantic_model.index
- semantic_model.meta
转换为 Saved Model 格式,以便我稍后可以将其转换为 TFLite 格式以进行推理。
在搜索 Whosebug 时,我遇到了这段代码,它正确地生成了 Saved_model.pb,但是正如某些评论中指出的那样,以这种方式执行此操作不会保留元图定义,这会在我稍后尝试时导致错误将其转换为 TFlite 格式或将其冻结。
import os
import tensorflow.compat.v1 as tf
tf.compat.v1.disable_eager_execution()
export_dir = '/tf-end-to-end/export_dir'
#trained_checkpoint_prefix = 'Models/semantic_model' \tf-end-to-end\Models
trained_checkpoint_prefix = 'PATH TO MODEL DIRECTORY'
tf.reset_default_graph()
graph = tf.Graph()
loader = tf.train.import_meta_graph(trained_checkpoint_prefix + ".meta" )
sess = tf.Session()
loader.restore(sess,trained_checkpoint_prefix)
builder = tf.saved_model.builder.SavedModelBuilder(export_dir)
builder.add_meta_graph_and_variables(sess, [tf.saved_model.tag_constants.TRAINING, tf.saved_model.tag_constants.SERVING], strip_default_attrs=True)
builder.save()
这是我在尝试使用 saved_model:
时遇到的错误
RuntTimeError: MetaGraphDef associated with tags {'serve'} could not be found in SavedModel
运行 showsavedmodelcli --all
不会在创建的 saved_model.
的签名定义下显示任何内容
我的问题是,如何维护数据并将其转换为 saved_model,以便稍后转换为 TFLite 格式?
模型结构和创建细节可以在这里看到,包括提到的检查点文件:https://github.com/OMR-Research/tf-end-to-end
我正在尝试转换预训练模型的这三个文件:
- semantic_model.data-00000-of-00001
- semantic_model.index
- semantic_model.meta
转换为 Saved Model 格式,以便我稍后可以将其转换为 TFLite 格式以进行推理。 在搜索 Whosebug 时,我遇到了这段代码,它正确地生成了 Saved_model.pb,但是正如某些评论中指出的那样,以这种方式执行此操作不会保留元图定义,这会在我稍后尝试时导致错误将其转换为 TFlite 格式或将其冻结。
import os
import tensorflow.compat.v1 as tf
tf.compat.v1.disable_eager_execution()
export_dir = '/tf-end-to-end/export_dir'
#trained_checkpoint_prefix = 'Models/semantic_model' \tf-end-to-end\Models
trained_checkpoint_prefix = 'PATH TO MODEL DIRECTORY'
tf.reset_default_graph()
graph = tf.Graph()
loader = tf.train.import_meta_graph(trained_checkpoint_prefix + ".meta" )
sess = tf.Session()
loader.restore(sess,trained_checkpoint_prefix)
builder = tf.saved_model.builder.SavedModelBuilder(export_dir)
builder.add_meta_graph_and_variables(sess, [tf.saved_model.tag_constants.TRAINING, tf.saved_model.tag_constants.SERVING], strip_default_attrs=True)
builder.save()
这是我在尝试使用 saved_model:
时遇到的错误RuntTimeError: MetaGraphDef associated with tags {'serve'} could not be found in SavedModel
运行 showsavedmodelcli --all
不会在创建的 saved_model.
我的问题是,如何维护数据并将其转换为 saved_model,以便稍后转换为 TFLite 格式?
模型结构和创建细节可以在这里看到,包括提到的检查点文件:https://github.com/OMR-Research/tf-end-to-end