tensorflow 加载多个pb模型节点冲突解决

           使用多个pb模型时,有时会遇到模型间节点相同的情况,造成图加载冲突,无法输出正确结果,更改节点信息较为麻烦,因此创建新图来解决

 下面是直接使用默认图tf.get_default_graph(), 如果模型间无冲突可使用。

    def __init__(self, graph_path, target_size=(300, 300), confidence_thresh=0.9):
        self.target_size = target_size
        self.confidence_threshold = confidence_thresh
        # load graph
        with tf.gfile.FastGFile(graph_path, 'rb') as f:
            graph_def = tf.GraphDef()
            graph_def.ParseFromString(f.read())
            _ = tf.import_graph_def(graph_def, name='')

        self.graph = tf.get_default_graph()
        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.03)
        self.fire_Detecction_sess = tf.Session(graph=self.graph, config=tf.ConfigProto(gpu_options=gpu_options))

        self.input = self.fire_Detecction_sess.graph.get_tensor_by_name("input_1:0")
        self.output = self.fire_Detecction_sess.graph.get_tensor_by_name("output_1:0")

 下面是模型间有节点同名冲突时,可用tf.Graph()为当前模型创建一个新图来解决问题。

    def __init__(self, graph_path, target_size=(300, 300), confidence_thresh=0.9):
        self.target_size = target_size
        self.confidence_threshold = confidence_thresh

        self.graph = tf.Graph()
        self.graph_def = tf.GraphDef()
        with tf.gfile.FastGFile(graph_path, 'rb') as f:
            self.graph_def.ParseFromString(f.read())
        with self.graph.as_default():
            tf.import_graph_def(self.graph_def, name='')

        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.03)
        self.fire_Detecction_sess = tf.Session(graph=self.graph, config=tf.ConfigProto(gpu_options=gpu_options))

        self.input = self.fire_Detecction_sess.graph.get_tensor_by_name("input_1:0")
        self.output = self.fire_Detecction_sess.graph.get_tensor_by_name("output_1:0")

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值