tensorflow需要在一个程序里面,恢复两张图的权值进行inference,怎么办?

restore two tensorflow graph  from the process

还按照普通的方法,

 with tf.name_scope(‘blabla’):
           with tf.variable_scope(tf.get_variable_scope(), reuse = False):
                #构建图等
                self.image = tf.placeholder(dtype=tf.int32, shape = [None, None, 3])
                config.eval_image_width, 3])
                self.processed_image, _, _, _, _,self.pad_mod = ssd_vgg_preprocessing.preprocess_image(self.image, None, None, None, None, 
                                                            out_shape = config.image_shape,
                                                            data_format = config.data_format, 
                                                            is_training = False)
                
                before_image = tf.cast(self.image, tf.float32)
                before_image = tf.expand_dims(before_image, 0)
                after_image = tf.expand_dims(self.processed_image, 0)
                b_image = tf.expand_dims(self.processed_image, axis = 0)
                # build model and loss
                self.net = pixel_link_symbol.PixelLinkNet(b_image,config=config)
                self.masks = pixel_link.tf_decode_score_map_to_mask_in_batch(
                    self.net.pixel_pos_scores, self.net.link_pos_scores)
#然后,得到变量列表
#这里如果以前构建了多个图,这里将把所有的变量都列出来。因为这里用的是默认GraphKeys.GLOBAL_VARIABLES,这里的所有变量
variables_to_restore = slim.get_variables_to_restore()
saver = tf.train.Saver(var_list = variables_to_restore)
self.sess = tf.Session()
saver.restore(self.sess, checkpath)

那么应该怎么办:

#如果有多个图,就每个使用一个tf.Graph()来包裹
g = tf.Graph()
with g.as_default():
     with tf.name_scope(‘blabla’):
           with tf.variable_scope(tf.get_variable_scope(), reuse = False):
                #构建图等
                self.image = tf.placeholder(dtype=tf.int32, shape = [None, None, 3])
                config.eval_image_width, 3])
                self.processed_image, _, _, _, _,self.pad_mod = ssd_vgg_preprocessing.preprocess_image(self.image, None, None, None, None, 
                                                            out_shape = config.image_shape,
                                                            data_format = config.data_format, 
                                                            is_training = False)
                
                before_image = tf.cast(self.image, tf.float32)
                before_image = tf.expand_dims(before_image, 0)
                after_image = tf.expand_dims(self.processed_image, 0)
                b_image = tf.expand_dims(self.processed_image, axis = 0)
                # build model and loss
                self.net = pixel_link_symbol.PixelLinkNet(b_image,config=config)
                self.masks = pixel_link.tf_decode_score_map_to_mask_in_batch(
                    self.net.pixel_pos_scores, self.net.link_pos_scores)
#然后,得到变量列表
#这里如果以前构建了多个图,这里将把所有的变量都列出来。因为这里用的是默认GraphKeys.GLOBAL_VARIABLES,这里的所有变量
#variables_to_restore = slim.get_variables_to_restore()
variables_to_restore = g.get_collection('variables')
saver = tf.train.Saver(var_list = variables_to_restore)
self.sess = tf.Session(graph=g)#这里要用上面构建的图
saver.restore(self.sess, checkpath)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值