slim.arg_scope()

slim.arg_scope()

slim.arg_scope可以定义一些函数的默认参数值,在scope内,我们重复用到这些函数时可以不用把所有参数都写一遍,注意它没有tf.variable_scope()划分图结构的功能,

1

2

3

4

5

6

7

8

9

10

11

12

with slim.arg_scope([slim.conv2d, slim.fully_connected],

                    trainable=True,

                    activation_fn=tf.nn.relu,

                    weights_initializer=tf.truncated_normal_initializer(stddev=0.01),

                    weights_regularizer=slim.l2_regularizer(0.0001)):

    with slim.arg_scope([slim.conv2d],

                        kernel_size=[3, 3],

                        padding='SAME',

                        normalizer_fn=slim.batch_norm):

        net = slim.conv2d(net, 64, scope='conv1'))

        net = slim.conv2d(net, 128, scope='conv2'))

        net = slim.conv2d(net, 256, [5, 5], scope='conv3'))

slim.arg_scope的用法基本都体现在上面了。一个slim.arg_scope内可以用list来同时定义多个函数的默认参数(前提是这些函数都有这些参数),另外,slim.arg_scope也允许相互嵌套。在其中调用的函数,可以不用重复写一些参数(例如kernel_size=[3, 3]),但也允许覆盖(例如最后一行,卷积核大小为[5,5])。
另外,还可以把这么多scope封装成函数:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

def new_arg_sc():

    with slim.arg_scope([slim.conv2d, slim.fully_connected],

                        trainable=True,

                        activation_fn=tf.nn.relu,

                        weights_initializer=tf.truncated_normal_initializer(stddev=0.01),

                        weights_regularizer=slim.l2_regularizer(0.0001)):

        with slim.arg_scope([slim.conv2d],

                            kernel_size=[3, 3],

                            padding='SAME',

                            normalizer_fn=slim.batch_norm) as sc:

            return sc

 

def main():

    ......

    with slim.arg_scope(new_arg_sc()):

        ......

 

slim.utils.collect_named_outputs()

将变量取个别名,并收集到collection中

1

net = slim.utils.collect_named_outputs(outputs_collections,sc.name,net)

 参数意义如下,

return:这个方法会返回本次添加的tensor对象,
参数二:意义是为tensor添加一个别名,并收集进collections中
查看源码可见实现如下

if collections:
    append_tensor_alias(outputs,alias)
    ops.add_to_collections(collections,outputs)
return outputs

据说本方法位置已经被转移到这里了,
from tensorflow.contrib.layers.python.layers import utils
utils.collect_named_outputs()

 

slim.utils.convert_collection_to_dict()

1

2

3

4

5

6

#集合转换为字典,{节点名:输出张量值}

end_points = slim.utils.convert_collection_to_dict(end_points_collection)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值