tf.Graph.name_scope

tf.Graph.name_scope(name)

返回一个上下文管理器,为操作创建一个层级的name

一个图维持一个name scope的栈。name_scope(...):声明将一个新的name放入context生命周期的一个栈中。

name参数如下:

  • 一个字符串(不以'/' 结尾)将穿件一个新scope name,在这个scope中,上下文中所创建的操作前都加上name这个前缀。如果name之前用过,将调用self.unique_name(name) 确定调用一个唯一的name
  • 使用g.name_scope(...)捕获之前的scope作为scope:声明将被视为一个“全局”的scope name,这允许重新进入一个已经存在的scope
  • None或空字符串将会重置当前的scope name 为最高级(空)的name scope
#!/user/bin/env python
# coding=utf-8

import tensorflow as tf

with tf.Graph().as_default() as g:
    c=tf.constant(5.0,name="c")
    assert c.op.name=="c"
    c_1=tf.constant(6.0,name="c")
    assert c_1.op.name=="c_1"

    #creates a scop called "nested"
    with g.name_scope("nested") as scope:
        nested_c=tf.constant(10.0,name="c")
        assert nested_c.op.name=="nested/c"
        
        #creates a nested scope called "inner"
        with g.name_scope("inner"):
            nested_inner_c=tf.constant(20.0,name="c")
            assert nested_inner_c.op.name=="nested/inner/c"

        #creates a nested scope called "inner_1"
        with g.name_scope("inner"):
            nested_inner_1_c=tf.constant(30.0,name="c")
            assert nested_inner_1_c.op.name=="nested/inner_1/c"

            #treats 'scope' as an absolute name scope, and 
            #switches to the "nested/" scope.
            with g.name_scope(scope):
                nested_d=tf.constant(40.0,name="d")
                assert nested_d.op.name=="nested/d"

                with g.name_scope(""):
                    e=tf.constant(50.0,name="e")
                    assert e.op.name=="e"
scope自身的name可以被g.name_scope(...)捕获作为一个scope:,把scope name存储到变量scope中。这个值可以用于命名一个操作的最终结果,例如:
inputs=tf.constant(...)
with g.name_scope('my_layer') as scope:
    weights=tf.Variable(...,name="weights")
    biases=tf.Variable(...,name="biases")
    affine=tf.matmul(inputs,weights)+biases
    output=tf.nn.relu(affine,name=scope)






















def test(checkpoint_dir, style_name, test_dir, if_adjust_brightness, img_size=[256,256]): # tf.reset_default_graph() result_dir = 'results/'+style_name check_folder(result_dir) test_files = glob('{}/*.*'.format(test_dir)) test_real = tf.placeholder(tf.float32, [1, None, None, 3], name='test') with tf.variable_scope("generator", reuse=False): test_generated = generator.G_net(test_real).fake saver = tf.train.Saver() gpu_options = tf.GPUOptions(allow_growth=True) with tf.Session(config=tf.ConfigProto(allow_soft_placement=True, gpu_options=gpu_options)) as sess: # tf.global_variables_initializer().run() # load model ckpt = tf.train.get_checkpoint_state(checkpoint_dir) # checkpoint file information if ckpt and ckpt.model_checkpoint_path: ckpt_name = os.path.basename(ckpt.model_checkpoint_path) # first line saver.restore(sess, os.path.join(checkpoint_dir, ckpt_name)) print(" [*] Success to read {}".format(os.path.join(checkpoint_dir, ckpt_name))) else: print(" [*] Failed to find a checkpoint") return # stats_graph(tf.get_default_graph()) begin = time.time() for sample_file in tqdm(test_files) : # print('Processing image: ' + sample_file) sample_image = np.asarray(load_test_data(sample_file, img_size)) image_path = os.path.join(result_dir,'{0}'.format(os.path.basename(sample_file))) fake_img = sess.run(test_generated, feed_dict = {test_real : sample_image}) if if_adjust_brightness: save_images(fake_img, image_path, sample_file) else: save_images(fake_img, image_path, None) end = time.time() print(f'test-time: {end-begin} s') print(f'one image test time : {(end-begin)/len(test_files)} s'什么意思
06-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值