slim.arg_scope()的使用
【https://blog.csdn.net/u013921430 转载】
slim是一种轻量级的tensorflow库,可以使模型的构建,训练,测试都变得更加简单。在slim库中对很多常用的函数进行了定义,slim.arg_scope()是slim库中经常用到的函数之一。函数的定义如下;
-
@tf_contextlib.contextmanager
-
def arg_scope(list_ops_or_scope, **kwargs):
-
"""Stores the default arguments for the given set of list_ops.
-
-
For usage, please see examples at top of the file.
-
-
Args:
-
list_ops_or_scope: List or tuple of operations to set argument scope for or
-
a dictionary containing the current scope. When list_ops_or_scope is a
-
dict, kwargs must be empty. When list_ops_or_scope is a list or tuple,
-
then every op in it need to be decorated with @add_arg_scope to work.
-
**kwargs: keyword=value that will define the defaults for each op in
-
list_ops. All the ops need to accept the given set of arguments.
-
-
Yields:
-
the current_scope, which is a dictionary of {op: {arg: value}}
-
Raises:
-
TypeError: if list_ops is not a list or a tuple.
-
ValueError: if any op in list_ops has not be decorated with @add_arg_scope.
-
"""
-
if isinstance(list_ops_or_scope, dict):
-
# Assumes that list_ops_or_scope is a scope that is being reused.
-
if kwargs:
-
raise ValueError('When attempting to re-use a scope by suppling a'
-
'dictionary, kwargs must be empty.')
-
current_scope = list_ops_or_scope.copy()
-
try:
-
_get_arg_stack().append(current_scope)
-
yield current_scope
-
finally:
-
_get_arg_stack().pop()
-
else:
-
# Assumes that list_ops_or_scope is a list/tuple of ops with kwargs.
-
if not isinstance(list_ops_or_scope, (list, tuple)):
-
raise TypeError('list_ops_or_scope must either be a list/tuple or reused'
-
'scope (i.e. dict)')
-
try:
-
current_scope = current_arg_scope().copy()
-
for op in list_ops_or_scope:
-
key_op = _key_op(op)
-
if not has_arg_scope(op):
-
raise ValueError('%s is not decorated with @add_arg_scope',
-
_name_op(op))
-
if key_op in current_scope:
-
current_kwargs = current_scope[key_op].copy()
-
current_kwargs.update(kwargs)
-
current_scope[key_op] = current_kwargs
-
else:
-
current_scope[key_op] = kwargs.copy()
-
_get_arg_stack().append(current_scope)
-
yield current_scope
-
finally:
-
_get_arg_stack().pop()
如注释中所说,这个函数的作用是给list_ops中的内容设置默认值。但是每个list_ops中的每个成员需要用@add_arg_scope修饰才行。所以使用slim.arg_scope()有两个步骤:
- 使用@slim.add_arg_scope修饰目标函数
- 用 slim.arg_scope()为目标函数设置默认参数.
例如如下代码;首先用@slim.add_arg_scope修饰目标函数fun1(),然后利用slim.arg_scope()为它设置默认参数。
-
import tensorflow as tf
-
slim =tf.contrib.slim
-
-
@slim.add_arg_scope
-
def fun1(a=0,b=0):
-
return (a+b)
-
-
with slim.arg_scope([fun1],a=10):
-
x=fun1(b=30)
-
print(x)
运行结果为:
40
平常所用到的slim.conv2d( ),slim.fully_connected( ),slim.max_pool2d( )等函数在他被定义的时候就已经添加了@add_arg_scope。以slim.conv2d( )为例;
-
@add_arg_scope
-
def convolution(inputs,
-
num_outputs,
-
kernel_size,
-
stride=1,
-
padding='SAME',
-
data_format=None,
-
rate=1,
-
activation_fn=nn.relu,
-
normalizer_fn=None,
-
normalizer_params=None,
-
weights_initializer=initializers.xavier_initializer(),
-
weights_regularizer=None,
-
biases_initializer=init_ops.zeros_initializer(),
-
biases_regularizer=None,
-
reuse=None,
-
variables_collections=None,
-
outputs_collections=None,
-
trainable=True,
-
scope=None):
所以,在使用过程中可以直接slim.conv2d( )等函数设置默认参数。例如在下面的代码中,不做单独声明的情况下,slim.conv2d, slim.max_pool2d, slim.avg_pool2d三个函数默认的步长都设为1,padding模式都是'VALID'的。但是也可以在调用时进行单独声明。这种参数设置方式在构建网络模型时,尤其是较深的网络时,可以节省时间。
-
with slim.arg_scope(
-
[slim.conv2d, slim.max_pool2d, slim.avg_pool2d],stride = 1, padding = 'VALID'):
-
net = slim.conv2d(inputs, 32, [3, 3], stride = 2, scope = 'Conv2d_1a_3x3')
-
net = slim.conv2d(net, 32, [3, 3], scope = 'Conv2d_2a_3x3')
-
net = slim.conv2d(net, 64, [3, 3], padding = 'SAME', scope = 'Conv2d_2b_3x3')
@修饰符
其实这种用法是python中常用到的。在python中@修饰符放在函数定义的上方,它将被修饰的函数作为参数,并返回修饰后的同名函数。形式如下;
-
@fun_a #等价于fun_a(fun_b)
-
def fun_b():
这在本质上讲跟直接调用被修饰的函数没什么区别,但是有时候也有用处,例如在调用被修饰函数前需要输出时间信息,我们可以在@后方的函数中添加输出时间信息的语句,这样每次我们只需要调用@后方的函数即可。
-
def funs(fun,factor=20):
-
x=fun()
-
print(factor*x)
-
-
-
@funs #等价funs(add(),fator=20)
-
def add(a=10,b=20):
-
return(a+b)
slim.arg_scope()的使用的更多相关文章
- slim.arg_scope中python技巧
slim.arg_scope函数说明如下: Stores the default arguments for the given set of list_ops. For usage, please ...
- tf.contrib.slim arg_scope
缘由 最近一直在看深度学习的代码,又一次看到了slim.arg_scope()的嵌套使用,具体代码如下: with slim.arg_scope( [slim.conv2d, slim.separab ...
- 使用多块GPU进行训练 1.slim.arg_scope(对于同等类型使用相同操作) 2.tf.name_scope(定义名字的范围) 3.tf.get_variable_scope().reuse_variable(参数的复用) 4.tf.py_func(构造函数)
1. slim.arg_scope(函数, 传参) # 对于同类的函数操作,都传入相同的参数 from tensorflow.contrib import slim as slim import te ...
- 【Tensorflow】slim.arg_scope()的使用
https://blog.csdn.net/u013921430/article/details/80915696
- TensorFlow和最近发布的slim
笔者将和大家分享一个结合了TensorFlow和最近发布的slim库的小应用,来实现图像分类.图像标注以及图像分割的任务,围绕着slim展开,包括其理论知识和应用场景. 之前自己尝试过许多其它的库,比 ...
- 用tensorlayer导入Slim模型迁移学习
上一篇博客[用tensorflow迁移学习猫狗分类]笔者讲到用tensorlayer的[VGG16模型]迁移学习图像分类,那麽问题来了,tensorlayer没提供的模型怎么办呢?别担心,tensor ...
- tf.contrib.slim add_arg_scope
上一篇文章中我们介绍了arg_scope函数,它在每一层嵌套中update当前字典中参数形成新的字典,并入栈.那么这些参数是怎么作用到代码块中的函数的呢?比如说如下情况: with slim.arg_ ...
- 第二十四节,TensorFlow下slim库函数的使用以及使用VGG网络进行预训练、迁移学习(附代码)
在介绍这一节之前,需要你对slim模型库有一些基本了解,具体可以参考第二十二节,TensorFlow中的图片分类模型库slim的使用.数据集处理,这一节我们会详细介绍slim模型库下面的一些函数的使用 ...
- 第二十二节,TensorFlow中的图片分类模型库slim的使用、数据集处理
Google在TensorFlow1.0,之后推出了一个叫slim的库,TF-slim是TensorFlow的一个新的轻量级的高级API接口.这个模块是在16年新推出的,其主要目的是来做所谓的“代码瘦 ...
随机推荐
- 将一个对象赋值给另一个对象(使用element CheckBox中length报错)
注意两个对象相似(比如form表单),千万不要直接赋值(会把对象的属性也变化),很容易漏掉一些属性.比如此次CheckBox报length的错误,就是因为用于存放checkbox复选框选项的数组进过赋 ...
- 如何解决“ VMware Workstation 不可恢复错误: (vcpu-0) vcpu-0:VERIFY vmcore/vmm/main/cpuid.c:386 bugNr=1036521”
第一次装虚拟机,装centos7遇到的坑: 1. 出现 “VMware Workstation 不可恢复错误: (vcpu-0) vcpu-0:VERIFY vmcore/vmm/main/cpuid ...
- dubbo服务调试管理实用命令
公司如果分项目组开发的,各个项目组调用各项目组的接口,有时候需要在联调环境调试对方的接口,可以直接telnet到dubbo的服务通过命令查看已经布的接口和方法,并能直接invoke具体的方法,我们可以 ...
- linux随笔-02
部署虚拟环境安装linux系统以及一些常用命令 工具: VmwareWorkStation 12.0——虚拟机软件(必需) RedHatEnterpriseLinux [RHEL]7.0——红帽操作 ...
- http请求访问响应慢问题解决的基本思路
第一步,检查网络 ping命令检查网络域名解析是否正常,ping服务器的延迟是否过大,如果过大可以检查Ip是否冲突,或者交换机网线是否正常插好,通过nmon还可以查看网络流量,一般用的千兆交换机理论速 ...
- Dubbox管理中心的部署及使用
安装: 我们在开发时,需要知道注册中心都注册了哪些服务,以便我们开发和测试.我们可以通过部署一个管理中心来实现.其实管理中心就是一个web应用,部署到tomcat即可. (1)编译源码,得到war包 ...
- Spring整合Struts2的两种方式
https://blog.csdn.net/cuiyaoqiang/article/details/51887594
- 手写Spring事务框架
Spring事务基于AOP环绕通知和异常通知 编程事务 声明事务 Spring事务底层使用编程事务+AOP进行包装的 = 声明事务 AOP应用场景: 事务 权限 参数验证 什么是AOP技术 AO ...
- Concurrent - 多线程
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11426916.html Java中有几种方法可以实现一个线程? 继承Thread类(不支持多继承) 实 ...
- C++11新特性之 Move semantics(移动语义)
https://blog.csdn.net/wangshubo1989/article/details/49748703 这篇讲到了vector的push_back的两种重载版本,左值版本和右值版本.