tensorflow checkpoint模型文件和pb文件加载,这样最好,预测时间短,不会泄露内存

背景:tensorflow需要加载checkpoint文件模型或者pb格式模型。

问题:怎么加载保存的checkpoint并预测,加载pb格式文件加载时多次预测会内存泄漏。

解决方法

1、checkpoint格式加载与预测

# 加载
 self.graph = tf.Graph()  # 为每个类(实例)单独创建一个graph
        with self.graph.as_default():
            self.check_point_file = tf.train.latest_checkpoint(self.model_path)
            self.saver = tf.train.import_meta_graph("{}.meta".format(self.check_point_file))  # 创建恢复器

            # 注意!恢复器必须要在新创建的图里面生成,否则会出错。
            self.sess = tf.Session()
            with self.sess.as_default():
                self.saver.restore(self.sess, self.check_point_file)

# 预测
def predict():
   with self.graph.as_default():
            with self.sess.as_default():
                input_x = self.graph.get_tensor_by_name("input_x:0")
                input_y = self.graph.get_tensor_by_name("input_y:0")
                q_y_raw = self.graph.get_tensor_by_name("representation/q_y_raw:0")
                qs_y_raw = self.graph.get_tensor_by_name("representation/qs_y_raw:0")
                qs_y_raw_out = self.sess.run(qs_y_raw, feed_dict={input_y: np.array(input_y_value, dtype=np.int32)})

2、pb格式 加载与预测

# 加载 
self.graph = tf.Graph()  # 为每个类(实例)单独创建一个graph
        with self.graph.as_default():
            self.sess = tf.Session(graph=self.graph)
            with self.sess.as_default():
                output_graph_def = tf.GraphDef()
                pb_path = wenlp_configs["sentence_matcher"]["pb_model_path"]
                with open(pb_path, "rb") as f:
                    output_graph_def.ParseFromString(f.read())
                    tf.import_graph_def(output_graph_def, name="")

# 预测
        with tf.Session(graph=self.graph) as self.sess:
            input_x = self.graph.get_tensor_by_name("input_x:0")
            input_y = self.graph.get_tensor_by_name("input_y:0")
            q_y_raw = self.graph.get_tensor_by_name("representation/q_y_raw:0")
            qs_y_raw = self.graph.get_tensor_by_name("representation/qs_y_raw:0")
            qs_y_raw_out = self.sess.run(qs_y_raw, feed_dict={input_y: np.array(input_y_value, dtype=np.int32)})

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ai君臣

学会的就要教给人

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值