slim库的导入:
import tensorflow as tf
import tensorflow.contrib.slim as slim
常用函数:
与tensorflow自带的函数相比,slim能够让我们不用重复写函数的参数。那么函数的参数写在哪里呢?核心方法就是slim.arg_scope。
slim.arg_scope
def arg_scope(list_ops_or_scope, **kwargs)
list_ops_or_scope:要用的函数的作用域,可以在需要使用的地方用@add_arg_scope 声明
**kwargs: keyword=value 定义了list_ops中要使用的变量
也就是说可以通过这个函数将不想重复写的参数通过这个函数自动赋值。
示例:
import tensorflow.contrib.slim as slim
@slim.add_arg_scope
def hh(name, add_arg):
print("name:", name)
print("add_arg:", add_arg)
with slim.arg_scope([hh], add_arg='this is add'):
hh('test')
#结果:
#name: test
#add_arg: this is add
之后是使用slim构建神经网络常用的函数。
slim.conv2d
slim.conv2d是对tf.conv2d的进一步封装。常见调用方式:
net = slim.conv2d(inputs, 256, [3, 3], stride=1, scope='conv1_1')