Tensorflow slim库

slim这个模块是在16年新推出的,其主要目的是来做所谓的“代码瘦身”。

github上面大部分tensorflow的工程都会涉及到它,不得不说,撇开Keras,TensorLayer,tfLearn这些个高级库不谈,光用tensorflow能不能写出简洁的代码?当然行,有slim就够了!

惟一的缺点是slim这玩意的中文的文档几乎绝迹。所以国内还是Keras,tensorLayer这些官方文档比较完备的高级库的天下。

 

一.简介

slim被放在tensorflow.contrib这个库下面,导入的方法如下:

 

import tensorflow.contrib.slim as slim

 

这样我们就可以使用slim了,既然说到了,先来扒一扒tensorflow.contrib这个库,tensorflow官方对它的描述是:此目录中的任何代码未经官方支持,可能会随时更改或删除。每个目录下都有指定的所有者。它旨在包含额外功能和贡献,最终会合并到核心TensorFlow中,但其接口可能仍然会发生变化,或者需要进行一些测试,看是否可以获得更广泛的接受。所以slim依然不属于原生tensorflow。

那么什么是slim?slim到底有什么用?

slim是一个使构建,训练,评估神经网络变得简单的库。它可以消除原生tensorflow里面很多重复的模板性的代码,让代码更紧凑,更具备可读性。另外slim提供了很多计算机视觉方面的著名模型(VGG, AlexNet等),我们不仅可以直接使用,甚至能以各种方式进行扩展。

 

slim的子模块及功能介绍:

arg_scope: provides a new scope named arg_scope that allows a user to define default arguments for specific operations within that scope.

除了基本的namescope,variabelscope外,又加了argscope,它是用来控制每一层的默认超参数的。(后面会详细说)

data: contains TF-slim's dataset definition, data providers, parallel_reader, and decoding utilities.

貌似slim里面还有一套自己的数据定义,这个跳过,我们用的不多。

evaluation: contains routines for evaluating models.

评估模型的一些方法,用的也不多

layers: contains high level layers for building models using tensorflow.

这个比较重要,slim的核心和精髓,一些复杂层的定义

learning: contains routines for training models.

一些训练规则

losses: contains commonly used loss functions.

一些loss

metrics: contains popular evaluation metrics.

评估模型的度量标准

nets: contains popular network definitions such as VGG and AlexNet models.

包含一些经典网络,VGG等,用的也比较多

queues: provides a context manager for easily and safely starting and closing QueueRunners.

文本队列管理,比较有用。

regularizers: contains weight regularizers.

包含一些正则规则

variables: provides convenience wrappers for variable creation and manipulation.

这个比较有用,我很喜欢slim管理变量的机制

具体子库就这么多拉,接下来干货时间!

 

二.slim定义模型

slim中定义一个变量的示例:

 

# Model Variables
weights = slim.model_variable('weights',
                              shape=[10, 10, 3 , 3],
                              initializer=tf.truncated_normal_initializer(stddev=0.1),
                              regularizer=slim.l2_regularizer(0.05),
                              device='/CPU:0')
model_variables = slim.get_model_variables()

# Regular variables
my_var = slim.variable('my_var',
                       shape=[20, 1],
                       initializer=tf.zeros_initializer())
regular_variables_and_model_variables = slim.get_variables()


如上,变量分为两类:模型变量和局部变量。局部变量是不作为模型参数保存的,而模型变量会再save的时候保存下来。这个玩过tensorflow的人都会明白,诸如global_step之类的就是局部变量。slim中可以写明变量存放的设备,正则和初始化规则。还有获取变量的函数也需要注意一下,get_variables是返回所有的变量。

 

 

slim中实现一个层:

首先让我们看看tensorflow怎么实现一个层,例如卷积层:

 

input = ...
with tf.name_scope('conv1_1') as scope:
  kernel = tf.Variable(tf.truncated_normal([3, 3, 64, 128], dtype=tf.float32,
                                           stddev=1e-1), name='weights')
  conv = tf.nn.conv2d(input, kernel, [1, 1, 1, 1], padding='SAME')
  biases = tf.Variable(tf.constant(0.0, shape=[128], dtype=tf.float32),
                       trainable=True, name='biases')
  bias = tf.nn.bias_add(conv, biases)
  conv1 = tf.nn.relu(bias, name=scope)


然后slim的实现:

 

 

input = ...
net = slim.conv2d(input, 128, [3, 3], scope='conv1_1')


但这个不是重要的,因为tenorflow目前也有大部分层的简单实现,这里比较吸引人的是slim中的repeat和stack操作:

 

假设定义三个相同的卷积层:

 

net = ...
net = slim.conv2d(net, 256, [3, 3], scope='conv3_1')
net = slim.conv2d(net, 256, [3, 3], scope='conv3_2')
net = slim.conv2d(net, 256, [3, 3], scope='conv3_3')
net = slim.max_pool2d(net, [2, 2], scope='pool2')


在slim中的repeat操作可以减少代码量:

 

 

net = slim.repeat(net, 3, slim.conv2d, 256, [3, 3], scope='conv3')
net = slim.max_pool2d(net, [2, 2], scope='pool2')


而stack是处理卷积核或者输出不一样的情况:

 

假设定义三层FC:

 

# Verbose way:
x = slim.fully_connected(x, 32, scope='fc/fc_1')
x = slim.fully_connected(x, 64, scope='fc/fc_2')
x = slim.fully_connected(x, 128, scope='fc/fc_3')


使用stack操作:

 

 

slim.stack(x, slim.fully_connected, [32, 64, 128], scope='fc')


同理卷积层也一样:

 

 

# 普通方法:
x = slim.conv2d(x, 32, [3, 3], scope='core/core_1')
x = slim.conv2d(x, 32, [1, 1], scope='core/core_2')
x = slim.conv2d(x, 64, [3, 3], scope='core/core_3')
x = slim.conv2d(x, 64, [1, 1], scope='core/core_4')

# 简便方法:
slim.stack(x, slim.conv2d, [(32, [3, 3]), (32, [1, 1]), (64, [3, 3]), (64, [1, 1])], scope='core')

 

 

slim中的argscope:

如果你的网络有大量相同的参数,如下:

 

net = slim.conv2d(inputs, 64, [11, 11], 4, padding='SAME',
                  weights_initializer=tf.truncated_normal_initializer(stddev=0.01),
                  weights_regularizer=slim.l2_regularizer(0.0005), scope='conv1')
net = slim.conv2d(net, 128, [11, 11], padding='VALID',
                  weights_initializer=tf.truncated_normal_initializer(stddev=0.01),
                  weights_regularizer=slim.l2_regularizer(0.0005), scope='conv2')
net = slim.conv2d(net, 256, [11, 11], padding='SAME',
                  weights_initializer=tf.truncated_normal_initializer(stddev=0.01),
                  weights_regularizer=slim.l2_regularizer(0.0005), scope='conv3')

 

 

然后我们用arg_scope处理一下:

with slim.arg_scope([slim.conv2d], padding='SAME',
                      weights_initializer=tf.truncated_normal_initializer(stddev=0.01)
                      weights_regularizer=slim.l2_regularizer(0.0005)):
    net = slim.conv2d(inputs, 64, [11, 11], scope='conv1')
    net = slim.conv2d(net, 128, [11, 11], padding='VALID', scope='conv2')
    net = slim.conv2d(net, 256, [11, 11], scope='conv3')


是不是一下子就变简洁了?这里额外说明一点,arg_scope的作用范围内,是定义了指定层的默认参数,若想特别指定某些层的参数,可以重新赋值(相当于重写),如上倒数第二行代码。那如果除了卷积层还有其他层呢?那就要如下定义:

 

 

with slim.arg_scope([slim.conv2d, slim.fully_connected],
                      activation_fn=tf.nn.relu,
                      weights_initializer=tf.truncated_normal_initializer(stddev=0.01),
                      weights_regularizer=slim.l2_regularizer(0.0005)):
  with slim.arg_scope([slim.conv2d], stride=1, padding='SAME'):
    net = slim.conv2d(inputs, 64, [11, 11], 4, padding='VALID', scope='conv1')
    net = slim.conv2d(net, 256, [5, 5],
                      weights_initializer=tf.truncated_normal_initializer(stddev=0.03),
                      scope='conv2')
    net = slim.fully_connected(net, 1000, activation_fn=None, scope='fc')


写两个arg_scope就行了。

 

采用如上方法,定义一个VGG也就十几行代码的事了。

 

def vgg16(inputs):
  with slim.arg_scope([slim.conv2d, slim.fully_connected],
                      activation_fn=tf.nn.relu,
                      weights_initializer=tf.truncated_normal_initializer(0.0, 0.01),
                      weights_regularizer=slim.l2_regularizer(0.0005)):
    net = slim.repeat(inputs, 2, slim.conv2d, 64, [3, 3], scope='conv1')
    net = slim.max_pool2d(net, [2, 2], scope='pool1')
    net = slim.repeat(net, 2, slim.conv2d, 128, [3, 3], scope='conv2')
    net = slim.max_pool2d(net, [2, 2], scope='pool2')
    net = slim.repeat(net, 3, slim.conv2d, 256, [3, 3], scope='conv3')
    net = slim.max_pool2d(net, [2, 2], scope='pool3')
    net = slim.repeat(net, 3, slim.conv2d, 512, [3, 3], scope='conv4')
    net = slim.max_pool2d(net, [2, 2], scope='pool4')
    net = slim.repeat(net, 3, slim.conv2d, 512, [3, 3], scope='conv5')
    net = slim.max_pool2d(net, [2, 2], scope='pool5')
    net = slim.fully_connected(net, 4096, scope='fc6')
    net = slim.dropout(net, 0.5, scope='dropout6')
    net = slim.fully_connected(net, 4096, scope='fc7')
    net = slim.dropout(net, 0.5, scope='dropout7')
    net = slim.fully_connected(net, 1000, activation_fn=None, scope='fc8')
  return net

 

 

三.训练模型

这个没什么好说的,说一下直接拿经典网络来训练吧。

 

import tensorflow as tf
vgg = tf.contrib.slim.nets.vgg

# Load the images and labels.
images, labels = ...

# Create the model.
predictions, _ = vgg.vgg_16(images)

# Define the loss functions and get the total loss.
loss = slim.losses.softmax_cross_entropy(predictions, labels)


是不是超级简单?

 

关于loss,要说一下定义自己的loss的方法,以及注意不要忘记加入到slim中让slim看到你的loss。

还有正则项也是需要手动添加进loss当中的,不然最后计算的时候就不优化正则目标了。

 

# Load the images and labels.
images, scene_labels, depth_labels, pose_labels = ...

# Create the model.
scene_predictions, depth_predictions, pose_predictions = CreateMultiTaskModel(images)

# Define the loss functions and get the total loss.
classification_loss = slim.losses.softmax_cross_entropy(scene_predictions, scene_labels)
sum_of_squares_loss = slim.losses.sum_of_squares(depth_predictions, depth_labels)
pose_loss = MyCustomLossFunction(pose_predictions, pose_labels)
slim.losses.add_loss(pose_loss) # Letting TF-Slim know about the additional loss.

# The following two ways to compute the total loss are equivalent:
regularization_loss = tf.add_n(slim.losses.get_regularization_losses())
total_loss1 = classification_loss + sum_of_squares_loss + pose_loss + regularization_loss

# (Regularization Loss is included in the total loss by default).
total_loss2 = slim.losses.get_total_loss()

 

 

四.读取保存模型变量

通过以下功能我们可以载入模型的部分变量:

 

# Create some variables.
v1 = slim.variable(name="v1", ...)
v2 = slim.variable(name="nested/v2", ...)
...

# Get list of variables to restore (which contains only 'v2').
variables_to_restore = slim.get_variables_by_name("v2")

# Create the saver which will be used to restore the variables.
restorer = tf.train.Saver(variables_to_restore)

with tf.Session() as sess:
  # Restore variables from disk.
  restorer.restore(sess, "/tmp/model.ckpt")
  print("Model restored.")


除了这种部分变量加载的方法外,我们甚至还能加载到不同名字的变量中。

 

假设我们定义的网络变量是conv1/weights,而从VGG加载的变量名为vgg16/conv1/weights,正常load肯定会报错(找不到变量名),但是可以这样:

 

def name_in_checkpoint(var):
  return 'vgg16/' + var.op.name

variables_to_restore = slim.get_model_variables()
variables_to_restore = {name_in_checkpoint(var):var for var in variables_to_restore}
restorer = tf.train.Saver(variables_to_restore)

with tf.Session() as sess:
  # Restore variables from disk.
  restorer.restore(sess, "/tmp/model.ckpt")

五 、tf.nn.conv2d和tf.contrib.slim.conv2d的区别

在查看代码的时候,看到有代码用到卷积层是tf.nn.conv2d,但是也有的使用的卷积层是tf.contrib.slim.conv2d,这两个函数调用的卷积层是否一致,在查看了API的文档,以及slim.conv2d的源码后,做如下总结:

首先是常见使用的tf.nn.conv2d的函数,其定义如下:

conv2d(

    input,

    filter,

    strides,

    padding,

    use_cudnn_on_gpu=None,

    data_format=None,

    name=None

)

input指需要做卷积的输入图像,它要求是一个Tensor,具有[batch_size, in_height, in_width, in_channels]这样的shape,具体含义是[训练时一个batch的图片数量, 图片高度, 图片宽度, 图像通道数],注意这是一个4维的Tensor,要求数据类型为float32和float64其中之一

filter用于指定CNN中的卷积核,它要求是一个Tensor,具有[filter_height, filter_width, in_channels, out_channels]这样的shape,具体含义是[卷积核的高度,卷积核的宽度,图像通道数,卷积核个数],要求类型与参数input相同,有一个地方需要注意,第三维in_channels,就是参数input的第四维,这里是维度一致,不是数值一致。这里out_channels指定的是卷积核的个数,而in_channels说明卷积核的维度与图像的维度一致,在做卷积的时候,单个卷积核在不同维度上对应的卷积图片,然后将in_channels个通道上的结果相加,加上bias来得到单个卷积核卷积图片的结果。

strides为卷积时在图像每一维的步长,这是一个一维的向量,长度为4,对应的是在input的4个维度上的步长

paddingstring类型的变量,只能是"SAME","VALID"其中之一,这个值决定了不同的卷积方式,SAME代表卷积核可以停留图像边缘,VALID表示不能,更详细的描述可以参考http://blog.csdn.net/mao_xiao_feng/article/details/53444333

use_cudnn_on_gpu指定是否使用cudnn加速,默认为true

data_format是用于指定输入的input的格式,默认为NHWC格式

 

结果返回一个Tensor,这个输出,就是我们常说的feature map。

 

而对于tf.contrib.slim.conv2d,其函数定义如下:

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):

inputs同样是指需要做卷积的输入图像

num_outputs指定卷积核的个数(就是filter的个数)

kernel_size用于指定卷积核的维度(卷积核的宽度,卷积核的高度)

stride为卷积时在图像每一维的步长

padding为padding的方式选择,VALID或者SAME

data_format是用于指定输入的input的格式

rate这个参数不是太理解,而且tf.nn.conv2d中也没有,对于使用atrous convolution的膨胀率(不是太懂这个atrous convolution)

activation_fn用于激活函数的指定,默认的为ReLU函数

normalizer_fn用于指定正则化函数

normalizer_params用于指定正则化函数的参数

weights_initializer用于指定权重的初始化程序

weights_regularizer为权重可选的正则化程序

biases_initializer用于指定biase的初始化程序

biases_regularizer: biases可选的正则化程序

reuse指定是否共享层或者和变量

variable_collections指定所有变量的集合列表或者字典

outputs_collections指定输出被添加的集合

trainable:卷积层的参数是否可被训练

scope:共享变量所指的variable_scope

 

在上述的API中,可以看出去除掉初始化的部分,那么两者并没有什么不同,只是tf.contrib.slim.conv2d提供了更多可以指定的初始化的部分,而对于tf.nn.conv2d而言,其指定filter的方式相比较tf.contrib.slim.conv2d来说,更加的复杂。去除掉少用的初始化部分,其实两者的API可以简化如下:

tf.contrib.slim.conv2d (inputs,

                num_outputs,[卷积核个数]

                kernel_size,[卷积核的高度,卷积核的宽度]

                stride=1,

                padding='SAME',

)

tf.nn.conv2d(

    input,(与上述一致)

    filter,([卷积核的高度,卷积核的宽度,图像通道数,卷积核个数])

    strides,

    padding,

)

可以说两者是几乎相同的,运行下列代码也可知这两者一致

import tensorflow as tf 

import tensorflow.contrib.slim as slim

 

x1 = tf.ones(shape=[1, 64, 64, 3]) 

w = tf.fill([5, 5, 3, 64], 1)

# print("rank is", tf.rank(x1))

y1 = tf.nn.conv2d(x1, w, strides=[1, 1, 1, 1], padding='SAME')

y2 = slim.conv2d(x1, 64, [5, 5], weights_initializer=tf.ones_initializer, padding='SAME')

 

 

with tf.Session() as sess: 

    sess.run(tf.global_variables_initializer()) 

    y1_value,y2_value,x1_value=sess.run([y1,y2,x1])

    print("shapes are", y1_value.shape, y2_value.shape)

    print(y1_value==y2_value)

    print(y1_value)

    print(y2_value)

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值