【问题标题】:Migrating .pb from Tensorflow 1.14 to 2.0将 .pb 从 Tensorflow 1.14 迁移到 2.0
【发布时间】:2019-10-17 04:51:15
【问题描述】:
我正在尝试在 Tensorflow 2.0 中运行 FaceNet 模型。我下载了一组预训练的权重(.pb 文件),并用于使用 tf.GraphDef() 在 TF 1.14 中加载图形。
我想知道我应该如何在 TF 2.0 中运行它:
我尝试使用 tf.SavedModel.load() 方法,但这会返回一个空的签名字典。
是否可以在新的 Tensorflow 2.0 版本中重用 TF 1.x pb 文件?如果是这样怎么办?
【问题讨论】:
标签:
python
tensorflow
tensorflow2.0
【解决方案1】:
好吧,根据这个:https://github.com/tensorflow/community/blob/master/sigs/testing/faq.md 似乎是
“从 TensorFlow 1.x 模型生成的冻结图不会在 TF 2.0 中工作”
我确实找到了将模型转换为已保存模型的方法:
class Net(object):
def __init__(self, model_path):
self.model_filepath = model_path
self.load_graph(model_filepath=self.model_filepath)
def load_graph(self, model_filepath):
print("Loading model...")
with tf.gfile.GFile(model_filepath, 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
with tf.Session() as sess:
with tf.Graph().as_default() as graph:
tf.import_graph_def(graph_def, name='')
signature = tf.saved_model.signature_def_utils.predict_signature_def(
inputs={'image_batch':graph.get_tensor_by_name('image_batch:0'),
'phase_train': graph.get_tensor_by_name('phase_train:0')},
outputs={'embeddings': graph.get_tensor_by_name('embeddings:0')}
)
builder = tf.saved_model.builder.SavedModelBuilder("./Output/")
builder.add_meta_graph_and_variables(
sess=sess,
tags=[tf.saved_model.tag_constants.SERVING],
signature_def_map={'serving_default': signature}
)
builder.save()
Net(path_to_pb_file)