tensorflow-ckpt转savemode记录

场景

我有一个tf1.X训练输出的ckpt格式的模型,需要转换为onnx格式。
直接用tf2onnx转onnx有问题,所以使用savemode作为中间格式,先将ckpt转为savemode,再将savemode转为onnx。

转为savemode的命令

importdocker  tensorflow.compat.v1 as tf
from tensorflow.python.saved_model.signature_def_utils_impl import predict_signature_def
#import tensorflow as tf

import os
tf.disable_eager_execution()

def read_graph_from_ckpt(ckpt_path, out_path ):     
    # 从meta文件加载网络结构
    saver = tf.train.import_meta_graph(ckpt_path+'.meta',clear_devices=True)
    graph = tf.get_default_graph()
    with tf.Session( graph=graph) as sess:
        sess.run(tf.global_variables_initializer()) 
        # 从ckpt加载参数
        saver.restore(sess, ckpt_path) 
        input_tf =graph.get_tensor_by_name('image_batch:0') 
        output_tf =graph.get_tensor_by_name('Logits_out/output:0') 
        
        #输入输出签名
        signature = predict_signature_def(inputs={'image_batch': input_tf},
                                  outputs={'Logits_out/output': output_tf})

        builder = tf.saved_model.builder.SavedModelBuilder(out_path)
        builder.add_meta_graph_and_variables(sess, ['serve'], strip_default_attrs=True, signature_def_map={'predict': signature})
        builder.save()
     
read_graph_from_ckpt('./tensorflow_landmark/model-tmp.ckpt', './saved_model')

转为onnx的命令

python3 -m tf2onnx.convert --saved-model ./saved_model --opset 10 --output tf_face_landmark.onnx

遇到的错误

MetaGraphDef associated with tags ‘serve’ could not be found in SavedModel. To inspect available tag-sets in the SavedModel, please use the SavedModel CLI: saved_model_cli

调用tf2onnx.convert时我没指定–tag参数,所以默认的tag是“serve”。我原始的savemode转换代码没里也没加tag,所以就匹配不到。
解决方法在上面代码里,就是在add_meta_graph_and_variables第二个参数里指定“serve”为tag.

No signatures found in model. Try --concrete_function instead.

没有输入输出的签名,也就是没指定输入输出的节点。
解决方法就在上面代码里,就是增加signature_def_map这个参数。

吐槽下tensorflow

我在tf2.0里好用的代码,在tf2.2就出错了。兼容性真实一言难尽。
搞个转换onnx的活,tf2.X的环境下怎么都不行,我后来安装了一个py36+tf1.15+tf2onnx的环境才走通全流程。

参考资料

将tensorflow 1.x & 2.x转化成onnx文件(以arcface-tf2人脸识别模型为例)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值